Kajetan Johannes Hammerle 3 anni fa
parent
commit
dbe54c0f76

+ 2 - 6
tests/ArrayTests.cpp

@@ -10,17 +10,13 @@ static void testToString1(Test& test) {
     a[0] = 1;
     a[1] = 243;
     a[2] = -423;
-    String s;
-    s.append(a);
-    test.checkEqual(String("[1, 243, -423]"), s, "array to string 1");
+    test.checkEqual(String("[1, 243, -423]"), String(a), "array to string 1");
 }
 
 static void testToString2(Test& test) {
     Array<int, 1> a;
     a[0] = 1;
-    String s;
-    s.append(a);
-    test.checkEqual(String("[1]"), s, "array to string 2");
+    test.checkEqual(String("[1]"), String(a), "array to string 2");
 }
 
 void ArrayTests::test() {

+ 14 - 18
tests/BitArrayTests.cpp

@@ -11,10 +11,10 @@ static void testSetRead(Test& test) {
     bits[1] = 2;
     bits[2] = 3;
     bits[3] = 4;
-    test.checkEqual(1, static_cast<int> (bits[0]), "set and read correct value");
-    test.checkEqual(2, static_cast<int> (bits[1]), "set and read correct value");
-    test.checkEqual(3, static_cast<int> (bits[2]), "set and read correct value");
-    test.checkEqual(4, static_cast<int> (bits[3]), "set and read correct value");
+    test.checkEqual(1, static_cast<int> (bits[0]), "set and read correct value 1");
+    test.checkEqual(2, static_cast<int> (bits[1]), "set and read correct value 2");
+    test.checkEqual(3, static_cast<int> (bits[2]), "set and read correct value 3");
+    test.checkEqual(4, static_cast<int> (bits[3]), "set and read correct value 4");
 }
 
 static void testBigSetRead(Test& test) {
@@ -51,20 +51,20 @@ static void testReadOnly(Test& test) {
     bits[2] = 3;
     bits[3] = 4;
     const BitArray<4, 3> bits2 = bits;
-    test.checkEqual(1, bits2[0], "can read from const");
-    test.checkEqual(2, bits2[1], "can read from const");
-    test.checkEqual(3, bits2[2], "can read from const");
-    test.checkEqual(4, bits2[3], "can read from const");
+    test.checkEqual(1, bits2[0], "can read from const 1");
+    test.checkEqual(2, bits2[1], "can read from const 2");
+    test.checkEqual(3, bits2[2], "can read from const 3");
+    test.checkEqual(4, bits2[3], "can read from const 4");
 }
 
 static void testChainedSet(Test& test) {
     BitArray<4, 3> bits;
     bits[0] = bits[2] = bits[3] = 2;
     bits[3] = bits[1] = 7;
-    test.checkEqual(2, static_cast<int> (bits[0]), "chained set sets correct value");
-    test.checkEqual(7, static_cast<int> (bits[1]), "chained set sets correct value");
-    test.checkEqual(2, static_cast<int> (bits[2]), "chained set sets correct value");
-    test.checkEqual(7, static_cast<int> (bits[3]), "chained set sets correct value");
+    test.checkEqual(2, static_cast<int> (bits[0]), "chained set sets correct value 1");
+    test.checkEqual(7, static_cast<int> (bits[1]), "chained set sets correct value 2");
+    test.checkEqual(2, static_cast<int> (bits[2]), "chained set sets correct value 3");
+    test.checkEqual(7, static_cast<int> (bits[3]), "chained set sets correct value 4");
 }
 
 static void testToString1(Test& test) {
@@ -73,17 +73,13 @@ static void testToString1(Test& test) {
     bits[1] = 2;
     bits[2] = 3;
     bits[3] = 4;
-    String s;
-    s.append(bits);
-    test.checkEqual(String("[1, 2, 3, 4]"), s, "bit array to string 1");
+    test.checkEqual(String("[1, 2, 3, 4]"), String(bits), "bit array to string 1");
 }
 
 static void testToString2(Test& test) {
     BitArray<1, 3> a;
     a[0] = 1;
-    String s;
-    s.append(a);
-    test.checkEqual(String("[1]"), s, "bit array to string 1");
+    test.checkEqual(String("[1]"), String(a), "bit array to string 1");
 }
 
 void BitArrayTests::test() {

+ 9 - 23
tests/HashMapTests.cpp

@@ -16,9 +16,7 @@ static void testAdd(Test& test) {
 
 static void testMultipleAdd(Test& test) {
     IntMap map;
-    map.add(5, 4);
-    map.add(10, 3);
-    map.add(15, 2);
+    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");
@@ -34,20 +32,16 @@ static void testSearch(Test& test) {
 
 static void testAddReplace(Test& test) {
     IntMap map;
-    map.add(5, 4);
-    map.add(5, 10);
+    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);
-    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");
+    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) {
@@ -99,27 +93,19 @@ static void testEmplace(Test& test) {
 
 static void testToString1(Test& test) {
     IntMap map;
-    map.add(1, 3);
-    map.add(2, 4);
-    map.add(3, 5);
-    String s;
-    s.append(map);
-    test.checkEqual(String("[1 = 3, 2 = 4, 3 = 5]"), s, "to string 1");
+    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);
-    String s;
-    s.append(map);
-    test.checkEqual(String("[1 = 3]"), s, "to string 2");
+    test.checkEqual(String("[1 = 3]"), String(map), "to string 2");
 }
 
 static void testToString3(Test& test) {
     IntMap map;
-    String s;
-    s.append(map);
-    test.checkEqual(String("[]"), s, "to string 3");
+    test.checkEqual(String("[]"), String(map), "to string 3");
 }
 
 void HashMapTests::test() {

+ 10 - 31
tests/ListTests.cpp

@@ -16,9 +16,7 @@ static void testAdd(Test& test) {
 
 static void testMultipleAdd(Test& test) {
     IntList list;
-    list.add(4);
-    list.add(3);
-    list.add(2);
+    list.add(4).add(3).add(2);
     test.checkEqual(4, list[0], "list contains added value");
     test.checkEqual(3, list[1], "list contains added value");
     test.checkEqual(2, list[2], "list contains added value");
@@ -34,10 +32,7 @@ static void testAddReplace(Test& test) {
 
 static void testClear(Test& test) {
     IntList list;
-    list.add(5);
-    list.add(4);
-    list.clear();
-
+    list.add(5).add(4).clear();
     test.checkEqual(0, list.getLength(), "list length is 0 after clear");
 }
 
@@ -54,9 +49,7 @@ static void testOverflow(Test& test) {
 
 static void testCopy(Test& test) {
     IntList list;
-    list.add(1);
-    list.add(2);
-    list.add(3);
+    list.add(1).add(2).add(3);
 
     IntList copy(list);
     test.checkEqual(list.getLength(), copy.getLength(), "list copy has same length");
@@ -67,9 +60,7 @@ static void testCopy(Test& test) {
 
 static void testCopyAssignment(Test& test) {
     IntList list;
-    list.add(1);
-    list.add(2);
-    list.add(3);
+    list.add(1).add(2).add(3);
 
     IntList copy;
     copy = list;
@@ -81,9 +72,7 @@ static void testCopyAssignment(Test& test) {
 
 static void testMove(Test& test) {
     IntList list;
-    list.add(1);
-    list.add(2);
-    list.add(3);
+    list.add(1).add(2).add(3);
 
     IntList move(std::move(list));
     test.checkEqual(0, list.getLength(), "moved list has length 0");
@@ -95,9 +84,7 @@ static void testMove(Test& test) {
 
 static void testMoveAssignment(Test& test) {
     IntList list;
-    list.add(1);
-    list.add(2);
-    list.add(3);
+    list.add(1).add(2).add(3);
 
     IntList move(std::move(list));
     test.checkEqual(0, list.getLength(), "assignment moved list has length 0");
@@ -109,27 +96,19 @@ static void testMoveAssignment(Test& test) {
 
 static void testToString1(Test& test) {
     IntList list;
-    list.add(1);
-    list.add(243);
-    list.add(-423);
-    String s;
-    s.append(list);
-    test.checkEqual(String("[1, 243, -423]"), s, "list to string 1");
+    list.add(1).add(243).add(-423);
+    test.checkEqual(String("[1, 243, -423]"), String(list), "list to string 1");
 }
 
 static void testToString2(Test& test) {
     IntList list;
     list.add(1);
-    String s;
-    s.append(list);
-    test.checkEqual(String("[1]"), s, "list to string 2");
+    test.checkEqual(String("[1]"), String(list), "list to string 2");
 }
 
 static void testToString3(Test& test) {
     IntList list;
-    String s;
-    s.append(list);
-    test.checkEqual(String("[]"), s, "list to string 3");
+    test.checkEqual(String("[]"), String(list), "list to string 3");
 }
 
 void ListTests::test() {

+ 1 - 5
tests/StringBufferTests.cpp

@@ -107,11 +107,7 @@ static void testHashCode(Test& test) {
 
 static void testAsHashMapKey(Test& test) {
     HashMap<String, int, 5> map;
-    
-    map.add(String("wusi"), 3);
-    map.add(String("hiThere"), 7);
-    map.add(String("baum123"), 5);
-    
+    map.add(String("wusi"), 3).add(String("hiThere"), 7).add(String("baum123"), 5);
     test.checkEqual(3, map.search(String("wusi"), 0), "strings works as hash key 1");
     test.checkEqual(7, map.search(String("hiThere"), 0), "strings works as hash key 2");
     test.checkEqual(5, map.search(String("baum123"), 0), "strings works as hash key 3");

+ 6 - 3
utils/HashMap.h

@@ -121,7 +121,7 @@ public:
         return true;
     }
 
-    void add(const K& key, const V& value) {
+    HashMap& add(const K& key, const V& value) {
         Search s = searchIndex(key);
         if(s.result == KEY_FOUND) {
             getValue(s.index) = value;
@@ -130,9 +130,10 @@ public:
             new (reinterpret_cast<K*> (keys) + s.index) K(key);
             new (reinterpret_cast<V*> (values) + s.index) V(value);
         }
+        return *this;
     }
 
-    void add(const K& key, const V&& value) {
+    HashMap& add(const K& key, const V&& value) {
         Search s = searchIndex(key);
         if(s.result == KEY_FOUND) {
             getValue(s.index) = std::move(value);
@@ -141,6 +142,7 @@ public:
             new (reinterpret_cast<K*> (keys) + s.index) K(key);
             new (reinterpret_cast<V*> (values) + s.index) V(std::move(value));
         }
+        return *this;
     }
 
     const V& search(const K& key, const V& notFound) const {
@@ -157,7 +159,7 @@ public:
         return searchIndex(key).result == KEY_FOUND;
     }
 
-    void clear() {
+    HashMap& clear() {
         K* k = reinterpret_cast<K*> (keys);
         V* v = reinterpret_cast<V*> (values);
         for(int i = 0; i < CAPACITY; i++) {
@@ -167,6 +169,7 @@ public:
             }
         }
         used.fill(false);
+        return *this;
     }
 
     template<int L>

+ 12 - 8
utils/List.h

@@ -25,11 +25,12 @@ class List final {
 public:
     List() = default;
 
-    void clear() {
+    List& clear() {
         for(int i = 0; i < index; i++) {
             (*this)[i].~T();
         }
         index = 0;
+        return *this;
     }
 
     ~List() {
@@ -78,29 +79,32 @@ public:
         return reinterpret_cast<const T*> (data) + index;
     }
 
-    void add(const T& t) {
+    List& add(const T& t) {
         if(index >= N) {
-            return;
+            return *this;
         }
         new (end()) T(t);
         index++;
+        return *this;
     }
 
-    void add(T&& t) {
+    List& add(T&& t) {
         if(index >= N) {
-            return;
+            return *this;
         }
         new (end()) T(std::move(t));
         index++;
+        return *this;
     }
 
     template<typename... Args>
-    void add(Args&&... args) {
+    List& add(Args&&... args) {
         if(index >= N) {
-            return;
+            return *this;
         }
         new (end()) T(args...);
         index++;
+        return *this;
     }
 
     T& operator[](int index) {
@@ -114,7 +118,7 @@ public:
     int getLength() const {
         return index;
     }
-    
+
     template<int L>
     void toString(StringBuffer<L>& s) const {
         s.append("[");

+ 4 - 3
utils/StringBuffer.h

@@ -25,9 +25,10 @@ public:
     StringBuffer() : length(0), hash(0) {
         data[0] = '\0';
     }
-
-    StringBuffer(const char* str) : StringBuffer() {
-        append(str);
+    
+    template<typename T>
+    StringBuffer(const T& t) : StringBuffer() {
+        append(t);
     }
 
     bool operator==(const char* str) const {