Browse Source

utils and tests

Kajetan Johannes Hammerle 3 years ago
parent
commit
2520bce011
7 changed files with 93 additions and 13 deletions
  1. 2 0
      Main.cpp
  2. 1 1
      meson.build
  3. 47 0
      tests/UtilsTests.cpp
  4. 8 0
      tests/UtilsTests.h
  5. 0 2
      tests/VectorTests.cpp
  6. 2 10
      utils/HashMap.h
  7. 33 0
      utils/Utils.h

+ 2 - 0
Main.cpp

@@ -13,6 +13,7 @@
 #include "tests/PlaneTests.h"
 #include "tests/FrustumTests.h"
 #include "tests/QuaternionTests.h"
+#include "tests/UtilsTests.h"
 
 int main() {
     ArrayTests::test();
@@ -30,5 +31,6 @@ int main() {
     PlaneTests::test();
     FrustumTests::test();
     QuaternionTests::test();
+    UtilsTests::test();
     return 0;
 }

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

+ 47 - 0
tests/UtilsTests.cpp

@@ -0,0 +1,47 @@
+#include "tests/UtilsTests.h"
+#include "tests/Test.h"
+#include "utils/Utils.h"
+
+const float eps = 0.0001f;
+
+static void testInterpolate(Test& test) {
+    test.checkFloat(7.5f, Utils::interpolate(5.0f, 10.0f, 0.5f), eps, "interpolate 1");
+    test.checkFloat(-2.0, Utils::interpolate(-10.0f, 10.0f, 0.4f), eps, "interpolate 2");
+    test.checkFloat(10.0f, Utils::interpolate(-3.0f, 10.0f, 1.0f), eps, "interpolate 3");
+    test.checkFloat(7.0f, Utils::interpolate(7.0f, 10.0f, 0.0f), eps, "interpolate 4");
+    test.checkFloat(6.0f, Utils::interpolate(0.0f, 10.0f, 0.6f), eps, "interpolate 5");
+}
+
+static void testPopCount(Test& test) {
+    test.checkEqual(4, Utils::popCount(0xF), "pop count 1");
+    test.checkEqual(0, Utils::popCount(0x0), "pop count 2");
+    test.checkEqual(2, Utils::popCount(0x6), "pop count 3");
+    test.checkEqual(7, Utils::popCount(0x7F), "pop count 4");
+    test.checkEqual(3, Utils::popCount(0x2A), "pop count 5");
+    test.checkEqual(32, Utils::popCount(0xFFFFFFFF), "pop count 6");
+    test.checkEqual(64, Utils::popCount(0xFFFFFFFFFFFFFFFFL), "pop count 7");
+    test.checkEqual(44, Utils::popCount(0xFFFF0FFFFFFF), "pop count 8");
+    test.checkEqual(32, Utils::popCount(-1), "pop count 9");
+}
+
+static void testRoundUpLog2(Test& test) {
+    test.checkEqual(0, Utils::roundUpLog2(-5), "round up log2 1");
+    test.checkEqual(0, Utils::roundUpLog2(0), "round up log2 2");
+    test.checkEqual(1, Utils::roundUpLog2(1), "round up log2 3");
+    test.checkEqual(1, Utils::roundUpLog2(2), "round up log2 4");
+    test.checkEqual(2, Utils::roundUpLog2(3), "round up log2 5");
+    test.checkEqual(2, Utils::roundUpLog2(4), "round up log2 6");
+    test.checkEqual(3, Utils::roundUpLog2(5), "round up log2 7");
+    test.checkEqual(4, Utils::roundUpLog2(10), "round up log2 8");
+    test.checkEqual(5, Utils::roundUpLog2(20), "round up log2 9");
+    test.checkEqual(16, Utils::roundUpLog2(35345), "round up log2 10");
+    test.checkEqual(31, Utils::roundUpLog2(0x7FFFFFFF), "round up log2 11");
+}
+
+void UtilsTests::test() {
+    Test test("Utils");
+    testInterpolate(test);
+    testPopCount(test);
+    testRoundUpLog2(test);
+    test.finalize();
+}

+ 8 - 0
tests/UtilsTests.h

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

+ 0 - 2
tests/VectorTests.cpp

@@ -31,8 +31,6 @@ static void testInitAndRead(Test& test) {
     test.checkEqual(7.0f, v4[1], "init 10");
     test.checkEqual(8.0f, v4[2], "init 11");
     test.checkEqual(9.0f, v4[3], "init 12");
-    
-    //v1.func(1.0f, 2.0f, 3.0f);
 }
 
 static void testSetAngles(Test& test) {

+ 2 - 10
utils/HashMap.h

@@ -2,6 +2,7 @@
 #define HASHMAP_H
 
 #include "utils/Array.h"
+#include "utils/Utils.h"
 #include "utils/List.h"
 #include "utils/StringBuffer.h"
 
@@ -9,16 +10,7 @@
 
 template<typename K, typename V, int N_MIN>
 class HashMap final {
-
-    static constexpr int getCapacity() {
-        int i = 1;
-        while(i < N_MIN) {
-            i <<= 1;
-        }
-        return i;
-    }
-
-    static constexpr int CAPACITY = getCapacity();
+    static constexpr int CAPACITY = 1 << Utils::roundUpLog2(N_MIN);
     static constexpr int MASK = CAPACITY - 1;
 
     Array<int, CAPACITY> used;

+ 33 - 0
utils/Utils.h

@@ -0,0 +1,33 @@
+#ifndef UTILS_H
+#define UTILS_H
+
+namespace Utils {
+
+    template<typename T>
+    T interpolate(const T& a, const T& b, float f) {
+        return a * (1.0f - f) + b * f;
+    }
+
+    template<typename T>
+    int popCount(const T& t) {
+        static constexpr int map[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
+        int sum = 0;
+        for(int i = 0; i < static_cast<int> (sizeof (T) * 8); i += 4) {
+            sum += map[(t >> i) & 0xF];
+        }
+        return sum;
+    }
+
+    constexpr int roundUpLog2(int i) {
+        if(i <= 0) {
+            return 0;
+        }
+        int c = 1;
+        while(((i - 1) >> c) > 0) {
+            c++;
+        }
+        return c;
+    }
+}
+
+#endif