123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- #include "tests/HashMapTests.h"
- #include "tests/Test.h"
- #include "utils/HashMap.h"
- #include "utils/StringBuffer.h"
- constexpr int MAP_MIN_CAPACITY = 5;
- typedef HashMap<int, int, MAP_MIN_CAPACITY> IntMap;
- typedef StringBuffer<50> String;
- 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).add(10, 3).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).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).add(4, 10).clear();
- test.checkEqual(false, map.contains(5), "does not contain cleared values 1");
- test.checkEqual(false, map.contains(4), "does not contain cleared values 2");
- }
- 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");
- }
- struct A {
- int a;
- int b;
- A(int a, int b) : a(a), b(b) {
- }
- bool operator==(const A& other) const {
- return a == other.a && b == other.b;
- }
- };
- std::ostream& operator<<(std::ostream& os, const A& a) {
- return os << "A(" << a.a << ", " << a.b << ")";
- }
- static void testEmplace(Test& test) {
- HashMap<int, A, 5> map;
- bool r1 = map.tryEmplace(0, 3, 4);
- bool r2 = map.tryEmplace(3, 4, 5);
- bool r3 = map.tryEmplace(20, 5, 6);
- bool r4 = map.tryEmplace(3, 6, 7);
- bool r5 = map.tryEmplace(20, 7, 8);
- test.checkEqual(A(3, 4), map.search(0, A(0, 0)), "contains emplaced value 1");
- test.checkEqual(A(4, 5), map.search(3, A(0, 0)), "contains emplaced value 2");
- test.checkEqual(A(5, 6), map.search(20, A(0, 0)), "contains emplaced value 3");
- test.checkEqual(false, r1, "emplacing returns correct value 1");
- test.checkEqual(false, r2, "emplacing returns correct value 2");
- test.checkEqual(false, r3, "emplacing returns correct value 3");
- test.checkEqual(true, r4, "emplacing returns correct value 4");
- test.checkEqual(true, r5, "emplacing returns correct value 5");
- }
- static void testToString1(Test& test) {
- IntMap map;
- map.add(1, 3).add(2, 4).add(3, 5);
- test.checkEqual(String("[1 = 3, 2 = 4, 3 = 5]"), String(map), "to string 1");
- }
- static void testToString2(Test& test) {
- IntMap map;
- map.add(1, 3);
- test.checkEqual(String("[1 = 3]"), String(map), "to string 2");
- }
- static void testToString3(Test& test) {
- IntMap map;
- test.checkEqual(String("[]"), String(map), "to string 3");
- }
- static void testCopy(Test& test) {
- IntMap map;
- map.add(1, 3).add(2, 4).add(3, 5);
- IntMap copy(map);
- test.checkEqual(map.search(1, 0), copy.search(1, -1), "copy has same values 1");
- test.checkEqual(map.search(2, 0), copy.search(2, -1), "copy has same values 2");
- test.checkEqual(map.search(3, 0), copy.search(3, -1), "copy has same values 3");
- }
- static void testCopyAssignment(Test& test) {
- IntMap map;
- map.add(1, 3).add(2, 4).add(3, 5);
- IntMap copy;
- copy = map;
- test.checkEqual(map.search(1, 0), copy.search(1, -1), "copy assignment has same values 1");
- test.checkEqual(map.search(2, 0), copy.search(2, -1), "copy assignment has same values 2");
- test.checkEqual(map.search(3, 0), copy.search(3, -1), "copy assignment has same values 3");
- }
- static void testMove(Test& test) {
- IntMap map;
- map.add(1, 3).add(2, 4).add(3, 5);
- IntMap move(std::move(map));
- test.checkEqual(3, move.search(1, -1), "move moves values 1");
- test.checkEqual(4, move.search(2, -1), "move moves values 2");
- test.checkEqual(5, move.search(3, -1), "move moves values 3");
- test.checkEqual(-1, map.search(1, -1), "move removes values 1");
- test.checkEqual(-1, map.search(2, -1), "move removes values 2");
- test.checkEqual(-1, map.search(3, -1), "move removes values 3");
- }
- static void testMoveAssignment(Test& test) {
- IntMap map;
- map.add(1, 3).add(2, 4).add(3, 5);
- IntMap move;
- move = std::move(map);
- test.checkEqual(3, move.search(1, -1), "move assignment moves values 1");
- test.checkEqual(4, move.search(2, -1), "move assignment moves values 2");
- test.checkEqual(5, move.search(3, -1), "move assignment moves values 3");
- test.checkEqual(-1, map.search(1, -1), "move assignment removes values 1");
- test.checkEqual(-1, map.search(2, -1), "move assignment removes values 2");
- test.checkEqual(-1, map.search(3, -1), "move assignment removes values 3");
- }
- void HashMapTests::test() {
- Test test("HashMap");
- testAdd(test);
- testMultipleAdd(test);
- testSearch(test);
- testAddReplace(test);
- testClear(test);
- testOverflow(test);
- testEmplace(test);
- testToString1(test);
- testToString2(test);
- testToString3(test);
- testCopy(test);
- testCopyAssignment(test);
- testMove(test);
- testMoveAssignment(test);
- test.finalize();
- }
|