#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), "contains added value"); test.checkEqual(4, map.search(5, -1), "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), "contains added value 1"); test.checkEqual(true, map.contains(10), "contains added value 2"); test.checkEqual(true, map.contains(15), "contains added value 3"); test.checkEqual(4, map.search(5, -1), "search finds added value 1"); test.checkEqual(3, map.search(10, -1), "search finds added value 2"); test.checkEqual(2, map.search(15, -1), "search finds added value 3"); } static void testSearch(Test& test) { IntMap map; test.checkEqual(-1, map.search(6, -1), "search does not find missing key"); } static void testAddReplace(Test& test) { IntMap map; map.add(5, 4); map.add(5, 10); test.checkEqual(true, map.contains(5), "contains replaced value"); test.checkEqual(10, map.search(5, -1), "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), "does not contain cleared values"); test.checkEqual(false, map.contains(4), "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), "still contains values after overflow"); } test.checkEqual(true, true, "survives overflow"); } void HashMapTests::test() { Test test("HashMap"); testAdd(test); testMultipleAdd(test); testSearch(test); testAddReplace(test); testClear(test); testOverflow(test); test.finalize(); }