Browse Source

string buffer and tests

Kajetan Johannes Hammerle 3 years ago
parent
commit
95c38c9389
5 changed files with 217 additions and 1 deletions
  1. 2 0
      Main.cpp
  2. 1 1
      meson.build
  3. 106 0
      tests/StringBufferTests.cpp
  4. 8 0
      tests/StringBufferTests.h
  5. 100 0
      utils/StringBuffer.h

+ 2 - 0
Main.cpp

@@ -1,10 +1,12 @@
 #include "tests/HashMapTests.h"
 #include "tests/ListTests.h"
 #include "tests/BitArrayTests.h"
+#include "tests/StringBufferTests.h"
 
 int main() {
     HashMapTests::test();
     ListTests::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']
+sources = ['Main.cpp', 'tests/Test.cpp', 'tests/HashMapTests.cpp', 'tests/ListTests.cpp', 'tests/BitArrayTests.cpp', 'tests/StringBufferTests.cpp']
 
 executable('tests', 
     sources: sources,

+ 106 - 0
tests/StringBufferTests.cpp

@@ -0,0 +1,106 @@
+#include "tests/StringBufferTests.h"
+#include "tests/Test.h"
+#include "utils/StringBuffer.h"
+
+typedef StringBuffer<20> String;
+
+static void testEquality(Test& test) {
+    String s("test");
+    test.checkEqual(true, s == "test", "equality with c-string");
+    test.checkEqual(true, s == String("test"), "equality with another string");
+    test.checkEqual(true, "test" == s, "inverse equality with c-string");
+    test.checkEqual(true, String("test") == s, "inverse equality with another string");
+    test.checkEqual(true, s == s, "equality with itself");
+}
+
+static void testInequality(Test& test) {
+    String s("test");
+    test.checkEqual(false, s != "test", "inequality with c-string");
+    test.checkEqual(false, s != String("test"), "inequality with another string");
+    test.checkEqual(false, "test" != s, "inverse inequality with c-string");
+    test.checkEqual(false, String("test") != s, "inverse inequality with another string");
+    test.checkEqual(false, s != s, "inequality with itself");
+}
+
+static void testStringAppend(Test& test) {
+    String s("test");
+    s.append("22").append("333").append("4444");
+    test.checkEqual(String("test223334444"), s, "multiple appends");
+}
+
+static void testStringAppendOverflow(Test& test) {
+    StringBuffer<5> s("te");
+    s.append("2").append("333").append("4444");
+    test.checkEqual(StringBuffer<5>("te23"), s, "multiple appends with overflow");
+    test.checkEqual(4, s.getLength(), "length after multiple appends with overflow");
+}
+
+static void testCharacters(Test& test) {
+    String s("test");
+    test.checkEqual('t', s[0], "character read 1");
+    test.checkEqual('e', s[1], "character read 2");
+    test.checkEqual('s', s[2], "character read 3");
+    test.checkEqual('t', s[3], "character read 4");
+}
+
+static void testLength(Test& test) {
+    String s("test");
+    test.checkEqual(4, s.getLength(), "length 1");
+    s.append("aaa");
+    test.checkEqual(7, s.getLength(), "length 2");
+}
+
+static void testInt(Test& test) {
+    String s("test");
+    s.append(524).append(-37);
+    test.checkEqual(String("test524-37"), s, "int append");
+}
+
+static void testOverflowConstructor(Test& test) {
+    StringBuffer<5> s("12345678");
+    test.checkEqual(StringBuffer<5>("1234"), s, "overflow constructor");
+    test.checkEqual(4, s.getLength(), "length after overflow constructor");
+}
+
+static void testIntOverflow(Test& test) {
+    StringBuffer<5> s("te");
+    s.append(123456);
+    test.checkEqual(StringBuffer<5>("te12"), s, "int append with overflow");
+    test.checkEqual(4, s.getLength(), "length after int append with overflow");
+}
+
+static void testFloat(Test& test) {
+    String s("test");
+    s.append(5.2378f).append(-500.4321f);
+    test.checkEqual(String("test5.24-500.43"), s, "float append");
+}
+
+static void testBool(Test& test) {
+    String s("test");
+    s.append(true).append(false).append(true);
+    test.checkEqual(String("testtruefalsetrue"), s, "bool append");
+}
+
+static void testClear(Test& test) {
+    String s("test");
+    s.append(1234).clear().append("wusi").append("1234");
+    test.checkEqual(String("wusi1234"), s, "clear");
+}
+
+
+void StringBufferTests::test() {
+    Test test("StringBuffer");
+    testEquality(test);
+    testInequality(test);
+    testStringAppend(test);
+    testStringAppendOverflow(test);
+    testCharacters(test);
+    testLength(test);
+    testInt(test);
+    testOverflowConstructor(test);
+    testIntOverflow(test);
+    testFloat(test);
+    testBool(test);
+    testClear(test);
+    test.finalize();
+}

+ 8 - 0
tests/StringBufferTests.h

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

+ 100 - 0
utils/StringBuffer.h

@@ -0,0 +1,100 @@
+#ifndef STRINGBUFFER_H
+#define STRINGBUFFER_H
+
+#include <cstring>
+
+template<int N>
+class StringBuffer final {
+    int length;
+    char data[N];
+
+public:
+
+    StringBuffer() : length(0) {
+        data[0] = '\0';
+    }
+
+    StringBuffer(const char* str) : StringBuffer() {
+        append(str);
+    }
+
+    bool operator==(const char* str) const {
+        return strcmp(data, str) == 0;
+    }
+
+    bool operator==(const StringBuffer& other) const {
+        return length == other.length && other == data;
+    }
+
+    bool operator!=(const char* str) const {
+        return !((*this) == str);
+    }
+
+    bool operator!=(const StringBuffer& other) const {
+        return !((*this) == other);
+    }
+
+    operator const char*() const {
+        return data;
+    }
+
+    char operator[](int index) const {
+        return data[index];
+    }
+
+    int getLength() const {
+        return length;
+    }
+
+    //StringBuffer& append(char c);
+
+    StringBuffer& append(const char* str) {
+        for(int i = 0; length < N - 1 && str[i] != '\0'; length++, i++) {
+            data[length] = str[i];
+        }
+        data[length] = '\0';
+        return *this;
+    }
+
+    template<typename T>
+    StringBuffer& append(const char* format, const T& t) {
+        int left = N - length;
+        int written = snprintf(data + length, left, format, t);
+        if(written < left) {
+            length += written;
+        } else {
+            length = N - 1;
+        }
+        return *this;
+    }
+
+    StringBuffer& append(int i) {
+        return append("%d", i);
+    }
+
+    StringBuffer& append(float i) {
+        return append("%.2f", i);
+    }
+
+    StringBuffer& append(bool b) {
+        return b ? append("true") : append("false");
+    }
+
+    StringBuffer& clear() {
+        length = 0;
+        data[0] = '\0';
+        return *this;
+    }
+};
+
+template<int N>
+bool operator==(const char* str, const StringBuffer<N> buffer) {
+    return buffer == str;
+}
+
+template<int N>
+bool operator!=(const char* str, const StringBuffer<N> buffer) {
+    return buffer != str;
+}
+
+#endif