HashMapTests.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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);
  17. map.add(10, 3);
  18. map.add(15, 2);
  19. test.checkEqual(true, map.contains(5), "contains added value 1");
  20. test.checkEqual(true, map.contains(10), "contains added value 2");
  21. test.checkEqual(true, map.contains(15), "contains added value 3");
  22. test.checkEqual(4, map.search(5, -1), "search finds added value 1");
  23. test.checkEqual(3, map.search(10, -1), "search finds added value 2");
  24. test.checkEqual(2, map.search(15, -1), "search finds added value 3");
  25. }
  26. static void testSearch(Test& test) {
  27. IntMap map;
  28. test.checkEqual(-1, map.search(6, -1), "search does not find missing key");
  29. }
  30. static void testAddReplace(Test& test) {
  31. IntMap map;
  32. map.add(5, 4);
  33. map.add(5, 10);
  34. test.checkEqual(true, map.contains(5), "contains replaced value");
  35. test.checkEqual(10, map.search(5, -1), "search finds replaced value");
  36. }
  37. static void testClear(Test& test) {
  38. IntMap map;
  39. map.add(5, 4);
  40. map.add(4, 10);
  41. map.clear();
  42. test.checkEqual(false, map.contains(5), "does not contain cleared values");
  43. test.checkEqual(false, map.contains(4), "does not contain cleared values");
  44. }
  45. static void testOverflow(Test& test) {
  46. IntMap map;
  47. for(int i = 0; i < 1000000; i++) {
  48. map.add(i, i);
  49. }
  50. for(int i = 0; i < MAP_MIN_CAPACITY; i++) {
  51. test.checkEqual(true, map.contains(i), "still contains values after overflow");
  52. }
  53. test.checkEqual(true, true, "survives overflow");
  54. }
  55. struct A {
  56. int a;
  57. int b;
  58. A(int a, int b) : a(a), b(b) {
  59. }
  60. bool operator==(const A& other) const {
  61. return a == other.a && b == other.b;
  62. }
  63. };
  64. std::ostream& operator<<(std::ostream& os, const A& a) {
  65. return os << "A(" << a.a << ", " << a.b << ")";
  66. }
  67. static void testEmplace(Test& test) {
  68. HashMap<int, A, 5> map;
  69. bool r1 = map.tryEmplace(0, 3, 4);
  70. bool r2 = map.tryEmplace(3, 4, 5);
  71. bool r3 = map.tryEmplace(20, 5, 6);
  72. bool r4 = map.tryEmplace(3, 6, 7);
  73. bool r5 = map.tryEmplace(20, 7, 8);
  74. test.checkEqual(A(3, 4), map.search(0, A(0, 0)), "contains emplaced value 1");
  75. test.checkEqual(A(4, 5), map.search(3, A(0, 0)), "contains emplaced value 2");
  76. test.checkEqual(A(5, 6), map.search(20, A(0, 0)), "contains emplaced value 3");
  77. test.checkEqual(false, r1, "emplacing returns correct value 1");
  78. test.checkEqual(false, r2, "emplacing returns correct value 2");
  79. test.checkEqual(false, r3, "emplacing returns correct value 3");
  80. test.checkEqual(true, r4, "emplacing returns correct value 4");
  81. test.checkEqual(true, r5, "emplacing returns correct value 5");
  82. }
  83. static void testToString1(Test& test) {
  84. IntMap map;
  85. map.add(1, 3);
  86. map.add(2, 4);
  87. map.add(3, 5);
  88. String s;
  89. s.append(map);
  90. test.checkEqual(String("[1 = 3, 2 = 4, 3 = 5]"), s, "to string 1");
  91. }
  92. static void testToString2(Test& test) {
  93. IntMap map;
  94. map.add(1, 3);
  95. String s;
  96. s.append(map);
  97. test.checkEqual(String("[1 = 3]"), s, "to string 2");
  98. }
  99. static void testToString3(Test& test) {
  100. IntMap map;
  101. String s;
  102. s.append(map);
  103. test.checkEqual(String("[]"), s, "to string 3");
  104. }
  105. void HashMapTests::test() {
  106. Test test("HashMap");
  107. testAdd(test);
  108. testMultipleAdd(test);
  109. testSearch(test);
  110. testAddReplace(test);
  111. testClear(test);
  112. testOverflow(test);
  113. testEmplace(test);
  114. testToString1(test);
  115. testToString2(test);
  116. testToString3(test);
  117. test.finalize();
  118. }