test_dictionary.h 16 KB

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