HashMapTests.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "tests/HashMapTests.h"
  2. #include "tests/Test.h"
  3. #include "utils/HashMap.h"
  4. constexpr int MAP_MIN_CAPACITY = 5;
  5. typedef HashMap<int, int, MAP_MIN_CAPACITY> IntMap;
  6. static void testAdd(Test& test) {
  7. IntMap map;
  8. map.add(5, 4);
  9. test.checkEqual(true, map.contains(5), "contains added value");
  10. test.checkEqual(4, map.search(5, -1), "search finds added value");
  11. }
  12. static void testMultipleAdd(Test& test) {
  13. IntMap map;
  14. map.add(5, 4);
  15. map.add(10, 3);
  16. map.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);
  31. map.add(5, 10);
  32. test.checkEqual(true, map.contains(5), "contains replaced value");
  33. test.checkEqual(10, map.search(5, -1), "search finds replaced value");
  34. }
  35. static void testClear(Test& test) {
  36. IntMap map;
  37. map.add(5, 4);
  38. map.add(4, 10);
  39. map.clear();
  40. test.checkEqual(false, map.contains(5), "does not contain cleared values");
  41. test.checkEqual(false, map.contains(4), "does not contain cleared values");
  42. }
  43. static void testOverflow(Test& test) {
  44. IntMap map;
  45. for(int i = 0; i < 1000000; i++) {
  46. map.add(i, i);
  47. }
  48. for(int i = 0; i < MAP_MIN_CAPACITY; i++) {
  49. test.checkEqual(true, map.contains(i), "still contains values after overflow");
  50. }
  51. test.checkEqual(true, true, "survives overflow");
  52. }
  53. struct A {
  54. int a;
  55. int b;
  56. A(int a, int b) : a(a), b(b) {
  57. }
  58. bool operator==(const A& other) const {
  59. return a == other.a && b == other.b;
  60. }
  61. };
  62. std::ostream& operator<<(std::ostream& os, const A& a) {
  63. return os << "A(" << a.a << ", " << a.b << ")";
  64. }
  65. static void testEmplace(Test& test) {
  66. HashMap<int, A, 5> map;
  67. bool r1 = map.tryEmplace(0, 3, 4);
  68. bool r2 = map.tryEmplace(3, 4, 5);
  69. bool r3 = map.tryEmplace(20, 5, 6);
  70. bool r4 = map.tryEmplace(3, 6, 7);
  71. bool r5 = map.tryEmplace(20, 7, 8);
  72. test.checkEqual(A(3, 4), map.search(0, A(0, 0)), "contains emplaced value 1");
  73. test.checkEqual(A(4, 5), map.search(3, A(0, 0)), "contains emplaced value 2");
  74. test.checkEqual(A(5, 6), map.search(20, A(0, 0)), "contains emplaced value 3");
  75. test.checkEqual(false, r1, "emplacing returns correct value 1");
  76. test.checkEqual(false, r2, "emplacing returns correct value 2");
  77. test.checkEqual(false, r3, "emplacing returns correct value 3");
  78. test.checkEqual(true, r4, "emplacing returns correct value 4");
  79. test.checkEqual(true, r5, "emplacing returns correct value 5");
  80. }
  81. void HashMapTests::test() {
  82. Test test("HashMap");
  83. testAdd(test);
  84. testMultipleAdd(test);
  85. testSearch(test);
  86. testAddReplace(test);
  87. testClear(test);
  88. testOverflow(test);
  89. testEmplace(test);
  90. test.finalize();
  91. }