summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/core/string/test_string.h14
-rw-r--r--tests/core/templates/test_hash_set.h228
-rw-r--r--tests/scene/test_text_edit.h8
-rw-r--r--tests/test_macros.h9
-rw-r--r--tests/test_main.cpp1
5 files changed, 251 insertions, 9 deletions
diff --git a/tests/core/string/test_string.h b/tests/core/string/test_string.h
index 58372a0ed6..0b191d2d94 100644
--- a/tests/core/string/test_string.h
+++ b/tests/core/string/test_string.h
@@ -1447,6 +1447,20 @@ TEST_CASE("[String] validate_node_name") {
CHECK(name_with_invalid_chars.validate_node_name() == "Name with invalid characters removed!");
}
+TEST_CASE("[String] validate_identifier") {
+ String empty_string;
+ CHECK(empty_string.validate_identifier() == "_");
+
+ String numeric_only = "12345";
+ CHECK(numeric_only.validate_identifier() == "_2345");
+
+ String name_with_spaces = "Name with spaces";
+ CHECK(name_with_spaces.validate_identifier() == "Name_with_spaces");
+
+ String name_with_invalid_chars = String::utf8("Invalid characters:@*#&世界");
+ CHECK(name_with_invalid_chars.validate_identifier() == "Invalid_characters_______");
+}
+
TEST_CASE("[String] Variant indexed get") {
Variant s = String("abcd");
bool valid = false;
diff --git a/tests/core/templates/test_hash_set.h b/tests/core/templates/test_hash_set.h
new file mode 100644
index 0000000000..93fc0b26a3
--- /dev/null
+++ b/tests/core/templates/test_hash_set.h
@@ -0,0 +1,228 @@
+/*************************************************************************/
+/* test_hash_set.h */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2022 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_HASH_SET_H
+#define TEST_HASH_SET_H
+
+#include "core/templates/hash_set.h"
+
+#include "tests/test_macros.h"
+
+namespace TestHashSet {
+
+TEST_CASE("[HashSet] Insert element") {
+ print_line("SMALL BEGIN MEM: ", Memory::get_mem_usage());
+ HashSet<int> set;
+ HashSet<int>::Iterator e = set.insert(42);
+
+ CHECK(e);
+ CHECK(*e == 42);
+ CHECK(set.has(42));
+ CHECK(set.find(42));
+ set.reset();
+ print_line("SMALL END MEM: ", Memory::get_mem_usage());
+}
+
+TEST_CASE("[HashSet] Insert existing element") {
+ HashSet<int> set;
+ set.insert(42);
+ set.insert(42);
+
+ CHECK(set.has(42));
+ CHECK(set.size() == 1);
+}
+
+TEST_CASE("[HashSet] Insert, iterate and remove many elements") {
+ const int elem_max = 12343;
+ HashSet<int> set;
+ for (int i = 0; i < elem_max; i++) {
+ set.insert(i);
+ }
+
+ //insert order should have been kept
+ int idx = 0;
+ for (const int &K : set) {
+ CHECK(idx == K);
+ CHECK(set.has(idx));
+ idx++;
+ }
+
+ Vector<int> elems_still_valid;
+
+ for (int i = 0; i < elem_max; i++) {
+ if ((i % 5) == 0) {
+ set.erase(i);
+ } else {
+ elems_still_valid.push_back(i);
+ }
+ }
+
+ CHECK(elems_still_valid.size() == set.size());
+
+ for (int i = 0; i < elems_still_valid.size(); i++) {
+ CHECK(set.has(elems_still_valid[i]));
+ }
+}
+
+TEST_CASE("[HashSet] Insert, iterate and remove many strings") {
+ // This tests a key that uses allocation, to see if any leaks occur
+
+ uint64_t pre_mem = Memory::get_mem_usage();
+ const int elem_max = 4018;
+ HashSet<String> set;
+ for (int i = 0; i < elem_max; i++) {
+ set.insert(itos(i));
+ }
+
+ //insert order should have been kept
+ int idx = 0;
+ for (const String &K : set) {
+ CHECK(itos(idx) == K);
+ CHECK(set.has(itos(idx)));
+ idx++;
+ }
+
+ Vector<String> elems_still_valid;
+
+ for (int i = 0; i < elem_max; i++) {
+ if ((i % 5) == 0) {
+ set.erase(itos(i));
+ } else {
+ elems_still_valid.push_back(itos(i));
+ }
+ }
+
+ CHECK(elems_still_valid.size() == set.size());
+
+ for (int i = 0; i < elems_still_valid.size(); i++) {
+ CHECK(set.has(elems_still_valid[i]));
+ }
+
+ elems_still_valid.clear();
+ set.reset();
+
+ CHECK(Memory::get_mem_usage() == pre_mem);
+}
+
+TEST_CASE("[HashSet] Erase via element") {
+ HashSet<int> set;
+ HashSet<int>::Iterator e = set.insert(42);
+ set.remove(e);
+ CHECK(!set.has(42));
+ CHECK(!set.find(42));
+}
+
+TEST_CASE("[HashSet] Erase via key") {
+ HashSet<int> set;
+ set.insert(42);
+ set.insert(49);
+ set.erase(42);
+ CHECK(!set.has(42));
+ CHECK(!set.find(42));
+}
+
+TEST_CASE("[HashSet] Insert and erase half elements") {
+ HashSet<int> set;
+ set.insert(1);
+ set.insert(2);
+ set.insert(3);
+ set.insert(4);
+ set.erase(1);
+ set.erase(3);
+
+ CHECK(set.size() == 2);
+ CHECK(set.has(2));
+ CHECK(set.has(4));
+}
+
+TEST_CASE("[HashSet] Size") {
+ HashSet<int> set;
+ set.insert(42);
+ set.insert(123);
+ set.insert(123);
+ set.insert(0);
+ set.insert(123485);
+
+ CHECK(set.size() == 4);
+}
+
+TEST_CASE("[HashSet] Iteration") {
+ HashSet<int> set;
+ set.insert(42);
+ set.insert(123);
+ set.insert(0);
+ set.insert(123485);
+
+ Vector<int> expected;
+ expected.push_back(42);
+ expected.push_back(123);
+ expected.push_back(0);
+ expected.push_back(123485);
+
+ int idx = 0;
+ for (const int &E : set) {
+ CHECK(expected[idx] == E);
+ ++idx;
+ }
+}
+
+TEST_CASE("[HashSet] Copy") {
+ HashSet<int> set;
+ set.insert(42);
+ set.insert(123);
+ set.insert(0);
+ set.insert(123485);
+
+ Vector<int> expected;
+ expected.push_back(42);
+ expected.push_back(123);
+ expected.push_back(0);
+ expected.push_back(123485);
+
+ HashSet<int> copy_assign = set;
+
+ int idx = 0;
+ for (const int &E : copy_assign) {
+ CHECK(expected[idx] == E);
+ ++idx;
+ }
+
+ HashSet<int> copy_construct(set);
+
+ idx = 0;
+ for (const int &E : copy_construct) {
+ CHECK(expected[idx] == E);
+ ++idx;
+ }
+}
+
+} // namespace TestHashSet
+
+#endif // TEST_HASH_MAP_H
diff --git a/tests/scene/test_text_edit.h b/tests/scene/test_text_edit.h
index a9a1a5fa71..4098dd7ace 100644
--- a/tests/scene/test_text_edit.h
+++ b/tests/scene/test_text_edit.h
@@ -1395,7 +1395,7 @@ TEST_CASE("[SceneTree][TextEdit] text entry") {
SIGNAL_DISCARD("lines_edited_from");
SIGNAL_DISCARD("caret_changed");
- // With selection should be a normal backsapce.
+ // With selection should be a normal backspace.
((Array)lines_edited_args[0])[0] = 1;
((Array)lines_edited_args[0])[1] = 1;
@@ -1469,7 +1469,7 @@ TEST_CASE("[SceneTree][TextEdit] text entry") {
SIGNAL_DISCARD("lines_edited_from");
SIGNAL_DISCARD("caret_changed");
- // With selection should be a normal backsapce.
+ // With selection should be a normal backspace.
((Array)lines_edited_args[0])[0] = 1;
((Array)lines_edited_args[0])[1] = 1;
@@ -1542,7 +1542,7 @@ TEST_CASE("[SceneTree][TextEdit] text entry") {
SIGNAL_DISCARD("lines_edited_from");
SIGNAL_DISCARD("caret_changed");
- // With selection should be a normal backsapce.
+ // With selection should be a normal backspace.
((Array)lines_edited_args[0])[0] = 1;
((Array)lines_edited_args[0])[1] = 1;
@@ -2784,7 +2784,7 @@ TEST_CASE("[SceneTree][TextEdit] line wrapping") {
SceneTree::get_singleton()->get_root()->add_child(text_edit);
text_edit->grab_focus();
- // Set size for boundry.
+ // Set size for boundary.
text_edit->set_size(Size2(800, 200));
text_edit->set_line(0, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec varius mattis leo, sed porta ex lacinia bibendum. Nunc bibendum pellentesque.");
CHECK_FALSE(text_edit->is_line_wrapped(0));
diff --git a/tests/test_macros.h b/tests/test_macros.h
index 189554bd1a..6029a9cfc7 100644
--- a/tests/test_macros.h
+++ b/tests/test_macros.h
@@ -125,10 +125,9 @@ typedef void (*TestFunc)();
extern HashMap<String, TestFunc> *test_commands;
int register_test_command(String p_command, TestFunc p_function);
-#define REGISTER_TEST_COMMAND(m_command, m_function) \
- DOCTEST_GLOBAL_NO_WARNINGS(DOCTEST_ANONYMOUS(_DOCTEST_ANON_VAR_)) = \
- register_test_command(m_command, m_function); \
- DOCTEST_GLOBAL_NO_WARNINGS_END()
+#define REGISTER_TEST_COMMAND(m_command, m_function) \
+ DOCTEST_GLOBAL_NO_WARNINGS(DOCTEST_ANONYMOUS(DOCTEST_ANON_VAR_), \
+ register_test_command(m_command, m_function))
// Utility macros to send an event actions to a given object
// Requires Message Queue and InputMap to be setup.
@@ -197,7 +196,7 @@ int register_test_command(String p_command, TestFunc p_function);
MessageQueue::get_singleton()->flush(); \
}
-// We toogle _print_error_enabled to prevent display server not supported warnings.
+// We toggle _print_error_enabled to prevent display server not supported warnings.
#define SEND_GUI_MOUSE_MOTION_EVENT(m_object, m_local_pos, m_mask, m_modifers) \
{ \
bool errors_enabled = _print_error_enabled; \
diff --git a/tests/test_main.cpp b/tests/test_main.cpp
index a5f6fb9b88..ac0cdf0cc1 100644
--- a/tests/test_main.cpp
+++ b/tests/test_main.cpp
@@ -60,6 +60,7 @@
#include "tests/core/string/test_translation.h"
#include "tests/core/templates/test_command_queue.h"
#include "tests/core/templates/test_hash_map.h"
+#include "tests/core/templates/test_hash_set.h"
#include "tests/core/templates/test_list.h"
#include "tests/core/templates/test_local_vector.h"
#include "tests/core/templates/test_lru.h"