test_hash_map.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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] Insert element") {
  36. HashMap<int, int> map;
  37. HashMap<int, int>::Iterator e = map.insert(42, 84);
  38. CHECK(e);
  39. CHECK(e->key == 42);
  40. CHECK(e->value == 84);
  41. CHECK(map[42] == 84);
  42. CHECK(map.has(42));
  43. CHECK(map.find(42));
  44. }
  45. TEST_CASE("[HashMap] Overwrite element") {
  46. HashMap<int, int> map;
  47. map.insert(42, 84);
  48. map.insert(42, 1234);
  49. CHECK(map[42] == 1234);
  50. }
  51. TEST_CASE("[HashMap] Erase via element") {
  52. HashMap<int, int> map;
  53. HashMap<int, int>::Iterator e = map.insert(42, 84);
  54. map.remove(e);
  55. CHECK(!map.has(42));
  56. CHECK(!map.find(42));
  57. }
  58. TEST_CASE("[HashMap] Erase via key") {
  59. HashMap<int, int> map;
  60. map.insert(42, 84);
  61. map.erase(42);
  62. CHECK(!map.has(42));
  63. CHECK(!map.find(42));
  64. }
  65. TEST_CASE("[HashMap] Size") {
  66. HashMap<int, int> map;
  67. map.insert(42, 84);
  68. map.insert(123, 84);
  69. map.insert(123, 84);
  70. map.insert(0, 84);
  71. map.insert(123485, 84);
  72. CHECK(map.size() == 4);
  73. }
  74. TEST_CASE("[HashMap] Iteration") {
  75. HashMap<int, int> map;
  76. map.insert(42, 84);
  77. map.insert(123, 12385);
  78. map.insert(0, 12934);
  79. map.insert(123485, 1238888);
  80. map.insert(123, 111111);
  81. Vector<Pair<int, int>> expected;
  82. expected.push_back(Pair<int, int>(42, 84));
  83. expected.push_back(Pair<int, int>(123, 111111));
  84. expected.push_back(Pair<int, int>(0, 12934));
  85. expected.push_back(Pair<int, int>(123485, 1238888));
  86. int idx = 0;
  87. for (const KeyValue<int, int> &E : map) {
  88. CHECK(expected[idx] == Pair<int, int>(E.key, E.value));
  89. ++idx;
  90. }
  91. }
  92. TEST_CASE("[HashMap] Const iteration") {
  93. HashMap<int, int> map;
  94. map.insert(42, 84);
  95. map.insert(123, 12385);
  96. map.insert(0, 12934);
  97. map.insert(123485, 1238888);
  98. map.insert(123, 111111);
  99. const HashMap<int, int> const_map = map;
  100. Vector<Pair<int, int>> expected;
  101. expected.push_back(Pair<int, int>(42, 84));
  102. expected.push_back(Pair<int, int>(123, 111111));
  103. expected.push_back(Pair<int, int>(0, 12934));
  104. expected.push_back(Pair<int, int>(123485, 1238888));
  105. expected.push_back(Pair<int, int>(123, 111111));
  106. int idx = 0;
  107. for (const KeyValue<int, int> &E : const_map) {
  108. CHECK(expected[idx] == Pair<int, int>(E.key, E.value));
  109. ++idx;
  110. }
  111. }
  112. } // namespace TestHashMap
  113. #endif // TEST_HASH_MAP_H