test_expression.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. expression.parse("2.E5") == OK,
  159. "The expression should parse successfully.");
  160. CHECK_MESSAGE(
  161. double(expression.execute()) == doctest::Approx(200'000),
  162. "The expression should return the expected result.");
  163. // The middle "e" is ignored here.
  164. CHECK_MESSAGE(
  165. expression.parse("2e5") == OK,
  166. "The expression should parse successfully.");
  167. CHECK_MESSAGE(
  168. double(expression.execute()) == doctest::Approx(2e5),
  169. "The expression should return the expected result.");
  170. CHECK_MESSAGE(
  171. expression.parse("2e.5") == OK,
  172. "The expression should parse successfully.");
  173. CHECK_MESSAGE(
  174. double(expression.execute()) == doctest::Approx(2),
  175. "The expression should return the expected result.");
  176. }
  177. TEST_CASE("[Expression] Underscored numeric literals") {
  178. Expression expression;
  179. CHECK_MESSAGE(
  180. expression.parse("1_000_000") == OK,
  181. "The expression should parse successfully.");
  182. CHECK_MESSAGE(
  183. expression.parse("1_000.000") == OK,
  184. "The expression should parse successfully.");
  185. CHECK_MESSAGE(
  186. expression.parse("0xff_99_00") == OK,
  187. "The expression should parse successfully.");
  188. CHECK_MESSAGE(
  189. expression.parse("0Xff_99_00") == OK,
  190. "The expression should parse successfully.");
  191. CHECK_MESSAGE(
  192. expression.parse("0b10_11_00") == OK,
  193. "The expression should parse successfully.");
  194. CHECK_MESSAGE(
  195. expression.parse("0B10_11_00") == OK,
  196. "The expression should parse successfully.");
  197. }
  198. TEST_CASE("[Expression] Built-in functions") {
  199. Expression expression;
  200. CHECK_MESSAGE(
  201. expression.parse("sqrt(pow(3, 2) + pow(4, 2))") == OK,
  202. "The expression should parse successfully.");
  203. CHECK_MESSAGE(
  204. int(expression.execute()) == 5,
  205. "`sqrt(pow(3, 2) + pow(4, 2))` should return the expected result.");
  206. CHECK_MESSAGE(
  207. expression.parse("snapped(sin(0.5), 0.01)") == OK,
  208. "The expression should parse successfully.");
  209. CHECK_MESSAGE(
  210. double(expression.execute()) == doctest::Approx(0.48),
  211. "`snapped(sin(0.5), 0.01)` should return the expected result.");
  212. CHECK_MESSAGE(
  213. expression.parse("pow(2.0, -2500)") == OK,
  214. "The expression should parse successfully.");
  215. CHECK_MESSAGE(
  216. Math::is_zero_approx(double(expression.execute())),
  217. "`pow(2.0, -2500)` should return the expected result (asymptotically zero).");
  218. }
  219. TEST_CASE("[Expression] Boolean expressions") {
  220. Expression expression;
  221. CHECK_MESSAGE(
  222. expression.parse("24 >= 12") == OK,
  223. "The boolean expression should parse successfully.");
  224. CHECK_MESSAGE(
  225. bool(expression.execute()),
  226. "The boolean expression should evaluate to `true`.");
  227. CHECK_MESSAGE(
  228. expression.parse("1.0 < 1.25 && 1.25 < 2.0") == 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("!2") == OK,
  235. "The boolean expression should parse successfully.");
  236. CHECK_MESSAGE(
  237. !bool(expression.execute()),
  238. "The boolean expression should evaluate to `false`.");
  239. CHECK_MESSAGE(
  240. expression.parse("!!2") == OK,
  241. "The boolean expression should parse successfully.");
  242. CHECK_MESSAGE(
  243. bool(expression.execute()),
  244. "The boolean expression should evaluate to `true`.");
  245. CHECK_MESSAGE(
  246. expression.parse("!0") == 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") == 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 && 5") == OK,
  259. "The boolean expression should parse successfully.");
  260. CHECK_MESSAGE(
  261. bool(expression.execute()),
  262. "The boolean expression should evaluate to `true`.");
  263. CHECK_MESSAGE(
  264. expression.parse("0 || 0") == OK,
  265. "The boolean expression should parse successfully.");
  266. CHECK_MESSAGE(
  267. !bool(expression.execute()),
  268. "The boolean expression should evaluate to `false`.");
  269. CHECK_MESSAGE(
  270. expression.parse("(2 <= 4) && (2 > 5)") == OK,
  271. "The boolean expression should parse successfully.");
  272. CHECK_MESSAGE(
  273. !bool(expression.execute()),
  274. "The boolean expression should evaluate to `false`.");
  275. }
  276. TEST_CASE("[Expression] Expressions with variables") {
  277. Expression expression;
  278. PackedStringArray parameter_names;
  279. parameter_names.push_back("foo");
  280. parameter_names.push_back("bar");
  281. CHECK_MESSAGE(
  282. expression.parse("foo + bar + 50", parameter_names) == OK,
  283. "The expression should parse successfully.");
  284. Array values;
  285. values.push_back(60);
  286. values.push_back(20);
  287. CHECK_MESSAGE(
  288. int(expression.execute(values)) == 130,
  289. "The expression should return the expected value.");
  290. PackedStringArray parameter_names_invalid;
  291. parameter_names_invalid.push_back("foo");
  292. parameter_names_invalid.push_back("baz"); // Invalid parameter name.
  293. CHECK_MESSAGE(
  294. expression.parse("foo + bar + 50", parameter_names_invalid) == OK,
  295. "The expression should parse successfully.");
  296. Array values_invalid;
  297. values_invalid.push_back(60);
  298. values_invalid.push_back(20);
  299. // Invalid parameters will parse successfully but print an error message when executing.
  300. ERR_PRINT_OFF;
  301. CHECK_MESSAGE(
  302. int(expression.execute(values_invalid)) == 0,
  303. "The expression should return the expected value.");
  304. ERR_PRINT_ON;
  305. // Mismatched argument count (more values than parameters).
  306. PackedStringArray parameter_names_mismatch;
  307. parameter_names_mismatch.push_back("foo");
  308. parameter_names_mismatch.push_back("bar");
  309. CHECK_MESSAGE(
  310. expression.parse("foo + bar + 50", parameter_names_mismatch) == OK,
  311. "The expression should parse successfully.");
  312. Array values_mismatch;
  313. values_mismatch.push_back(60);
  314. values_mismatch.push_back(20);
  315. values_mismatch.push_back(110);
  316. CHECK_MESSAGE(
  317. int(expression.execute(values_mismatch)) == 130,
  318. "The expression should return the expected value.");
  319. // Mismatched argument count (more parameters than values).
  320. PackedStringArray parameter_names_mismatch2;
  321. parameter_names_mismatch2.push_back("foo");
  322. parameter_names_mismatch2.push_back("bar");
  323. parameter_names_mismatch2.push_back("baz");
  324. CHECK_MESSAGE(
  325. expression.parse("foo + bar + baz + 50", parameter_names_mismatch2) == OK,
  326. "The expression should parse successfully.");
  327. Array values_mismatch2;
  328. values_mismatch2.push_back(60);
  329. values_mismatch2.push_back(20);
  330. // Having more parameters than values will parse successfully but print an
  331. // error message when executing.
  332. ERR_PRINT_OFF;
  333. CHECK_MESSAGE(
  334. int(expression.execute(values_mismatch2)) == 0,
  335. "The expression should return the expected value.");
  336. ERR_PRINT_ON;
  337. }
  338. TEST_CASE("[Expression] Invalid expressions") {
  339. Expression expression;
  340. CHECK_MESSAGE(
  341. expression.parse("\\") == ERR_INVALID_PARAMETER,
  342. "The expression shouldn't parse successfully.");
  343. CHECK_MESSAGE(
  344. expression.parse("0++") == ERR_INVALID_PARAMETER,
  345. "The expression shouldn't parse successfully.");
  346. CHECK_MESSAGE(
  347. expression.parse("()") == ERR_INVALID_PARAMETER,
  348. "The expression shouldn't parse successfully.");
  349. CHECK_MESSAGE(
  350. expression.parse("()()") == ERR_INVALID_PARAMETER,
  351. "The expression shouldn't parse successfully.");
  352. CHECK_MESSAGE(
  353. expression.parse("() - ()") == ERR_INVALID_PARAMETER,
  354. "The expression shouldn't parse successfully.");
  355. CHECK_MESSAGE(
  356. expression.parse("() * 12345") == ERR_INVALID_PARAMETER,
  357. "The expression shouldn't parse successfully.");
  358. CHECK_MESSAGE(
  359. expression.parse("() * 12345") == ERR_INVALID_PARAMETER,
  360. "The expression shouldn't parse successfully.");
  361. CHECK_MESSAGE(
  362. expression.parse("123'456") == ERR_INVALID_PARAMETER,
  363. "The expression shouldn't parse successfully.");
  364. CHECK_MESSAGE(
  365. expression.parse("123\"456") == ERR_INVALID_PARAMETER,
  366. "The expression shouldn't parse successfully.");
  367. }
  368. TEST_CASE("[Expression] Unusual expressions") {
  369. Expression expression;
  370. // Redundant parentheses don't cause a parse error as long as they're matched.
  371. CHECK_MESSAGE(
  372. expression.parse("(((((((((((((((666)))))))))))))))") == OK,
  373. "The expression should parse successfully.");
  374. // Using invalid identifiers doesn't cause a parse error.
  375. ERR_PRINT_OFF;
  376. CHECK_MESSAGE(
  377. expression.parse("hello + hello") == OK,
  378. "The expression should parse successfully.");
  379. CHECK_MESSAGE(
  380. int(expression.execute()) == 0,
  381. "The expression should return the expected result.");
  382. ERR_PRINT_ON;
  383. ERR_PRINT_OFF;
  384. CHECK_MESSAGE(
  385. expression.parse("$1.00 + ???5") == OK,
  386. "The expression should parse successfully.");
  387. CHECK_MESSAGE(
  388. int(expression.execute()) == 0,
  389. "The expression should return the expected result.");
  390. ERR_PRINT_ON;
  391. // Commas can't be used as a decimal parameter.
  392. CHECK_MESSAGE(
  393. expression.parse("123,456") == OK,
  394. "The expression should parse successfully.");
  395. CHECK_MESSAGE(
  396. int(expression.execute()) == 123,
  397. "The expression should return the expected result.");
  398. // Spaces can't be used as a separator for large numbers.
  399. CHECK_MESSAGE(
  400. expression.parse("123 456") == OK,
  401. "The expression should parse successfully.");
  402. CHECK_MESSAGE(
  403. int(expression.execute()) == 123,
  404. "The expression should return the expected result.");
  405. // Division by zero is accepted, even though it prints an error message normally.
  406. CHECK_MESSAGE(
  407. expression.parse("-25.4 / 0") == OK,
  408. "The expression should parse successfully.");
  409. ERR_PRINT_OFF;
  410. CHECK_MESSAGE(
  411. Math::is_inf(double(expression.execute())),
  412. "`-25.4 / 0` should return inf.");
  413. ERR_PRINT_ON;
  414. CHECK_MESSAGE(
  415. expression.parse("0 / 0") == OK,
  416. "The expression should parse successfully.");
  417. ERR_PRINT_OFF;
  418. CHECK_MESSAGE(
  419. int(expression.execute()) == 0,
  420. "`0 / 0` should return 0.");
  421. ERR_PRINT_ON;
  422. // The tests below currently crash the engine.
  423. //
  424. //CHECK_MESSAGE(
  425. // expression.parse("(-9223372036854775807 - 1) % -1") == OK,
  426. // "The expression should parse successfully.");
  427. //CHECK_MESSAGE(
  428. // int64_t(expression.execute()) == 0,
  429. // "`(-9223372036854775807 - 1) % -1` should return the expected result.");
  430. //
  431. //CHECK_MESSAGE(
  432. // expression.parse("(-9223372036854775807 - 1) / -1") == OK,
  433. // "The expression should parse successfully.");
  434. //CHECK_MESSAGE(
  435. // int64_t(expression.execute()) == 0,
  436. // "`(-9223372036854775807 - 1) / -1` should return the expected result.");
  437. }
  438. } // namespace TestExpression
  439. #endif // TEST_EXPRESSION_H