1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/*************************************************************************/
/* test_list.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 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_LIST_H
#define TEST_LIST_H
#include "core/list.h"
#include "tests/test_macros.h"
namespace TestList {
static void populate_integers(List<int> &p_list, List<int>::Element *r_elements[], int num_elements) {
p_list.clear();
for (int i = 0; i < num_elements; ++i) {
List<int>::Element *n = p_list.push_back(i);
r_elements[i] = n;
}
}
TEST_CASE("[List] Swap adjacent front and back") {
List<int> list;
List<int>::Element *n[2];
populate_integers(list, n, 2);
list.swap(list.front(), list.back());
CHECK(list.front()->prev() == nullptr);
CHECK(list.front() != list.front()->next());
CHECK(list.front() == n[1]);
CHECK(list.back() == n[0]);
CHECK(list.back()->next() == nullptr);
CHECK(list.back() != list.back()->prev());
}
TEST_CASE("[List] Swap first adjacent pair") {
List<int> list;
List<int>::Element *n[4];
populate_integers(list, n, 4);
list.swap(n[0], n[1]);
CHECK(list.front()->prev() == nullptr);
CHECK(list.front() != list.front()->next());
CHECK(list.front() == n[1]);
CHECK(list.front()->next() == n[0]);
CHECK(list.back()->prev() == n[2]);
CHECK(list.back() == n[3]);
CHECK(list.back()->next() == nullptr);
CHECK(list.back() != list.back()->prev());
}
TEST_CASE("[List] Swap middle adjacent pair") {
List<int> list;
List<int>::Element *n[4];
populate_integers(list, n, 4);
list.swap(n[1], n[2]);
CHECK(list.front()->prev() == nullptr);
CHECK(list.front() == n[0]);
CHECK(list.front()->next() == n[2]);
CHECK(list.back()->prev() == n[1]);
CHECK(list.back() == n[3]);
CHECK(list.back()->next() == nullptr);
}
TEST_CASE("[List] Swap last adjacent pair") {
List<int> list;
List<int>::Element *n[4];
populate_integers(list, n, 4);
list.swap(n[2], n[3]);
CHECK(list.front()->prev() == nullptr);
CHECK(list.front() == n[0]);
CHECK(list.front()->next() == n[1]);
CHECK(list.back()->prev() == n[3]);
CHECK(list.back() == n[2]);
CHECK(list.back()->next() == nullptr);
}
TEST_CASE("[List] Swap first cross pair") {
List<int> list;
List<int>::Element *n[4];
populate_integers(list, n, 4);
list.swap(n[0], n[2]);
CHECK(list.front()->prev() == nullptr);
CHECK(list.front() == n[2]);
CHECK(list.front()->next() == n[1]);
CHECK(list.back()->prev() == n[0]);
CHECK(list.back() == n[3]);
CHECK(list.back()->next() == nullptr);
}
TEST_CASE("[List] Swap last cross pair") {
List<int> list;
List<int>::Element *n[4];
populate_integers(list, n, 4);
list.swap(n[1], n[3]);
CHECK(list.front()->prev() == nullptr);
CHECK(list.front() == n[0]);
CHECK(list.front()->next() == n[3]);
CHECK(list.back()->prev() == n[2]);
CHECK(list.back() == n[1]);
CHECK(list.back()->next() == nullptr);
}
TEST_CASE("[List] Swap edges") {
List<int> list;
List<int>::Element *n[4];
populate_integers(list, n, 4);
list.swap(n[1], n[3]);
CHECK(list.front()->prev() == nullptr);
CHECK(list.front() == n[0]);
CHECK(list.front()->next() == n[3]);
CHECK(list.back()->prev() == n[2]);
CHECK(list.back() == n[1]);
CHECK(list.back()->next() == nullptr);
}
TEST_CASE("[List] Swap middle (values check)") {
List<String> list;
List<String>::Element *n_str1 = list.push_back("Still");
List<String>::Element *n_str2 = list.push_back("waiting");
List<String>::Element *n_str3 = list.push_back("for");
List<String>::Element *n_str4 = list.push_back("Godot.");
CHECK(n_str1->get() == "Still");
CHECK(n_str4->get() == "Godot.");
CHECK(list.front()->get() == "Still");
CHECK(list.front()->next()->get() == "waiting");
CHECK(list.back()->prev()->get() == "for");
CHECK(list.back()->get() == "Godot.");
list.swap(n_str2, n_str3);
CHECK(list.front()->next()->get() == "for");
CHECK(list.back()->prev()->get() == "waiting");
}
TEST_CASE("[List] Swap front and back (values check)") {
List<Variant> list;
Variant str = "Godot";
List<Variant>::Element *n_str = list.push_back(str);
Variant color = Color(0, 0, 1);
List<Variant>::Element *n_color = list.push_back(color);
CHECK(list.front()->get() == "Godot");
CHECK(list.back()->get() == Color(0, 0, 1));
list.swap(n_str, n_color);
CHECK(list.front()->get() == Color(0, 0, 1));
CHECK(list.back()->get() == "Godot");
}
TEST_CASE("[List] Swap adjacent back and front (reverse order of elements)") {
List<int> list;
List<int>::Element *n[2];
populate_integers(list, n, 2);
list.swap(n[1], n[0]);
List<int>::Element *it = list.front();
while (it) {
List<int>::Element *prev_it = it;
it = it->next();
if (it == prev_it) {
FAIL_CHECK("Infinite loop detected.");
break;
}
}
}
static void swap_random(List<int> &p_list, List<int>::Element *r_elements[], size_t p_size, size_t p_iterations) {
Math::seed(0);
for (size_t test_i = 0; test_i < p_iterations; ++test_i) {
// A and B elements have corresponding indices as values.
const int a_idx = static_cast<int>(Math::rand() % p_size);
const int b_idx = static_cast<int>(Math::rand() % p_size);
List<int>::Element *a = p_list.find(a_idx); // via find.
List<int>::Element *b = r_elements[b_idx]; // via pointer.
int va = a->get();
int vb = b->get();
p_list.swap(a, b);
CHECK(va == a->get());
CHECK(vb == b->get());
size_t element_count = 0;
// Fully traversable after swap?
List<int>::Element *it = p_list.front();
while (it) {
element_count += 1;
List<int>::Element *prev_it = it;
it = it->next();
if (it == prev_it) {
FAIL_CHECK("Infinite loop detected.");
break;
}
}
// We should not lose anything in the process.
if (element_count != p_size) {
FAIL_CHECK("Element count mismatch.");
break;
}
}
}
TEST_CASE("[Stress][List] Swap random 100 elements, 500 iterations.") {
List<int> list;
List<int>::Element *n[100];
populate_integers(list, n, 100);
swap_random(list, n, 100, 500);
}
TEST_CASE("[Stress][List] Swap random 10 elements, 1000 iterations.") {
List<int> list;
List<int>::Element *n[10];
populate_integers(list, n, 10);
swap_random(list, n, 10, 1000);
}
} // namespace TestList
#endif // TEST_LIST_H
|