Browse Source

formatting

Kajetan Johannes Hammerle 3 years ago
parent
commit
e1f0ee9054
3 changed files with 111 additions and 56 deletions
  1. 2 1
      math/Vector.h
  2. 108 55
      tests/VectorTests.cpp
  3. 1 0
      utils/Array.h

+ 2 - 1
math/Vector.h

@@ -20,7 +20,8 @@ public:
     Vector(float a, Args&&... args) {
         const int size = sizeof...(args) + 1;
         float init[size] = {a, args...};
-        static_assert(N == size, "vector size and amount of float arguments do not match");
+        static_assert(N == size,
+                      "vector size and amount of float arguments do not match");
         for(int i = 0; i < N; i++) {
             values[i] = init[i];
         }

+ 108 - 55
tests/VectorTests.cpp

@@ -1,16 +1,19 @@
 #include <cmath>
 
-#include "tests/VectorTests.h"
+#include "math/Vector.h"
 #include "tests/Test.h"
+#include "tests/VectorTests.h"
 #include "utils/StringBuffer.h"
-#include "math/Vector.h"
 
 const float eps = 0.0001f;
 
 template<int N>
-static void compareVectors(Test& test, const Vector<N>& wanted, const Vector<N>& actual, const char* text) {
+static void compareVectors(Test& test, const Vector<N>& wanted,
+                           const Vector<N>& actual, const char* text) {
     for(int i = 0; i < N; i++) {
-        test.checkFloat(wanted[i], actual[i], eps, StringBuffer<50>(text).append(" (").append(i).append(")"));
+        test.checkFloat(
+            wanted[i], actual[i], eps,
+            StringBuffer<50>(text).append(" (").append(i).append(")"));
     }
 }
 
@@ -35,51 +38,82 @@ static void testInitAndRead(Test& test) {
 
 static void testSetAngles(Test& test) {
     float root = sqrtf(2.0f) * 0.5f;
-    compareVectors(test, Vector3(1.0f, 0.0f, 0.0f), Vector3().setAngles(0.0f, 0.0f), "angle 0 | 0");
-    compareVectors(test, Vector3(root, 0.0f, -root), Vector3().setAngles(45.0f, 0.0f), "angle 45 | 0");
-    compareVectors(test, Vector3(0.0f, 0.0f, -1.0f), Vector3().setAngles(90.0f, 0.0f), "angle 90 | 0");
-    compareVectors(test, Vector3(-root, 0.0f, -root), Vector3().setAngles(135.0f, 0.0f), "angle 135 | 0");
-    compareVectors(test, Vector3(-1.0f, 0.0f, 0.0f), Vector3().setAngles(180.0f, 0.0f), "angle 180 | 0");
-    compareVectors(test, Vector3(-root, 0.0f, root), Vector3().setAngles(225.0f, 0.0f), "angle 225 | 0");
-    compareVectors(test, Vector3(0.0f, 0.0f, 1.0f), Vector3().setAngles(270.0f, 0.0f), "angle 270 | 0");
-    compareVectors(test, Vector3(root, 0.0f, root), Vector3().setAngles(315.0f, 0.0f), "angle 315 | 0");
-
-    compareVectors(test, Vector3(0.0f, 1.0f, 0.0f), Vector3().setAngles(0.0f, 90.0f), "angle 0 | 90");
-    compareVectors(test, Vector3(0.0f, 1.0f, 0.0f), Vector3().setAngles(90.0f, 90.0f), "angle 90 | 90");
-    compareVectors(test, Vector3(0.0f, 1.0f, 0.0f), Vector3().setAngles(180.0f, 90.0f), "angle 180 | 90");
-    compareVectors(test, Vector3(0.0f, 1.0f, 0.0f), Vector3().setAngles(270.0f, 90.0f), "angle 270 | 90");
-
-    compareVectors(test, Vector3(0.0f, -1.0f, 0.0f), Vector3().setAngles(0.0f, -90.0f), "angle 0 | -90");
-    compareVectors(test, Vector3(0.0f, -1.0f, 0.0f), Vector3().setAngles(90.0f, -90.0f), "angle 90 | -90");
-    compareVectors(test, Vector3(0.0f, -1.0f, 0.0f), Vector3().setAngles(180.0f, -90.0f), "angle 180 | -90");
-    compareVectors(test, Vector3(0.0f, -1.0f, 0.0f), Vector3().setAngles(270.0f, -90.0f), "angle 270 | -90");
-
-    compareVectors(test, Vector3(root, root, 0.0f), Vector3().setAngles(0.0f, 45.0f), "angle 0 | 45");
-    compareVectors(test, Vector3(0.0f, root, -root), Vector3().setAngles(90.0f, 45.0f), "angle 90 | 45");
-    compareVectors(test, Vector3(-root, root, 0.0f), Vector3().setAngles(180.0f, 45.0f), "angle 180 | 45");
-    compareVectors(test, Vector3(0.0f, root, root), Vector3().setAngles(270.0f, 45.0f), "angle 270 | 45");
-
-    compareVectors(test, Vector3(root, -root, 0.0f), Vector3().setAngles(0.0f, -45.0f), "angle 0 | -45");
-    compareVectors(test, Vector3(0.0f, -root, -root), Vector3().setAngles(90.0f, -45.0f), "angle 90 | -45");
-    compareVectors(test, Vector3(-root, -root, 0.0f), Vector3().setAngles(180.0f, -45.0f), "angle 180 | -45");
-    compareVectors(test, Vector3(0.0f, -root, root), Vector3().setAngles(270.0f, -45.0f), "angle 270 | -45");
-
-    compareVectors(test, Vector3(0.5f, root, -0.5f), Vector3().setAngles(45.0f, 45.0f), "angle 45 | 45");
+    compareVectors(test, Vector3(1.0f, 0.0f, 0.0f),
+                   Vector3().setAngles(0.0f, 0.0f), "angle 0 | 0");
+    compareVectors(test, Vector3(root, 0.0f, -root),
+                   Vector3().setAngles(45.0f, 0.0f), "angle 45 | 0");
+    compareVectors(test, Vector3(0.0f, 0.0f, -1.0f),
+                   Vector3().setAngles(90.0f, 0.0f), "angle 90 | 0");
+    compareVectors(test, Vector3(-root, 0.0f, -root),
+                   Vector3().setAngles(135.0f, 0.0f), "angle 135 | 0");
+    compareVectors(test, Vector3(-1.0f, 0.0f, 0.0f),
+                   Vector3().setAngles(180.0f, 0.0f), "angle 180 | 0");
+    compareVectors(test, Vector3(-root, 0.0f, root),
+                   Vector3().setAngles(225.0f, 0.0f), "angle 225 | 0");
+    compareVectors(test, Vector3(0.0f, 0.0f, 1.0f),
+                   Vector3().setAngles(270.0f, 0.0f), "angle 270 | 0");
+    compareVectors(test, Vector3(root, 0.0f, root),
+                   Vector3().setAngles(315.0f, 0.0f), "angle 315 | 0");
+
+    compareVectors(test, Vector3(0.0f, 1.0f, 0.0f),
+                   Vector3().setAngles(0.0f, 90.0f), "angle 0 | 90");
+    compareVectors(test, Vector3(0.0f, 1.0f, 0.0f),
+                   Vector3().setAngles(90.0f, 90.0f), "angle 90 | 90");
+    compareVectors(test, Vector3(0.0f, 1.0f, 0.0f),
+                   Vector3().setAngles(180.0f, 90.0f), "angle 180 | 90");
+    compareVectors(test, Vector3(0.0f, 1.0f, 0.0f),
+                   Vector3().setAngles(270.0f, 90.0f), "angle 270 | 90");
+
+    compareVectors(test, Vector3(0.0f, -1.0f, 0.0f),
+                   Vector3().setAngles(0.0f, -90.0f), "angle 0 | -90");
+    compareVectors(test, Vector3(0.0f, -1.0f, 0.0f),
+                   Vector3().setAngles(90.0f, -90.0f), "angle 90 | -90");
+    compareVectors(test, Vector3(0.0f, -1.0f, 0.0f),
+                   Vector3().setAngles(180.0f, -90.0f), "angle 180 | -90");
+    compareVectors(test, Vector3(0.0f, -1.0f, 0.0f),
+                   Vector3().setAngles(270.0f, -90.0f), "angle 270 | -90");
+
+    compareVectors(test, Vector3(root, root, 0.0f),
+                   Vector3().setAngles(0.0f, 45.0f), "angle 0 | 45");
+    compareVectors(test, Vector3(0.0f, root, -root),
+                   Vector3().setAngles(90.0f, 45.0f), "angle 90 | 45");
+    compareVectors(test, Vector3(-root, root, 0.0f),
+                   Vector3().setAngles(180.0f, 45.0f), "angle 180 | 45");
+    compareVectors(test, Vector3(0.0f, root, root),
+                   Vector3().setAngles(270.0f, 45.0f), "angle 270 | 45");
+
+    compareVectors(test, Vector3(root, -root, 0.0f),
+                   Vector3().setAngles(0.0f, -45.0f), "angle 0 | -45");
+    compareVectors(test, Vector3(0.0f, -root, -root),
+                   Vector3().setAngles(90.0f, -45.0f), "angle 90 | -45");
+    compareVectors(test, Vector3(-root, -root, 0.0f),
+                   Vector3().setAngles(180.0f, -45.0f), "angle 180 | -45");
+    compareVectors(test, Vector3(0.0f, -root, root),
+                   Vector3().setAngles(270.0f, -45.0f), "angle 270 | -45");
+
+    compareVectors(test, Vector3(0.5f, root, -0.5f),
+                   Vector3().setAngles(45.0f, 45.0f), "angle 45 | 45");
 }
 
 static void testCross(Test& test) {
     compareVectors(test, Vector3(0.0f, 0.0f, 1.0f),
-            Vector3(1.0f, 0.0f, 0.0f).cross(Vector3(0.0f, 1.0f, 0.0f)), "cross 1");
+                   Vector3(1.0f, 0.0f, 0.0f).cross(Vector3(0.0f, 1.0f, 0.0f)),
+                   "cross 1");
     compareVectors(test, Vector3(0.0f, -1.0f, 0.0f),
-            Vector3(1.0f, 0.0f, 0.0f).cross(Vector3(0.0f, 0.0f, 1.0f)), "cross 2");
+                   Vector3(1.0f, 0.0f, 0.0f).cross(Vector3(0.0f, 0.0f, 1.0f)),
+                   "cross 2");
     compareVectors(test, Vector3(0.0f, 0.0f, -1.0f),
-            Vector3(0.0f, 1.0f, 0.0f).cross(Vector3(1.0f, 0.0f, 0.0f)), "cross 3");
+                   Vector3(0.0f, 1.0f, 0.0f).cross(Vector3(1.0f, 0.0f, 0.0f)),
+                   "cross 3");
     compareVectors(test, Vector3(1.0f, 0.0f, 0.0f),
-            Vector3(0.0f, 1.0f, 0.0f).cross(Vector3(0.0f, 0.0f, 1.0f)), "cross 4");
+                   Vector3(0.0f, 1.0f, 0.0f).cross(Vector3(0.0f, 0.0f, 1.0f)),
+                   "cross 4");
     compareVectors(test, Vector3(0.0f, 1.0f, 0.0f),
-            Vector3(0.0f, 0.0f, 1.0f).cross(Vector3(1.0f, 0.0f, 0.0f)), "cross 5");
+                   Vector3(0.0f, 0.0f, 1.0f).cross(Vector3(1.0f, 0.0f, 0.0f)),
+                   "cross 5");
     compareVectors(test, Vector3(-1.0f, 0.0f, 0.0f),
-            Vector3(0.0f, 0.0f, 1.0f).cross(Vector3(0.0f, 1.0f, 0.0f)), "cross 6");
+                   Vector3(0.0f, 0.0f, 1.0f).cross(Vector3(0.0f, 1.0f, 0.0f)),
+                   "cross 6");
 }
 
 static void testSetAdd(Test& test) {
@@ -91,8 +125,11 @@ static void testSetAdd(Test& test) {
 }
 
 static void testAdd(Test& test) {
-    compareVectors(test, Vector3(1.0f, 2.0f, 3.0f), Vector3() + Vector3(1.0f, 2.0f, 3.0f), "add 1");
-    compareVectors(test, Vector3(3.0f, 5.0f, 7.0f), Vector3(1.0f, 2.0f, 3.0f) + Vector3(2.0f, 3.0f, 4.0f), "add 2");
+    compareVectors(test, Vector3(1.0f, 2.0f, 3.0f),
+                   Vector3() + Vector3(1.0f, 2.0f, 3.0f), "add 1");
+    compareVectors(test, Vector3(3.0f, 5.0f, 7.0f),
+                   Vector3(1.0f, 2.0f, 3.0f) + Vector3(2.0f, 3.0f, 4.0f),
+                   "add 2");
 }
 
 static void testSetSub(Test& test) {
@@ -104,12 +141,16 @@ static void testSetSub(Test& test) {
 }
 
 static void testSub(Test& test) {
-    compareVectors(test, Vector3(1.0f, 2.0f, 3.0f), Vector3() - Vector3(-1.0f, -2.0f, -3.0f), "sub 1");
-    compareVectors(test, Vector3(-1.0f, -1.0f, -1.0f), Vector3(1.0f, 2.0f, 3.0f) - Vector3(2.0f, 3.0f, 4.0f), "sub 2");
+    compareVectors(test, Vector3(1.0f, 2.0f, 3.0f),
+                   Vector3() - Vector3(-1.0f, -2.0f, -3.0f), "sub 1");
+    compareVectors(test, Vector3(-1.0f, -1.0f, -1.0f),
+                   Vector3(1.0f, 2.0f, 3.0f) - Vector3(2.0f, 3.0f, 4.0f),
+                   "sub 2");
 }
 
 static void testInvert(Test& test) {
-    compareVectors(test, Vector3(-1.0f, 2.0f, 3.0f), -Vector3(1.0f, -2.0f, -3.0f), "invert");
+    compareVectors(test, Vector3(-1.0f, 2.0f, 3.0f),
+                   -Vector3(1.0f, -2.0f, -3.0f), "invert");
 }
 
 static void testSetMul(Test& test) {
@@ -121,22 +162,31 @@ static void testSetMul(Test& test) {
 }
 
 static void testMul(Test& test) {
-    compareVectors(test, Vector3(-3.0f, -6.0f, -9.0f), 3.0f * Vector3(-1.0f, -2.0f, -3.0f), "mul 1");
-    compareVectors(test, Vector3(3.0f, 6.0f, 9.0f), Vector3(1.0f, 2.0f, 3.0f) * 3.0, "mul 2");
+    compareVectors(test, Vector3(-3.0f, -6.0f, -9.0f),
+                   3.0f * Vector3(-1.0f, -2.0f, -3.0f), "mul 1");
+    compareVectors(test, Vector3(3.0f, 6.0f, 9.0f),
+                   Vector3(1.0f, 2.0f, 3.0f) * 3.0, "mul 2");
 }
 
 static void testDot(Test& test) {
-    test.checkFloat(9.0f, Vector3(-4.0f, 2.0f, -3.0f).dot(Vector3(-1.0f, -2.0f, -3.0f)), eps, "dot 1");
-    test.checkFloat(-22.0f, Vector3(2.0f, 2.0f, -4.0f).dot(Vector3(1.0f, -2.0f, 5.0f)), eps, "dot 2");
+    test.checkFloat(
+        9.0f, Vector3(-4.0f, 2.0f, -3.0f).dot(Vector3(-1.0f, -2.0f, -3.0f)),
+        eps, "dot 1");
+    test.checkFloat(-22.0f,
+                    Vector3(2.0f, 2.0f, -4.0f).dot(Vector3(1.0f, -2.0f, 5.0f)),
+                    eps, "dot 2");
 }
 
 static void testSquareLength(Test& test) {
-    test.checkFloat(29.0f, Vector3(-4.0f, 2.0f, -3.0f).squareLength(), eps, "square length 1");
-    test.checkFloat(24.0f, Vector3(2.0f, 2.0f, -4.0f).squareLength(), eps, "square length 2");
+    test.checkFloat(29.0f, Vector3(-4.0f, 2.0f, -3.0f).squareLength(), eps,
+                    "square length 1");
+    test.checkFloat(24.0f, Vector3(2.0f, 2.0f, -4.0f).squareLength(), eps,
+                    "square length 2");
 }
 
 static void testLength(Test& test) {
-    test.checkFloat(3.0f, Vector3(-2.0f, 2.0f, -1.0f).length(), eps, "length 1");
+    test.checkFloat(3.0f, Vector3(-2.0f, 2.0f, -1.0f).length(), eps,
+                    "length 1");
     test.checkFloat(7.0f, Vector3(6.0f, 2.0f, -3.0f).length(), eps, "length 2");
 }
 
@@ -145,7 +195,7 @@ static void testNormalize(Test& test) {
     Vector3 v2 = v1 * (1.0f / 3.0f);
     v1.normalize();
     compareVectors(test, v2, v1, "normalize 1");
-    
+
     Vector3 v3(6.0f, 2.0f, -3.0f);
     Vector3 v4 = v3 * (1.0f / 7.0f);
     v3.normalize();
@@ -154,8 +204,11 @@ static void testNormalize(Test& test) {
 
 static void testToString(Test& test) {
     StringBuffer<50> s;
-    s.append(Vector<1>()).append(Vector<2>(2.0f, 3.0f)).append(Vector<3>(4.0f, 5.0f, 6.0f));
-    test.checkEqual(StringBuffer<50>("[0.00][2.00, 3.00][4.00, 5.00, 6.00]"), s, "to string");
+    s.append(Vector<1>())
+        .append(Vector<2>(2.0f, 3.0f))
+        .append(Vector<3>(4.0f, 5.0f, 6.0f));
+    test.checkEqual(StringBuffer<50>("[0.00][2.00, 3.00][4.00, 5.00, 6.00]"), s,
+                    "to string");
 }
 
 void VectorTests::test() {

+ 1 - 0
utils/Array.h

@@ -2,6 +2,7 @@
 #define ARRAY_H
 
 #include "utils/StringBuffer.h"
+#include <initializer_list>
 
 template<typename T, int N>
 class Array final {