summaryrefslogtreecommitdiff
path: root/tests/test_expression.h
blob: 8c026a35f5f34d4632fa2c550bf6ecaefb806671 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*************************************************************************/
/*  test_expression.h                                                    */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#ifndef TEST_EXPRESSION_H
#define TEST_EXPRESSION_H

#include "core/math/expression.h"

#include "tests/test_macros.h"

namespace TestExpression {

TEST_CASE("[Expression] Integer arithmetic") {
	Expression expression;

	CHECK_MESSAGE(
			expression.parse("-123456") == OK,
			"Integer identity should parse successfully.");
	CHECK_MESSAGE(
			int(expression.execute()) == -123456,
			"Integer identity should return the expected result.");

	CHECK_MESSAGE(
			expression.parse("2 + 3") == OK,
			"Integer addition should parse successfully.");
	CHECK_MESSAGE(
			int(expression.execute()) == 5,
			"Integer addition should return the expected result.");

	CHECK_MESSAGE(
			expression.parse("999999999999 + 999999999999") == OK,
			"Large integer addition should parse successfully.");
	CHECK_MESSAGE(
			int64_t(expression.execute()) == 1'999'999'999'998,
			"Large integer addition should return the expected result.");

	CHECK_MESSAGE(
			expression.parse("25 / 10") == OK,
			"Integer / integer division should parse successfully.");
	CHECK_MESSAGE(
			int(expression.execute()) == 2,
			"Integer / integer divsion should return the expected result.");

	CHECK_MESSAGE(
			expression.parse("2 * (6 + 14) / 2 - 5") == OK,
			"Integer multiplication-addition-subtraction-division should parse successfully.");
	CHECK_MESSAGE(
			int(expression.execute()) == 15,
			"Integer multiplication-addition-subtraction-division should return the expected result.");
}

TEST_CASE("[Expression] Floating-point arithmetic") {
	Expression expression;

	CHECK_MESSAGE(
			expression.parse("-123.456") == OK,
			"Float identity should parse successfully.");
	CHECK_MESSAGE(
			Math::is_equal_approx(float(expression.execute()), -123.456),
			"Float identity should return the expected result.");

	CHECK_MESSAGE(
			expression.parse("2.0 + 3.0") == OK,
			"Float addition should parse successfully.");
	CHECK_MESSAGE(
			Math::is_equal_approx(float(expression.execute()), 5),
			"Float addition should return the expected result.");

	CHECK_MESSAGE(
			expression.parse("3.0 / 10") == OK,
			"Float / integer division should parse successfully.");
	CHECK_MESSAGE(
			Math::is_equal_approx(float(expression.execute()), 0.3),
			"Float / integer divsion should return the expected result.");

	CHECK_MESSAGE(
			expression.parse("3 / 10.0") == OK,
			"Basic integer / float division should parse successfully.");
	CHECK_MESSAGE(
			Math::is_equal_approx(float(expression.execute()), 0.3),
			"Basic integer / float divsion should return the expected result.");

	CHECK_MESSAGE(
			expression.parse("3.0 / 10.0") == OK,
			"Float / float division should parse successfully.");
	CHECK_MESSAGE(
			Math::is_equal_approx(float(expression.execute()), 0.3),
			"Float / float divsion should return the expected result.");

	CHECK_MESSAGE(
			expression.parse("2.5 * (6.0 + 14.25) / 2.0 - 5.12345") == OK,
			"Float multiplication-addition-subtraction-division should parse successfully.");
	CHECK_MESSAGE(
			Math::is_equal_approx(float(expression.execute()), 20.18905),
			"Float multiplication-addition-subtraction-division should return the expected result.");
}

TEST_CASE("[Expression] Scientific notation") {
	Expression expression;

	CHECK_MESSAGE(
			expression.parse("2.e5") == OK,
			"The expression should parse successfully.");
	CHECK_MESSAGE(
			Math::is_equal_approx(float(expression.execute()), 200'000),
			"The expression should return the expected result.");

	// The middle "e" is ignored here.
	CHECK_MESSAGE(
			expression.parse("2e5") == OK,
			"The expression should parse successfully.");
	CHECK_MESSAGE(
			Math::is_equal_approx(float(expression.execute()), 25),
			"The expression should return the expected result.");

	CHECK_MESSAGE(
			expression.parse("2e.5") == OK,
			"The expression should parse successfully.");
	CHECK_MESSAGE(
			Math::is_equal_approx(float(expression.execute()), 2),
			"The expression should return the expected result.");
}

TEST_CASE("[Expression] Underscored numeric literals") {
	Expression expression;

	CHECK_MESSAGE(
			expression.parse("1_000_000") == OK,
			"The expression should parse successfully.");
	CHECK_MESSAGE(
			expression.parse("1_000.000") == OK,
			"The expression should parse successfully.");
	CHECK_MESSAGE(
			expression.parse("0xff_99_00") == OK,
			"The expression should parse successfully.");
}

TEST_CASE("[Expression] Built-in functions") {
	Expression expression;

	CHECK_MESSAGE(
			expression.parse("sqrt(pow(3, 2) + pow(4, 2))") == OK,
			"The expression should parse successfully.");
	CHECK_MESSAGE(
			int(expression.execute()) == 5,
			"`sqrt(pow(3, 2) + pow(4, 2))` should return the expected result.");

	CHECK_MESSAGE(
			expression.parse("stepify(sin(0.5), 0.01)") == OK,
			"The expression should parse successfully.");
	CHECK_MESSAGE(
			Math::is_equal_approx(float(expression.execute()), 0.48),
			"`stepify(sin(0.5), 0.01)` should return the expected result.");

	CHECK_MESSAGE(
			expression.parse("pow(2.0, -2500)") == OK,
			"The expression should parse successfully.");
	CHECK_MESSAGE(
			Math::is_zero_approx(float(expression.execute())),
			"`pow(2.0, -2500)` should return the expected result (asymptotically zero).");
}

TEST_CASE("[Expression] Boolean expressions") {
	Expression expression;

	CHECK_MESSAGE(
			expression.parse("24 >= 12") == OK,
			"The boolean expression should parse successfully.");
	CHECK_MESSAGE(
			bool(expression.execute()),
			"The boolean expression should evaluate to `true`.");

	CHECK_MESSAGE(
			expression.parse("1.0 < 1.25 && 1.25 < 2.0") == OK,
			"The boolean expression should parse successfully.");
	CHECK_MESSAGE(
			bool(expression.execute()),
			"The boolean expression should evaluate to `true`.");

	CHECK_MESSAGE(
			expression.parse("!2") == OK,
			"The boolean expression should parse successfully.");
	CHECK_MESSAGE(
			!bool(expression.execute()),
			"The boolean expression should evaluate to `false`.");

	CHECK_MESSAGE(
			expression.parse("!!2") == OK,
			"The boolean expression should parse successfully.");
	CHECK_MESSAGE(
			bool(expression.execute()),
			"The boolean expression should evaluate to `true`.");

	CHECK_MESSAGE(
			expression.parse("!0") == OK,
			"The boolean expression should parse successfully.");
	CHECK_MESSAGE(
			bool(expression.execute()),
			"The boolean expression should evaluate to `true`.");

	CHECK_MESSAGE(
			expression.parse("!!0") == OK,
			"The boolean expression should parse successfully.");
	CHECK_MESSAGE(
			!bool(expression.execute()),
			"The boolean expression should evaluate to `false`.");

	CHECK_MESSAGE(
			expression.parse("2 && 5") == OK,
			"The boolean expression should parse successfully.");
	CHECK_MESSAGE(
			bool(expression.execute()),
			"The boolean expression should evaluate to `true`.");

	CHECK_MESSAGE(
			expression.parse("0 || 0") == OK,
			"The boolean expression should parse successfully.");
	CHECK_MESSAGE(
			!bool(expression.execute()),
			"The boolean expression should evaluate to `false`.");

	CHECK_MESSAGE(
			expression.parse("(2 <= 4) && (2 > 5)") == OK,
			"The boolean expression should parse successfully.");
	CHECK_MESSAGE(
			!bool(expression.execute()),
			"The boolean expression should evaluate to `false`.");
}

TEST_CASE("[Expression] Expressions with variables") {
	Expression expression;

	PackedStringArray parameter_names;
	parameter_names.push_back("foo");
	parameter_names.push_back("bar");
	CHECK_MESSAGE(
			expression.parse("foo + bar + 50", parameter_names) == OK,
			"The expression should parse successfully.");
	Array values;
	values.push_back(60);
	values.push_back(20);
	CHECK_MESSAGE(
			int(expression.execute(values)) == 130,
			"The expression should return the expected value.");

	PackedStringArray parameter_names_invalid;
	parameter_names_invalid.push_back("foo");
	parameter_names_invalid.push_back("baz"); // Invalid parameter name.
	CHECK_MESSAGE(
			expression.parse("foo + bar + 50", parameter_names_invalid) == OK,
			"The expression should parse successfully.");
	Array values_invalid;
	values_invalid.push_back(60);
	values_invalid.push_back(20);
	// Invalid parameters will parse successfully but print an error message when executing.
	ERR_PRINT_OFF;
	CHECK_MESSAGE(
			int(expression.execute(values_invalid)) == 0,
			"The expression should return the expected value.");
	ERR_PRINT_ON;

	// Mismatched argument count (more values than parameters).
	PackedStringArray parameter_names_mismatch;
	parameter_names_mismatch.push_back("foo");
	parameter_names_mismatch.push_back("bar");
	CHECK_MESSAGE(
			expression.parse("foo + bar + 50", parameter_names_mismatch) == OK,
			"The expression should parse successfully.");
	Array values_mismatch;
	values_mismatch.push_back(60);
	values_mismatch.push_back(20);
	values_mismatch.push_back(110);
	CHECK_MESSAGE(
			int(expression.execute(values_mismatch)) == 130,
			"The expression should return the expected value.");

	// Mismatched argument count (more parameters than values).
	PackedStringArray parameter_names_mismatch2;
	parameter_names_mismatch2.push_back("foo");
	parameter_names_mismatch2.push_back("bar");
	parameter_names_mismatch2.push_back("baz");
	CHECK_MESSAGE(
			expression.parse("foo + bar + baz + 50", parameter_names_mismatch2) == OK,
			"The expression should parse successfully.");
	Array values_mismatch2;
	values_mismatch2.push_back(60);
	values_mismatch2.push_back(20);
	// Having more parameters than values will parse successfully but print an
	// error message when executing.
	ERR_PRINT_OFF;
	CHECK_MESSAGE(
			int(expression.execute(values_mismatch2)) == 0,
			"The expression should return the expected value.");
	ERR_PRINT_ON;
}

TEST_CASE("[Expression] Invalid expressions") {
	Expression expression;

	CHECK_MESSAGE(
			expression.parse("\\") == ERR_INVALID_PARAMETER,
			"The expression shouldn't parse successfully.");

	CHECK_MESSAGE(
			expression.parse("0++") == ERR_INVALID_PARAMETER,
			"The expression shouldn't parse successfully.");

	CHECK_MESSAGE(
			expression.parse("()") == ERR_INVALID_PARAMETER,
			"The expression shouldn't parse successfully.");

	CHECK_MESSAGE(
			expression.parse("()()") == ERR_INVALID_PARAMETER,
			"The expression shouldn't parse successfully.");

	CHECK_MESSAGE(
			expression.parse("() - ()") == ERR_INVALID_PARAMETER,
			"The expression shouldn't parse successfully.");

	CHECK_MESSAGE(
			expression.parse("() * 12345") == ERR_INVALID_PARAMETER,
			"The expression shouldn't parse successfully.");

	CHECK_MESSAGE(
			expression.parse("() * 12345") == ERR_INVALID_PARAMETER,
			"The expression shouldn't parse successfully.");

	CHECK_MESSAGE(
			expression.parse("123'456") == ERR_INVALID_PARAMETER,
			"The expression shouldn't parse successfully.");

	CHECK_MESSAGE(
			expression.parse("123\"456") == ERR_INVALID_PARAMETER,
			"The expression shouldn't parse successfully.");
}

TEST_CASE("[Expression] Unusual expressions") {
	Expression expression;

	// Redundant parentheses don't cause a parse error as long as they're matched.
	CHECK_MESSAGE(
			expression.parse("(((((((((((((((666)))))))))))))))") == OK,
			"The expression should parse successfully.");

	// Using invalid identifiers doesn't cause a parse error.
	ERR_PRINT_OFF;
	CHECK_MESSAGE(
			expression.parse("hello + hello") == OK,
			"The expression should parse successfully.");
	CHECK_MESSAGE(
			int(expression.execute()) == 0,
			"The expression should return the expected result.");
	ERR_PRINT_ON;

	ERR_PRINT_OFF;
	CHECK_MESSAGE(
			expression.parse("$1.00 + €5") == OK,
			"The expression should parse successfully.");
	CHECK_MESSAGE(
			int(expression.execute()) == 0,
			"The expression should return the expected result.");
	ERR_PRINT_ON;

	// Commas can't be used as a decimal parameter.
	CHECK_MESSAGE(
			expression.parse("123,456") == OK,
			"The expression should parse successfully.");
	CHECK_MESSAGE(
			int(expression.execute()) == 123,
			"The expression should return the expected result.");

	// Spaces can't be used as a separator for large numbers.
	CHECK_MESSAGE(
			expression.parse("123 456") == OK,
			"The expression should parse successfully.");
	CHECK_MESSAGE(
			int(expression.execute()) == 123,
			"The expression should return the expected result.");

	// Division by zero is accepted, even though it prints an error message normally.
	CHECK_MESSAGE(
			expression.parse("-25.4 / 0") == OK,
			"The expression should parse successfully.");
	ERR_PRINT_OFF;
	CHECK_MESSAGE(
			Math::is_zero_approx(float(expression.execute())),
			"`-25.4 / 0` should return 0.");
	ERR_PRINT_ON;

	CHECK_MESSAGE(
			expression.parse("0 / 0") == OK,
			"The expression should parse successfully.");
	ERR_PRINT_OFF;
	CHECK_MESSAGE(
			int(expression.execute()) == 0,
			"`0 / 0` should return 0.");
	ERR_PRINT_ON;

	// The tests below currently crash the engine.
	//
	//CHECK_MESSAGE(
	//		expression.parse("(-9223372036854775807 - 1) % -1") == OK,
	//		"The expression should parse successfully.");
	//CHECK_MESSAGE(
	//		int64_t(expression.execute()) == 0,
	//		"`(-9223372036854775807 - 1) % -1` should return the expected result.");
	//
	//CHECK_MESSAGE(
	//		expression.parse("(-9223372036854775807 - 1) / -1") == OK,
	//		"The expression should parse successfully.");
	//CHECK_MESSAGE(
	//		int64_t(expression.execute()) == 0,
	//		"`(-9223372036854775807 - 1) / -1` should return the expected result.");
}

} // namespace TestExpression

#endif // TEST_EXPRESSION_H