HashMapTests.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "tests/HashMapTests.h"
  2. #include "tests/Test.h"
  3. #include "utils/HashMap.h"
  4. #include "utils/StringBuffer.h"
  5. constexpr int MAP_MIN_CAPACITY = 5;
  6. typedef HashMap<int, int, MAP_MIN_CAPACITY> IntMap;
  7. typedef StringBuffer<50> String;
  8. static void testAdd(Test& test) {
  9. IntMap map;
  10. map.add(5, 4);
  11. test.checkEqual(true, map.contains(5), "contains added value");
  12. test.checkEqual(4, map.search(5, -1), "search finds added value");
  13. }
  14. static void testMultipleAdd(Test& test) {
  15. IntMap map;
  16. map.add(5, 4).add(10, 3).add(15, 2);
  17. test.checkEqual(true, map.contains(5), "contains added value 1");
  18. test.checkEqual(true, map.contains(10), "contains added value 2");
  19. test.checkEqual(true, map.contains(15), "contains added value 3");
  20. test.checkEqual(4, map.search(5, -1), "search finds added value 1");
  21. test.checkEqual(3, map.search(10, -1), "search finds added value 2");
  22. test.checkEqual(2, map.search(15, -1), "search finds added value 3");
  23. }
  24. static void testSearch(Test& test) {
  25. IntMap map;
  26. test.checkEqual(-1, map.search(6, -1), "search does not find missing key");
  27. }
  28. static void testAddReplace(Test& test) {
  29. IntMap map;
  30. map.add(5, 4).add(5, 10);
  31. test.checkEqual(true, map.contains(5), "contains replaced value");
  32. test.checkEqual(10, map.search(5, -1), "search finds replaced value");
  33. }
  34. static void testClear(Test& test) {
  35. IntMap map;
  36. map.add(5, 4).add(4, 10).clear();
  37. test.checkEqual(false, map.contains(5), "does not contain cleared values 1");
  38. test.checkEqual(false, map.contains(4), "does not contain cleared values 2");
  39. }
  40. static void testOverflow(Test& test) {
  41. IntMap map;
  42. for(int i = 0; i < 1000000; i++) {
  43. map.add(i, i);
  44. }
  45. for(int i = 0; i < MAP_MIN_CAPACITY; i++) {
  46. test.checkEqual(true, map.contains(i), "still contains values after overflow");
  47. }
  48. test.checkEqual(true, true, "survives overflow");
  49. }
  50. struct A {
  51. int a;
  52. int b;
  53. A(int a, int b) : a(a), b(b) {
  54. }
  55. bool operator==(const A& other) const {
  56. return a == other.a && b == other.b;
  57. }
  58. };
  59. std::ostream& operator<<(std::ostream& os, const A& a) {
  60. return os << "A(" << a.a << ", " << a.b << ")";
  61. }
  62. static void testEmplace(Test& test) {
  63. HashMap<int, A, 5> map;
  64. bool r1 = map.tryEmplace(0, 3, 4);
  65. bool r2 = map.tryEmplace(3, 4, 5);
  66. bool r3 = map.tryEmplace(20, 5, 6);
  67. bool r4 = map.tryEmplace(3, 6, 7);
  68. bool r5 = map.tryEmplace(20, 7, 8);
  69. test.checkEqual(A(3, 4), map.search(0, A(0, 0)), "contains emplaced value 1");
  70. test.checkEqual(A(4, 5), map.search(3, A(0, 0)), "contains emplaced value 2");
  71. test.checkEqual(A(5, 6), map.search(20, A(0, 0)), "contains emplaced value 3");
  72. test.checkEqual(false, r1, "emplacing returns correct value 1");
  73. test.checkEqual(false, r2, "emplacing returns correct value 2");
  74. test.checkEqual(false, r3, "emplacing returns correct value 3");
  75. test.checkEqual(true, r4, "emplacing returns correct value 4");
  76. test.checkEqual(true, r5, "emplacing returns correct value 5");
  77. }
  78. static void testToString1(Test& test) {
  79. IntMap map;
  80. map.add(1, 3).add(2, 4).add(3, 5);
  81. test.checkEqual(String("[1 = 3, 2 = 4, 3 = 5]"), String(map), "to string 1");
  82. }
  83. static void testToString2(Test& test) {
  84. IntMap map;
  85. map.add(1, 3);
  86. test.checkEqual(String("[1 = 3]"), String(map), "to string 2");
  87. }
  88. static void testToString3(Test& test) {
  89. IntMap map;
  90. test.checkEqual(String("[]"), String(map), "to string 3");
  91. }
  92. static void testCopy(Test& test) {
  93. IntMap map;
  94. map.add(1, 3).add(2, 4).add(3, 5);
  95. IntMap copy(map);
  96. test.checkEqual(map.search(1, 0), copy.search(1, -1), "copy has same values 1");
  97. test.checkEqual(map.search(2, 0), copy.search(2, -1), "copy has same values 2");
  98. test.checkEqual(map.search(3, 0), copy.search(3, -1), "copy has same values 3");
  99. }
  100. static void testCopyAssignment(Test& test) {
  101. IntMap map;
  102. map.add(1, 3).add(2, 4).add(3, 5);
  103. IntMap copy;
  104. copy = map;
  105. test.checkEqual(map.search(1, 0), copy.search(1, -1), "copy assignment has same values 1");
  106. test.checkEqual(map.search(2, 0), copy.search(2, -1), "copy assignment has same values 2");
  107. test.checkEqual(map.search(3, 0), copy.search(3, -1), "copy assignment has same values 3");
  108. }
  109. static void testMove(Test& test) {
  110. IntMap map;
  111. map.add(1, 3).add(2, 4).add(3, 5);
  112. IntMap move(std::move(map));
  113. test.checkEqual(3, move.search(1, -1), "move moves values 1");
  114. test.checkEqual(4, move.search(2, -1), "move moves values 2");
  115. test.checkEqual(5, move.search(3, -1), "move moves values 3");
  116. test.checkEqual(-1, map.search(1, -1), "move removes values 1");
  117. test.checkEqual(-1, map.search(2, -1), "move removes values 2");
  118. test.checkEqual(-1, map.search(3, -1), "move removes values 3");
  119. }
  120. static void testMoveAssignment(Test& test) {
  121. IntMap map;
  122. map.add(1, 3).add(2, 4).add(3, 5);
  123. IntMap move;
  124. move = std::move(map);
  125. test.checkEqual(3, move.search(1, -1), "move assignment moves values 1");
  126. test.checkEqual(4, move.search(2, -1), "move assignment moves values 2");
  127. test.checkEqual(5, move.search(3, -1), "move assignment moves values 3");
  128. test.checkEqual(-1, map.search(1, -1), "move assignment removes values 1");
  129. test.checkEqual(-1, map.search(2, -1), "move assignment removes values 2");
  130. test.checkEqual(-1, map.search(3, -1), "move assignment removes values 3");
  131. }
  132. void HashMapTests::test() {
  133. Test test("HashMap");
  134. testAdd(test);
  135. testMultipleAdd(test);
  136. testSearch(test);
  137. testAddReplace(test);
  138. testClear(test);
  139. testOverflow(test);
  140. testEmplace(test);
  141. testToString1(test);
  142. testToString2(test);
  143. testToString3(test);
  144. testCopy(test);
  145. testCopyAssignment(test);
  146. testMove(test);
  147. testMoveAssignment(test);
  148. test.finalize();
  149. }