test_ordered_hash_map.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**************************************************************************/
  2. /* test_ordered_hash_map.cpp */
  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. #include "test_ordered_hash_map.h"
  31. #include "core/ordered_hash_map.h"
  32. #include "core/os/os.h"
  33. #include "core/pair.h"
  34. #include "core/vector.h"
  35. namespace TestOrderedHashMap {
  36. bool test_insert() {
  37. OrderedHashMap<int, int> map;
  38. OrderedHashMap<int, int>::Element e = map.insert(42, 84);
  39. return e && e.key() == 42 && e.get() == 84 && e.value() == 84 && map[42] == 84 && map.has(42) && map.find(42);
  40. }
  41. bool test_insert_overwrite() {
  42. OrderedHashMap<int, int> map;
  43. map.insert(42, 84);
  44. map.insert(42, 1234);
  45. return map[42] == 1234;
  46. }
  47. bool test_erase_via_element() {
  48. OrderedHashMap<int, int> map;
  49. OrderedHashMap<int, int>::Element e = map.insert(42, 84);
  50. map.erase(e);
  51. return !e && !map.has(42) && !map.find(42);
  52. }
  53. bool test_erase_via_key() {
  54. OrderedHashMap<int, int> map;
  55. map.insert(42, 84);
  56. map.erase(42);
  57. return !map.has(42) && !map.find(42);
  58. }
  59. bool test_size() {
  60. OrderedHashMap<int, int> map;
  61. map.insert(42, 84);
  62. map.insert(123, 84);
  63. map.insert(123, 84);
  64. map.insert(0, 84);
  65. map.insert(123485, 84);
  66. return map.size() == 4;
  67. }
  68. bool test_iteration() {
  69. OrderedHashMap<int, int> map;
  70. map.insert(42, 84);
  71. map.insert(123, 12385);
  72. map.insert(0, 12934);
  73. map.insert(123485, 1238888);
  74. map.insert(123, 111111);
  75. Vector<Pair<int, int>> expected;
  76. expected.push_back(Pair<int, int>(42, 84));
  77. expected.push_back(Pair<int, int>(123, 111111));
  78. expected.push_back(Pair<int, int>(0, 12934));
  79. expected.push_back(Pair<int, int>(123485, 1238888));
  80. int idx = 0;
  81. for (OrderedHashMap<int, int>::Element E = map.front(); E; E = E.next()) {
  82. if (expected[idx] != Pair<int, int>(E.key(), E.value())) {
  83. return false;
  84. }
  85. ++idx;
  86. }
  87. return true;
  88. }
  89. bool test_const_iteration(const OrderedHashMap<int, int> &map) {
  90. Vector<Pair<int, int>> expected;
  91. expected.push_back(Pair<int, int>(42, 84));
  92. expected.push_back(Pair<int, int>(123, 111111));
  93. expected.push_back(Pair<int, int>(0, 12934));
  94. expected.push_back(Pair<int, int>(123485, 1238888));
  95. int idx = 0;
  96. for (OrderedHashMap<int, int>::ConstElement E = map.front(); E; E = E.next()) {
  97. if (expected[idx] != Pair<int, int>(E.key(), E.value())) {
  98. return false;
  99. }
  100. ++idx;
  101. }
  102. return true;
  103. }
  104. bool test_const_iteration() {
  105. OrderedHashMap<int, int> map;
  106. map.insert(42, 84);
  107. map.insert(123, 12385);
  108. map.insert(0, 12934);
  109. map.insert(123485, 1238888);
  110. map.insert(123, 111111);
  111. return test_const_iteration(map);
  112. }
  113. typedef bool (*TestFunc)();
  114. TestFunc test_funcs[] = {
  115. test_insert,
  116. test_insert_overwrite,
  117. test_erase_via_element,
  118. test_erase_via_key,
  119. test_size,
  120. test_iteration,
  121. test_const_iteration,
  122. nullptr
  123. };
  124. MainLoop *test() {
  125. int count = 0;
  126. int passed = 0;
  127. while (true) {
  128. if (!test_funcs[count]) {
  129. break;
  130. }
  131. bool pass = test_funcs[count]();
  132. if (pass) {
  133. passed++;
  134. }
  135. OS::get_singleton()->print("\t%s\n", pass ? "PASS" : "FAILED");
  136. count++;
  137. }
  138. OS::get_singleton()->print("\n\n\n");
  139. OS::get_singleton()->print("*************\n");
  140. OS::get_singleton()->print("***TOTALS!***\n");
  141. OS::get_singleton()->print("*************\n");
  142. OS::get_singleton()->print("Passed %i of %i tests\n", passed, count);
  143. return nullptr;
  144. }
  145. } // namespace TestOrderedHashMap