test_dictionary.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /**************************************************************************/
  2. /* test_dictionary.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_DICTIONARY_H
  31. #define TEST_DICTIONARY_H
  32. #include "core/variant/typed_dictionary.h"
  33. #include "tests/test_macros.h"
  34. namespace TestDictionary {
  35. static inline Array build_array() {
  36. return Array();
  37. }
  38. template <typename... Targs>
  39. static inline Array build_array(Variant item, Targs... Fargs) {
  40. Array a = build_array(Fargs...);
  41. a.push_front(item);
  42. return a;
  43. }
  44. static inline Dictionary build_dictionary() {
  45. return Dictionary();
  46. }
  47. template <typename... Targs>
  48. static inline Dictionary build_dictionary(Variant key, Variant item, Targs... Fargs) {
  49. Dictionary d = build_dictionary(Fargs...);
  50. d[key] = item;
  51. return d;
  52. }
  53. TEST_CASE("[Dictionary] Assignment using bracket notation ([])") {
  54. Dictionary map;
  55. map["Hello"] = 0;
  56. CHECK(int(map["Hello"]) == 0);
  57. map["Hello"] = 3;
  58. CHECK(int(map["Hello"]) == 3);
  59. map["World!"] = 4;
  60. CHECK(int(map["World!"]) == 4);
  61. map[StringName("HelloName")] = 6;
  62. CHECK(int(map[StringName("HelloName")]) == 6);
  63. CHECK(int(map.find_key(6).get_type()) == Variant::STRING_NAME);
  64. map[StringName("HelloName")] = 7;
  65. CHECK(int(map[StringName("HelloName")]) == 7);
  66. // Test String and StringName are equivalent.
  67. map[StringName("Hello")] = 8;
  68. CHECK(int(map["Hello"]) == 8);
  69. map["Hello"] = 9;
  70. CHECK(int(map[StringName("Hello")]) == 9);
  71. // Test non-string keys, since keys can be of any Variant type.
  72. map[12345] = -5;
  73. CHECK(int(map[12345]) == -5);
  74. map[false] = 128;
  75. CHECK(int(map[false]) == 128);
  76. map[Vector2(10, 20)] = 30;
  77. CHECK(int(map[Vector2(10, 20)]) == 30);
  78. map[0] = 400;
  79. CHECK(int(map[0]) == 400);
  80. // Check that assigning 0 doesn't overwrite the value for `false`.
  81. CHECK(int(map[false]) == 128);
  82. // Ensure read-only maps aren't modified by non-existing keys.
  83. const int length = map.size();
  84. map.make_read_only();
  85. CHECK(int(map["This key does not exist"].get_type()) == Variant::NIL);
  86. CHECK(map.size() == length);
  87. }
  88. TEST_CASE("[Dictionary] get_key_lists()") {
  89. Dictionary map;
  90. List<Variant> keys;
  91. List<Variant> *ptr = &keys;
  92. map.get_key_list(ptr);
  93. CHECK(keys.is_empty());
  94. map[1] = 3;
  95. map.get_key_list(ptr);
  96. CHECK(keys.size() == 1);
  97. CHECK(int(keys.front()->get()) == 1);
  98. map[2] = 4;
  99. map.get_key_list(ptr);
  100. CHECK(keys.size() == 3);
  101. }
  102. TEST_CASE("[Dictionary] get_key_at_index()") {
  103. Dictionary map;
  104. map[4] = 3;
  105. Variant val = map.get_key_at_index(0);
  106. CHECK(int(val) == 4);
  107. map[3] = 1;
  108. val = map.get_key_at_index(0);
  109. CHECK(int(val) == 4);
  110. val = map.get_key_at_index(1);
  111. CHECK(int(val) == 3);
  112. }
  113. TEST_CASE("[Dictionary] getptr()") {
  114. Dictionary map;
  115. map[1] = 3;
  116. Variant *key = map.getptr(1);
  117. CHECK(int(*key) == 3);
  118. key = map.getptr(2);
  119. CHECK(key == nullptr);
  120. }
  121. TEST_CASE("[Dictionary] get_valid()") {
  122. Dictionary map;
  123. map[1] = 3;
  124. Variant val = map.get_valid(1);
  125. CHECK(int(val) == 3);
  126. }
  127. TEST_CASE("[Dictionary] get()") {
  128. Dictionary map;
  129. map[1] = 3;
  130. Variant val = map.get(1, -1);
  131. CHECK(int(val) == 3);
  132. }
  133. TEST_CASE("[Dictionary] size(), empty() and clear()") {
  134. Dictionary map;
  135. CHECK(map.size() == 0);
  136. CHECK(map.is_empty());
  137. map[1] = 3;
  138. CHECK(map.size() == 1);
  139. CHECK(!map.is_empty());
  140. map.clear();
  141. CHECK(map.size() == 0);
  142. CHECK(map.is_empty());
  143. }
  144. TEST_CASE("[Dictionary] has() and has_all()") {
  145. Dictionary map;
  146. CHECK(map.has(1) == false);
  147. map[1] = 3;
  148. CHECK(map.has(1));
  149. Array keys;
  150. keys.push_back(1);
  151. CHECK(map.has_all(keys));
  152. keys.push_back(2);
  153. CHECK(map.has_all(keys) == false);
  154. }
  155. TEST_CASE("[Dictionary] keys() and values()") {
  156. Dictionary map;
  157. Array keys = map.keys();
  158. Array values = map.values();
  159. CHECK(keys.is_empty());
  160. CHECK(values.is_empty());
  161. map[1] = 3;
  162. keys = map.keys();
  163. values = map.values();
  164. CHECK(int(keys[0]) == 1);
  165. CHECK(int(values[0]) == 3);
  166. }
  167. TEST_CASE("[Dictionary] Duplicate dictionary") {
  168. // d = {1: {1: 1}, {2: 2}: [2], [3]: 3}
  169. Dictionary k2 = build_dictionary(2, 2);
  170. Array k3 = build_array(3);
  171. Dictionary d = build_dictionary(1, build_dictionary(1, 1), k2, build_array(2), k3, 3);
  172. // Deep copy
  173. Dictionary deep_d = d.duplicate(true);
  174. CHECK_MESSAGE(deep_d.id() != d.id(), "Should create a new dictionary");
  175. CHECK_MESSAGE(Dictionary(deep_d[1]).id() != Dictionary(d[1]).id(), "Should clone nested dictionary");
  176. CHECK_MESSAGE(Array(deep_d[k2]).id() != Array(d[k2]).id(), "Should clone nested array");
  177. CHECK_EQ(deep_d, d);
  178. deep_d[0] = 0;
  179. CHECK_NE(deep_d, d);
  180. deep_d.erase(0);
  181. Dictionary(deep_d[1]).operator[](0) = 0;
  182. CHECK_NE(deep_d, d);
  183. Dictionary(deep_d[1]).erase(0);
  184. CHECK_EQ(deep_d, d);
  185. // Keys should also be copied
  186. k2[0] = 0;
  187. CHECK_NE(deep_d, d);
  188. k2.erase(0);
  189. CHECK_EQ(deep_d, d);
  190. k3.push_back(0);
  191. CHECK_NE(deep_d, d);
  192. k3.pop_back();
  193. CHECK_EQ(deep_d, d);
  194. // Shallow copy
  195. Dictionary shallow_d = d.duplicate(false);
  196. CHECK_MESSAGE(shallow_d.id() != d.id(), "Should create a new array");
  197. CHECK_MESSAGE(Dictionary(shallow_d[1]).id() == Dictionary(d[1]).id(), "Should keep nested dictionary");
  198. CHECK_MESSAGE(Array(shallow_d[k2]).id() == Array(d[k2]).id(), "Should keep nested array");
  199. CHECK_EQ(shallow_d, d);
  200. shallow_d[0] = 0;
  201. CHECK_NE(shallow_d, d);
  202. shallow_d.erase(0);
  203. #if 0 // TODO: recursion in dict key currently is buggy
  204. // Keys should also be shallowed
  205. k2[0] = 0;
  206. CHECK_EQ(shallow_d, d);
  207. k2.erase(0);
  208. k3.push_back(0);
  209. CHECK_EQ(shallow_d, d);
  210. #endif
  211. }
  212. TEST_CASE("[Dictionary] Duplicate recursive dictionary") {
  213. // Self recursive
  214. Dictionary d;
  215. d[1] = d;
  216. Dictionary d_shallow = d.duplicate(false);
  217. CHECK_EQ(d, d_shallow);
  218. // Deep copy of recursive dictionary endup with recursion limit and return
  219. // an invalid result (multiple nested dictionaries), the point is we should
  220. // not end up with a segfault and an error log should be printed
  221. ERR_PRINT_OFF;
  222. d.duplicate(true);
  223. ERR_PRINT_ON;
  224. // Nested recursive
  225. Dictionary d1;
  226. Dictionary d2;
  227. d1[2] = d2;
  228. d2[1] = d1;
  229. Dictionary d1_shallow = d1.duplicate(false);
  230. CHECK_EQ(d1, d1_shallow);
  231. // Same deep copy issue as above
  232. ERR_PRINT_OFF;
  233. d1.duplicate(true);
  234. ERR_PRINT_ON;
  235. // Break the recursivity otherwise Dictionary teardown will leak memory
  236. d.clear();
  237. d1.clear();
  238. d2.clear();
  239. }
  240. #if 0 // TODO: duplicate recursion in dict key is currently buggy
  241. TEST_CASE("[Dictionary] Duplicate recursive dictionary on keys") {
  242. // Self recursive
  243. Dictionary d;
  244. d[d] = d;
  245. Dictionary d_shallow = d.duplicate(false);
  246. CHECK_EQ(d, d_shallow);
  247. // Deep copy of recursive dictionary endup with recursion limit and return
  248. // an invalid result (multiple nested dictionaries), the point is we should
  249. // not end up with a segfault and an error log should be printed
  250. ERR_PRINT_OFF;
  251. d.duplicate(true);
  252. ERR_PRINT_ON;
  253. // Nested recursive
  254. Dictionary d1;
  255. Dictionary d2;
  256. d1[d2] = d2;
  257. d2[d1] = d1;
  258. Dictionary d1_shallow = d1.duplicate(false);
  259. CHECK_EQ(d1, d1_shallow);
  260. // Same deep copy issue as above
  261. ERR_PRINT_OFF;
  262. d1.duplicate(true);
  263. ERR_PRINT_ON;
  264. // Break the recursivity otherwise Dictionary teardown will leak memory
  265. d.clear();
  266. d1.clear();
  267. d2.clear();
  268. }
  269. #endif
  270. TEST_CASE("[Dictionary] Hash dictionary") {
  271. // d = {1: {1: 1}, {2: 2}: [2], [3]: 3}
  272. Dictionary k2 = build_dictionary(2, 2);
  273. Array k3 = build_array(3);
  274. Dictionary d = build_dictionary(1, build_dictionary(1, 1), k2, build_array(2), k3, 3);
  275. uint32_t original_hash = d.hash();
  276. // Modify dict change the hash
  277. d[0] = 0;
  278. CHECK_NE(d.hash(), original_hash);
  279. d.erase(0);
  280. CHECK_EQ(d.hash(), original_hash);
  281. // Modify nested item change the hash
  282. Dictionary(d[1]).operator[](0) = 0;
  283. CHECK_NE(d.hash(), original_hash);
  284. Dictionary(d[1]).erase(0);
  285. Array(d[k2]).push_back(0);
  286. CHECK_NE(d.hash(), original_hash);
  287. Array(d[k2]).pop_back();
  288. // Modify a key change the hash
  289. k2[0] = 0;
  290. CHECK_NE(d.hash(), original_hash);
  291. k2.erase(0);
  292. CHECK_EQ(d.hash(), original_hash);
  293. k3.push_back(0);
  294. CHECK_NE(d.hash(), original_hash);
  295. k3.pop_back();
  296. CHECK_EQ(d.hash(), original_hash);
  297. // Duplication doesn't change the hash
  298. Dictionary d2 = d.duplicate(true);
  299. CHECK_EQ(d2.hash(), original_hash);
  300. }
  301. TEST_CASE("[Dictionary] Hash recursive dictionary") {
  302. Dictionary d;
  303. d[1] = d;
  304. // Hash should reach recursion limit, we just make sure this doesn't blow up
  305. ERR_PRINT_OFF;
  306. d.hash();
  307. ERR_PRINT_ON;
  308. // Break the recursivity otherwise Dictionary teardown will leak memory
  309. d.clear();
  310. }
  311. #if 0 // TODO: recursion in dict key is currently buggy
  312. TEST_CASE("[Dictionary] Hash recursive dictionary on keys") {
  313. Dictionary d;
  314. d[d] = 1;
  315. // Hash should reach recursion limit, we just make sure this doesn't blow up
  316. ERR_PRINT_OFF;
  317. d.hash();
  318. ERR_PRINT_ON;
  319. // Break the recursivity otherwise Dictionary teardown will leak memory
  320. d.clear();
  321. }
  322. #endif
  323. TEST_CASE("[Dictionary] Empty comparison") {
  324. Dictionary d1;
  325. Dictionary d2;
  326. // test both operator== and operator!=
  327. CHECK_EQ(d1, d2);
  328. CHECK_FALSE(d1 != d2);
  329. }
  330. TEST_CASE("[Dictionary] Flat comparison") {
  331. Dictionary d1 = build_dictionary(1, 1);
  332. Dictionary d2 = build_dictionary(1, 1);
  333. Dictionary other_d = build_dictionary(2, 1);
  334. // test both operator== and operator!=
  335. CHECK_EQ(d1, d1); // compare self
  336. CHECK_FALSE(d1 != d1);
  337. CHECK_EQ(d1, d2); // different equivalent arrays
  338. CHECK_FALSE(d1 != d2);
  339. CHECK_NE(d1, other_d); // different arrays with different content
  340. CHECK_FALSE(d1 == other_d);
  341. }
  342. TEST_CASE("[Dictionary] Nested dictionary comparison") {
  343. // d1 = {1: {2: {3: 4}}}
  344. Dictionary d1 = build_dictionary(1, build_dictionary(2, build_dictionary(3, 4)));
  345. Dictionary d2 = d1.duplicate(true);
  346. // other_d = {1: {2: {3: 0}}}
  347. Dictionary other_d = build_dictionary(1, build_dictionary(2, build_dictionary(3, 0)));
  348. // test both operator== and operator!=
  349. CHECK_EQ(d1, d1); // compare self
  350. CHECK_FALSE(d1 != d1);
  351. CHECK_EQ(d1, d2); // different equivalent arrays
  352. CHECK_FALSE(d1 != d2);
  353. CHECK_NE(d1, other_d); // different arrays with different content
  354. CHECK_FALSE(d1 == other_d);
  355. }
  356. TEST_CASE("[Dictionary] Nested array comparison") {
  357. // d1 = {1: [2, 3]}
  358. Dictionary d1 = build_dictionary(1, build_array(2, 3));
  359. Dictionary d2 = d1.duplicate(true);
  360. // other_d = {1: [2, 0]}
  361. Dictionary other_d = build_dictionary(1, build_array(2, 0));
  362. // test both operator== and operator!=
  363. CHECK_EQ(d1, d1); // compare self
  364. CHECK_FALSE(d1 != d1);
  365. CHECK_EQ(d1, d2); // different equivalent arrays
  366. CHECK_FALSE(d1 != d2);
  367. CHECK_NE(d1, other_d); // different arrays with different content
  368. CHECK_FALSE(d1 == other_d);
  369. }
  370. TEST_CASE("[Dictionary] Recursive comparison") {
  371. Dictionary d1;
  372. d1[1] = d1;
  373. Dictionary d2;
  374. d2[1] = d2;
  375. // Comparison should reach recursion limit
  376. ERR_PRINT_OFF;
  377. CHECK_EQ(d1, d2);
  378. CHECK_FALSE(d1 != d2);
  379. ERR_PRINT_ON;
  380. d1[2] = 2;
  381. d2[2] = 2;
  382. // Comparison should reach recursion limit
  383. ERR_PRINT_OFF;
  384. CHECK_EQ(d1, d2);
  385. CHECK_FALSE(d1 != d2);
  386. ERR_PRINT_ON;
  387. d1[3] = 3;
  388. d2[3] = 0;
  389. // Comparison should reach recursion limit
  390. ERR_PRINT_OFF;
  391. CHECK_NE(d1, d2);
  392. CHECK_FALSE(d1 == d2);
  393. ERR_PRINT_ON;
  394. // Break the recursivity otherwise Dictionary teardown will leak memory
  395. d1.clear();
  396. d2.clear();
  397. }
  398. #if 0 // TODO: recursion in dict key is currently buggy
  399. TEST_CASE("[Dictionary] Recursive comparison on keys") {
  400. Dictionary d1;
  401. // Hash computation should reach recursion limit
  402. ERR_PRINT_OFF;
  403. d1[d1] = 1;
  404. ERR_PRINT_ON;
  405. Dictionary d2;
  406. // Hash computation should reach recursion limit
  407. ERR_PRINT_OFF;
  408. d2[d2] = 1;
  409. ERR_PRINT_ON;
  410. // Comparison should reach recursion limit
  411. ERR_PRINT_OFF;
  412. CHECK_EQ(d1, d2);
  413. CHECK_FALSE(d1 != d2);
  414. ERR_PRINT_ON;
  415. d1[2] = 2;
  416. d2[2] = 2;
  417. // Comparison should reach recursion limit
  418. ERR_PRINT_OFF;
  419. CHECK_EQ(d1, d2);
  420. CHECK_FALSE(d1 != d2);
  421. ERR_PRINT_ON;
  422. d1[3] = 3;
  423. d2[3] = 0;
  424. // Comparison should reach recursion limit
  425. ERR_PRINT_OFF;
  426. CHECK_NE(d1, d2);
  427. CHECK_FALSE(d1 == d2);
  428. ERR_PRINT_ON;
  429. // Break the recursivity otherwise Dictionary teardown will leak memory
  430. d1.clear();
  431. d2.clear();
  432. }
  433. #endif
  434. TEST_CASE("[Dictionary] Recursive self comparison") {
  435. Dictionary d1;
  436. Dictionary d2;
  437. d1[1] = d2;
  438. d2[1] = d1;
  439. CHECK_EQ(d1, d1);
  440. CHECK_FALSE(d1 != d1);
  441. // Break the recursivity otherwise Dictionary teardown will leak memory
  442. d1.clear();
  443. d2.clear();
  444. }
  445. TEST_CASE("[Dictionary] Order and find") {
  446. Dictionary d;
  447. d[4] = "four";
  448. d[8] = "eight";
  449. d[12] = "twelve";
  450. d["4"] = "four";
  451. Array keys;
  452. keys.append(4);
  453. keys.append(8);
  454. keys.append(12);
  455. keys.append("4");
  456. CHECK_EQ(d.keys(), keys);
  457. CHECK_EQ(d.find_key("four"), Variant(4));
  458. CHECK_EQ(d.find_key("does not exist"), Variant());
  459. }
  460. TEST_CASE("[Dictionary] Typed copying") {
  461. TypedDictionary<int, int> d1;
  462. d1[0] = 1;
  463. TypedDictionary<double, double> d2;
  464. d2[0] = 1.0;
  465. Dictionary d3 = d1;
  466. TypedDictionary<int, int> d4 = d3;
  467. Dictionary d5 = d2;
  468. TypedDictionary<int, int> d6 = d5;
  469. d3[0] = 2;
  470. d4[0] = 3;
  471. // Same typed TypedDictionary should be shared.
  472. CHECK_EQ(d1[0], Variant(3));
  473. CHECK_EQ(d3[0], Variant(3));
  474. CHECK_EQ(d4[0], Variant(3));
  475. d5[0] = 2.0;
  476. d6[0] = 3.0;
  477. // Different typed TypedDictionary should not be shared.
  478. CHECK_EQ(d2[0], Variant(2.0));
  479. CHECK_EQ(d5[0], Variant(2.0));
  480. CHECK_EQ(d6[0], Variant(3.0));
  481. d1.clear();
  482. d2.clear();
  483. d3.clear();
  484. d4.clear();
  485. d5.clear();
  486. d6.clear();
  487. }
  488. } // namespace TestDictionary
  489. #endif // TEST_DICTIONARY_H