Browse Source

color and tests

Kajetan Johannes Hammerle 3 years ago
parent
commit
fea130a18d
5 changed files with 117 additions and 2 deletions
  1. 2 1
      Main.cpp
  2. 26 1
      meson.build
  3. 50 0
      tests/ColorTests.cpp
  4. 8 0
      tests/ColorTests.h
  5. 31 0
      utils/Color.h

+ 2 - 1
Main.cpp

@@ -14,7 +14,7 @@
 #include "tests/FrustumTests.h"
 #include "tests/QuaternionTests.h"
 #include "tests/UtilsTests.h"
-#include "utils/Types.h"
+#include "tests/ColorTests.h"
 
 int main() {
     ArrayTests::test();
@@ -33,5 +33,6 @@ int main() {
     FrustumTests::test();
     QuaternionTests::test();
     UtilsTests::test();
+    ColorTests::test();
     return 0;
 }

+ 26 - 1
meson.build

@@ -1,6 +1,31 @@
 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', 'tests/MatrixStackTests.cpp', 'tests/PlaneTests.cpp', 'math/Plane.cpp', 'tests/FrustumTests.cpp', 'math/Frustum.cpp', 'utils/Size.cpp', 'tests/QuaternionTests.cpp', 'math/Quaternion.cpp', 'tests/UtilsTests.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',
+    'tests/PlaneTests.cpp',
+    'math/Plane.cpp',
+    'tests/FrustumTests.cpp',
+    'math/Frustum.cpp',
+    'utils/Size.cpp',
+    'tests/QuaternionTests.cpp',
+    'math/Quaternion.cpp',
+    'tests/UtilsTests.cpp',
+    'tests/ColorTests.cpp']
 
 executable('tests', 
     sources: sources,

+ 50 - 0
tests/ColorTests.cpp

@@ -0,0 +1,50 @@
+#include "tests/ColorTests.h"
+#include "tests/Test.h"
+#include "utils/Color.h"
+
+const float eps = 0.0001f;
+
+static void testColor1(Test& test) {
+    Color1 c = Color1(36);
+    test.checkEqual(static_cast<Color1::Channel>(36), c.data[0], "build 1 | 1");
+    test.checkFloat(36.0f / 255.0f, c.asFloat(0), eps, "build 1 | 2");
+}
+
+static void testColor2(Test& test) {
+    Color2 c = Color2(36, 100);
+    test.checkEqual(static_cast<Color2::Channel>(36), c.data[0], "build 2 | 1");
+    test.checkEqual(static_cast<Color2::Channel>(100), c.data[1], "build 2 | 2");
+    test.checkFloat(36.0f / 255.0f, c.asFloat(0), eps, "build 2 | 3");
+    test.checkFloat(100.0f / 255.0f, c.asFloat(1), eps, "build 2 | 4");
+}
+
+static void testColor3(Test& test) {
+    Color3 c = Color3(36, 100, 200);
+    test.checkEqual(static_cast<Color3::Channel>(36), c.data[0], "build 3 | 1");
+    test.checkEqual(static_cast<Color3::Channel>(100), c.data[1], "build 3 | 2");
+    test.checkEqual(static_cast<Color3::Channel>(200), c.data[2], "build 3 | 3");
+    test.checkFloat(36.0f / 255.0f, c.asFloat(0), eps, "build 3 | 4");
+    test.checkFloat(100.0f / 255.0f, c.asFloat(1), eps, "build 3 | 5");
+    test.checkFloat(200.0f / 255.0f, c.asFloat(2), eps, "build 3 | 6");
+}
+
+static void testColor4(Test& test) {
+    Color4 c = Color4(36, 100, 200, 142);
+    test.checkEqual(static_cast<Color4::Channel>(36), c.data[0], "build 4 | 1");
+    test.checkEqual(static_cast<Color4::Channel>(100), c.data[1], "build 4 | 2");
+    test.checkEqual(static_cast<Color4::Channel>(200), c.data[2], "build 4 | 3");
+    test.checkEqual(static_cast<Color4::Channel>(142), c.data[3], "build 4 | 4");
+    test.checkFloat(36.0f / 255.0f, c.asFloat(0), eps, "build 4 | 5");
+    test.checkFloat(100.0f / 255.0f, c.asFloat(1), eps, "build 4 | 6");
+    test.checkFloat(200.0f / 255.0f, c.asFloat(2), eps, "build 4 | 7");
+    test.checkFloat(142.0f / 255.0f, c.asFloat(3), eps, "build 4 | 8");
+}
+
+void ColorTests::test() {
+    Test test("Color");
+    testColor1(test);
+    testColor2(test);
+    testColor3(test);    
+    testColor4(test);
+    test.finalize();
+}

+ 8 - 0
tests/ColorTests.h

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

+ 31 - 0
utils/Color.h

@@ -0,0 +1,31 @@
+#ifndef COLOR_H
+#define COLOR_H
+
+template<int N>
+struct Color {
+    static_assert(N <= 4, "a color can have at most 4 channels");
+    typedef unsigned char Channel;
+
+    Channel data[N];
+
+    template<typename... Args>
+    Color(Channel a, Args&&... args) {
+        const int size = sizeof...(args) + 1;
+        int init[size] = {a, args...};
+        static_assert(N == size, "color size and amount of channel arguments do not match");
+        for(int i = 0; i < N; i++) {
+            data[i] = init[i];
+        }
+    }
+
+    float asFloat(int index) const {
+        return data[index] * (1.0f / 255.0f);
+    }
+};
+
+typedef Color<4> Color4;
+typedef Color<3> Color3;
+typedef Color<2> Color2;
+typedef Color<1> Color1;
+
+#endif