HashMapTests.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. void HashMapTests::test() {
  93. Test test("HashMap");
  94. testAdd(test);
  95. testMultipleAdd(test);
  96. testSearch(test);
  97. testAddReplace(test);
  98. testClear(test);
  99. testOverflow(test);
  100. testEmplace(test);
  101. testToString1(test);
  102. testToString2(test);
  103. testToString3(test);
  104. test.finalize();
  105. }