Просмотр исходного кода

hash map tests for copy / move, fixed move assignment test of list

Kajetan Johannes Hammerle 3 лет назад
Родитель
Сommit
5898761982
3 измененных файлов с 60 добавлено и 7 удалено
  1. 50 2
      tests/HashMapTests.cpp
  2. 2 1
      tests/ListTests.cpp
  3. 8 4
      utils/HashMap.h

+ 50 - 2
tests/HashMapTests.cpp

@@ -73,13 +73,13 @@ std::ostream& operator<<(std::ostream& os, const A& a) {
 
 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");
@@ -108,6 +108,50 @@ static void testToString3(Test& test) {
     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);
@@ -120,5 +164,9 @@ void HashMapTests::test() {
     testToString1(test);
     testToString2(test);
     testToString3(test);
+    testCopy(test);
+    testCopyAssignment(test);
+    testMove(test);
+    testMoveAssignment(test);
     test.finalize();
 }

+ 2 - 1
tests/ListTests.cpp

@@ -86,7 +86,8 @@ static void testMoveAssignment(Test& test) {
     IntList list;
     list.add(1).add(2).add(3);
 
-    IntList move(std::move(list));
+    IntList move;
+    move = std::move(list);
     test.checkEqual(0, list.getLength(), "assignment moved list has length 0");
     test.checkEqual(3, move.getLength(), "assignment moved list passes length");
     test.checkEqual(1, move[0], "assignment moved list passes values");

+ 8 - 4
utils/HashMap.h

@@ -94,8 +94,10 @@ public:
     }
 
     HashMap& operator=(const HashMap& other) {
-        clear();
-        copy(other);
+        if(&other != this) {
+            clear();
+            copy(other);
+        }
         return *this;
     }
 
@@ -104,8 +106,10 @@ public:
     }
 
     HashMap& operator=(HashMap&& other) {
-        clear();
-        move(other);
+        if(&other != this) {
+            clear();
+            move(other);
+        }
         return *this;
     }