test_ordered_hash_map.cpp 5.2 KB

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