summaryrefslogtreecommitdiffstats
path: root/tests/core
diff options
context:
space:
mode:
authorSpartan322 <Megacake1234@gmail.com>2024-11-12 13:46:08 -0500
committerSpartan322 <Megacake1234@gmail.com>2024-11-12 13:46:59 -0500
commit3a73c6ebd18bff0fa125be58d3ac9c7a63bab61d (patch)
treec7341bd56c977259578b127886c9a88eeef11820 /tests/core
parent5094c2a5f7d506b0e685120f14d1df42e1e9d495 (diff)
parentcb411fa960f0b7fdbd97dcdb4c90f9346360ee0e (diff)
downloadredot-engine-3a73c6ebd18bff0fa125be58d3ac9c7a63bab61d.tar.gz
Merge commit godotengine/godot@cb411fa960f0b7fdbd97dcdb4c90f9346360ee0e
Diffstat (limited to 'tests/core')
-rw-r--r--tests/core/io/test_marshalls.h99
-rw-r--r--tests/core/string/test_translation.h2
-rw-r--r--tests/core/string/test_translation_server.h88
-rw-r--r--tests/core/variant/test_callable.h64
4 files changed, 230 insertions, 23 deletions
diff --git a/tests/core/io/test_marshalls.h b/tests/core/io/test_marshalls.h
index d97b0e34c5..d449f5e754 100644
--- a/tests/core/io/test_marshalls.h
+++ b/tests/core/io/test_marshalls.h
@@ -162,7 +162,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);
@@ -176,7 +176,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);
@@ -194,7 +194,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");
@@ -216,7 +216,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);
@@ -234,7 +234,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");
@@ -337,10 +337,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");
@@ -372,7 +372,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).
@@ -388,6 +388,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/string/test_translation.h b/tests/core/string/test_translation.h
index a31aa044ae..cf96c356bc 100644
--- a/tests/core/string/test_translation.h
+++ b/tests/core/string/test_translation.h
@@ -163,7 +163,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 e2d530f1ef..00a9d51bc0 100644
--- a/tests/core/string/test_translation_server.h
+++ b/tests/core/string/test_translation_server.h
@@ -39,27 +39,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");
}
@@ -112,18 +122,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";
@@ -132,6 +174,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/variant/test_callable.h b/tests/core/variant/test_callable.h
index 92610b7a76..97558331e2 100644
--- a/tests/core/variant/test_callable.h
+++ b/tests/core/variant/test_callable.h
@@ -137,6 +137,70 @@ TEST_CASE("[Callable] Argument count") {
memdelete(my_test);
}
+
+class TestBoundUnboundArgumentCount : public Object {
+ GDCLASS(TestBoundUnboundArgumentCount, Object);
+
+protected:
+ static void _bind_methods() {
+ ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func", &TestBoundUnboundArgumentCount::test_func, MethodInfo("test_func"));
+ }
+
+public:
+ Variant test_func(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
+ Array result;
+ result.resize(p_argcount);
+ for (int i = 0; i < p_argcount; i++) {
+ result[i] = *p_args[i];
+ }
+ return result;
+ }
+
+ static String get_output(const Callable &p_callable) {
+ Array effective_args;
+ effective_args.push_back(7);
+ effective_args.push_back(8);
+ effective_args.push_back(9);
+
+ effective_args.resize(3 - p_callable.get_unbound_arguments_count());
+ effective_args.append_array(p_callable.get_bound_arguments());
+
+ return vformat(
+ "%d %d %s %s %s",
+ p_callable.get_unbound_arguments_count(),
+ p_callable.get_bound_arguments_count(),
+ p_callable.get_bound_arguments(),
+ p_callable.call(7, 8, 9),
+ effective_args);
+ }
+};
+
+TEST_CASE("[Callable] Bound and unbound argument count") {
+ String (*get_output)(const Callable &) = TestBoundUnboundArgumentCount::get_output;
+
+ TestBoundUnboundArgumentCount *test_instance = memnew(TestBoundUnboundArgumentCount);
+
+ Callable test_func = Callable(test_instance, "test_func");
+
+ CHECK(get_output(test_func) == "0 0 [] [7, 8, 9] [7, 8, 9]");
+ CHECK(get_output(test_func.bind(1, 2)) == "0 2 [1, 2] [7, 8, 9, 1, 2] [7, 8, 9, 1, 2]");
+ CHECK(get_output(test_func.bind(1, 2).unbind(1)) == "1 2 [1, 2] [7, 8, 1, 2] [7, 8, 1, 2]");
+ CHECK(get_output(test_func.bind(1, 2).unbind(1).bind(3, 4)) == "0 3 [3, 1, 2] [7, 8, 9, 3, 1, 2] [7, 8, 9, 3, 1, 2]");
+ CHECK(get_output(test_func.bind(1, 2).unbind(1).bind(3, 4).unbind(1)) == "1 3 [3, 1, 2] [7, 8, 3, 1, 2] [7, 8, 3, 1, 2]");
+
+ CHECK(get_output(test_func.bind(1).bind(2).bind(3).unbind(1)) == "1 3 [3, 2, 1] [7, 8, 3, 2, 1] [7, 8, 3, 2, 1]");
+ CHECK(get_output(test_func.bind(1).bind(2).unbind(1).bind(3)) == "0 2 [2, 1] [7, 8, 9, 2, 1] [7, 8, 9, 2, 1]");
+ CHECK(get_output(test_func.bind(1).unbind(1).bind(2).bind(3)) == "0 2 [3, 1] [7, 8, 9, 3, 1] [7, 8, 9, 3, 1]");
+ CHECK(get_output(test_func.unbind(1).bind(1).bind(2).bind(3)) == "0 2 [3, 2] [7, 8, 9, 3, 2] [7, 8, 9, 3, 2]");
+
+ CHECK(get_output(test_func.unbind(1).unbind(1).unbind(1).bind(1, 2, 3)) == "0 0 [] [7, 8, 9] [7, 8, 9]");
+ CHECK(get_output(test_func.unbind(1).unbind(1).bind(1, 2, 3).unbind(1)) == "1 1 [1] [7, 8, 1] [7, 8, 1]");
+ CHECK(get_output(test_func.unbind(1).bind(1, 2, 3).unbind(1).unbind(1)) == "2 2 [1, 2] [7, 1, 2] [7, 1, 2]");
+ CHECK(get_output(test_func.bind(1, 2, 3).unbind(1).unbind(1).unbind(1)) == "3 3 [1, 2, 3] [1, 2, 3] [1, 2, 3]");
+
+ memdelete(test_instance);
+}
+
} // namespace TestCallable
#endif // TEST_CALLABLE_H