diff options
Diffstat (limited to 'tests/core')
-rw-r--r-- | tests/core/config/test_project_settings.h | 2 | ||||
-rw-r--r-- | tests/core/io/test_marshalls.h | 99 | ||||
-rw-r--r-- | tests/core/math/test_aabb.h | 2 | ||||
-rw-r--r-- | tests/core/math/test_color.h | 2 | ||||
-rw-r--r-- | tests/core/math/test_rect2.h | 2 | ||||
-rw-r--r-- | tests/core/object/test_object.h | 4 | ||||
-rw-r--r-- | tests/core/string/test_fuzzy_search.h | 83 | ||||
-rw-r--r-- | tests/core/string/test_string.h | 61 | ||||
-rw-r--r-- | tests/core/string/test_translation.h | 2 | ||||
-rw-r--r-- | tests/core/string/test_translation_server.h | 88 | ||||
-rw-r--r-- | tests/core/templates/test_a_hash_map.h | 295 | ||||
-rw-r--r-- | tests/core/variant/test_variant_utility.h | 2 |
12 files changed, 599 insertions, 43 deletions
diff --git a/tests/core/config/test_project_settings.h b/tests/core/config/test_project_settings.h index 0e1058a626..5a000b19a0 100644 --- a/tests/core/config/test_project_settings.h +++ b/tests/core/config/test_project_settings.h @@ -40,7 +40,7 @@ class TestProjectSettingsInternalsAccessor { public: static String &resource_path() { return ProjectSettings::get_singleton()->resource_path; - }; + } }; namespace TestProjectSettings { diff --git a/tests/core/io/test_marshalls.h b/tests/core/io/test_marshalls.h index de8d6e1406..6716984681 100644 --- a/tests/core/io/test_marshalls.h +++ b/tests/core/io/test_marshalls.h @@ -160,7 +160,7 @@ TEST_CASE("[Marshalls] NIL Variant encoding") { uint8_t buffer[4]; CHECK(encode_variant(variant, buffer, r_len) == OK); - CHECK_MESSAGE(r_len == 4, "Length == 4 bytes for header"); + CHECK_MESSAGE(r_len == 4, "Length == 4 bytes for header."); CHECK_MESSAGE(buffer[0] == 0x00, "Variant::NIL"); CHECK(buffer[1] == 0x00); CHECK(buffer[2] == 0x00); @@ -174,7 +174,7 @@ TEST_CASE("[Marshalls] INT 32 bit Variant encoding") { uint8_t buffer[8]; CHECK(encode_variant(variant, buffer, r_len) == OK); - CHECK_MESSAGE(r_len == 8, "Length == 4 bytes for header + 4 bytes for int32_t"); + CHECK_MESSAGE(r_len == 8, "Length == 4 bytes for header + 4 bytes for `int32_t`."); CHECK_MESSAGE(buffer[0] == 0x02, "Variant::INT"); CHECK(buffer[1] == 0x00); CHECK(buffer[2] == 0x00); @@ -192,7 +192,7 @@ TEST_CASE("[Marshalls] INT 64 bit Variant encoding") { uint8_t buffer[12]; CHECK(encode_variant(variant, buffer, r_len) == OK); - CHECK_MESSAGE(r_len == 12, "Length == 4 bytes for header + 8 bytes for int64_t"); + CHECK_MESSAGE(r_len == 12, "Length == 4 bytes for header + 8 bytes for `int64_t`."); CHECK_MESSAGE(buffer[0] == 0x02, "Variant::INT"); CHECK(buffer[1] == 0x00); CHECK_MESSAGE(buffer[2] == 0x01, "HEADER_DATA_FLAG_64"); @@ -214,7 +214,7 @@ TEST_CASE("[Marshalls] FLOAT single precision Variant encoding") { uint8_t buffer[8]; CHECK(encode_variant(variant, buffer, r_len) == OK); - CHECK_MESSAGE(r_len == 8, "Length == 4 bytes for header + 4 bytes for float"); + CHECK_MESSAGE(r_len == 8, "Length == 4 bytes for header + 4 bytes for `float`."); CHECK_MESSAGE(buffer[0] == 0x03, "Variant::FLOAT"); CHECK(buffer[1] == 0x00); CHECK(buffer[2] == 0x00); @@ -232,7 +232,7 @@ TEST_CASE("[Marshalls] FLOAT double precision Variant encoding") { uint8_t buffer[12]; CHECK(encode_variant(variant, buffer, r_len) == OK); - CHECK_MESSAGE(r_len == 12, "Length == 4 bytes for header + 8 bytes for double"); + CHECK_MESSAGE(r_len == 12, "Length == 4 bytes for header + 8 bytes for `double`."); CHECK_MESSAGE(buffer[0] == 0x03, "Variant::FLOAT"); CHECK(buffer[1] == 0x00); CHECK_MESSAGE(buffer[2] == 0x01, "HEADER_DATA_FLAG_64"); @@ -335,10 +335,10 @@ TEST_CASE("[Marshalls] Typed array encoding") { uint8_t buffer[24]; CHECK(encode_variant(array, buffer, r_len) == OK); - CHECK_MESSAGE(r_len == 24, "Length == 4 bytes for header + 4 bytes for array type + 4 bytes for array size + 12 bytes for element"); + CHECK_MESSAGE(r_len == 24, "Length == 4 bytes for header + 4 bytes for array type + 4 bytes for array size + 12 bytes for element."); CHECK_MESSAGE(buffer[0] == 0x1c, "Variant::ARRAY"); CHECK(buffer[1] == 0x00); - CHECK_MESSAGE(buffer[2] == 0x01, "HEADER_DATA_FIELD_TYPED_ARRAY_BUILTIN"); + CHECK_MESSAGE(buffer[2] == 0x01, "CONTAINER_TYPE_KIND_BUILTIN"); CHECK(buffer[3] == 0x00); // Check array type. CHECK_MESSAGE(buffer[4] == 0x02, "Variant::INT"); @@ -370,7 +370,7 @@ TEST_CASE("[Marshalls] Typed array decoding") { Variant variant; int r_len; uint8_t buffer[] = { - 0x1c, 0x00, 0x01, 0x00, // Variant::ARRAY, HEADER_DATA_FIELD_TYPED_ARRAY_BUILTIN + 0x1c, 0x00, 0x01, 0x00, // Variant::ARRAY, CONTAINER_TYPE_KIND_BUILTIN 0x02, 0x00, 0x00, 0x00, // Array type (Variant::INT). 0x01, 0x00, 0x00, 0x00, // Array size. 0x02, 0x00, 0x01, 0x00, // Element type (Variant::INT, HEADER_DATA_FLAG_64). @@ -386,6 +386,89 @@ TEST_CASE("[Marshalls] Typed array decoding") { CHECK(array[0] == Variant(uint64_t(0x0f123456789abcdef))); } +TEST_CASE("[Marshalls] Typed dicttionary encoding") { + int r_len; + Dictionary dictionary; + dictionary.set_typed(Variant::INT, StringName(), Ref<Script>(), Variant::INT, StringName(), Ref<Script>()); + dictionary[Variant(uint64_t(0x0f123456789abcdef))] = Variant(uint64_t(0x0f123456789abcdef)); + uint8_t buffer[40]; + + CHECK(encode_variant(dictionary, buffer, r_len) == OK); + CHECK_MESSAGE(r_len == 40, "Length == 4 bytes for header + 8 bytes for dictionary type + 4 bytes for dictionary size + 24 bytes for key-value pair."); + CHECK_MESSAGE(buffer[0] == 0x1b, "Variant::DICTIONARY"); + CHECK(buffer[1] == 0x00); + CHECK_MESSAGE(buffer[2] == 0x05, "key: CONTAINER_TYPE_KIND_BUILTIN | value: CONTAINER_TYPE_KIND_BUILTIN"); + CHECK(buffer[3] == 0x00); + // Check dictionary key type. + CHECK_MESSAGE(buffer[4] == 0x02, "Variant::INT"); + CHECK(buffer[5] == 0x00); + CHECK(buffer[6] == 0x00); + CHECK(buffer[7] == 0x00); + // Check dictionary value type. + CHECK_MESSAGE(buffer[8] == 0x02, "Variant::INT"); + CHECK(buffer[9] == 0x00); + CHECK(buffer[10] == 0x00); + CHECK(buffer[11] == 0x00); + // Check dictionary size. + CHECK(buffer[12] == 0x01); + CHECK(buffer[13] == 0x00); + CHECK(buffer[14] == 0x00); + CHECK(buffer[15] == 0x00); + // Check key type. + CHECK_MESSAGE(buffer[16] == 0x02, "Variant::INT"); + CHECK(buffer[17] == 0x00); + CHECK_MESSAGE(buffer[18] == 0x01, "HEADER_DATA_FLAG_64"); + CHECK(buffer[19] == 0x00); + // Check key value. + CHECK(buffer[20] == 0xef); + CHECK(buffer[21] == 0xcd); + CHECK(buffer[22] == 0xab); + CHECK(buffer[23] == 0x89); + CHECK(buffer[24] == 0x67); + CHECK(buffer[25] == 0x45); + CHECK(buffer[26] == 0x23); + CHECK(buffer[27] == 0xf1); + // Check value type. + CHECK_MESSAGE(buffer[28] == 0x02, "Variant::INT"); + CHECK(buffer[29] == 0x00); + CHECK_MESSAGE(buffer[30] == 0x01, "HEADER_DATA_FLAG_64"); + CHECK(buffer[31] == 0x00); + // Check value value. + CHECK(buffer[32] == 0xef); + CHECK(buffer[33] == 0xcd); + CHECK(buffer[34] == 0xab); + CHECK(buffer[35] == 0x89); + CHECK(buffer[36] == 0x67); + CHECK(buffer[37] == 0x45); + CHECK(buffer[38] == 0x23); + CHECK(buffer[39] == 0xf1); +} + +TEST_CASE("[Marshalls] Typed dictionary decoding") { + Variant variant; + int r_len; + uint8_t buffer[] = { + 0x1b, 0x00, 0x05, 0x00, // Variant::DICTIONARY, key: CONTAINER_TYPE_KIND_BUILTIN | value: CONTAINER_TYPE_KIND_BUILTIN + 0x02, 0x00, 0x00, 0x00, // Dictionary key type (Variant::INT). + 0x02, 0x00, 0x00, 0x00, // Dictionary value type (Variant::INT). + 0x01, 0x00, 0x00, 0x00, // Dictionary size. + 0x02, 0x00, 0x01, 0x00, // Key type (Variant::INT, HEADER_DATA_FLAG_64). + 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0xf1, // Key value. + 0x02, 0x00, 0x01, 0x00, // Value type (Variant::INT, HEADER_DATA_FLAG_64). + 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0xf1, // Value value. + }; + + CHECK(decode_variant(variant, buffer, 40, &r_len) == OK); + CHECK(r_len == 40); + CHECK(variant.get_type() == Variant::DICTIONARY); + Dictionary dictionary = variant; + CHECK(dictionary.get_typed_key_builtin() == Variant::INT); + CHECK(dictionary.get_typed_value_builtin() == Variant::INT); + CHECK(dictionary.size() == 1); + CHECK(dictionary.has(Variant(uint64_t(0x0f123456789abcdef)))); + CHECK(dictionary[Variant(uint64_t(0x0f123456789abcdef))] == Variant(uint64_t(0x0f123456789abcdef))); +} + } // namespace TestMarshalls #endif // TEST_MARSHALLS_H diff --git a/tests/core/math/test_aabb.h b/tests/core/math/test_aabb.h index 741e6af5d4..eb6977fd69 100644 --- a/tests/core/math/test_aabb.h +++ b/tests/core/math/test_aabb.h @@ -48,7 +48,7 @@ TEST_CASE("[AABB] Constructor methods") { TEST_CASE("[AABB] String conversion") { CHECK_MESSAGE( - String(AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6))) == "[P: (-1.5, 2, -2.5), S: (4, 5, 6)]", + String(AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6))) == "[P: (-1.5, 2.0, -2.5), S: (4.0, 5.0, 6.0)]", "The string representation should match the expected value."); } diff --git a/tests/core/math/test_color.h b/tests/core/math/test_color.h index bd2d4f40e5..3d36079102 100644 --- a/tests/core/math/test_color.h +++ b/tests/core/math/test_color.h @@ -140,7 +140,7 @@ TEST_CASE("[Color] Conversion methods") { cyan.to_rgba64() == 0x0000'ffff'ffff'ffff, "The returned 64-bit BGR number should match the expected value."); CHECK_MESSAGE( - String(cyan) == "(0, 1, 1, 1)", + String(cyan) == "(0.0, 1.0, 1.0, 1.0)", "The string representation should match the expected value."); } diff --git a/tests/core/math/test_rect2.h b/tests/core/math/test_rect2.h index c4368808a6..a93bfeb71b 100644 --- a/tests/core/math/test_rect2.h +++ b/tests/core/math/test_rect2.h @@ -57,7 +57,7 @@ TEST_CASE("[Rect2] Constructor methods") { TEST_CASE("[Rect2] String conversion") { // Note: This also depends on the Vector2 string representation. CHECK_MESSAGE( - String(Rect2(0, 100, 1280, 720)) == "[P: (0, 100), S: (1280, 720)]", + String(Rect2(0, 100, 1280, 720)) == "[P: (0.0, 100.0), S: (1280.0, 720.0)]", "The string representation should match the expected value."); } diff --git a/tests/core/object/test_object.h b/tests/core/object/test_object.h index e703698ec6..55c53e2d03 100644 --- a/tests/core/object/test_object.h +++ b/tests/core/object/test_object.h @@ -95,10 +95,10 @@ public: } bool property_can_revert(const StringName &p_name) const override { return false; - }; + } bool property_get_revert(const StringName &p_name, Variant &r_ret) const override { return false; - }; + } void get_method_list(List<MethodInfo> *p_list) const override { } bool has_method(const StringName &p_method) const override { diff --git a/tests/core/string/test_fuzzy_search.h b/tests/core/string/test_fuzzy_search.h new file mode 100644 index 0000000000..d647ebdd1a --- /dev/null +++ b/tests/core/string/test_fuzzy_search.h @@ -0,0 +1,83 @@ +/**************************************************************************/ +/* test_fuzzy_search.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_FUZZY_SEARCH_H +#define TEST_FUZZY_SEARCH_H + +#include "core/string/fuzzy_search.h" +#include "tests/test_macros.h" + +namespace TestFuzzySearch { + +struct FuzzySearchTestCase { + String query; + String expected; +}; + +// Ideally each of these test queries should represent a different aspect, and potentially bottleneck, of the search process. +const FuzzySearchTestCase test_cases[] = { + // Short query, many matches, few adjacent characters + { "///gd", "./menu/hud/hud.gd" }, + // Filename match with typo + { "sm.png", "./entity/blood_sword/sam.png" }, + // Multipart filename word matches + { "ham ", "./entity/game_trap/ha_missed_me.wav" }, + // Single word token matches + { "push background", "./entity/background_zone1/background/push.png" }, + // Long token matches + { "background_freighter background png", "./entity/background_freighter/background/background.png" }, + // Many matches, many short tokens + { "menu menu characters wav", "./menu/menu/characters/smoker/0.wav" }, + // Maximize total matches + { "entity gd", "./entity/entity_man.gd" } +}; + +Vector<String> load_test_data() { + Ref<FileAccess> fp = FileAccess::open(TestUtils::get_data_path("fuzzy_search/project_dir_tree.txt"), FileAccess::READ); + REQUIRE(fp.is_valid()); + return fp->get_as_utf8_string().split("\n"); +} + +TEST_CASE("[FuzzySearch] Test fuzzy search results") { + FuzzySearch search; + Vector<FuzzySearchResult> results; + Vector<String> targets = load_test_data(); + + for (FuzzySearchTestCase test_case : test_cases) { + search.set_query(test_case.query); + search.search_all(targets, results); + CHECK_GT(results.size(), 0); + CHECK_EQ(results[0].target, test_case.expected); + } +} + +} //namespace TestFuzzySearch + +#endif // TEST_FUZZY_SEARCH_H diff --git a/tests/core/string/test_string.h b/tests/core/string/test_string.h index 8559737e74..9adc97e845 100644 --- a/tests/core/string/test_string.h +++ b/tests/core/string/test_string.h @@ -389,6 +389,19 @@ TEST_CASE("[String] Find") { MULTICHECK_STRING_INT_EQ(s, rfind, "", 15, -1); } +TEST_CASE("[String] Find character") { + String s = "racecar"; + CHECK_EQ(s.find_char('r'), 0); + CHECK_EQ(s.find_char('r', 1), 6); + CHECK_EQ(s.find_char('e'), 3); + CHECK_EQ(s.find_char('e', 4), -1); + + CHECK_EQ(s.rfind_char('r'), 6); + CHECK_EQ(s.rfind_char('r', 5), 0); + CHECK_EQ(s.rfind_char('e'), 3); + CHECK_EQ(s.rfind_char('e', 2), -1); +} + TEST_CASE("[String] Find case insensitive") { String s = "Pretty Whale Whale"; MULTICHECK_STRING_EQ(s, findn, "WHA", 7); @@ -455,16 +468,32 @@ TEST_CASE("[String] Erasing") { } TEST_CASE("[String] Number to string") { - CHECK(String::num(0) == "0"); - CHECK(String::num(0.0) == "0"); // No trailing zeros. - CHECK(String::num(-0.0) == "-0"); // Includes sign even for zero. + CHECK(String::num(0) == "0.0"); // The method takes double, so always add zeros. + CHECK(String::num(0.0) == "0.0"); + CHECK(String::num(-0.0) == "-0.0"); // Includes sign even for zero. CHECK(String::num(3.141593) == "3.141593"); CHECK(String::num(3.141593, 3) == "3.142"); + CHECK(String::num(42.100023, 4) == "42.1"); // No trailing zeros. CHECK(String::num_scientific(30000000) == "3e+07"); + + // String::num_int64 tests. CHECK(String::num_int64(3141593) == "3141593"); + CHECK(String::num_int64(-3141593) == "-3141593"); CHECK(String::num_int64(0xA141593, 16) == "a141593"); CHECK(String::num_int64(0xA141593, 16, true) == "A141593"); - CHECK(String::num(42.100023, 4) == "42.1"); // No trailing zeros. + ERR_PRINT_OFF; + CHECK(String::num_int64(3141593, 1) == ""); // Invalid base < 2. + CHECK(String::num_int64(3141593, 37) == ""); // Invalid base > 36. + ERR_PRINT_ON; + + // String::num_uint64 tests. + CHECK(String::num_uint64(4294967295) == "4294967295"); + CHECK(String::num_uint64(0xF141593, 16) == "f141593"); + CHECK(String::num_uint64(0xF141593, 16, true) == "F141593"); + ERR_PRINT_OFF; + CHECK(String::num_uint64(4294967295, 1) == ""); // Invalid base < 2. + CHECK(String::num_uint64(4294967295, 37) == ""); // Invalid base > 36. + ERR_PRINT_ON; // String::num_real tests. CHECK(String::num_real(1.0) == "1.0"); @@ -476,15 +505,15 @@ TEST_CASE("[String] Number to string") { CHECK(String::num_real(3.141593) == "3.141593"); CHECK(String::num_real(3.141) == "3.141"); // No trailing zeros. #ifdef REAL_T_IS_DOUBLE - CHECK_MESSAGE(String::num_real(123.456789) == "123.456789", "Prints the appropriate amount of digits for real_t = double."); - CHECK_MESSAGE(String::num_real(-123.456789) == "-123.456789", "Prints the appropriate amount of digits for real_t = double."); - CHECK_MESSAGE(String::num_real(Math_PI) == "3.14159265358979", "Prints the appropriate amount of digits for real_t = double."); - CHECK_MESSAGE(String::num_real(3.1415f) == "3.1414999961853", "Prints more digits of 32-bit float when real_t = double (ones that would be reliable for double) and no trailing zero."); + CHECK_MESSAGE(String::num_real(real_t(123.456789)) == "123.456789", "Prints the appropriate amount of digits for real_t = double."); + CHECK_MESSAGE(String::num_real(real_t(-123.456789)) == "-123.456789", "Prints the appropriate amount of digits for real_t = double."); + CHECK_MESSAGE(String::num_real(real_t(Math_PI)) == "3.14159265358979", "Prints the appropriate amount of digits for real_t = double."); + CHECK_MESSAGE(String::num_real(real_t(3.1415f)) == "3.1414999961853", "Prints more digits of 32-bit float when real_t = double (ones that would be reliable for double) and no trailing zero."); #else - CHECK_MESSAGE(String::num_real(123.456789) == "123.4568", "Prints the appropriate amount of digits for real_t = float."); - CHECK_MESSAGE(String::num_real(-123.456789) == "-123.4568", "Prints the appropriate amount of digits for real_t = float."); - CHECK_MESSAGE(String::num_real(Math_PI) == "3.141593", "Prints the appropriate amount of digits for real_t = float."); - CHECK_MESSAGE(String::num_real(3.1415f) == "3.1415", "Prints only reliable digits of 32-bit float when real_t = float."); + CHECK_MESSAGE(String::num_real(real_t(123.456789)) == "123.4568", "Prints the appropriate amount of digits for real_t = float."); + CHECK_MESSAGE(String::num_real(real_t(-123.456789)) == "-123.4568", "Prints the appropriate amount of digits for real_t = float."); + CHECK_MESSAGE(String::num_real(real_t(Math_PI)) == "3.141593", "Prints the appropriate amount of digits for real_t = float."); + CHECK_MESSAGE(String::num_real(real_t(3.1415f)) == "3.1415", "Prints only reliable digits of 32-bit float when real_t = float."); #endif // REAL_T_IS_DOUBLE // Checks doubles with many decimal places. @@ -493,7 +522,7 @@ TEST_CASE("[String] Number to string") { CHECK(String::num(-0.0000012345432123454321) == "-0.00000123454321"); CHECK(String::num(-10000.0000012345432123454321) == "-10000.0000012345"); CHECK(String::num(0.0000000000012345432123454321) == "0.00000000000123"); - CHECK(String::num(0.0000000000012345432123454321, 3) == "0"); + CHECK(String::num(0.0000000000012345432123454321, 3) == "0.0"); // Note: When relevant (remainder > 0.5), the last digit gets rounded up, // which can also lead to not include a trailing zero, e.g. "...89" -> "...9". @@ -1238,6 +1267,12 @@ TEST_CASE("[String] is_subsequence_of") { CHECK(String("Sub").is_subsequence_ofn(a)); } +TEST_CASE("[String] is_lowercase") { + CHECK(String("abcd1234 !@#$%^&*()_-=+,.<>/\\|[]{};':\"`~").is_lowercase()); + CHECK(String("").is_lowercase()); + CHECK(!String("abc_ABC").is_lowercase()); +} + TEST_CASE("[String] match") { CHECK(String("img1.png").match("*.png")); CHECK(!String("img1.jpeg").match("*.png")); diff --git a/tests/core/string/test_translation.h b/tests/core/string/test_translation.h index 7c389191e3..2b1069d40c 100644 --- a/tests/core/string/test_translation.h +++ b/tests/core/string/test_translation.h @@ -161,7 +161,7 @@ TEST_CASE("[TranslationCSV] CSV import") { List<String> gen_files; - Error result = import_csv_translation->import(TestUtils::get_data_path("translations.csv"), + Error result = import_csv_translation->import(0, TestUtils::get_data_path("translations.csv"), "", options, nullptr, &gen_files); CHECK(result == OK); CHECK(gen_files.size() == 4); diff --git a/tests/core/string/test_translation_server.h b/tests/core/string/test_translation_server.h index ac1599f2e8..6668a7b57b 100644 --- a/tests/core/string/test_translation_server.h +++ b/tests/core/string/test_translation_server.h @@ -37,27 +37,37 @@ namespace TestTranslationServer { TEST_CASE("[TranslationServer] Translation operations") { - Ref<Translation> t = memnew(Translation); - t->set_locale("uk"); - t->add_message("Good Morning", String::utf8("Добрий ранок")); + Ref<Translation> t1 = memnew(Translation); + t1->set_locale("uk"); + t1->add_message("Good Morning", String(U"Добрий ранок")); + + Ref<Translation> t2 = memnew(Translation); + t2->set_locale("uk"); + t2->add_message("Hello Godot", String(U"你好戈多")); TranslationServer *ts = TranslationServer::get_singleton(); + // Adds translation for UK locale for the first time. int l_count_before = ts->get_loaded_locales().size(); - ts->add_translation(t); + ts->add_translation(t1); int l_count_after = ts->get_loaded_locales().size(); - // Newly created Translation object should be added to the list, so the counter should increase, too. CHECK(l_count_after > l_count_before); - Ref<Translation> trans = ts->get_translation_object("uk"); - CHECK(trans.is_valid()); + // Adds translation for UK locale again. + ts->add_translation(t2); + CHECK_EQ(ts->get_loaded_locales().size(), l_count_after); + + // Removing that translation. + ts->remove_translation(t2); + CHECK_EQ(ts->get_loaded_locales().size(), l_count_after); + + CHECK(ts->get_translation_object("uk").is_valid()); ts->set_locale("uk"); CHECK(ts->translate("Good Morning") == String::utf8("Добрий ранок")); - ts->remove_translation(t); - trans = ts->get_translation_object("uk"); - CHECK(trans.is_null()); + ts->remove_translation(t1); + CHECK(ts->get_translation_object("uk").is_null()); // If no suitable Translation object has been found - the original message should be returned. CHECK(ts->translate("Good Morning") == "Good Morning"); } @@ -110,18 +120,50 @@ TEST_CASE("[TranslationServer] Comparing locales") { locale_a = "sr-Latn-CS"; locale_b = "sr-Latn-RS"; - // Two elements from locales match. + // Script matches (+1) but country doesn't (-1). res = ts->compare_locales(locale_a, locale_b); - CHECK(res == 2); + CHECK(res == 5); locale_a = "uz-Cyrl-UZ"; locale_b = "uz-Latn-UZ"; - // Two elements match, but they are not sequentual. + // Country matches (+1) but script doesn't (-1). + res = ts->compare_locales(locale_a, locale_b); + + CHECK(res == 5); + + locale_a = "aa-Latn-ER"; + locale_b = "aa-Latn-ER-saaho"; + + // Script and country match (+2) with variant on one locale (+0). res = ts->compare_locales(locale_a, locale_b); - CHECK(res == 2); + CHECK(res == 7); + + locale_a = "uz-Cyrl-UZ"; + locale_b = "uz-Latn-KG"; + + // Both script and country mismatched (-2). + res = ts->compare_locales(locale_a, locale_b); + + CHECK(res == 3); + + locale_a = "es-ES"; + locale_b = "es-AR"; + + // Mismatched country (-1). + res = ts->compare_locales(locale_a, locale_b); + + CHECK(res == 4); + + locale_a = "es"; + locale_b = "es-AR"; + + // No country for one locale (+0). + res = ts->compare_locales(locale_a, locale_b); + + CHECK(res == 5); locale_a = "es-EC"; locale_b = "fr-LU"; @@ -130,6 +172,24 @@ TEST_CASE("[TranslationServer] Comparing locales") { res = ts->compare_locales(locale_a, locale_b); CHECK(res == 0); + + locale_a = "zh-HK"; + locale_b = "zh"; + + // In full standardization, zh-HK becomes zh_Hant_HK and zh becomes + // zh_Hans_CN. Both script and country mismatch (-2). + res = ts->compare_locales(locale_a, locale_b); + + CHECK(res == 3); + + locale_a = "zh-CN"; + locale_b = "zh"; + + // In full standardization, zh and zh-CN both become zh_Hans_CN for an + // exact match. + res = ts->compare_locales(locale_a, locale_b); + + CHECK(res == 10); } } // namespace TestTranslationServer diff --git a/tests/core/templates/test_a_hash_map.h b/tests/core/templates/test_a_hash_map.h new file mode 100644 index 0000000000..e67ee7b441 --- /dev/null +++ b/tests/core/templates/test_a_hash_map.h @@ -0,0 +1,295 @@ +/**************************************************************************/ +/* test_a_hash_map.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_A_HASH_MAP_H +#define TEST_A_HASH_MAP_H + +#include "core/templates/a_hash_map.h" + +#include "tests/test_macros.h" + +namespace TestAHashMap { + +TEST_CASE("[AHashMap] Insert element") { + AHashMap<int, int> map; + AHashMap<int, int>::Iterator e = map.insert(42, 84); + + CHECK(e); + CHECK(e->key == 42); + CHECK(e->value == 84); + CHECK(map[42] == 84); + CHECK(map.has(42)); + CHECK(map.find(42)); +} + +TEST_CASE("[AHashMap] Overwrite element") { + AHashMap<int, int> map; + map.insert(42, 84); + map.insert(42, 1234); + + CHECK(map[42] == 1234); +} + +TEST_CASE("[AHashMap] Erase via element") { + AHashMap<int, int> map; + AHashMap<int, int>::Iterator e = map.insert(42, 84); + map.remove(e); + CHECK(!map.has(42)); + CHECK(!map.find(42)); +} + +TEST_CASE("[AHashMap] Erase via key") { + AHashMap<int, int> map; + map.insert(42, 84); + map.erase(42); + CHECK(!map.has(42)); + CHECK(!map.find(42)); +} + +TEST_CASE("[AHashMap] Size") { + AHashMap<int, int> map; + map.insert(42, 84); + map.insert(123, 84); + map.insert(123, 84); + map.insert(0, 84); + map.insert(123485, 84); + + CHECK(map.size() == 4); +} + +TEST_CASE("[AHashMap] Iteration") { + AHashMap<int, int> map; + + map.insert(42, 84); + map.insert(123, 12385); + map.insert(0, 12934); + map.insert(123485, 1238888); + map.insert(123, 111111); + + Vector<Pair<int, int>> expected; + expected.push_back(Pair<int, int>(42, 84)); + expected.push_back(Pair<int, int>(123, 111111)); + expected.push_back(Pair<int, int>(0, 12934)); + expected.push_back(Pair<int, int>(123485, 1238888)); + + int idx = 0; + for (const KeyValue<int, int> &E : map) { + CHECK(expected[idx] == Pair<int, int>(E.key, E.value)); + idx++; + } + + idx--; + for (AHashMap<int, int>::Iterator it = map.last(); it; --it) { + CHECK(expected[idx] == Pair<int, int>(it->key, it->value)); + idx--; + } +} + +TEST_CASE("[AHashMap] Const iteration") { + AHashMap<int, int> map; + map.insert(42, 84); + map.insert(123, 12385); + map.insert(0, 12934); + map.insert(123485, 1238888); + map.insert(123, 111111); + + const AHashMap<int, int> const_map = map; + + Vector<Pair<int, int>> expected; + expected.push_back(Pair<int, int>(42, 84)); + expected.push_back(Pair<int, int>(123, 111111)); + expected.push_back(Pair<int, int>(0, 12934)); + expected.push_back(Pair<int, int>(123485, 1238888)); + expected.push_back(Pair<int, int>(123, 111111)); + + int idx = 0; + for (const KeyValue<int, int> &E : const_map) { + CHECK(expected[idx] == Pair<int, int>(E.key, E.value)); + idx++; + } + + idx--; + for (AHashMap<int, int>::ConstIterator it = const_map.last(); it; --it) { + CHECK(expected[idx] == Pair<int, int>(it->key, it->value)); + idx--; + } +} + +TEST_CASE("[AHashMap] Replace key") { + AHashMap<int, int> map; + map.insert(42, 84); + map.insert(0, 12934); + CHECK(map.replace_key(0, 1)); + CHECK(map.has(1)); + CHECK(map[1] == 12934); +} + +TEST_CASE("[AHashMap] Clear") { + AHashMap<int, int> map; + map.insert(42, 84); + map.insert(123, 12385); + map.insert(0, 12934); + + map.clear(); + CHECK(!map.has(42)); + CHECK(map.size() == 0); + CHECK(map.is_empty()); +} + +TEST_CASE("[AHashMap] Get") { + AHashMap<int, int> map; + map.insert(42, 84); + map.insert(123, 12385); + map.insert(0, 12934); + + CHECK(map.get(123) == 12385); + map.get(123) = 10; + CHECK(map.get(123) == 10); + + CHECK(*map.getptr(0) == 12934); + *map.getptr(0) = 1; + CHECK(*map.getptr(0) == 1); + + CHECK(map.get(42) == 84); + CHECK(map.getptr(-10) == nullptr); +} + +TEST_CASE("[AHashMap] Insert, iterate and remove many elements") { + const int elem_max = 1234; + AHashMap<int, int> map; + for (int i = 0; i < elem_max; i++) { + map.insert(i, i); + } + + //insert order should have been kept + int idx = 0; + for (auto &K : map) { + CHECK(idx == K.key); + CHECK(idx == K.value); + CHECK(map.has(idx)); + idx++; + } + + Vector<int> elems_still_valid; + + for (int i = 0; i < elem_max; i++) { + if ((i % 5) == 0) { + map.erase(i); + } else { + elems_still_valid.push_back(i); + } + } + + CHECK(elems_still_valid.size() == map.size()); + + for (int i = 0; i < elems_still_valid.size(); i++) { + CHECK(map.has(elems_still_valid[i])); + } +} + +TEST_CASE("[AHashMap] Insert, iterate and remove many strings") { + const int elem_max = 432; + AHashMap<String, String> map; + for (int i = 0; i < elem_max; i++) { + map.insert(itos(i), itos(i)); + } + + //insert order should have been kept + int idx = 0; + for (auto &K : map) { + CHECK(itos(idx) == K.key); + CHECK(itos(idx) == K.value); + CHECK(map.has(itos(idx))); + idx++; + } + + Vector<String> elems_still_valid; + + for (int i = 0; i < elem_max; i++) { + if ((i % 5) == 0) { + map.erase(itos(i)); + } else { + elems_still_valid.push_back(itos(i)); + } + } + + CHECK(elems_still_valid.size() == map.size()); + + for (int i = 0; i < elems_still_valid.size(); i++) { + CHECK(map.has(elems_still_valid[i])); + } + + elems_still_valid.clear(); +} + +TEST_CASE("[AHashMap] Copy constructor") { + AHashMap<int, int> map0; + const uint32_t count = 5; + for (uint32_t i = 0; i < count; i++) { + map0.insert(i, i); + } + AHashMap<int, int> map1(map0); + CHECK(map0.size() == map1.size()); + CHECK(map0.get_capacity() == map1.get_capacity()); + CHECK(*map0.getptr(0) == *map1.getptr(0)); +} + +TEST_CASE("[AHashMap] Operator =") { + AHashMap<int, int> map0; + AHashMap<int, int> map1; + const uint32_t count = 5; + map1.insert(1234, 1234); + for (uint32_t i = 0; i < count; i++) { + map0.insert(i, i); + } + map1 = map0; + CHECK(map0.size() == map1.size()); + CHECK(map0.get_capacity() == map1.get_capacity()); + CHECK(*map0.getptr(0) == *map1.getptr(0)); +} + +TEST_CASE("[AHashMap] Array methods") { + AHashMap<int, int> map; + for (int i = 0; i < 100; i++) { + map.insert(100 - i, i); + } + for (int i = 0; i < 100; i++) { + CHECK(map.get_by_index(i).value == i); + } + int index = map.get_index(1); + CHECK(map.get_by_index(index).value == 99); + CHECK(map.erase_by_index(index)); + CHECK(!map.erase_by_index(index)); + CHECK(map.get_index(1) == -1); +} + +} // namespace TestAHashMap + +#endif // TEST_A_HASH_MAP_H diff --git a/tests/core/variant/test_variant_utility.h b/tests/core/variant/test_variant_utility.h index 93458b63f4..34b4880d51 100644 --- a/tests/core/variant/test_variant_utility.h +++ b/tests/core/variant/test_variant_utility.h @@ -89,7 +89,7 @@ TEST_CASE("[VariantUtility] Type conversion") { converted = VariantUtilityFunctions::type_convert(basis, Variant::Type::STRING); CHECK(converted.get_type() == Variant::Type::STRING); - CHECK(converted == Variant("[X: (1.2, 0, 0), Y: (0, 3.4, 0), Z: (0, 0, 5.6)]")); + CHECK(converted == Variant("[X: (1.2, 0.0, 0.0), Y: (0.0, 3.4, 0.0), Z: (0.0, 0.0, 5.6)]")); } { |