#include "tests/HashMapTests.h" #include "tests/Test.h" #include "utils/HashMap.h" constexpr int MAP_MIN_CAPACITY = 5; typedef HashMap 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(); }