HashMapTests.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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), "map contains added value");
  10. test.checkEqual(true, map.search(5, -1) == 4, "map 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), "map contains added value 1");
  18. test.checkEqual(true, map.contains(10), "map contains added value 2");
  19. test.checkEqual(true, map.contains(15), "map contains added value 3");
  20. test.checkEqual(true, map.search(5, -1) == 4, "map search finds added value 1");
  21. test.checkEqual(true, map.search(10, -1) == 3, "map search finds added value 2");
  22. test.checkEqual(true, map.search(15, -1) == 2, "map search finds added value 3");
  23. }
  24. static void testAddReplace(Test& test) {
  25. IntMap map;
  26. map.add(5, 4);
  27. map.add(5, 10);
  28. test.checkEqual(true, map.contains(5), "map contains replaced value");
  29. test.checkEqual(true, map.search(5, -1) == 10, "map search finds replaced value");
  30. }
  31. static void testClear(Test& test) {
  32. IntMap map;
  33. map.add(5, 4);
  34. map.add(4, 10);
  35. map.clear();
  36. test.checkEqual(false, map.contains(5), "map does not contain cleared values");
  37. test.checkEqual(false, map.contains(4), "map does not contain cleared values");
  38. }
  39. static void testOverflow(Test& test) {
  40. IntMap map;
  41. for(int i = 0; i < 1000000; i++) {
  42. map.add(i, i);
  43. }
  44. for(int i = 0; i < MAP_MIN_CAPACITY; i++) {
  45. test.checkEqual(true, map.contains(i), "map still contains values after overflow");
  46. }
  47. test.checkEqual(true, true, "map survives overflow");
  48. }
  49. void HashMapTests::test() {
  50. Test test("HashMap");
  51. testAdd(test);
  52. testMultipleAdd(test);
  53. testAddReplace(test);
  54. testClear(test);
  55. testOverflow(test);
  56. test.finalize();
  57. }