Browse Source

to string for arrays and lists

Kajetan Johannes Hammerle 3 years ago
parent
commit
e2133fcd39
8 changed files with 116 additions and 4 deletions
  1. 4 2
      Main.cpp
  2. 1 1
      meson.build
  3. 31 0
      tests/ArrayTests.cpp
  4. 8 0
      tests/ArrayTests.h
  5. 30 0
      tests/ListTests.cpp
  6. 16 1
      utils/Array.h
  7. 15 0
      utils/List.h
  8. 11 0
      utils/StringBuffer.h

+ 4 - 2
Main.cpp

@@ -1,12 +1,14 @@
+#include "tests/ArrayTests.h"
 #include "tests/HashMapTests.h"
 #include "tests/ListTests.h"
 #include "tests/BitArrayTests.h"
 #include "tests/StringBufferTests.h"
 
 int main() {
+    ArrayTests::test();
     HashMapTests::test();
     ListTests::test();
-    BitArrayTests::test();   
+    BitArrayTests::test();
     StringBufferTests::test();
     return 0;
-}
+}

+ 1 - 1
meson.build

@@ -1,6 +1,6 @@
 project('gaming core tests', 'cpp')
 
-sources = ['Main.cpp', 'tests/Test.cpp', 'tests/HashMapTests.cpp', 'tests/ListTests.cpp', 'tests/BitArrayTests.cpp', 'tests/StringBufferTests.cpp']
+sources = ['Main.cpp', 'tests/Test.cpp', 'tests/ArrayTests.cpp', 'tests/HashMapTests.cpp', 'tests/ListTests.cpp', 'tests/BitArrayTests.cpp', 'tests/StringBufferTests.cpp']
 
 executable('tests', 
     sources: sources,

+ 31 - 0
tests/ArrayTests.cpp

@@ -0,0 +1,31 @@
+#include "tests/ArrayTests.h"
+#include "tests/Test.h"
+#include "utils/Array.h"
+#include "utils/StringBuffer.h"
+
+typedef StringBuffer<50> String;
+
+static void testToString1(Test& test) {
+    Array<int, 3> a;
+    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");
+}
+
+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");
+}
+
+void ArrayTests::test() {
+    Test test("Array");
+    testToString1(test);
+    testToString2(test);
+    test.finalize();
+}

+ 8 - 0
tests/ArrayTests.h

@@ -0,0 +1,8 @@
+#ifndef ARRAYTESTS_H
+#define ARRAYTESTS_H
+
+namespace ArrayTests {
+    void test();
+}
+
+#endif

+ 30 - 0
tests/ListTests.cpp

@@ -1,8 +1,10 @@
 #include "tests/ListTests.h"
 #include "tests/Test.h"
 #include "utils/List.h"
+#include "utils/StringBuffer.h"
 
 typedef List<int, 20> IntList;
+typedef StringBuffer<50> String;
 
 static void testAdd(Test& test) {
     IntList list;
@@ -105,6 +107,31 @@ static void testMoveAssignment(Test& test) {
     test.checkEqual(3, move[2], "assignment moved list passes values");
 }
 
+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");
+}
+
+static void testToString2(Test& test) {
+    IntList list;
+    list.add(1);
+    String s;
+    s.append(list);
+    test.checkEqual(String("[1]"), s, "list to string 2");
+}
+
+static void testToString3(Test& test) {
+    IntList list;
+    String s;
+    s.append(list);
+    test.checkEqual(String("[]"), s, "list to string 3");
+}
+
 void ListTests::test() {
     Test test("List");
     testAdd(test);
@@ -116,5 +143,8 @@ void ListTests::test() {
     testCopyAssignment(test);
     testMove(test);
     testMoveAssignment(test);
+    testToString1(test);
+    testToString2(test);
+    testToString3(test);
     test.finalize();
 }

+ 16 - 1
utils/Array.h

@@ -1,6 +1,8 @@
 #ifndef ARRAY_H
 #define ARRAY_H
 
+#include "utils/StringBuffer.h"
+
 template<typename T, int N>
 class Array final {
     T data[N];
@@ -11,7 +13,7 @@ public:
     Array(const T& t) {
         fill(t);
     }
-    
+
     void fill(const T& t) {
         for(int i = 0; i < N; i++) {
             data[i] = t;
@@ -45,6 +47,19 @@ public:
     constexpr int getLength() const {
         return N;
     }
+
+    template<int L>
+    void toString(StringBuffer<L>& s) const {
+        s.append("[");
+        for(int i = 0; i < N - 1; i++) {
+            s.append(data[i]);
+            s.append(", ");
+        }
+        if(N > 0) {
+            s.append(data[N - 1]);
+        }
+        s.append("]");
+    }
 };
 
 #endif

+ 15 - 0
utils/List.h

@@ -3,6 +3,8 @@
 
 #include <utility>
 
+#include "utils/StringBuffer.h"
+
 template<typename T, int N>
 class List final {
     char data[sizeof (T) * N];
@@ -112,6 +114,19 @@ public:
     int getLength() const {
         return index;
     }
+    
+    template<int L>
+    void toString(StringBuffer<L>& s) const {
+        s.append("[");
+        for(int i = 0; i < index - 1; i++) {
+            s.append((*this)[i]);
+            s.append(", ");
+        }
+        if(index > 0) {
+            s.append((*this)[index - 1]);
+        }
+        s.append("]");
+    }
 };
 
 #endif

+ 11 - 0
utils/StringBuffer.h

@@ -1,6 +1,7 @@
 #ifndef STRINGBUFFER_H
 #define STRINGBUFFER_H
 
+#include <iostream>
 #include <cstring>
 
 template<int N>
@@ -101,6 +102,12 @@ public:
         return b ? append("true") : append("false");
     }
 
+    template<typename T>
+    StringBuffer& append(const T& t) {
+        t.toString(*this);
+        return *this;
+    }
+
     StringBuffer& clear() {
         length = 0;
         hash = 0;
@@ -111,6 +118,10 @@ public:
     int hashCode() const {
         return hash;
     }
+
+    void print() {
+        std::cout << data;
+    }
 };
 
 template<int N>