Browse Source

matrix stack and tests

Kajetan Johannes Hammerle 3 years ago
parent
commit
352b738398
6 changed files with 163 additions and 1 deletions
  1. 2 0
      Main.cpp
  2. 49 0
      math/MatrixStack.h
  3. 1 1
      meson.build
  4. 99 0
      tests/MatrixStackTests.cpp
  5. 8 0
      tests/MatrixStackTests.h
  6. 4 0
      utils/StringBuffer.h

+ 2 - 0
Main.cpp

@@ -9,6 +9,7 @@
 #include "tests/VectorTests.h"
 #include "tests/MatrixTests.h"
 #include "tests/StackTests.h"
+#include "tests/MatrixStackTests.h"
 
 int main() {
     ArrayTests::test();
@@ -22,5 +23,6 @@ int main() {
     VectorTests::test();
     MatrixTests::test();
     StackTests::test();
+    MatrixStackTests::test();
     return 0;
 }

+ 49 - 0
math/MatrixStack.h

@@ -0,0 +1,49 @@
+#ifndef MATRIXSTACK_H
+#define MATRIXSTACK_H
+
+#include "utils/Stack.h"
+#include "math/Matrix.h"
+
+template<int N>
+class MatrixStack final {
+    Stack<Matrix, N> stack;
+
+public:
+
+    MatrixStack() {
+        stack.push(Matrix());
+    }
+
+    bool pop() {
+        stack.pop();
+        if(stack.isEmpty()) {
+            stack.push(Matrix());
+            return true;
+        }
+        return false;
+    }
+
+    bool push() {
+        return stack.push(stack.peek());
+    }
+
+    Matrix& peek() {
+        return stack.peek();
+    }
+
+    const Matrix& peek() const {
+        return stack.peek();
+    }
+
+    void clear() {
+        stack.clear();
+        stack.push(Matrix());
+    }
+
+    template<int L>
+    void toString(StringBuffer<L>& s) const {
+        s.append(stack);
+    }
+};
+
+#endif

+ 1 - 1
meson.build

@@ -1,6 +1,6 @@
 project('gaming core tests', 'cpp')
 
-sources = ['Main.cpp', 'tests/Test.cpp', 'tests/ArrayTests.cpp', 'tests/HashMapTests.cpp', 'tests/ListTests.cpp', 'tests/BitArrayTests.cpp', 'tests/StringBufferTests.cpp', 'tests/RandomTests.cpp', 'utils/Random.cpp', 'tests/RingBufferTests.cpp', 'tests/SplitStringTests.cpp', 'tests/VectorTests.cpp', 'math/Vector.cpp', 'math/Matrix.cpp', 'tests/MatrixTests.cpp', 'tests/StackTests.cpp']
+sources = ['Main.cpp', 'tests/Test.cpp', 'tests/ArrayTests.cpp', 'tests/HashMapTests.cpp', 'tests/ListTests.cpp', 'tests/BitArrayTests.cpp', 'tests/StringBufferTests.cpp', 'tests/RandomTests.cpp', 'utils/Random.cpp', 'tests/RingBufferTests.cpp', 'tests/SplitStringTests.cpp', 'tests/VectorTests.cpp', 'math/Vector.cpp', 'math/Matrix.cpp', 'tests/MatrixTests.cpp', 'tests/StackTests.cpp', 'tests/MatrixStackTests.cpp']
 
 executable('tests', 
     sources: sources,

+ 99 - 0
tests/MatrixStackTests.cpp

@@ -0,0 +1,99 @@
+#include "tests/MatrixStackTests.h"
+#include "tests/Test.h"
+#include "math/MatrixStack.h"
+#include "utils/StringBuffer.h"
+
+typedef MatrixStack<5> Matrices;
+typedef StringBuffer<500> String;
+
+static void testInit(Test& test) {
+    Matrix m;
+    Matrices stack;
+    for(int i = 0; i < 16; i++) {
+        test.checkEqual(m.getValues()[i], stack.peek().getValues()[i], "contains identity matrix");
+    }
+}
+
+static void testPop(Test& test) {
+    Matrices stack;
+    stack.peek().scale(2.0f);
+    stack.push();
+    test.checkEqual(false, stack.pop(), "pop returns false when there is something to pop");
+    test.checkEqual(true, stack.pop(), "pop returns true when the lowest matrix was popped");
+    Matrix m;
+    for(int i = 0; i < 16; i++) {
+        test.checkEqual(m.getValues()[i], stack.peek().getValues()[i], "contains identity matrix");
+    }
+}
+
+static void testPush(Test& test) {
+    Matrices stack;
+    for(int i = 0; i < 4; i++) {
+        test.checkEqual(false, stack.push(), "push returns false without overflow");
+    }
+    for(int i = 0; i < 1000000; i++) {
+        test.checkEqual(true, stack.push(), "push returns true with overflow");
+    }
+    test.checkEqual(true, true, "survives overflow");
+}
+
+static void testToString1(Test& test) {
+    Matrices stack;
+    stack.peek().scale(2.0f);
+    stack.push();
+    stack.peek().scale(3.0f);
+    stack.push();
+    stack.peek().scale(4.0f);
+    test.checkEqual(String("["
+            "[[2.00, 0.00, 0.00, 0.00], "
+            "[0.00, 2.00, 0.00, 0.00], "
+            "[0.00, 0.00, 2.00, 0.00], "
+            "[0.00, 0.00, 0.00, 1.00]], "
+            "[[6.00, 0.00, 0.00, 0.00], "
+            "[0.00, 6.00, 0.00, 0.00], "
+            "[0.00, 0.00, 6.00, 0.00], "
+            "[0.00, 0.00, 0.00, 1.00]], "
+            "[[24.00, 0.00, 0.00, 0.00], "
+            "[0.00, 24.00, 0.00, 0.00], "
+            "[0.00, 0.00, 24.00, 0.00], "
+            "[0.00, 0.00, 0.00, 1.00]]"
+            "]"), String(stack), "to string 1");
+}
+
+static void testToString2(Test& test) {
+    Matrices stack;
+    stack.peek().scale(2.0f);
+    stack.push();
+    stack.peek().scale(3.0f);
+    test.checkEqual(String("["
+            "[[2.00, 0.00, 0.00, 0.00], "
+            "[0.00, 2.00, 0.00, 0.00], "
+            "[0.00, 0.00, 2.00, 0.00], "
+            "[0.00, 0.00, 0.00, 1.00]], "
+            "[[6.00, 0.00, 0.00, 0.00], "
+            "[0.00, 6.00, 0.00, 0.00], "
+            "[0.00, 0.00, 6.00, 0.00], "
+            "[0.00, 0.00, 0.00, 1.00]]"
+            "]"), String(stack), "to string 2");
+}
+
+static void testToString3(Test& test) {
+    Matrices stack;
+    test.checkEqual(String("["
+            "[[1.00, 0.00, 0.00, 0.00], "
+            "[0.00, 1.00, 0.00, 0.00], "
+            "[0.00, 0.00, 1.00, 0.00], "
+            "[0.00, 0.00, 0.00, 1.00]]"
+            "]"), String(stack), "to string 3");
+}
+
+void MatrixStackTests::test() {
+    Test test("MatrixStack");
+    testInit(test);
+    testPop(test);
+    testPush(test);
+    testToString1(test);
+    testToString2(test);
+    testToString3(test);
+    test.finalize();
+}

+ 8 - 0
tests/MatrixStackTests.h

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

+ 4 - 0
utils/StringBuffer.h

@@ -123,6 +123,10 @@ public:
     void print() {
         std::cout << data;
     }
+    
+    void printLine() {
+        std::cout << data << '\n';
+    }
 };
 
 template<int N>