summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2024-08-27 22:27:30 +0200
committerRémi Verschelde <rverschelde@gmail.com>2024-08-27 22:27:30 +0200
commit9e1c63a051e1f3714f77a64ad256a7861f16f84e (patch)
tree47e3249751d9c25af0b2590845199e4937b1ecaf /tests
parentd35bee9cdd0c74bfdbee8bef701121664752b4d7 (diff)
parent040f241f392017e1a9767d728b9cd87f0f3f0860 (diff)
downloadredot-engine-9e1c63a051e1f3714f77a64ad256a7861f16f84e.tar.gz
Merge pull request #94748 from aaronp64/tree_perf
Improve `Tree` performance
Diffstat (limited to 'tests')
-rw-r--r--tests/scene/test_tree.h151
-rw-r--r--tests/test_main.cpp1
2 files changed, 152 insertions, 0 deletions
diff --git a/tests/scene/test_tree.h b/tests/scene/test_tree.h
new file mode 100644
index 0000000000..41ef39d621
--- /dev/null
+++ b/tests/scene/test_tree.h
@@ -0,0 +1,151 @@
+/**************************************************************************/
+/* test_tree.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_TREE_H
+#define TEST_TREE_H
+
+#include "scene/gui/tree.h"
+
+#include "tests/test_macros.h"
+
+namespace TestTree {
+
+TEST_CASE("[SceneTree][Tree]") {
+ SUBCASE("[Tree] Create and remove items.") {
+ Tree *tree = memnew(Tree);
+ TreeItem *root = tree->create_item();
+
+ TreeItem *child1 = tree->create_item();
+ CHECK_EQ(root->get_child_count(), 1);
+
+ TreeItem *child2 = tree->create_item(root);
+ CHECK_EQ(root->get_child_count(), 2);
+
+ TreeItem *child3 = tree->create_item(root, 0);
+ CHECK_EQ(root->get_child_count(), 3);
+
+ CHECK_EQ(root->get_child(0), child3);
+ CHECK_EQ(root->get_child(1), child1);
+ CHECK_EQ(root->get_child(2), child2);
+
+ root->remove_child(child3);
+ CHECK_EQ(root->get_child_count(), 2);
+
+ root->add_child(child3);
+ CHECK_EQ(root->get_child_count(), 3);
+
+ TreeItem *child4 = root->create_child();
+ CHECK_EQ(root->get_child_count(), 4);
+
+ CHECK_EQ(root->get_child(0), child1);
+ CHECK_EQ(root->get_child(1), child2);
+ CHECK_EQ(root->get_child(2), child3);
+ CHECK_EQ(root->get_child(3), child4);
+
+ memdelete(tree);
+ }
+
+ SUBCASE("[Tree] Clear items.") {
+ Tree *tree = memnew(Tree);
+ TreeItem *root = tree->create_item();
+
+ for (int i = 0; i < 10; i++) {
+ tree->create_item();
+ }
+ CHECK_EQ(root->get_child_count(), 10);
+
+ root->clear_children();
+ CHECK_EQ(root->get_child_count(), 0);
+
+ memdelete(tree);
+ }
+
+ SUBCASE("[Tree] Get last item.") {
+ Tree *tree = memnew(Tree);
+ TreeItem *root = tree->create_item();
+
+ TreeItem *last;
+ for (int i = 0; i < 10; i++) {
+ last = tree->create_item();
+ }
+ CHECK_EQ(root->get_child_count(), 10);
+ CHECK_EQ(tree->get_last_item(), last);
+
+ // Check nested.
+ TreeItem *old_last = last;
+ for (int i = 0; i < 10; i++) {
+ last = tree->create_item(old_last);
+ }
+ CHECK_EQ(tree->get_last_item(), last);
+
+ memdelete(tree);
+ }
+
+ SUBCASE("[Tree] Previous and Next items.") {
+ Tree *tree = memnew(Tree);
+ TreeItem *root = tree->create_item();
+
+ TreeItem *child1 = tree->create_item();
+ TreeItem *child2 = tree->create_item();
+ TreeItem *child3 = tree->create_item();
+ CHECK_EQ(child1->get_next(), child2);
+ CHECK_EQ(child1->get_next_in_tree(), child2);
+ CHECK_EQ(child2->get_next(), child3);
+ CHECK_EQ(child2->get_next_in_tree(), child3);
+ CHECK_EQ(child3->get_next(), nullptr);
+ CHECK_EQ(child3->get_next_in_tree(), nullptr);
+
+ CHECK_EQ(child1->get_prev(), nullptr);
+ CHECK_EQ(child1->get_prev_in_tree(), root);
+ CHECK_EQ(child2->get_prev(), child1);
+ CHECK_EQ(child2->get_prev_in_tree(), child1);
+ CHECK_EQ(child3->get_prev(), child2);
+ CHECK_EQ(child3->get_prev_in_tree(), child2);
+
+ TreeItem *nested1 = tree->create_item(child2);
+ TreeItem *nested2 = tree->create_item(child2);
+ TreeItem *nested3 = tree->create_item(child2);
+
+ CHECK_EQ(child1->get_next(), child2);
+ CHECK_EQ(child1->get_next_in_tree(), child2);
+ CHECK_EQ(child2->get_next(), child3);
+ CHECK_EQ(child2->get_next_in_tree(), nested1);
+ CHECK_EQ(child3->get_prev(), child2);
+ CHECK_EQ(child3->get_prev_in_tree(), nested3);
+ CHECK_EQ(nested1->get_prev_in_tree(), child2);
+ CHECK_EQ(nested1->get_next_in_tree(), nested2);
+
+ memdelete(tree);
+ }
+}
+
+} // namespace TestTree
+
+#endif // TEST_TREE_H
diff --git a/tests/test_main.cpp b/tests/test_main.cpp
index edadc52a16..27f23701a9 100644
--- a/tests/test_main.cpp
+++ b/tests/test_main.cpp
@@ -133,6 +133,7 @@
#include "tests/scene/test_color_picker.h"
#include "tests/scene/test_graph_node.h"
#include "tests/scene/test_text_edit.h"
+#include "tests/scene/test_tree.h"
#endif // ADVANCED_GUI_DISABLED
#ifndef _3D_DISABLED