test_ordered_hash_map.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*************************************************************************/
  2. /* test_ordered_hash_map.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_ORDERED_HASH_MAP_H
  31. #define TEST_ORDERED_HASH_MAP_H
  32. #include "core/os/os.h"
  33. #include "core/templates/ordered_hash_map.h"
  34. #include "core/templates/pair.h"
  35. #include "core/templates/vector.h"
  36. #include "tests/test_macros.h"
  37. namespace TestOrderedHashMap {
  38. TEST_CASE("[OrderedHashMap] Insert element") {
  39. OrderedHashMap<int, int> map;
  40. OrderedHashMap<int, int>::Element e = map.insert(42, 84);
  41. CHECK(e);
  42. CHECK(e.key() == 42);
  43. CHECK(e.get() == 84);
  44. CHECK(e.value() == 84);
  45. CHECK(map[42] == 84);
  46. CHECK(map.has(42));
  47. CHECK(map.find(42));
  48. }
  49. TEST_CASE("[OrderedHashMap] Overwrite element") {
  50. OrderedHashMap<int, int> map;
  51. map.insert(42, 84);
  52. map.insert(42, 1234);
  53. CHECK(map[42] == 1234);
  54. }
  55. TEST_CASE("[OrderedHashMap] Erase via element") {
  56. OrderedHashMap<int, int> map;
  57. OrderedHashMap<int, int>::Element e = map.insert(42, 84);
  58. map.erase(e);
  59. CHECK(!e);
  60. CHECK(!map.has(42));
  61. CHECK(!map.find(42));
  62. }
  63. TEST_CASE("[OrderedHashMap] Erase via key") {
  64. OrderedHashMap<int, int> map;
  65. map.insert(42, 84);
  66. map.erase(42);
  67. CHECK(!map.has(42));
  68. CHECK(!map.find(42));
  69. }
  70. TEST_CASE("[OrderedHashMap] Size") {
  71. OrderedHashMap<int, int> map;
  72. map.insert(42, 84);
  73. map.insert(123, 84);
  74. map.insert(123, 84);
  75. map.insert(0, 84);
  76. map.insert(123485, 84);
  77. CHECK(map.size() == 4);
  78. }
  79. TEST_CASE("[OrderedHashMap] Iteration") {
  80. OrderedHashMap<int, int> map;
  81. map.insert(42, 84);
  82. map.insert(123, 12385);
  83. map.insert(0, 12934);
  84. map.insert(123485, 1238888);
  85. map.insert(123, 111111);
  86. Vector<Pair<int, int>> expected;
  87. expected.push_back(Pair<int, int>(42, 84));
  88. expected.push_back(Pair<int, int>(123, 111111));
  89. expected.push_back(Pair<int, int>(0, 12934));
  90. expected.push_back(Pair<int, int>(123485, 1238888));
  91. int idx = 0;
  92. for (OrderedHashMap<int, int>::Element E = map.front(); E; E = E.next()) {
  93. CHECK(expected[idx] == Pair<int, int>(E.key(), E.value()));
  94. ++idx;
  95. }
  96. }
  97. TEST_CASE("[OrderedHashMap] Const iteration") {
  98. OrderedHashMap<int, int> map;
  99. map.insert(42, 84);
  100. map.insert(123, 12385);
  101. map.insert(0, 12934);
  102. map.insert(123485, 1238888);
  103. map.insert(123, 111111);
  104. const OrderedHashMap<int, int> const_map = map;
  105. Vector<Pair<int, int>> expected;
  106. expected.push_back(Pair<int, int>(42, 84));
  107. expected.push_back(Pair<int, int>(123, 111111));
  108. expected.push_back(Pair<int, int>(0, 12934));
  109. expected.push_back(Pair<int, int>(123485, 1238888));
  110. int idx = 0;
  111. for (OrderedHashMap<int, int>::ConstElement E = const_map.front(); E; E = E.next()) {
  112. CHECK(expected[idx] == Pair<int, int>(E.key(), E.value()));
  113. ++idx;
  114. }
  115. }
  116. } // namespace TestOrderedHashMap
  117. #endif // TEST_ORDERED_HASH_MAP_H