test_list.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /**************************************************************************/
  2. /* test_list.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. #ifndef TEST_LIST_H
  31. #define TEST_LIST_H
  32. #include "core/templates/list.h"
  33. #include "tests/test_macros.h"
  34. namespace TestList {
  35. static void populate_integers(List<int> &p_list, List<int>::Element *r_elements[], int num_elements) {
  36. p_list.clear();
  37. for (int i = 0; i < num_elements; ++i) {
  38. List<int>::Element *n = p_list.push_back(i);
  39. r_elements[i] = n;
  40. }
  41. }
  42. TEST_CASE("[List] Push/pop back") {
  43. List<String> list;
  44. List<String>::Element *n;
  45. n = list.push_back("A");
  46. CHECK(n->get() == "A");
  47. n = list.push_back("B");
  48. CHECK(n->get() == "B");
  49. n = list.push_back("C");
  50. CHECK(n->get() == "C");
  51. CHECK(list.size() == 3);
  52. CHECK(!list.is_empty());
  53. String v;
  54. v = list.back()->get();
  55. list.pop_back();
  56. CHECK(v == "C");
  57. v = list.back()->get();
  58. list.pop_back();
  59. CHECK(v == "B");
  60. v = list.back()->get();
  61. list.pop_back();
  62. CHECK(v == "A");
  63. CHECK(list.size() == 0);
  64. CHECK(list.is_empty());
  65. CHECK(list.back() == nullptr);
  66. CHECK(list.front() == nullptr);
  67. }
  68. TEST_CASE("[List] Push/pop front") {
  69. List<String> list;
  70. List<String>::Element *n;
  71. n = list.push_front("A");
  72. CHECK(n->get() == "A");
  73. n = list.push_front("B");
  74. CHECK(n->get() == "B");
  75. n = list.push_front("C");
  76. CHECK(n->get() == "C");
  77. CHECK(list.size() == 3);
  78. CHECK(!list.is_empty());
  79. String v;
  80. v = list.front()->get();
  81. list.pop_front();
  82. CHECK(v == "C");
  83. v = list.front()->get();
  84. list.pop_front();
  85. CHECK(v == "B");
  86. v = list.front()->get();
  87. list.pop_front();
  88. CHECK(v == "A");
  89. CHECK(list.size() == 0);
  90. CHECK(list.is_empty());
  91. CHECK(list.back() == nullptr);
  92. CHECK(list.front() == nullptr);
  93. }
  94. TEST_CASE("[List] Set and get") {
  95. List<String> list;
  96. list.push_back("A");
  97. List<String>::Element *n = list.front();
  98. CHECK(n->get() == "A");
  99. n->set("X");
  100. CHECK(n->get() == "X");
  101. }
  102. TEST_CASE("[List] Insert before") {
  103. List<String> list;
  104. List<String>::Element *a = list.push_back("A");
  105. List<String>::Element *b = list.push_back("B");
  106. List<String>::Element *c = list.push_back("C");
  107. list.insert_before(b, "I");
  108. CHECK(a->next()->get() == "I");
  109. CHECK(c->prev()->prev()->get() == "I");
  110. CHECK(list.front()->next()->get() == "I");
  111. CHECK(list.back()->prev()->prev()->get() == "I");
  112. }
  113. TEST_CASE("[List] Insert after") {
  114. List<String> list;
  115. List<String>::Element *a = list.push_back("A");
  116. List<String>::Element *b = list.push_back("B");
  117. List<String>::Element *c = list.push_back("C");
  118. list.insert_after(b, "I");
  119. CHECK(a->next()->next()->get() == "I");
  120. CHECK(c->prev()->get() == "I");
  121. CHECK(list.front()->next()->next()->get() == "I");
  122. CHECK(list.back()->prev()->get() == "I");
  123. }
  124. TEST_CASE("[List] Insert before null") {
  125. List<String> list;
  126. List<String>::Element *a = list.push_back("A");
  127. List<String>::Element *b = list.push_back("B");
  128. List<String>::Element *c = list.push_back("C");
  129. list.insert_before(nullptr, "I");
  130. CHECK(a->next()->get() == "B");
  131. CHECK(b->get() == "B");
  132. CHECK(c->prev()->prev()->get() == "A");
  133. CHECK(list.front()->next()->get() == "B");
  134. CHECK(list.back()->prev()->prev()->get() == "B");
  135. CHECK(list.back()->get() == "I");
  136. }
  137. TEST_CASE("[List] Insert after null") {
  138. List<String> list;
  139. List<String>::Element *a = list.push_back("A");
  140. List<String>::Element *b = list.push_back("B");
  141. List<String>::Element *c = list.push_back("C");
  142. list.insert_after(nullptr, "I");
  143. CHECK(a->next()->get() == "B");
  144. CHECK(b->get() == "B");
  145. CHECK(c->prev()->prev()->get() == "A");
  146. CHECK(list.front()->next()->get() == "B");
  147. CHECK(list.back()->prev()->prev()->get() == "B");
  148. CHECK(list.back()->get() == "I");
  149. }
  150. TEST_CASE("[List] Find") {
  151. List<int> list;
  152. List<int>::Element *n[10];
  153. // Indices match values.
  154. populate_integers(list, n, 10);
  155. for (int i = 0; i < 10; ++i) {
  156. CHECK(n[i]->get() == list.find(i)->get());
  157. }
  158. }
  159. TEST_CASE("[List] Erase (by value)") {
  160. List<int> list;
  161. List<int>::Element *n[4];
  162. // Indices match values.
  163. populate_integers(list, n, 4);
  164. CHECK(list.front()->next()->next()->get() == 2);
  165. bool erased = list.erase(2); // 0, 1, 3.
  166. CHECK(erased);
  167. CHECK(list.size() == 3);
  168. // The pointer n[2] points to the freed memory which is not reset to zero,
  169. // so the below assertion may pass, but this relies on undefined behavior.
  170. // CHECK(n[2]->get() == 2);
  171. CHECK(list.front()->get() == 0);
  172. CHECK(list.front()->next()->next()->get() == 3);
  173. CHECK(list.back()->get() == 3);
  174. CHECK(list.back()->prev()->get() == 1);
  175. CHECK(n[1]->next()->get() == 3);
  176. CHECK(n[3]->prev()->get() == 1);
  177. erased = list.erase(9000); // Doesn't exist.
  178. CHECK(!erased);
  179. }
  180. TEST_CASE("[List] Erase (by element)") {
  181. List<int> list;
  182. List<int>::Element *n[4];
  183. // Indices match values.
  184. populate_integers(list, n, 4);
  185. bool erased = list.erase(n[2]);
  186. CHECK(erased);
  187. CHECK(list.size() == 3);
  188. CHECK(n[1]->next()->get() == 3);
  189. CHECK(n[3]->prev()->get() == 1);
  190. }
  191. TEST_CASE("[List] Element erase") {
  192. List<int> list;
  193. List<int>::Element *n[4];
  194. // Indices match values.
  195. populate_integers(list, n, 4);
  196. n[2]->erase();
  197. CHECK(list.size() == 3);
  198. CHECK(n[1]->next()->get() == 3);
  199. CHECK(n[3]->prev()->get() == 1);
  200. }
  201. TEST_CASE("[List] Clear") {
  202. List<int> list;
  203. List<int>::Element *n[100];
  204. populate_integers(list, n, 100);
  205. list.clear();
  206. CHECK(list.size() == 0);
  207. CHECK(list.is_empty());
  208. }
  209. TEST_CASE("[List] Invert") {
  210. List<int> list;
  211. List<int>::Element *n[4];
  212. populate_integers(list, n, 4);
  213. list.reverse();
  214. CHECK(list.front()->get() == 3);
  215. CHECK(list.front()->next()->get() == 2);
  216. CHECK(list.back()->prev()->get() == 1);
  217. CHECK(list.back()->get() == 0);
  218. }
  219. TEST_CASE("[List] Move to front") {
  220. List<int> list;
  221. List<int>::Element *n[4];
  222. populate_integers(list, n, 4);
  223. list.move_to_front(n[3]);
  224. CHECK(list.front()->get() == 3);
  225. CHECK(list.back()->get() == 2);
  226. }
  227. TEST_CASE("[List] Move to back") {
  228. List<int> list;
  229. List<int>::Element *n[4];
  230. populate_integers(list, n, 4);
  231. list.move_to_back(n[0]);
  232. CHECK(list.back()->get() == 0);
  233. CHECK(list.front()->get() == 1);
  234. }
  235. TEST_CASE("[List] Move before") {
  236. List<int> list;
  237. List<int>::Element *n[4];
  238. populate_integers(list, n, 4);
  239. list.move_before(n[3], n[1]);
  240. CHECK(list.front()->next()->get() == n[3]->get());
  241. }
  242. TEST_CASE("[List] Sort") {
  243. List<String> list;
  244. list.push_back("D");
  245. list.push_back("B");
  246. list.push_back("A");
  247. list.push_back("C");
  248. list.sort();
  249. CHECK(list.front()->get() == "A");
  250. CHECK(list.front()->next()->get() == "B");
  251. CHECK(list.back()->prev()->get() == "C");
  252. CHECK(list.back()->get() == "D");
  253. }
  254. TEST_CASE("[List] Swap adjacent front and back") {
  255. List<int> list;
  256. List<int>::Element *n[2];
  257. populate_integers(list, n, 2);
  258. list.swap(list.front(), list.back());
  259. CHECK(list.front()->prev() == nullptr);
  260. CHECK(list.front() != list.front()->next());
  261. CHECK(list.front() == n[1]);
  262. CHECK(list.back() == n[0]);
  263. CHECK(list.back()->next() == nullptr);
  264. CHECK(list.back() != list.back()->prev());
  265. }
  266. TEST_CASE("[List] Swap first adjacent pair") {
  267. List<int> list;
  268. List<int>::Element *n[4];
  269. populate_integers(list, n, 4);
  270. list.swap(n[0], n[1]);
  271. CHECK(list.front()->prev() == nullptr);
  272. CHECK(list.front() != list.front()->next());
  273. CHECK(list.front() == n[1]);
  274. CHECK(list.front()->next() == n[0]);
  275. CHECK(list.back()->prev() == n[2]);
  276. CHECK(list.back() == n[3]);
  277. CHECK(list.back()->next() == nullptr);
  278. CHECK(list.back() != list.back()->prev());
  279. }
  280. TEST_CASE("[List] Swap middle adjacent pair") {
  281. List<int> list;
  282. List<int>::Element *n[4];
  283. populate_integers(list, n, 4);
  284. list.swap(n[1], n[2]);
  285. CHECK(list.front()->prev() == nullptr);
  286. CHECK(list.front() == n[0]);
  287. CHECK(list.front()->next() == n[2]);
  288. CHECK(list.back()->prev() == n[1]);
  289. CHECK(list.back() == n[3]);
  290. CHECK(list.back()->next() == nullptr);
  291. }
  292. TEST_CASE("[List] Swap last adjacent pair") {
  293. List<int> list;
  294. List<int>::Element *n[4];
  295. populate_integers(list, n, 4);
  296. list.swap(n[2], n[3]);
  297. CHECK(list.front()->prev() == nullptr);
  298. CHECK(list.front() == n[0]);
  299. CHECK(list.front()->next() == n[1]);
  300. CHECK(list.back()->prev() == n[3]);
  301. CHECK(list.back() == n[2]);
  302. CHECK(list.back()->next() == nullptr);
  303. }
  304. TEST_CASE("[List] Swap first cross pair") {
  305. List<int> list;
  306. List<int>::Element *n[4];
  307. populate_integers(list, n, 4);
  308. list.swap(n[0], n[2]);
  309. CHECK(list.front()->prev() == nullptr);
  310. CHECK(list.front() == n[2]);
  311. CHECK(list.front()->next() == n[1]);
  312. CHECK(list.back()->prev() == n[0]);
  313. CHECK(list.back() == n[3]);
  314. CHECK(list.back()->next() == nullptr);
  315. }
  316. TEST_CASE("[List] Swap last cross pair") {
  317. List<int> list;
  318. List<int>::Element *n[4];
  319. populate_integers(list, n, 4);
  320. list.swap(n[1], n[3]);
  321. CHECK(list.front()->prev() == nullptr);
  322. CHECK(list.front() == n[0]);
  323. CHECK(list.front()->next() == n[3]);
  324. CHECK(list.back()->prev() == n[2]);
  325. CHECK(list.back() == n[1]);
  326. CHECK(list.back()->next() == nullptr);
  327. }
  328. TEST_CASE("[List] Swap edges") {
  329. List<int> list;
  330. List<int>::Element *n[4];
  331. populate_integers(list, n, 4);
  332. list.swap(n[1], n[3]);
  333. CHECK(list.front()->prev() == nullptr);
  334. CHECK(list.front() == n[0]);
  335. CHECK(list.front()->next() == n[3]);
  336. CHECK(list.back()->prev() == n[2]);
  337. CHECK(list.back() == n[1]);
  338. CHECK(list.back()->next() == nullptr);
  339. }
  340. TEST_CASE("[List] Swap middle (values check)") {
  341. List<String> list;
  342. List<String>::Element *n_str1 = list.push_back("Still");
  343. List<String>::Element *n_str2 = list.push_back("waiting");
  344. List<String>::Element *n_str3 = list.push_back("for");
  345. List<String>::Element *n_str4 = list.push_back("Godot.");
  346. CHECK(n_str1->get() == "Still");
  347. CHECK(n_str4->get() == "Godot.");
  348. CHECK(list.front()->get() == "Still");
  349. CHECK(list.front()->next()->get() == "waiting");
  350. CHECK(list.back()->prev()->get() == "for");
  351. CHECK(list.back()->get() == "Godot.");
  352. list.swap(n_str2, n_str3);
  353. CHECK(list.front()->next()->get() == "for");
  354. CHECK(list.back()->prev()->get() == "waiting");
  355. }
  356. TEST_CASE("[List] Swap front and back (values check)") {
  357. List<Variant> list;
  358. Variant str = "Godot";
  359. List<Variant>::Element *n_str = list.push_back(str);
  360. Variant color = Color(0, 0, 1);
  361. List<Variant>::Element *n_color = list.push_back(color);
  362. CHECK(list.front()->get() == "Godot");
  363. CHECK(list.back()->get() == Color(0, 0, 1));
  364. list.swap(n_str, n_color);
  365. CHECK(list.front()->get() == Color(0, 0, 1));
  366. CHECK(list.back()->get() == "Godot");
  367. }
  368. TEST_CASE("[List] Swap adjacent back and front (reverse order of elements)") {
  369. List<int> list;
  370. List<int>::Element *n[2];
  371. populate_integers(list, n, 2);
  372. list.swap(n[1], n[0]);
  373. List<int>::Element *it = list.front();
  374. while (it) {
  375. List<int>::Element *prev_it = it;
  376. it = it->next();
  377. if (it == prev_it) {
  378. FAIL_CHECK("Infinite loop detected.");
  379. break;
  380. }
  381. }
  382. }
  383. static void swap_random(List<int> &p_list, List<int>::Element *r_elements[], size_t p_size, size_t p_iterations) {
  384. Math::seed(0);
  385. for (size_t test_i = 0; test_i < p_iterations; ++test_i) {
  386. // A and B elements have corresponding indices as values.
  387. const int a_idx = static_cast<int>(Math::rand() % p_size);
  388. const int b_idx = static_cast<int>(Math::rand() % p_size);
  389. List<int>::Element *a = p_list.find(a_idx); // via find.
  390. List<int>::Element *b = r_elements[b_idx]; // via pointer.
  391. int va = a->get();
  392. int vb = b->get();
  393. p_list.swap(a, b);
  394. CHECK(va == a->get());
  395. CHECK(vb == b->get());
  396. size_t element_count = 0;
  397. // Fully traversable after swap?
  398. List<int>::Element *it = p_list.front();
  399. while (it) {
  400. element_count += 1;
  401. List<int>::Element *prev_it = it;
  402. it = it->next();
  403. if (it == prev_it) {
  404. FAIL_CHECK("Infinite loop detected.");
  405. break;
  406. }
  407. }
  408. // We should not lose anything in the process.
  409. if (element_count != p_size) {
  410. FAIL_CHECK("Element count mismatch.");
  411. break;
  412. }
  413. }
  414. }
  415. TEST_CASE("[Stress][List] Swap random 100 elements, 500 iterations.") {
  416. List<int> list;
  417. List<int>::Element *n[100];
  418. populate_integers(list, n, 100);
  419. swap_random(list, n, 100, 500);
  420. }
  421. TEST_CASE("[Stress][List] Swap random 10 elements, 1000 iterations.") {
  422. List<int> list;
  423. List<int>::Element *n[10];
  424. populate_integers(list, n, 10);
  425. swap_random(list, n, 10, 1000);
  426. }
  427. } // namespace TestList
  428. #endif // TEST_LIST_H