HashMapTests.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. void HashMapTests::test() {
  54. Test test("HashMap");
  55. testAdd(test);
  56. testMultipleAdd(test);
  57. testSearch(test);
  58. testAddReplace(test);
  59. testClear(test);
  60. testOverflow(test);
  61. test.finalize();
  62. }