test_expression.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. #pragma once
  31. #include "core/math/expression.h"
  32. #include "tests/test_macros.h"
  33. namespace TestExpression {
  34. TEST_CASE("[Expression] Integer arithmetic") {
  35. Expression expression;
  36. CHECK_MESSAGE(
  37. expression.parse("-123456") == OK,
  38. "Integer identity should parse successfully.");
  39. CHECK_MESSAGE(
  40. int(expression.execute()) == -123456,
  41. "Integer identity should return the expected result.");
  42. CHECK_MESSAGE(
  43. expression.parse("2 + 3") == OK,
  44. "Integer addition should parse successfully.");
  45. CHECK_MESSAGE(
  46. int(expression.execute()) == 5,
  47. "Integer addition should return the expected result.");
  48. CHECK_MESSAGE(
  49. expression.parse("999999999999 + 999999999999") == OK,
  50. "Large integer addition should parse successfully.");
  51. CHECK_MESSAGE(
  52. int64_t(expression.execute()) == 1'999'999'999'998,
  53. "Large integer addition should return the expected result.");
  54. CHECK_MESSAGE(
  55. expression.parse("25 / 10") == OK,
  56. "Integer / integer division should parse successfully.");
  57. CHECK_MESSAGE(
  58. int(expression.execute()) == 2,
  59. "Integer / integer division should return the expected result.");
  60. CHECK_MESSAGE(
  61. expression.parse("2 * (6 + 14) / 2 - 5") == OK,
  62. "Integer multiplication-addition-subtraction-division should parse successfully.");
  63. CHECK_MESSAGE(
  64. int(expression.execute()) == 15,
  65. "Integer multiplication-addition-subtraction-division should return the expected result.");
  66. }
  67. TEST_CASE("[Expression] Floating-point arithmetic") {
  68. Expression expression;
  69. CHECK_MESSAGE(
  70. expression.parse("-123.456") == OK,
  71. "Float identity should parse successfully.");
  72. CHECK_MESSAGE(
  73. double(expression.execute()) == doctest::Approx(-123.456),
  74. "Float identity should return the expected result.");
  75. CHECK_MESSAGE(
  76. expression.parse("2.0 + 3.0") == OK,
  77. "Float addition should parse successfully.");
  78. CHECK_MESSAGE(
  79. double(expression.execute()) == doctest::Approx(5),
  80. "Float addition should return the expected result.");
  81. CHECK_MESSAGE(
  82. expression.parse("3.0 / 10") == OK,
  83. "Float / integer division should parse successfully.");
  84. CHECK_MESSAGE(
  85. double(expression.execute()) == doctest::Approx(0.3),
  86. "Float / integer division should return the expected result.");
  87. CHECK_MESSAGE(
  88. expression.parse("3 / 10.0") == OK,
  89. "Basic integer / float division should parse successfully.");
  90. CHECK_MESSAGE(
  91. double(expression.execute()) == doctest::Approx(0.3),
  92. "Basic integer / float division should return the expected result.");
  93. CHECK_MESSAGE(
  94. expression.parse("3.0 / 10.0") == OK,
  95. "Float / float division should parse successfully.");
  96. CHECK_MESSAGE(
  97. double(expression.execute()) == doctest::Approx(0.3),
  98. "Float / float division should return the expected result.");
  99. CHECK_MESSAGE(
  100. expression.parse("2.5 * (6.0 + 14.25) / 2.0 - 5.12345") == OK,
  101. "Float multiplication-addition-subtraction-division should parse successfully.");
  102. CHECK_MESSAGE(
  103. double(expression.execute()) == doctest::Approx(20.18905),
  104. "Float multiplication-addition-subtraction-division should return the expected result.");
  105. }
  106. TEST_CASE("[Expression] Floating-point notation") {
  107. Expression expression;
  108. CHECK_MESSAGE(
  109. expression.parse("2.") == OK,
  110. "The expression should parse successfully.");
  111. CHECK_MESSAGE(
  112. double(expression.execute()) == doctest::Approx(2.0),
  113. "The expression should return the expected result.");
  114. CHECK_MESSAGE(
  115. expression.parse("(2.)") == OK,
  116. "The expression should parse successfully.");
  117. CHECK_MESSAGE(
  118. double(expression.execute()) == doctest::Approx(2.0),
  119. "The expression should return the expected result.");
  120. CHECK_MESSAGE(
  121. expression.parse(".3") == OK,
  122. "The expression should parse successfully.");
  123. CHECK_MESSAGE(
  124. double(expression.execute()) == doctest::Approx(0.3),
  125. "The expression should return the expected result.");
  126. CHECK_MESSAGE(
  127. expression.parse("2.+5.") == OK,
  128. "The expression should parse successfully.");
  129. CHECK_MESSAGE(
  130. double(expression.execute()) == doctest::Approx(7.0),
  131. "The expression should return the expected result.");
  132. CHECK_MESSAGE(
  133. expression.parse(".3-.8") == OK,
  134. "The expression should parse successfully.");
  135. CHECK_MESSAGE(
  136. double(expression.execute()) == doctest::Approx(-0.5),
  137. "The expression should return the expected result.");
  138. CHECK_MESSAGE(
  139. expression.parse("2.+.2") == OK,
  140. "The expression should parse successfully.");
  141. CHECK_MESSAGE(
  142. double(expression.execute()) == doctest::Approx(2.2),
  143. "The expression should return the expected result.");
  144. CHECK_MESSAGE(
  145. expression.parse(".0*0.") == OK,
  146. "The expression should parse successfully.");
  147. CHECK_MESSAGE(
  148. double(expression.execute()) == doctest::Approx(0.0),
  149. "The expression should return the expected result.");
  150. }
  151. TEST_CASE("[Expression] Scientific notation") {
  152. Expression expression;
  153. CHECK_MESSAGE(
  154. expression.parse("2.e5") == OK,
  155. "The expression should parse successfully.");
  156. CHECK_MESSAGE(
  157. expression.parse("2.E5") == OK,
  158. "The expression should parse successfully.");
  159. CHECK_MESSAGE(
  160. double(expression.execute()) == doctest::Approx(200'000),
  161. "The expression should return the expected result.");
  162. // The middle "e" is ignored here.
  163. CHECK_MESSAGE(
  164. expression.parse("2e5") == OK,
  165. "The expression should parse successfully.");
  166. CHECK_MESSAGE(
  167. double(expression.execute()) == doctest::Approx(2e5),
  168. "The expression should return the expected result.");
  169. CHECK_MESSAGE(
  170. expression.parse("2e.5") == OK,
  171. "The expression should parse successfully.");
  172. CHECK_MESSAGE(
  173. double(expression.execute()) == doctest::Approx(2),
  174. "The expression should return the expected result.");
  175. }
  176. TEST_CASE("[Expression] Underscored numeric literals") {
  177. Expression expression;
  178. CHECK_MESSAGE(
  179. expression.parse("1_000_000") == OK,
  180. "The expression should parse successfully.");
  181. CHECK_MESSAGE(
  182. expression.parse("1_000.000") == OK,
  183. "The expression should parse successfully.");
  184. CHECK_MESSAGE(
  185. expression.parse("0xff_99_00") == OK,
  186. "The expression should parse successfully.");
  187. CHECK_MESSAGE(
  188. expression.parse("0Xff_99_00") == OK,
  189. "The expression should parse successfully.");
  190. CHECK_MESSAGE(
  191. expression.parse("0b10_11_00") == OK,
  192. "The expression should parse successfully.");
  193. CHECK_MESSAGE(
  194. expression.parse("0B10_11_00") == OK,
  195. "The expression should parse successfully.");
  196. }
  197. TEST_CASE("[Expression] Built-in functions") {
  198. Expression expression;
  199. CHECK_MESSAGE(
  200. expression.parse("sqrt(pow(3, 2) + pow(4, 2))") == OK,
  201. "The expression should parse successfully.");
  202. CHECK_MESSAGE(
  203. int(expression.execute()) == 5,
  204. "`sqrt(pow(3, 2) + pow(4, 2))` should return the expected result.");
  205. CHECK_MESSAGE(
  206. expression.parse("snapped(sin(0.5), 0.01)") == OK,
  207. "The expression should parse successfully.");
  208. CHECK_MESSAGE(
  209. double(expression.execute()) == doctest::Approx(0.48),
  210. "`snapped(sin(0.5), 0.01)` should return the expected result.");
  211. CHECK_MESSAGE(
  212. expression.parse("pow(2.0, -2500)") == OK,
  213. "The expression should parse successfully.");
  214. CHECK_MESSAGE(
  215. Math::is_zero_approx(double(expression.execute())),
  216. "`pow(2.0, -2500)` should return the expected result (asymptotically zero).");
  217. }
  218. TEST_CASE("[Expression] Boolean expressions") {
  219. Expression expression;
  220. CHECK_MESSAGE(
  221. expression.parse("24 >= 12") == OK,
  222. "The boolean expression should parse successfully.");
  223. CHECK_MESSAGE(
  224. bool(expression.execute()),
  225. "The boolean expression should evaluate to `true`.");
  226. CHECK_MESSAGE(
  227. expression.parse("1.0 < 1.25 && 1.25 < 2.0") == OK,
  228. "The boolean expression should parse successfully.");
  229. CHECK_MESSAGE(
  230. bool(expression.execute()),
  231. "The boolean expression should evaluate to `true`.");
  232. CHECK_MESSAGE(
  233. expression.parse("!2") == OK,
  234. "The boolean expression should parse successfully.");
  235. CHECK_MESSAGE(
  236. !bool(expression.execute()),
  237. "The boolean expression should evaluate to `false`.");
  238. CHECK_MESSAGE(
  239. expression.parse("!!2") == OK,
  240. "The boolean expression should parse successfully.");
  241. CHECK_MESSAGE(
  242. bool(expression.execute()),
  243. "The boolean expression should evaluate to `true`.");
  244. CHECK_MESSAGE(
  245. expression.parse("!0") == OK,
  246. "The boolean expression should parse successfully.");
  247. CHECK_MESSAGE(
  248. bool(expression.execute()),
  249. "The boolean expression should evaluate to `true`.");
  250. CHECK_MESSAGE(
  251. expression.parse("!!0") == OK,
  252. "The boolean expression should parse successfully.");
  253. CHECK_MESSAGE(
  254. !bool(expression.execute()),
  255. "The boolean expression should evaluate to `false`.");
  256. CHECK_MESSAGE(
  257. expression.parse("2 && 5") == OK,
  258. "The boolean expression should parse successfully.");
  259. CHECK_MESSAGE(
  260. bool(expression.execute()),
  261. "The boolean expression should evaluate to `true`.");
  262. CHECK_MESSAGE(
  263. expression.parse("0 || 0") == OK,
  264. "The boolean expression should parse successfully.");
  265. CHECK_MESSAGE(
  266. !bool(expression.execute()),
  267. "The boolean expression should evaluate to `false`.");
  268. CHECK_MESSAGE(
  269. expression.parse("(2 <= 4) && (2 > 5)") == OK,
  270. "The boolean expression should parse successfully.");
  271. CHECK_MESSAGE(
  272. !bool(expression.execute()),
  273. "The boolean expression should evaluate to `false`.");
  274. }
  275. TEST_CASE("[Expression] Expressions with variables") {
  276. Expression expression;
  277. PackedStringArray parameter_names = { "foo", "bar" };
  278. CHECK_MESSAGE(
  279. expression.parse("foo + bar + 50", parameter_names) == OK,
  280. "The expression should parse successfully.");
  281. Array values = { 60, 20 };
  282. CHECK_MESSAGE(
  283. int(expression.execute(values)) == 130,
  284. "The expression should return the expected value.");
  285. PackedStringArray parameter_names_invalid;
  286. parameter_names_invalid.push_back("foo");
  287. parameter_names_invalid.push_back("baz"); // Invalid parameter name.
  288. CHECK_MESSAGE(
  289. expression.parse("foo + bar + 50", parameter_names_invalid) == OK,
  290. "The expression should parse successfully.");
  291. Array values_invalid = { 60, 20 };
  292. // Invalid parameters will parse successfully but print an error message when executing.
  293. ERR_PRINT_OFF;
  294. CHECK_MESSAGE(
  295. int(expression.execute(values_invalid)) == 0,
  296. "The expression should return the expected value.");
  297. ERR_PRINT_ON;
  298. // Mismatched argument count (more values than parameters).
  299. PackedStringArray parameter_names_mismatch = { "foo", "bar" };
  300. CHECK_MESSAGE(
  301. expression.parse("foo + bar + 50", parameter_names_mismatch) == OK,
  302. "The expression should parse successfully.");
  303. Array values_mismatch = { 60, 20, 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 = { "foo", "bar", "baz" };
  309. CHECK_MESSAGE(
  310. expression.parse("foo + bar + baz + 50", parameter_names_mismatch2) == OK,
  311. "The expression should parse successfully.");
  312. Array values_mismatch2 = { 60, 20 };
  313. // Having more parameters than values will parse successfully but print an
  314. // error message when executing.
  315. ERR_PRINT_OFF;
  316. CHECK_MESSAGE(
  317. int(expression.execute(values_mismatch2)) == 0,
  318. "The expression should return the expected value.");
  319. ERR_PRINT_ON;
  320. }
  321. TEST_CASE("[Expression] Invalid expressions") {
  322. Expression expression;
  323. CHECK_MESSAGE(
  324. expression.parse("\\") == ERR_INVALID_PARAMETER,
  325. "The expression shouldn't parse successfully.");
  326. CHECK_MESSAGE(
  327. expression.parse("0++") == ERR_INVALID_PARAMETER,
  328. "The expression shouldn't parse successfully.");
  329. CHECK_MESSAGE(
  330. expression.parse("()") == ERR_INVALID_PARAMETER,
  331. "The expression shouldn't parse successfully.");
  332. CHECK_MESSAGE(
  333. expression.parse("()()") == ERR_INVALID_PARAMETER,
  334. "The expression shouldn't parse successfully.");
  335. CHECK_MESSAGE(
  336. expression.parse("() - ()") == ERR_INVALID_PARAMETER,
  337. "The expression shouldn't parse successfully.");
  338. CHECK_MESSAGE(
  339. expression.parse("() * 12345") == ERR_INVALID_PARAMETER,
  340. "The expression shouldn't parse successfully.");
  341. CHECK_MESSAGE(
  342. expression.parse("() * 12345") == ERR_INVALID_PARAMETER,
  343. "The expression shouldn't parse successfully.");
  344. CHECK_MESSAGE(
  345. expression.parse("123'456") == ERR_INVALID_PARAMETER,
  346. "The expression shouldn't parse successfully.");
  347. CHECK_MESSAGE(
  348. expression.parse("123\"456") == ERR_INVALID_PARAMETER,
  349. "The expression shouldn't parse successfully.");
  350. }
  351. TEST_CASE("[Expression] Unusual expressions") {
  352. Expression expression;
  353. // Redundant parentheses don't cause a parse error as long as they're matched.
  354. CHECK_MESSAGE(
  355. expression.parse("(((((((((((((((666)))))))))))))))") == OK,
  356. "The expression should parse successfully.");
  357. // Using invalid identifiers doesn't cause a parse error.
  358. ERR_PRINT_OFF;
  359. CHECK_MESSAGE(
  360. expression.parse("hello + hello") == OK,
  361. "The expression should parse successfully.");
  362. CHECK_MESSAGE(
  363. int(expression.execute()) == 0,
  364. "The expression should return the expected result.");
  365. ERR_PRINT_ON;
  366. ERR_PRINT_OFF;
  367. CHECK_MESSAGE(
  368. expression.parse("$1.00 + ???5") == OK,
  369. "The expression should parse successfully.");
  370. CHECK_MESSAGE(
  371. int(expression.execute()) == 0,
  372. "The expression should return the expected result.");
  373. ERR_PRINT_ON;
  374. // Commas can't be used as a decimal parameter.
  375. CHECK_MESSAGE(
  376. expression.parse("123,456") == OK,
  377. "The expression should parse successfully.");
  378. CHECK_MESSAGE(
  379. int(expression.execute()) == 123,
  380. "The expression should return the expected result.");
  381. // Spaces can't be used as a separator for large numbers.
  382. CHECK_MESSAGE(
  383. expression.parse("123 456") == OK,
  384. "The expression should parse successfully.");
  385. CHECK_MESSAGE(
  386. int(expression.execute()) == 123,
  387. "The expression should return the expected result.");
  388. // Division by zero is accepted, even though it prints an error message normally.
  389. CHECK_MESSAGE(
  390. expression.parse("-25.4 / 0") == OK,
  391. "The expression should parse successfully.");
  392. ERR_PRINT_OFF;
  393. CHECK_MESSAGE(
  394. Math::is_inf(double(expression.execute())),
  395. "`-25.4 / 0` should return inf.");
  396. ERR_PRINT_ON;
  397. CHECK_MESSAGE(
  398. expression.parse("0 / 0") == OK,
  399. "The expression should parse successfully.");
  400. ERR_PRINT_OFF;
  401. CHECK_MESSAGE(
  402. int(expression.execute()) == 0,
  403. "`0 / 0` should return 0.");
  404. ERR_PRINT_ON;
  405. // The tests below currently crash the engine.
  406. //
  407. //CHECK_MESSAGE(
  408. // expression.parse("(-9223372036854775807 - 1) % -1") == OK,
  409. // "The expression should parse successfully.");
  410. //CHECK_MESSAGE(
  411. // int64_t(expression.execute()) == 0,
  412. // "`(-9223372036854775807 - 1) % -1` should return the expected result.");
  413. //
  414. //CHECK_MESSAGE(
  415. // expression.parse("(-9223372036854775807 - 1) / -1") == OK,
  416. // "The expression should parse successfully.");
  417. //CHECK_MESSAGE(
  418. // int64_t(expression.execute()) == 0,
  419. // "`(-9223372036854775807 - 1) / -1` should return the expected result.");
  420. }
  421. } // namespace TestExpression