test_hash_map.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #ifndef TEST_HASH_MAP_H
  31. #define TEST_HASH_MAP_H
  32. #include "core/templates/hash_map.h"
  33. #include "tests/test_macros.h"
  34. namespace TestHashMap {
  35. TEST_CASE("[HashMap] List initialization") {
  36. HashMap<int, String> map{ { 0, "A" }, { 1, "B" }, { 2, "C" }, { 3, "D" }, { 4, "E" } };
  37. CHECK(map.size() == 5);
  38. CHECK(map[0] == "A");
  39. CHECK(map[1] == "B");
  40. CHECK(map[2] == "C");
  41. CHECK(map[3] == "D");
  42. CHECK(map[4] == "E");
  43. }
  44. TEST_CASE("[HashMap] List initialization with existing elements") {
  45. HashMap<int, String> map{ { 0, "A" }, { 0, "B" }, { 0, "C" }, { 0, "D" }, { 0, "E" } };
  46. CHECK(map.size() == 1);
  47. CHECK(map[0] == "E");
  48. }
  49. TEST_CASE("[HashMap] Insert element") {
  50. HashMap<int, int> map;
  51. HashMap<int, int>::Iterator e = map.insert(42, 84);
  52. CHECK(e);
  53. CHECK(e->key == 42);
  54. CHECK(e->value == 84);
  55. CHECK(map[42] == 84);
  56. CHECK(map.has(42));
  57. CHECK(map.find(42));
  58. }
  59. TEST_CASE("[HashMap] Overwrite element") {
  60. HashMap<int, int> map;
  61. map.insert(42, 84);
  62. map.insert(42, 1234);
  63. CHECK(map[42] == 1234);
  64. }
  65. TEST_CASE("[HashMap] Erase via element") {
  66. HashMap<int, int> map;
  67. HashMap<int, int>::Iterator e = map.insert(42, 84);
  68. map.remove(e);
  69. CHECK(!map.has(42));
  70. CHECK(!map.find(42));
  71. }
  72. TEST_CASE("[HashMap] Erase via key") {
  73. HashMap<int, int> map;
  74. map.insert(42, 84);
  75. map.erase(42);
  76. CHECK(!map.has(42));
  77. CHECK(!map.find(42));
  78. }
  79. TEST_CASE("[HashMap] Size") {
  80. HashMap<int, int> map;
  81. map.insert(42, 84);
  82. map.insert(123, 84);
  83. map.insert(123, 84);
  84. map.insert(0, 84);
  85. map.insert(123485, 84);
  86. CHECK(map.size() == 4);
  87. }
  88. TEST_CASE("[HashMap] Iteration") {
  89. HashMap<int, int> map;
  90. map.insert(42, 84);
  91. map.insert(123, 12385);
  92. map.insert(0, 12934);
  93. map.insert(123485, 1238888);
  94. map.insert(123, 111111);
  95. Vector<Pair<int, int>> expected;
  96. expected.push_back(Pair<int, int>(42, 84));
  97. expected.push_back(Pair<int, int>(123, 111111));
  98. expected.push_back(Pair<int, int>(0, 12934));
  99. expected.push_back(Pair<int, int>(123485, 1238888));
  100. int idx = 0;
  101. for (const KeyValue<int, int> &E : map) {
  102. CHECK(expected[idx] == Pair<int, int>(E.key, E.value));
  103. ++idx;
  104. }
  105. }
  106. TEST_CASE("[HashMap] Const iteration") {
  107. HashMap<int, int> map;
  108. map.insert(42, 84);
  109. map.insert(123, 12385);
  110. map.insert(0, 12934);
  111. map.insert(123485, 1238888);
  112. map.insert(123, 111111);
  113. const HashMap<int, int> const_map = map;
  114. Vector<Pair<int, int>> expected;
  115. expected.push_back(Pair<int, int>(42, 84));
  116. expected.push_back(Pair<int, int>(123, 111111));
  117. expected.push_back(Pair<int, int>(0, 12934));
  118. expected.push_back(Pair<int, int>(123485, 1238888));
  119. expected.push_back(Pair<int, int>(123, 111111));
  120. int idx = 0;
  121. for (const KeyValue<int, int> &E : const_map) {
  122. CHECK(expected[idx] == Pair<int, int>(E.key, E.value));
  123. ++idx;
  124. }
  125. }
  126. } // namespace TestHashMap
  127. #endif // TEST_HASH_MAP_H