test_expression.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*************************************************************************/
  2. /* test_expression.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef TEST_EXPRESSION_H
  31. #define TEST_EXPRESSION_H
  32. #include "core/math/expression.h"
  33. #include "tests/test_macros.h"
  34. namespace TestExpression {
  35. TEST_CASE("[Expression] Integer arithmetic") {
  36. Expression expression;
  37. CHECK_MESSAGE(
  38. expression.parse("-123456") == OK,
  39. "Integer identity should parse successfully.");
  40. CHECK_MESSAGE(
  41. int(expression.execute()) == -123456,
  42. "Integer identity should return the expected result.");
  43. CHECK_MESSAGE(
  44. expression.parse("2 + 3") == OK,
  45. "Integer addition should parse successfully.");
  46. CHECK_MESSAGE(
  47. int(expression.execute()) == 5,
  48. "Integer addition should return the expected result.");
  49. CHECK_MESSAGE(
  50. expression.parse("999999999999 + 999999999999") == OK,
  51. "Large integer addition should parse successfully.");
  52. CHECK_MESSAGE(
  53. int64_t(expression.execute()) == 1'999'999'999'998,
  54. "Large integer addition should return the expected result.");
  55. CHECK_MESSAGE(
  56. expression.parse("25 / 10") == OK,
  57. "Integer / integer division should parse successfully.");
  58. CHECK_MESSAGE(
  59. int(expression.execute()) == 2,
  60. "Integer / integer division should return the expected result.");
  61. CHECK_MESSAGE(
  62. expression.parse("2 * (6 + 14) / 2 - 5") == OK,
  63. "Integer multiplication-addition-subtraction-division should parse successfully.");
  64. CHECK_MESSAGE(
  65. int(expression.execute()) == 15,
  66. "Integer multiplication-addition-subtraction-division should return the expected result.");
  67. }
  68. TEST_CASE("[Expression] Floating-point arithmetic") {
  69. Expression expression;
  70. CHECK_MESSAGE(
  71. expression.parse("-123.456") == OK,
  72. "Float identity should parse successfully.");
  73. CHECK_MESSAGE(
  74. Math::is_equal_approx(double(expression.execute()), -123.456),
  75. "Float identity should return the expected result.");
  76. CHECK_MESSAGE(
  77. expression.parse("2.0 + 3.0") == OK,
  78. "Float addition should parse successfully.");
  79. CHECK_MESSAGE(
  80. Math::is_equal_approx(double(expression.execute()), 5),
  81. "Float addition should return the expected result.");
  82. CHECK_MESSAGE(
  83. expression.parse("3.0 / 10") == OK,
  84. "Float / integer division should parse successfully.");
  85. CHECK_MESSAGE(
  86. Math::is_equal_approx(double(expression.execute()), 0.3),
  87. "Float / integer division should return the expected result.");
  88. CHECK_MESSAGE(
  89. expression.parse("3 / 10.0") == OK,
  90. "Basic integer / float division should parse successfully.");
  91. CHECK_MESSAGE(
  92. Math::is_equal_approx(double(expression.execute()), 0.3),
  93. "Basic integer / float division should return the expected result.");
  94. CHECK_MESSAGE(
  95. expression.parse("3.0 / 10.0") == OK,
  96. "Float / float division should parse successfully.");
  97. CHECK_MESSAGE(
  98. Math::is_equal_approx(double(expression.execute()), 0.3),
  99. "Float / float division should return the expected result.");
  100. CHECK_MESSAGE(
  101. expression.parse("2.5 * (6.0 + 14.25) / 2.0 - 5.12345") == OK,
  102. "Float multiplication-addition-subtraction-division should parse successfully.");
  103. CHECK_MESSAGE(
  104. Math::is_equal_approx(double(expression.execute()), 20.18905),
  105. "Float multiplication-addition-subtraction-division should return the expected result.");
  106. }
  107. TEST_CASE("[Expression] Scientific notation") {
  108. Expression expression;
  109. CHECK_MESSAGE(
  110. expression.parse("2.e5") == OK,
  111. "The expression should parse successfully.");
  112. CHECK_MESSAGE(
  113. Math::is_equal_approx(double(expression.execute()), 200'000),
  114. "The expression should return the expected result.");
  115. // The middle "e" is ignored here.
  116. CHECK_MESSAGE(
  117. expression.parse("2e5") == OK,
  118. "The expression should parse successfully.");
  119. CHECK_MESSAGE(
  120. Math::is_equal_approx(double(expression.execute()), 2e5),
  121. "The expression should return the expected result.");
  122. CHECK_MESSAGE(
  123. expression.parse("2e.5") == OK,
  124. "The expression should parse successfully.");
  125. CHECK_MESSAGE(
  126. Math::is_equal_approx(double(expression.execute()), 2),
  127. "The expression should return the expected result.");
  128. }
  129. TEST_CASE("[Expression] Underscored numeric literals") {
  130. Expression expression;
  131. CHECK_MESSAGE(
  132. expression.parse("1_000_000") == OK,
  133. "The expression should parse successfully.");
  134. CHECK_MESSAGE(
  135. expression.parse("1_000.000") == OK,
  136. "The expression should parse successfully.");
  137. CHECK_MESSAGE(
  138. expression.parse("0xff_99_00") == OK,
  139. "The expression should parse successfully.");
  140. }
  141. TEST_CASE("[Expression] Built-in functions") {
  142. Expression expression;
  143. CHECK_MESSAGE(
  144. expression.parse("sqrt(pow(3, 2) + pow(4, 2))") == OK,
  145. "The expression should parse successfully.");
  146. CHECK_MESSAGE(
  147. int(expression.execute()) == 5,
  148. "`sqrt(pow(3, 2) + pow(4, 2))` should return the expected result.");
  149. CHECK_MESSAGE(
  150. expression.parse("snapped(sin(0.5), 0.01)") == OK,
  151. "The expression should parse successfully.");
  152. CHECK_MESSAGE(
  153. Math::is_equal_approx(double(expression.execute()), 0.48),
  154. "`snapped(sin(0.5), 0.01)` should return the expected result.");
  155. CHECK_MESSAGE(
  156. expression.parse("pow(2.0, -2500)") == OK,
  157. "The expression should parse successfully.");
  158. CHECK_MESSAGE(
  159. Math::is_zero_approx(double(expression.execute())),
  160. "`pow(2.0, -2500)` should return the expected result (asymptotically zero).");
  161. }
  162. TEST_CASE("[Expression] Boolean expressions") {
  163. Expression expression;
  164. CHECK_MESSAGE(
  165. expression.parse("24 >= 12") == OK,
  166. "The boolean expression should parse successfully.");
  167. CHECK_MESSAGE(
  168. bool(expression.execute()),
  169. "The boolean expression should evaluate to `true`.");
  170. CHECK_MESSAGE(
  171. expression.parse("1.0 < 1.25 && 1.25 < 2.0") == OK,
  172. "The boolean expression should parse successfully.");
  173. CHECK_MESSAGE(
  174. bool(expression.execute()),
  175. "The boolean expression should evaluate to `true`.");
  176. CHECK_MESSAGE(
  177. expression.parse("!2") == OK,
  178. "The boolean expression should parse successfully.");
  179. CHECK_MESSAGE(
  180. !bool(expression.execute()),
  181. "The boolean expression should evaluate to `false`.");
  182. CHECK_MESSAGE(
  183. expression.parse("!!2") == OK,
  184. "The boolean expression should parse successfully.");
  185. CHECK_MESSAGE(
  186. bool(expression.execute()),
  187. "The boolean expression should evaluate to `true`.");
  188. CHECK_MESSAGE(
  189. expression.parse("!0") == OK,
  190. "The boolean expression should parse successfully.");
  191. CHECK_MESSAGE(
  192. bool(expression.execute()),
  193. "The boolean expression should evaluate to `true`.");
  194. CHECK_MESSAGE(
  195. expression.parse("!!0") == OK,
  196. "The boolean expression should parse successfully.");
  197. CHECK_MESSAGE(
  198. !bool(expression.execute()),
  199. "The boolean expression should evaluate to `false`.");
  200. CHECK_MESSAGE(
  201. expression.parse("2 && 5") == OK,
  202. "The boolean expression should parse successfully.");
  203. CHECK_MESSAGE(
  204. bool(expression.execute()),
  205. "The boolean expression should evaluate to `true`.");
  206. CHECK_MESSAGE(
  207. expression.parse("0 || 0") == OK,
  208. "The boolean expression should parse successfully.");
  209. CHECK_MESSAGE(
  210. !bool(expression.execute()),
  211. "The boolean expression should evaluate to `false`.");
  212. CHECK_MESSAGE(
  213. expression.parse("(2 <= 4) && (2 > 5)") == OK,
  214. "The boolean expression should parse successfully.");
  215. CHECK_MESSAGE(
  216. !bool(expression.execute()),
  217. "The boolean expression should evaluate to `false`.");
  218. }
  219. TEST_CASE("[Expression] Expressions with variables") {
  220. Expression expression;
  221. PackedStringArray parameter_names;
  222. parameter_names.push_back("foo");
  223. parameter_names.push_back("bar");
  224. CHECK_MESSAGE(
  225. expression.parse("foo + bar + 50", parameter_names) == OK,
  226. "The expression should parse successfully.");
  227. Array values;
  228. values.push_back(60);
  229. values.push_back(20);
  230. CHECK_MESSAGE(
  231. int(expression.execute(values)) == 130,
  232. "The expression should return the expected value.");
  233. PackedStringArray parameter_names_invalid;
  234. parameter_names_invalid.push_back("foo");
  235. parameter_names_invalid.push_back("baz"); // Invalid parameter name.
  236. CHECK_MESSAGE(
  237. expression.parse("foo + bar + 50", parameter_names_invalid) == OK,
  238. "The expression should parse successfully.");
  239. Array values_invalid;
  240. values_invalid.push_back(60);
  241. values_invalid.push_back(20);
  242. // Invalid parameters will parse successfully but print an error message when executing.
  243. ERR_PRINT_OFF;
  244. CHECK_MESSAGE(
  245. int(expression.execute(values_invalid)) == 0,
  246. "The expression should return the expected value.");
  247. ERR_PRINT_ON;
  248. // Mismatched argument count (more values than parameters).
  249. PackedStringArray parameter_names_mismatch;
  250. parameter_names_mismatch.push_back("foo");
  251. parameter_names_mismatch.push_back("bar");
  252. CHECK_MESSAGE(
  253. expression.parse("foo + bar + 50", parameter_names_mismatch) == OK,
  254. "The expression should parse successfully.");
  255. Array values_mismatch;
  256. values_mismatch.push_back(60);
  257. values_mismatch.push_back(20);
  258. values_mismatch.push_back(110);
  259. CHECK_MESSAGE(
  260. int(expression.execute(values_mismatch)) == 130,
  261. "The expression should return the expected value.");
  262. // Mismatched argument count (more parameters than values).
  263. PackedStringArray parameter_names_mismatch2;
  264. parameter_names_mismatch2.push_back("foo");
  265. parameter_names_mismatch2.push_back("bar");
  266. parameter_names_mismatch2.push_back("baz");
  267. CHECK_MESSAGE(
  268. expression.parse("foo + bar + baz + 50", parameter_names_mismatch2) == OK,
  269. "The expression should parse successfully.");
  270. Array values_mismatch2;
  271. values_mismatch2.push_back(60);
  272. values_mismatch2.push_back(20);
  273. // Having more parameters than values will parse successfully but print an
  274. // error message when executing.
  275. ERR_PRINT_OFF;
  276. CHECK_MESSAGE(
  277. int(expression.execute(values_mismatch2)) == 0,
  278. "The expression should return the expected value.");
  279. ERR_PRINT_ON;
  280. }
  281. TEST_CASE("[Expression] Invalid expressions") {
  282. Expression expression;
  283. CHECK_MESSAGE(
  284. expression.parse("\\") == ERR_INVALID_PARAMETER,
  285. "The expression shouldn't parse successfully.");
  286. CHECK_MESSAGE(
  287. expression.parse("0++") == ERR_INVALID_PARAMETER,
  288. "The expression shouldn't parse successfully.");
  289. CHECK_MESSAGE(
  290. expression.parse("()") == ERR_INVALID_PARAMETER,
  291. "The expression shouldn't parse successfully.");
  292. CHECK_MESSAGE(
  293. expression.parse("()()") == ERR_INVALID_PARAMETER,
  294. "The expression shouldn't parse successfully.");
  295. CHECK_MESSAGE(
  296. expression.parse("() - ()") == ERR_INVALID_PARAMETER,
  297. "The expression shouldn't parse successfully.");
  298. CHECK_MESSAGE(
  299. expression.parse("() * 12345") == ERR_INVALID_PARAMETER,
  300. "The expression shouldn't parse successfully.");
  301. CHECK_MESSAGE(
  302. expression.parse("() * 12345") == ERR_INVALID_PARAMETER,
  303. "The expression shouldn't parse successfully.");
  304. CHECK_MESSAGE(
  305. expression.parse("123'456") == ERR_INVALID_PARAMETER,
  306. "The expression shouldn't parse successfully.");
  307. CHECK_MESSAGE(
  308. expression.parse("123\"456") == ERR_INVALID_PARAMETER,
  309. "The expression shouldn't parse successfully.");
  310. }
  311. TEST_CASE("[Expression] Unusual expressions") {
  312. Expression expression;
  313. // Redundant parentheses don't cause a parse error as long as they're matched.
  314. CHECK_MESSAGE(
  315. expression.parse("(((((((((((((((666)))))))))))))))") == OK,
  316. "The expression should parse successfully.");
  317. // Using invalid identifiers doesn't cause a parse error.
  318. ERR_PRINT_OFF;
  319. CHECK_MESSAGE(
  320. expression.parse("hello + hello") == OK,
  321. "The expression should parse successfully.");
  322. CHECK_MESSAGE(
  323. int(expression.execute()) == 0,
  324. "The expression should return the expected result.");
  325. ERR_PRINT_ON;
  326. ERR_PRINT_OFF;
  327. CHECK_MESSAGE(
  328. expression.parse("$1.00 + ???5") == OK,
  329. "The expression should parse successfully.");
  330. CHECK_MESSAGE(
  331. int(expression.execute()) == 0,
  332. "The expression should return the expected result.");
  333. ERR_PRINT_ON;
  334. // Commas can't be used as a decimal parameter.
  335. CHECK_MESSAGE(
  336. expression.parse("123,456") == OK,
  337. "The expression should parse successfully.");
  338. CHECK_MESSAGE(
  339. int(expression.execute()) == 123,
  340. "The expression should return the expected result.");
  341. // Spaces can't be used as a separator for large numbers.
  342. CHECK_MESSAGE(
  343. expression.parse("123 456") == OK,
  344. "The expression should parse successfully.");
  345. CHECK_MESSAGE(
  346. int(expression.execute()) == 123,
  347. "The expression should return the expected result.");
  348. // Division by zero is accepted, even though it prints an error message normally.
  349. CHECK_MESSAGE(
  350. expression.parse("-25.4 / 0") == OK,
  351. "The expression should parse successfully.");
  352. ERR_PRINT_OFF;
  353. CHECK_MESSAGE(
  354. Math::is_inf(double(expression.execute())),
  355. "`-25.4 / 0` should return inf.");
  356. ERR_PRINT_ON;
  357. CHECK_MESSAGE(
  358. expression.parse("0 / 0") == OK,
  359. "The expression should parse successfully.");
  360. ERR_PRINT_OFF;
  361. CHECK_MESSAGE(
  362. int(expression.execute()) == 0,
  363. "`0 / 0` should return 0.");
  364. ERR_PRINT_ON;
  365. // The tests below currently crash the engine.
  366. //
  367. //CHECK_MESSAGE(
  368. // expression.parse("(-9223372036854775807 - 1) % -1") == OK,
  369. // "The expression should parse successfully.");
  370. //CHECK_MESSAGE(
  371. // int64_t(expression.execute()) == 0,
  372. // "`(-9223372036854775807 - 1) % -1` should return the expected result.");
  373. //
  374. //CHECK_MESSAGE(
  375. // expression.parse("(-9223372036854775807 - 1) / -1") == OK,
  376. // "The expression should parse successfully.");
  377. //CHECK_MESSAGE(
  378. // int64_t(expression.execute()) == 0,
  379. // "`(-9223372036854775807 - 1) / -1` should return the expected result.");
  380. }
  381. } // namespace TestExpression
  382. #endif // TEST_EXPRESSION_H