test_expression.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /**************************************************************************/
  2. /* test_expression.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. double(expression.execute()) == doctest::Approx(-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. double(expression.execute()) == doctest::Approx(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. double(expression.execute()) == doctest::Approx(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. double(expression.execute()) == doctest::Approx(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. double(expression.execute()) == doctest::Approx(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. double(expression.execute()) == doctest::Approx(20.18905),
  105. "Float multiplication-addition-subtraction-division should return the expected result.");
  106. }
  107. TEST_CASE("[Expression] Floating-point notation") {
  108. Expression expression;
  109. CHECK_MESSAGE(
  110. expression.parse("2.") == OK,
  111. "The expression should parse successfully.");
  112. CHECK_MESSAGE(
  113. double(expression.execute()) == doctest::Approx(2.0),
  114. "The expression should return the expected result.");
  115. CHECK_MESSAGE(
  116. expression.parse("(2.)") == OK,
  117. "The expression should parse successfully.");
  118. CHECK_MESSAGE(
  119. double(expression.execute()) == doctest::Approx(2.0),
  120. "The expression should return the expected result.");
  121. CHECK_MESSAGE(
  122. expression.parse(".3") == OK,
  123. "The expression should parse successfully.");
  124. CHECK_MESSAGE(
  125. double(expression.execute()) == doctest::Approx(0.3),
  126. "The expression should return the expected result.");
  127. CHECK_MESSAGE(
  128. expression.parse("2.+5.") == OK,
  129. "The expression should parse successfully.");
  130. CHECK_MESSAGE(
  131. double(expression.execute()) == doctest::Approx(7.0),
  132. "The expression should return the expected result.");
  133. CHECK_MESSAGE(
  134. expression.parse(".3-.8") == OK,
  135. "The expression should parse successfully.");
  136. CHECK_MESSAGE(
  137. double(expression.execute()) == doctest::Approx(-0.5),
  138. "The expression should return the expected result.");
  139. CHECK_MESSAGE(
  140. expression.parse("2.+.2") == OK,
  141. "The expression should parse successfully.");
  142. CHECK_MESSAGE(
  143. double(expression.execute()) == doctest::Approx(2.2),
  144. "The expression should return the expected result.");
  145. CHECK_MESSAGE(
  146. expression.parse(".0*0.") == OK,
  147. "The expression should parse successfully.");
  148. CHECK_MESSAGE(
  149. double(expression.execute()) == doctest::Approx(0.0),
  150. "The expression should return the expected result.");
  151. }
  152. TEST_CASE("[Expression] Scientific notation") {
  153. Expression expression;
  154. CHECK_MESSAGE(
  155. expression.parse("2.e5") == OK,
  156. "The expression should parse successfully.");
  157. CHECK_MESSAGE(
  158. double(expression.execute()) == doctest::Approx(200'000),
  159. "The expression should return the expected result.");
  160. // The middle "e" is ignored here.
  161. CHECK_MESSAGE(
  162. expression.parse("2e5") == OK,
  163. "The expression should parse successfully.");
  164. CHECK_MESSAGE(
  165. double(expression.execute()) == doctest::Approx(2e5),
  166. "The expression should return the expected result.");
  167. CHECK_MESSAGE(
  168. expression.parse("2e.5") == OK,
  169. "The expression should parse successfully.");
  170. CHECK_MESSAGE(
  171. double(expression.execute()) == doctest::Approx(2),
  172. "The expression should return the expected result.");
  173. }
  174. TEST_CASE("[Expression] Underscored numeric literals") {
  175. Expression expression;
  176. CHECK_MESSAGE(
  177. expression.parse("1_000_000") == OK,
  178. "The expression should parse successfully.");
  179. CHECK_MESSAGE(
  180. expression.parse("1_000.000") == OK,
  181. "The expression should parse successfully.");
  182. CHECK_MESSAGE(
  183. expression.parse("0xff_99_00") == OK,
  184. "The expression should parse successfully.");
  185. }
  186. TEST_CASE("[Expression] Built-in functions") {
  187. Expression expression;
  188. CHECK_MESSAGE(
  189. expression.parse("sqrt(pow(3, 2) + pow(4, 2))") == OK,
  190. "The expression should parse successfully.");
  191. CHECK_MESSAGE(
  192. int(expression.execute()) == 5,
  193. "`sqrt(pow(3, 2) + pow(4, 2))` should return the expected result.");
  194. CHECK_MESSAGE(
  195. expression.parse("snapped(sin(0.5), 0.01)") == OK,
  196. "The expression should parse successfully.");
  197. CHECK_MESSAGE(
  198. double(expression.execute()) == doctest::Approx(0.48),
  199. "`snapped(sin(0.5), 0.01)` should return the expected result.");
  200. CHECK_MESSAGE(
  201. expression.parse("pow(2.0, -2500)") == OK,
  202. "The expression should parse successfully.");
  203. CHECK_MESSAGE(
  204. Math::is_zero_approx(double(expression.execute())),
  205. "`pow(2.0, -2500)` should return the expected result (asymptotically zero).");
  206. }
  207. TEST_CASE("[Expression] Boolean expressions") {
  208. Expression expression;
  209. CHECK_MESSAGE(
  210. expression.parse("24 >= 12") == OK,
  211. "The boolean expression should parse successfully.");
  212. CHECK_MESSAGE(
  213. bool(expression.execute()),
  214. "The boolean expression should evaluate to `true`.");
  215. CHECK_MESSAGE(
  216. expression.parse("1.0 < 1.25 && 1.25 < 2.0") == OK,
  217. "The boolean expression should parse successfully.");
  218. CHECK_MESSAGE(
  219. bool(expression.execute()),
  220. "The boolean expression should evaluate to `true`.");
  221. CHECK_MESSAGE(
  222. expression.parse("!2") == OK,
  223. "The boolean expression should parse successfully.");
  224. CHECK_MESSAGE(
  225. !bool(expression.execute()),
  226. "The boolean expression should evaluate to `false`.");
  227. CHECK_MESSAGE(
  228. expression.parse("!!2") == OK,
  229. "The boolean expression should parse successfully.");
  230. CHECK_MESSAGE(
  231. bool(expression.execute()),
  232. "The boolean expression should evaluate to `true`.");
  233. CHECK_MESSAGE(
  234. expression.parse("!0") == OK,
  235. "The boolean expression should parse successfully.");
  236. CHECK_MESSAGE(
  237. bool(expression.execute()),
  238. "The boolean expression should evaluate to `true`.");
  239. CHECK_MESSAGE(
  240. expression.parse("!!0") == OK,
  241. "The boolean expression should parse successfully.");
  242. CHECK_MESSAGE(
  243. !bool(expression.execute()),
  244. "The boolean expression should evaluate to `false`.");
  245. CHECK_MESSAGE(
  246. expression.parse("2 && 5") == OK,
  247. "The boolean expression should parse successfully.");
  248. CHECK_MESSAGE(
  249. bool(expression.execute()),
  250. "The boolean expression should evaluate to `true`.");
  251. CHECK_MESSAGE(
  252. expression.parse("0 || 0") == OK,
  253. "The boolean expression should parse successfully.");
  254. CHECK_MESSAGE(
  255. !bool(expression.execute()),
  256. "The boolean expression should evaluate to `false`.");
  257. CHECK_MESSAGE(
  258. expression.parse("(2 <= 4) && (2 > 5)") == OK,
  259. "The boolean expression should parse successfully.");
  260. CHECK_MESSAGE(
  261. !bool(expression.execute()),
  262. "The boolean expression should evaluate to `false`.");
  263. }
  264. TEST_CASE("[Expression] Expressions with variables") {
  265. Expression expression;
  266. PackedStringArray parameter_names;
  267. parameter_names.push_back("foo");
  268. parameter_names.push_back("bar");
  269. CHECK_MESSAGE(
  270. expression.parse("foo + bar + 50", parameter_names) == OK,
  271. "The expression should parse successfully.");
  272. Array values;
  273. values.push_back(60);
  274. values.push_back(20);
  275. CHECK_MESSAGE(
  276. int(expression.execute(values)) == 130,
  277. "The expression should return the expected value.");
  278. PackedStringArray parameter_names_invalid;
  279. parameter_names_invalid.push_back("foo");
  280. parameter_names_invalid.push_back("baz"); // Invalid parameter name.
  281. CHECK_MESSAGE(
  282. expression.parse("foo + bar + 50", parameter_names_invalid) == OK,
  283. "The expression should parse successfully.");
  284. Array values_invalid;
  285. values_invalid.push_back(60);
  286. values_invalid.push_back(20);
  287. // Invalid parameters will parse successfully but print an error message when executing.
  288. ERR_PRINT_OFF;
  289. CHECK_MESSAGE(
  290. int(expression.execute(values_invalid)) == 0,
  291. "The expression should return the expected value.");
  292. ERR_PRINT_ON;
  293. // Mismatched argument count (more values than parameters).
  294. PackedStringArray parameter_names_mismatch;
  295. parameter_names_mismatch.push_back("foo");
  296. parameter_names_mismatch.push_back("bar");
  297. CHECK_MESSAGE(
  298. expression.parse("foo + bar + 50", parameter_names_mismatch) == OK,
  299. "The expression should parse successfully.");
  300. Array values_mismatch;
  301. values_mismatch.push_back(60);
  302. values_mismatch.push_back(20);
  303. values_mismatch.push_back(110);
  304. CHECK_MESSAGE(
  305. int(expression.execute(values_mismatch)) == 130,
  306. "The expression should return the expected value.");
  307. // Mismatched argument count (more parameters than values).
  308. PackedStringArray parameter_names_mismatch2;
  309. parameter_names_mismatch2.push_back("foo");
  310. parameter_names_mismatch2.push_back("bar");
  311. parameter_names_mismatch2.push_back("baz");
  312. CHECK_MESSAGE(
  313. expression.parse("foo + bar + baz + 50", parameter_names_mismatch2) == OK,
  314. "The expression should parse successfully.");
  315. Array values_mismatch2;
  316. values_mismatch2.push_back(60);
  317. values_mismatch2.push_back(20);
  318. // Having more parameters than values will parse successfully but print an
  319. // error message when executing.
  320. ERR_PRINT_OFF;
  321. CHECK_MESSAGE(
  322. int(expression.execute(values_mismatch2)) == 0,
  323. "The expression should return the expected value.");
  324. ERR_PRINT_ON;
  325. }
  326. TEST_CASE("[Expression] Invalid expressions") {
  327. Expression expression;
  328. CHECK_MESSAGE(
  329. expression.parse("\\") == ERR_INVALID_PARAMETER,
  330. "The expression shouldn't parse successfully.");
  331. CHECK_MESSAGE(
  332. expression.parse("0++") == ERR_INVALID_PARAMETER,
  333. "The expression shouldn't parse successfully.");
  334. CHECK_MESSAGE(
  335. expression.parse("()") == ERR_INVALID_PARAMETER,
  336. "The expression shouldn't parse successfully.");
  337. CHECK_MESSAGE(
  338. expression.parse("()()") == ERR_INVALID_PARAMETER,
  339. "The expression shouldn't parse successfully.");
  340. CHECK_MESSAGE(
  341. expression.parse("() - ()") == ERR_INVALID_PARAMETER,
  342. "The expression shouldn't parse successfully.");
  343. CHECK_MESSAGE(
  344. expression.parse("() * 12345") == ERR_INVALID_PARAMETER,
  345. "The expression shouldn't parse successfully.");
  346. CHECK_MESSAGE(
  347. expression.parse("() * 12345") == ERR_INVALID_PARAMETER,
  348. "The expression shouldn't parse successfully.");
  349. CHECK_MESSAGE(
  350. expression.parse("123'456") == ERR_INVALID_PARAMETER,
  351. "The expression shouldn't parse successfully.");
  352. CHECK_MESSAGE(
  353. expression.parse("123\"456") == ERR_INVALID_PARAMETER,
  354. "The expression shouldn't parse successfully.");
  355. }
  356. TEST_CASE("[Expression] Unusual expressions") {
  357. Expression expression;
  358. // Redundant parentheses don't cause a parse error as long as they're matched.
  359. CHECK_MESSAGE(
  360. expression.parse("(((((((((((((((666)))))))))))))))") == OK,
  361. "The expression should parse successfully.");
  362. // Using invalid identifiers doesn't cause a parse error.
  363. ERR_PRINT_OFF;
  364. CHECK_MESSAGE(
  365. expression.parse("hello + hello") == OK,
  366. "The expression should parse successfully.");
  367. CHECK_MESSAGE(
  368. int(expression.execute()) == 0,
  369. "The expression should return the expected result.");
  370. ERR_PRINT_ON;
  371. ERR_PRINT_OFF;
  372. CHECK_MESSAGE(
  373. expression.parse("$1.00 + ???5") == OK,
  374. "The expression should parse successfully.");
  375. CHECK_MESSAGE(
  376. int(expression.execute()) == 0,
  377. "The expression should return the expected result.");
  378. ERR_PRINT_ON;
  379. // Commas can't be used as a decimal parameter.
  380. CHECK_MESSAGE(
  381. expression.parse("123,456") == OK,
  382. "The expression should parse successfully.");
  383. CHECK_MESSAGE(
  384. int(expression.execute()) == 123,
  385. "The expression should return the expected result.");
  386. // Spaces can't be used as a separator for large numbers.
  387. CHECK_MESSAGE(
  388. expression.parse("123 456") == OK,
  389. "The expression should parse successfully.");
  390. CHECK_MESSAGE(
  391. int(expression.execute()) == 123,
  392. "The expression should return the expected result.");
  393. // Division by zero is accepted, even though it prints an error message normally.
  394. CHECK_MESSAGE(
  395. expression.parse("-25.4 / 0") == OK,
  396. "The expression should parse successfully.");
  397. ERR_PRINT_OFF;
  398. CHECK_MESSAGE(
  399. Math::is_inf(double(expression.execute())),
  400. "`-25.4 / 0` should return inf.");
  401. ERR_PRINT_ON;
  402. CHECK_MESSAGE(
  403. expression.parse("0 / 0") == OK,
  404. "The expression should parse successfully.");
  405. ERR_PRINT_OFF;
  406. CHECK_MESSAGE(
  407. int(expression.execute()) == 0,
  408. "`0 / 0` should return 0.");
  409. ERR_PRINT_ON;
  410. // The tests below currently crash the engine.
  411. //
  412. //CHECK_MESSAGE(
  413. // expression.parse("(-9223372036854775807 - 1) % -1") == OK,
  414. // "The expression should parse successfully.");
  415. //CHECK_MESSAGE(
  416. // int64_t(expression.execute()) == 0,
  417. // "`(-9223372036854775807 - 1) % -1` should return the expected result.");
  418. //
  419. //CHECK_MESSAGE(
  420. // expression.parse("(-9223372036854775807 - 1) / -1") == OK,
  421. // "The expression should parse successfully.");
  422. //CHECK_MESSAGE(
  423. // int64_t(expression.execute()) == 0,
  424. // "`(-9223372036854775807 - 1) / -1` should return the expected result.");
  425. }
  426. } // namespace TestExpression
  427. #endif // TEST_EXPRESSION_H