Debian Codegraph @

tests/core/templates/test_hash_map.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_hash_map.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/templates/hash_map.h"
34
35 #include "tests/test_macros.h"
36
37 namespace TestHashMap {
38
39 TEST_CASE("[HashMap] List initialization") {
40 HashMap<int, String> map{ { 0, "A" }, { 1, "B" }, { 2, "C" }, { 3, "D" }, { 4, "E" } };
41
42 CHECK(map.size() == 5);
43 CHECK(map[0] == "A");
44 CHECK(map[1] == "B");
45 CHECK(map[2] == "C");
46 CHECK(map[3] == "D");
47 CHECK(map[4] == "E");
48 }
49
50 TEST_CASE("[HashMap] List initialization with existing elements") {
51 HashMap<int, String> map{ { 0, "A" }, { 0, "B" }, { 0, "C" }, { 0, "D" }, { 0, "E" } };
52
53 CHECK(map.size() == 1);
54 CHECK(map[0] == "E");
55 }
56
57 TEST_CASE("[HashMap] Insert element") {
58 HashMap<int, int> map;
59 HashMap<int, int>::Iterator e = map.insert(42, 84);
60
61 CHECK(e);
62 CHECK(e->key == 42);
63 CHECK(e->value == 84);
64 CHECK(map[42] == 84);
65 CHECK(map.has(42));
66 CHECK(map.find(42));
67 }
68
69 TEST_CASE("[HashMap] Overwrite element") {
70 HashMap<int, int> map;
71 map.insert(42, 84);
72 map.insert(42, 1234);
73
74 CHECK(map[42] == 1234);
75 }
76
77 TEST_CASE("[HashMap] Erase via element") {
78 HashMap<int, int> map;
79 HashMap<int, int>::Iterator e = map.insert(42, 84);
80 map.remove(e);
81 CHECK(!map.has(42));
82 CHECK(!map.find(42));
83 }
84
85 TEST_CASE("[HashMap] Erase via key") {
86 HashMap<int, int> map;
87 map.insert(42, 84);
88 map.erase(42);
89 CHECK(!map.has(42));
90 CHECK(!map.find(42));
91 }
92
93 TEST_CASE("[HashMap] Size") {
94 HashMap<int, int> map;
95 map.insert(42, 84);
96 map.insert(123, 84);
97 map.insert(123, 84);
98 map.insert(0, 84);
99 map.insert(123485, 84);
100
101 CHECK(map.size() == 4);
102 }
103
104 TEST_CASE("[HashMap] Iteration") {
105 HashMap<int, int> map;
106 map.insert(42, 84);
107 map.insert(123, 12385);
108 map.insert(0, 12934);
109 map.insert(123485, 1238888);
110 map.insert(123, 111111);
111
112 Vector<Pair<int, int>> expected;
113 expected.push_back(Pair<int, int>(42, 84));
114 expected.push_back(Pair<int, int>(123, 111111));
115 expected.push_back(Pair<int, int>(0, 12934));
116 expected.push_back(Pair<int, int>(123485, 1238888));
117
118 int idx = 0;
119 for (const KeyValue<int, int> &E : map) {
120 CHECK(expected[idx] == Pair<int, int>(E.key, E.value));
121 ++idx;
122 }
123 }
124
125 TEST_CASE("[HashMap] Const iteration") {
126 HashMap<int, int> map;
127 map.insert(42, 84);
128 map.insert(123, 12385);
129 map.insert(0, 12934);
130 map.insert(123485, 1238888);
131 map.insert(123, 111111);
132
133 const HashMap<int, int> const_map = map;
134
135 Vector<Pair<int, int>> expected;
136 expected.push_back(Pair<int, int>(42, 84));
137 expected.push_back(Pair<int, int>(123, 111111));
138 expected.push_back(Pair<int, int>(0, 12934));
139 expected.push_back(Pair<int, int>(123485, 1238888));
140 expected.push_back(Pair<int, int>(123, 111111));
141
142 int idx = 0;
143 for (const KeyValue<int, int> &E : const_map) {
144 CHECK(expected[idx] == Pair<int, int>(E.key, E.value));
145 ++idx;
146 }
147 }
148
149 TEST_CASE("[HashMap] Sort") {
150 HashMap<int, int> hashmap;
151 int shuffled_ints[]{ 6, 1, 9, 8, 3, 0, 4, 5, 7, 2 };
152
153 for (int i : shuffled_ints) {
154 hashmap[i] = i;
155 }
156 hashmap.sort();
157
158 int i = 0;
159 for (const KeyValue<int, int> &kv : hashmap) {
160 CHECK_EQ(kv.key, i);
161 i++;
162 }
163
164 struct ReverseSort {
165 bool operator()(const KeyValue<int, int> &p_a, const KeyValue<int, int> &p_b) {
166 return p_a.key > p_b.key;
167 }
168 };
169 hashmap.sort_custom<ReverseSort>();
170
171 for (const KeyValue<int, int> &kv : hashmap) {
172 i--;
173 CHECK_EQ(kv.key, i);
174 }
175 }
176 } // namespace TestHashMap