1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- #include "tests/HashMapTests.h"
- #include "tests/Test.h"
- #include "utils/HashMap.h"
- constexpr int MAP_MIN_CAPACITY = 5;
- typedef HashMap<int, int, MAP_MIN_CAPACITY> IntMap;
- static void testAdd(Test& test) {
- IntMap map;
- map.add(5, 4);
- test.checkEqual(true, map.contains(5), "map contains added value");
- test.checkEqual(true, map.search(5, -1) == 4, "map search finds added value");
- }
- static void testMultipleAdd(Test& test) {
- IntMap map;
- map.add(5, 4);
- map.add(10, 3);
- map.add(15, 2);
- test.checkEqual(true, map.contains(5), "map contains added value 1");
- test.checkEqual(true, map.contains(10), "map contains added value 2");
- test.checkEqual(true, map.contains(15), "map contains added value 3");
- test.checkEqual(true, map.search(5, -1) == 4, "map search finds added value 1");
- test.checkEqual(true, map.search(10, -1) == 3, "map search finds added value 2");
- test.checkEqual(true, map.search(15, -1) == 2, "map search finds added value 3");
- }
- static void testAddReplace(Test& test) {
- IntMap map;
- map.add(5, 4);
- map.add(5, 10);
- test.checkEqual(true, map.contains(5), "map contains replaced value");
- test.checkEqual(true, map.search(5, -1) == 10, "map search finds replaced value");
- }
- static void testClear(Test& test) {
- IntMap map;
- map.add(5, 4);
- map.add(4, 10);
- map.clear();
- test.checkEqual(false, map.contains(5), "map does not contain cleared values");
- test.checkEqual(false, map.contains(4), "map does not contain cleared values");
- }
- static void testOverflow(Test& test) {
- IntMap map;
- for(int i = 0; i < 1000000; i++) {
- map.add(i, i);
- }
- for(int i = 0; i < MAP_MIN_CAPACITY; i++) {
- test.checkEqual(true, map.contains(i), "map still contains values after overflow");
- }
- test.checkEqual(true, true, "map survives overflow");
- }
- void HashMapTests::test() {
- Test test("HashMap");
- testAdd(test);
- testMultipleAdd(test);
- testAddReplace(test);
- testClear(test);
- testOverflow(test);
- test.finalize();
- }
|