123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- /**************************************************************************/
- /* test_expression.h */
- /**************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /**************************************************************************/
- /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
- /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
- /* */
- /* 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 division 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(
- double(expression.execute()) == doctest::Approx(-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(
- double(expression.execute()) == doctest::Approx(5),
- "Float addition should return the expected result.");
- CHECK_MESSAGE(
- expression.parse("3.0 / 10") == OK,
- "Float / integer division should parse successfully.");
- CHECK_MESSAGE(
- double(expression.execute()) == doctest::Approx(0.3),
- "Float / integer division should return the expected result.");
- CHECK_MESSAGE(
- expression.parse("3 / 10.0") == OK,
- "Basic integer / float division should parse successfully.");
- CHECK_MESSAGE(
- double(expression.execute()) == doctest::Approx(0.3),
- "Basic integer / float division should return the expected result.");
- CHECK_MESSAGE(
- expression.parse("3.0 / 10.0") == OK,
- "Float / float division should parse successfully.");
- CHECK_MESSAGE(
- double(expression.execute()) == doctest::Approx(0.3),
- "Float / float division 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(
- double(expression.execute()) == doctest::Approx(20.18905),
- "Float multiplication-addition-subtraction-division should return the expected result.");
- }
- TEST_CASE("[Expression] Floating-point notation") {
- Expression expression;
- CHECK_MESSAGE(
- expression.parse("2.") == OK,
- "The expression should parse successfully.");
- CHECK_MESSAGE(
- double(expression.execute()) == doctest::Approx(2.0),
- "The expression should return the expected result.");
- CHECK_MESSAGE(
- expression.parse("(2.)") == OK,
- "The expression should parse successfully.");
- CHECK_MESSAGE(
- double(expression.execute()) == doctest::Approx(2.0),
- "The expression should return the expected result.");
- CHECK_MESSAGE(
- expression.parse(".3") == OK,
- "The expression should parse successfully.");
- CHECK_MESSAGE(
- double(expression.execute()) == doctest::Approx(0.3),
- "The expression should return the expected result.");
- CHECK_MESSAGE(
- expression.parse("2.+5.") == OK,
- "The expression should parse successfully.");
- CHECK_MESSAGE(
- double(expression.execute()) == doctest::Approx(7.0),
- "The expression should return the expected result.");
- CHECK_MESSAGE(
- expression.parse(".3-.8") == OK,
- "The expression should parse successfully.");
- CHECK_MESSAGE(
- double(expression.execute()) == doctest::Approx(-0.5),
- "The expression should return the expected result.");
- CHECK_MESSAGE(
- expression.parse("2.+.2") == OK,
- "The expression should parse successfully.");
- CHECK_MESSAGE(
- double(expression.execute()) == doctest::Approx(2.2),
- "The expression should return the expected result.");
- CHECK_MESSAGE(
- expression.parse(".0*0.") == OK,
- "The expression should parse successfully.");
- CHECK_MESSAGE(
- double(expression.execute()) == doctest::Approx(0.0),
- "The expression 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(
- double(expression.execute()) == doctest::Approx(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(
- double(expression.execute()) == doctest::Approx(2e5),
- "The expression should return the expected result.");
- CHECK_MESSAGE(
- expression.parse("2e.5") == OK,
- "The expression should parse successfully.");
- CHECK_MESSAGE(
- double(expression.execute()) == doctest::Approx(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("snapped(sin(0.5), 0.01)") == OK,
- "The expression should parse successfully.");
- CHECK_MESSAGE(
- double(expression.execute()) == doctest::Approx(0.48),
- "`snapped(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(double(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_inf(double(expression.execute())),
- "`-25.4 / 0` should return inf.");
- 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
|