diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_main.cpp | 59 | ||||
-rw-r--r-- | tests/test_main.h | 5 | ||||
-rw-r--r-- | tests/test_math.cpp | 2 | ||||
-rw-r--r-- | tests/test_string.h | 2 | ||||
-rw-r--r-- | tests/test_variant.h | 111 |
5 files changed, 122 insertions, 57 deletions
diff --git a/tests/test_main.cpp b/tests/test_main.cpp index 0fb9f2fcda..7c656517b6 100644 --- a/tests/test_main.cpp +++ b/tests/test_main.cpp @@ -32,8 +32,6 @@ #include "core/list.h" -#ifdef DEBUG_ENABLED - #include "test_astar.h" #include "test_basis.h" #include "test_class_db.h" @@ -48,42 +46,18 @@ #include "test_shader_lang.h" #include "test_string.h" #include "test_validate_testing.h" +#include "test_variant.h" #include "modules/modules_tests.gen.h" #include "thirdparty/doctest/doctest.h" -const char **tests_get_names() { - static const char *test_names[] = { - "*", - "all", - "math", - "basis", - "physics_2d", - "physics_3d", - "render", - "oa_hash_map", - "class_db", - "gui", - "shaderlang", - "gd_tokenizer", - "gd_parser", - "gd_compiler", - "gd_bytecode", - "ordered_hash_map", - "astar", - nullptr - }; - - return test_names; -} - int test_main(int argc, char *argv[]) { - // doctest runner for when legacy unit tests are no found + // Doctest runner. doctest::Context test_context; List<String> valid_arguments; - // clean arguments of --test from the args + // Clean arguments of "--test" from the args. int argument_count = 0; for (int x = 0; x < argc; x++) { if (strncmp(argv[x], "--test", 6) != 0) { @@ -91,15 +65,14 @@ int test_main(int argc, char *argv[]) { argument_count++; } } - - // convert godot command line arguments back to standard arguments. + // Convert Godot command line arguments back to standard arguments. char **args = new char *[valid_arguments.size()]; for (int x = 0; x < valid_arguments.size(); x++) { - // operation to convert godot string to non wchar string + // Operation to convert Godot string to non wchar string. const char *str = valid_arguments[x].utf8().ptr(); - // allocate the string copy + // Allocate the string copy. args[x] = new char[strlen(str) + 1]; - // copy this into memory + // Copy this into memory. std::memcpy(args[x], str, strlen(str) + 1); } @@ -108,22 +81,8 @@ int test_main(int argc, char *argv[]) { test_context.setOption("order-by", "name"); test_context.setOption("abort-after", 5); test_context.setOption("no-breaks", true); - delete[] args; - return test_context.run(); -} -#else - -const char **tests_get_names() { - static const char *test_names[] = { - nullptr - }; - - return test_names; -} + delete[] args; -int test_main(int argc, char *argv[]) { - return 0; + return test_context.run(); } - -#endif diff --git a/tests/test_main.h b/tests/test_main.h index 8273b74eac..983bfde402 100644 --- a/tests/test_main.h +++ b/tests/test_main.h @@ -31,11 +31,6 @@ #ifndef TEST_MAIN_H #define TEST_MAIN_H -#include "core/list.h" -#include "core/os/main_loop.h" -#include "core/ustring.h" - -const char **tests_get_names(); int test_main(int argc, char *argv[]); #endif // TEST_MAIN_H diff --git a/tests/test_math.cpp b/tests/test_math.cpp index 5f84bad4e9..84a85be2f6 100644 --- a/tests/test_math.cpp +++ b/tests/test_math.cpp @@ -242,7 +242,7 @@ class GetClassAndNamespace { if (code[idx] == '-' || (code[idx] >= '0' && code[idx] <= '9')) { //a number const CharType *rptr; - double number = String::to_double(&code[idx], &rptr); + double number = String::to_float(&code[idx], &rptr); idx += (rptr - &code[idx]); value = number; return TK_NUMBER; diff --git a/tests/test_string.h b/tests/test_string.h index 25fd513a1a..310be31f4b 100644 --- a/tests/test_string.h +++ b/tests/test_string.h @@ -209,7 +209,7 @@ TEST_CASE("[String] String to float") { static const double num[4] = { -12348298412.2, 0.05, 2.0002, -0.0001 }; for (int i = 0; i < 4; i++) { - CHECK(!(ABS(String(nums[i]).to_double() - num[i]) > 0.00001)); + CHECK(!(ABS(String(nums[i]).to_float() - num[i]) > 0.00001)); } } diff --git a/tests/test_variant.h b/tests/test_variant.h new file mode 100644 index 0000000000..06dcfde664 --- /dev/null +++ b/tests/test_variant.h @@ -0,0 +1,111 @@ +/*************************************************************************/ +/* test_variant.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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_VARIANT_H +#define TEST_VARIANT_H + +#include "core/variant.h" +#include "core/variant_parser.h" + +#include "thirdparty/doctest/doctest.h" + +namespace TestVariant { + +TEST_CASE("[Variant] Writer and parser integer") { + int64_t a32 = 2147483648; // 2^31, so out of bounds for 32-bit signed int [-2^31,-2^31-1]. + String a32_str; + VariantWriter::write_to_string(a32, a32_str); + + CHECK_MESSAGE(a32_str != "-2147483648", "Should not wrap around"); + + int64_t b64 = 9223372036854775807; // 2^63-1, upper bound for signed 64-bit int. + String b64_str; + VariantWriter::write_to_string(b64, b64_str); + + CHECK_MESSAGE(b64_str == "9223372036854775807", "Should not wrap around."); + + VariantParser::StreamString ss; + String errs; + int line; + Variant b64_parsed; + int64_t b64_int_parsed; + + ss.s = b64_str; + VariantParser::parse(&ss, b64_parsed, errs, line); + b64_int_parsed = b64_parsed; + + CHECK_MESSAGE(b64_int_parsed == 9223372036854775807, "Should parse back."); + + ss.s = "9223372036854775808"; // Overflowed by one. + VariantParser::parse(&ss, b64_parsed, errs, line); + b64_int_parsed = b64_parsed; + + CHECK_MESSAGE(b64_int_parsed == 9223372036854775807, "The result should be clamped to max value."); + + ss.s = "1e100"; // Googol! Scientific notation. + VariantParser::parse(&ss, b64_parsed, errs, line); + b64_int_parsed = b64_parsed; + + CHECK_MESSAGE(b64_int_parsed == 9223372036854775807, "The result should be clamped to max value."); +} + +TEST_CASE("[Variant] Writer and parser float") { + // Assuming real_t is double. + real_t a64 = 340282346638528859811704183484516925440.0; // std::numeric_limits<real_t>::max() + String a64_str; + VariantWriter::write_to_string(a64, a64_str); + + CHECK_MESSAGE(a64_str == "3.40282e+38", "Writes in scientific notation."); + CHECK_MESSAGE(a64_str != "inf", "Should not overflow."); + CHECK_MESSAGE(a64_str != "nan", "The result should be defined."); + + VariantParser::StreamString ss; + String errs; + int line; + Variant b64_parsed; + real_t b64_float_parsed; + + ss.s = a64_str; + VariantParser::parse(&ss, b64_parsed, errs, line); + b64_float_parsed = b64_parsed; + + CHECK_MESSAGE(b64_float_parsed == 340282001837565597733306976381245063168.0, "Should parse back."); + // Loses precision, but that's alright. + + ss.s = "1.0e+100"; // Float version of Googol! + VariantParser::parse(&ss, b64_parsed, errs, line); + b64_float_parsed = b64_parsed; + + CHECK_MESSAGE(b64_float_parsed == 340282001837565597733306976381245063168.0, "Should not overflow."); +} + +} // namespace TestVariant + +#endif // TEST_VARIANT_H |