Browse Source

heap array and tests

Kajetan Johannes Hammerle 3 years ago
parent
commit
23a0ac3535
6 changed files with 130 additions and 2 deletions
  1. 2 0
      Main.cpp
  2. 1 0
      meson.build
  3. 2 2
      tests/ArrayTests.cpp
  4. 27 0
      tests/HeapArrayTests.cpp
  5. 8 0
      tests/HeapArrayTests.h
  6. 90 0
      utils/HeapArray.h

+ 2 - 0
Main.cpp

@@ -8,6 +8,7 @@
 #include "tests/ColorTests.h"
 #include "tests/FrustumTests.h"
 #include "tests/HashMapTests.h"
+#include "tests/HeapArrayTests.h"
 #include "tests/ListTests.h"
 #include "tests/MatrixStackTests.h"
 #include "tests/MatrixTests.h"
@@ -32,6 +33,7 @@ int main(int argAmount, char** args) {
         return 0;
     }
     ArrayTests::test();
+    HeapArrayTests::test();
     HashMapTests::test();
     ListTests::test();
     BitArrayTests::test();

+ 1 - 0
meson.build

@@ -3,6 +3,7 @@ project('gaming core tests', 'cpp')
 sources = ['Main.cpp',
     'tests/Test.cpp',
     'tests/ArrayTests.cpp',
+    'tests/HeapArrayTests.cpp',
     'tests/HashMapTests.cpp',
     'tests/ListTests.cpp',
     'tests/BitArrayTests.cpp',

+ 2 - 2
tests/ArrayTests.cpp

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

+ 27 - 0
tests/HeapArrayTests.cpp

@@ -0,0 +1,27 @@
+#include "tests/HeapArrayTests.h"
+#include "tests/Test.h"
+#include "utils/HeapArray.h"
+#include "utils/StringBuffer.h"
+
+typedef StringBuffer<50> String;
+
+static void testToString1(Test& test) {
+    HeapArray<int> a(3);
+    a[0] = 1;
+    a[1] = 243;
+    a[2] = -423;
+    test.checkEqual(String("[1, 243, -423]"), String(a), "to string 1");
+}
+
+static void testToString2(Test& test) {
+    HeapArray<int> a(1);
+    a[0] = 1;
+    test.checkEqual(String("[1]"), String(a), "to string 2");
+}
+
+void HeapArrayTests::test() {
+    Test test("HeapArray");
+    testToString1(test);
+    testToString2(test);
+    test.finalize();
+}

+ 8 - 0
tests/HeapArrayTests.h

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

+ 90 - 0
utils/HeapArray.h

@@ -0,0 +1,90 @@
+#ifndef HEAPARRAY_H
+#define HEAPARRAY_H
+
+#include "utils/StringBuffer.h"
+
+template<typename T>
+class HeapArray final {
+    int length;
+    T* data;
+
+public:
+    HeapArray(int length) : length(length), data(new T[length]) {
+    }
+
+    HeapArray(int length, const T& t) : HeapArray(length) {
+        fill(t);
+    }
+
+    ~HeapArray() {
+        delete[] data;
+    }
+
+    HeapArray(const HeapArray& other) = delete;
+    HeapArray& operator=(const HeapArray& other) = delete;
+
+    HeapArray(HeapArray&& other) : length(other.length), data(other.data) {
+        other.length = 0;
+        other.data = nullptr;
+    }
+
+    HeapArray& operator=(HeapArray&& other) {
+        if(this != &other) {
+            delete[] data;
+            length = other.length;
+            data = other.data;
+            other.length = 0;
+            other.data = nullptr;
+        }
+        return *this;
+    }
+
+    void fill(const T& t) {
+        for(int i = 0; i < length; i++) {
+            data[i] = t;
+        }
+    }
+
+    T& operator[](int index) {
+        return data[index];
+    }
+
+    const T& operator[](int index) const {
+        return data[index];
+    }
+
+    T* begin() {
+        return data;
+    }
+
+    T* end() {
+        return data + length;
+    }
+
+    const T* begin() const {
+        return data;
+    }
+
+    const T* end() const {
+        return data + length;
+    }
+
+    int getLength() const {
+        return length;
+    }
+
+    template<int L>
+    void toString(StringBuffer<L>& s) const {
+        s.append("[");
+        for(int i = 0; i < length - 1; i++) {
+            s.append(data[i]);
+            s.append(", ");
+        }
+        if(length > 0) {
+            s.append(data[length - 1]);
+        }
+        s.append("]");
+    }
+};
+
+#endif