Browse Source

More tests for code coverage

Kajetan Johannes Hammerle 2 months ago
parent
commit
54f4ee0d09

+ 1 - 0
CMakeLists.txt

@@ -56,6 +56,7 @@ set(SRC_TESTS
     "test/modules/RandomTests.cpp"
     "test/modules/ThreadTests.cpp"
     "test/modules/FileReaderTests.cpp"
+    "test/modules/ErrorTests.cpp"
 )
 
 set(SRC_PERFORMANCE

+ 0 - 2
include/core/io/FileReader.hpp

@@ -4,8 +4,6 @@
 #include "core/io/File.hpp"
 
 namespace Core {
-    using Path = String8<PATH_MAX>;
-
     class FileReader final {
         void* file;
         Path path;

+ 1 - 1
include/core/utils/ArrayString.hpp

@@ -166,7 +166,7 @@ namespace Core {
         }
 
         check_return Error append(float f) {
-            return convertAppend(static_cast<double>(f));
+            return convertAppend(f);
         }
 
         check_return Error append(double d) {

+ 1 - 0
test/Main.cpp

@@ -24,6 +24,7 @@ int main(int argAmount, const char**) {
     Core::testClock(light);
     Core::testColor();
     Core::testComponents();
+    Core::testError();
     Core::testFileReader();
     Core::testFrustum();
     Core::testHashMap(light);

+ 1 - 0
test/Tests.hpp

@@ -14,6 +14,7 @@ namespace Core {
     void testClock(bool light);
     void testColor();
     void testComponents();
+    void testError();
     void testFileReader();
     void testFrustum();
     void testHashMap(bool light);

+ 14 - 0
test/modules/ArrayListTests.cpp

@@ -143,6 +143,19 @@ static void testRemove() {
     CORE_TEST_EQUAL(0, list.getLength());
 }
 
+static void testForRange() {
+    IntList list;
+    CORE_TEST_ERROR(list.add(1));
+    CORE_TEST_ERROR(list.add(2));
+    CORE_TEST_ERROR(list.add(3));
+    for(int& i : list) {
+        i++;
+    }
+    for(int i = 0; i < list.getLength(); i++) {
+        CORE_TEST_EQUAL(i + 2, list[i]);
+    }
+}
+
 void Core::testArrayList(bool light) {
     testAdd();
     testMultipleAdd();
@@ -157,4 +170,5 @@ void Core::testArrayList(bool light) {
     testToString2();
     testToString3();
     testRemove();
+    testForRange();
 }

+ 26 - 0
test/modules/ArrayTests.cpp

@@ -17,7 +17,33 @@ static void testToString2() {
     CORE_TEST_STRING("[1]", a);
 }
 
+static void testReadConst() {
+    Core::Array<int, 3> a;
+    for(int i = 0; i < a.getLength(); i++) {
+        a[i] = i;
+    }
+    const Core::Array<int, 3>& c = a;
+    for(int i = 0; i < c.getLength(); i++) {
+        CORE_TEST_EQUAL(i, c[i]);
+    }
+}
+
+static void testRangeFor() {
+    Core::Array<int, 3> a;
+    for(int i = 0; i < a.getLength(); i++) {
+        a[i] = i;
+    }
+    for(int& i : a) {
+        i++;
+    }
+    for(int i = 0; i < a.getLength(); i++) {
+        CORE_TEST_EQUAL(i + 1, a[i]);
+    }
+}
+
 void Core::testArray() {
     testToString1();
     testToString2();
+    testReadConst();
+    testRangeFor();
 }

+ 39 - 0
test/modules/BitArrayTests.cpp

@@ -105,6 +105,42 @@ static void testToString3() {
     CORE_TEST_STRING("[]", bits);
 }
 
+static void testResizeExact() {
+    Core::BitArray bits;
+    CORE_TEST_EQUAL(0, bits.getInternalByteSize());
+    // the size in bytes matches the internal storage type
+    int elements = CORE_SIZE(int);
+    CORE_TEST_ERROR(bits.resize(elements, 8));
+    for(int i = 0; i < elements; i++) {
+        bits.set(i, i);
+    }
+    for(int i = 0; i < elements; i++) {
+        CORE_TEST_EQUAL(i, bits.get(i));
+    }
+    CORE_TEST_EQUAL(CORE_SIZE(int), bits.getInternalByteSize());
+}
+
+static void testMoveAssignment() {
+    Core::BitArray bits;
+    CORE_TEST_ERROR(bits.resize(8, 8));
+    for(int i = 0; i < bits.getLength(); i++) {
+        bits.set(i, i);
+    }
+    Core::BitArray m;
+    m = Core::move(bits);
+    CORE_TEST_EQUAL(8, m.getLength());
+    for(int i = 0; i < m.getLength(); i++) {
+        CORE_TEST_EQUAL(i, m.get(i));
+    }
+}
+
+static void testNegativeArgument() {
+    Core::BitArray bits;
+    CORE_TEST_EQUAL(Core::Error::NEGATIVE_ARGUMENT, bits.resize(-1, 5));
+    CORE_TEST_EQUAL(Core::Error::NEGATIVE_ARGUMENT, bits.resize(5, -1));
+    CORE_TEST_EQUAL(Core::Error::NEGATIVE_ARGUMENT, bits.resize(-1, -1));
+}
+
 void Core::testBitArray() {
     testSetRead();
     testOutOfBoundsSetRead();
@@ -115,4 +151,7 @@ void Core::testBitArray() {
     testToString1();
     testToString2();
     testToString3();
+    testResizeExact();
+    testMoveAssignment();
+    testNegativeArgument();
 }

+ 10 - 5
test/modules/BufferedValueTests.cpp

@@ -4,18 +4,21 @@
 
 template class Core::BufferedValue<Core::Vector2>;
 
+using BufferedFloat = Core::BufferedValue<float>;
+
 const float eps = 0.0001f;
 
 static void testInit() {
-    Core::BufferedValue<float> b = 5.0f;
+    BufferedFloat b = 5.0f;
     CORE_TEST_FLOAT(5.0f, b.get(0.0f), eps);
     CORE_TEST_FLOAT(5.0f, b.get(0.5f), eps);
     CORE_TEST_FLOAT(5.0f, b.get(1.0f), eps);
     CORE_TEST_FLOAT(5.0f, b, eps);
+    CORE_TEST_FLOAT(5.0f, static_cast<const BufferedFloat&>(b), eps);
 }
 
 static void testInterpolate() {
-    Core::BufferedValue<float> b = 5.0f;
+    BufferedFloat b = 5.0f;
     b = 7.0f;
     CORE_TEST_FLOAT(5.0f, b.get(0.0f), eps);
     CORE_TEST_FLOAT(6.0f, b.get(0.5f), eps);
@@ -24,7 +27,7 @@ static void testInterpolate() {
 }
 
 static void testUpdate() {
-    Core::BufferedValue<float> b = 5.0f;
+    BufferedFloat b = 5.0f;
     b = 7.0f;
     b.update();
     CORE_TEST_FLOAT(7.0f, b.get(0.0f), eps);
@@ -34,7 +37,7 @@ static void testUpdate() {
 }
 
 static void testCalculate() {
-    Core::BufferedValue<float> b = 5.0f;
+    BufferedFloat b = 5.0f;
     b = 7.0f;
     b += 3.0f;
     CORE_TEST_FLOAT(5.0f, b.get(0.0f), eps);
@@ -46,7 +49,8 @@ static void testCalculate() {
 
 static void testVector2() {
     Core::Vector2 base(5.0f, 6.0f);
-    Core::BufferedValue<Core::Vector2> b = base;
+    using BufferedVector2 = Core::BufferedValue<Core::Vector2>;
+    BufferedVector2 b = base;
 
     CORE_TEST_VECTOR(base, b.get(1.0f));
     b = Core::Vector2(7.0f, 5.0);
@@ -79,6 +83,7 @@ static void testVector2() {
     CORE_TEST_VECTOR(Core::Vector2(1.0f, 1.0f), b.get(1.0f));
     b[0] += 3;
     CORE_TEST_VECTOR(Core::Vector2(4.0f, 1.0f), b.get(1.0f));
+    CORE_TEST_FLOAT(4.0f, static_cast<const BufferedVector2&>(b)[0], eps);
 }
 
 void Core::testBufferedValue() {

+ 4 - 2
test/modules/ColorTests.cpp

@@ -32,8 +32,9 @@ static void testColor3() {
     CORE_TEST_FLOAT(200.0f / 255.0f, c.asFloat(2), eps);
 }
 
+template<typename T>
 static void testColor4() {
-    Core::Color4 c(36, 100, 200, 142);
+    T c(36, 100, 200, 142);
     CORE_TEST_EQUAL(36, c[0]);
     CORE_TEST_EQUAL(100, c[1]);
     CORE_TEST_EQUAL(200, c[2]);
@@ -56,6 +57,7 @@ void Core::testColor() {
     testColor1();
     testColor2();
     testColor3();
-    testColor4();
+    testColor4<Core::Color4>();
+    testColor4<const Core::Color4>();
     testColor4Empty();
 }

+ 26 - 0
test/modules/ErrorTests.cpp

@@ -0,0 +1,26 @@
+#include "../Tests.hpp"
+#include "core/utils/Error.hpp"
+
+static void test(Core::Error e, const char* s) {
+    CORE_TEST_STRING(getErrorName(e), s);
+}
+
+void Core::testError() {
+    test(Error::NONE, "NONE");
+    test(Error::NEGATIVE_ARGUMENT, "NEGATIVE_ARGUMENT");
+    test(Error::CAPACITY_REACHED, "CAPACITY_REACHED");
+    test(Error::BLOCKED_STDOUT, "BLOCKED_STDOUT");
+    test(Error::OUT_OF_MEMORY, "OUT_OF_MEMORY");
+    test(Error::INVALID_CHAR, "INVALID_CHAR");
+    test(Error::NOT_FOUND, "NOT_FOUND");
+    test(Error::INVALID_STATE, "INVALID_STATE");
+    test(Error::INVALID_INDEX, "INVALID_INDEX");
+    test(Error::INVALID_ARGUMENT, "INVALID_ARGUMENT");
+    test(Error::TIME_NOT_AVAILABLE, "TIME_NOT_AVAILABLE");
+    test(Error::SLEEP_INTERRUPTED, "SLEEP_INTERRUPTED");
+    test(Error::THREAD_ERROR, "THREAD_ERROR");
+    test(Error::EXISTING_KEY, "EXISTING_KEY");
+    test(Error::CANNOT_OPEN_FILE, "CANNOT_OPEN_FILE");
+    test(Error::END_OF_FILE, "END_OF_FILE");
+    test(static_cast<Error>(200), "?");
+}

+ 37 - 0
test/modules/FileReaderTests.cpp

@@ -17,6 +17,22 @@ static void testReadChar() {
     CORE_TEST_STRING("abc\nBaum\n", s);
 }
 
+static void testReadCharPath() {
+    Core::FileReader r;
+    Core::Path path;
+    CORE_TEST_ERROR(path.append(TEST_FILE));
+    CORE_TEST_ERROR(r.open(path));
+    Core::String8<128> s;
+    while(true) {
+        int c = 0;
+        if(r.readChar(c) != Core::Error::NONE) {
+            break;
+        }
+        CORE_TEST_ERROR(s.append(static_cast<c32>(c)));
+    }
+    CORE_TEST_STRING("abc\nBaum\n", s);
+}
+
 static void testReadChars() {
     Core::FileReader r;
     CORE_TEST_ERROR(r.open(TEST_FILE));
@@ -61,10 +77,31 @@ static void testMoveAssignment() {
     CORE_TEST_EQUAL('a', c);
 }
 
+static void testInvalidAccess() {
+    char buffer[4];
+    Core::FileReader r;
+    CORE_TEST_EQUAL(Core::Error::INVALID_STATE,
+                    r.readChars(buffer, sizeof(buffer)));
+    CORE_TEST_ERROR(r.open(TEST_FILE));
+    CORE_TEST_EQUAL(Core::Error::INVALID_ARGUMENT, r.readChars(buffer, -1));
+    CORE_TEST_EQUAL(Core::Error::INVALID_STATE, r.open(TEST_FILE));
+}
+
+static void testInvalidAccessPath() {
+    Core::FileReader r;
+    Core::Path path;
+    CORE_TEST_ERROR(path.append(TEST_FILE));
+    CORE_TEST_ERROR(r.open(path));
+    CORE_TEST_EQUAL(Core::Error::INVALID_STATE, r.open(path));
+}
+
 void Core::testFileReader() {
     testReadChar();
+    testReadCharPath();
     testReadChars();
     testReadCharsOverflow();
     testMoveConstruct();
     testMoveAssignment();
+    testInvalidAccess();
+    testInvalidAccessPath();
 }

+ 10 - 0
test/modules/FrustumTests.cpp

@@ -46,8 +46,18 @@ static void testSphereIsInside() {
     CORE_TEST_TRUE(f.isInside(V3(-50.0f, 0.0f, 5.0f), 50.0f));
 }
 
+static void testUpdateProjection() {
+    Core::Frustum f(60.0f, 0.1f, 1000.0f);
+    CORE_TEST_STRING("[[1.30, 0.00, 0.00, 0.00], "
+                     "[0.00, 1.73, 0.00, 0.00], "
+                     "[0.00, 0.00, -1.00, -0.20], "
+                     "[0.00, 0.00, -1.00, 0.00]]",
+                     f.updateProjection(Core::IntVector2(400, 300)));
+}
+
 void Core::testFrustum() {
     testToString();
     testPointIsInside();
     testSphereIsInside();
+    testUpdateProjection();
 }

+ 16 - 0
test/modules/MatrixStackTests.cpp

@@ -99,6 +99,20 @@ static void testToString3() {
                      stack);
 }
 
+static void testInvalidPop() {
+    Matrices stack;
+    CORE_TEST_EQUAL(Core::Error::INVALID_STATE, stack.pop());
+}
+
+static void testConstPeek() {
+    const Matrices stack;
+    CORE_TEST_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]]",
+                     stack.peek());
+}
+
 void Core::testMatrixStack(bool light) {
     testInit();
     testPop();
@@ -107,4 +121,6 @@ void Core::testMatrixStack(bool light) {
     testToString1();
     testToString2();
     testToString3();
+    testInvalidPop();
+    testConstPeek();
 }

+ 6 - 0
test/modules/RingBufferTests.cpp

@@ -258,6 +258,11 @@ static void testOverall() {
     CORE_TEST_EQUAL(0, Tester::sum);
 }
 
+static void testInvalidRemove() {
+    Core::RingBuffer<int, 3> buffer;
+    CORE_TEST_EQUAL(Core::Error::INVALID_STATE, buffer.remove());
+}
+
 void Core::testRingBuffer() {
     testReadAndWrite();
     testOverflow();
@@ -269,4 +274,5 @@ void Core::testRingBuffer() {
     testMoveDestruct();
     testMoveAssignmentDestruct();
     testOverall();
+    testInvalidRemove();
 }

+ 14 - 0
test/modules/StackTests.cpp

@@ -11,10 +11,13 @@ static void testPushPopPeek() {
     CORE_TEST_ERROR(stack.push(2));
     CORE_TEST_ERROR(stack.push(3));
     CORE_TEST_EQUAL(3, stack.peek());
+    CORE_TEST_EQUAL(3, static_cast<const T&>(stack).peek());
     CORE_TEST_ERROR(stack.pop());
     CORE_TEST_EQUAL(2, stack.peek());
+    CORE_TEST_EQUAL(2, static_cast<const T&>(stack).peek());
     CORE_TEST_ERROR(stack.pop());
     CORE_TEST_EQUAL(1, stack.peek());
+    CORE_TEST_EQUAL(1, static_cast<const T&>(stack).peek());
     CORE_TEST_ERROR(stack.pop());
     CORE_TEST_TRUE(stack.isEmpty());
 }
@@ -61,6 +64,16 @@ static void testPop(int amount) {
     }
 }
 
+template<typename T>
+static void testClear() {
+    T stack;
+    CORE_TEST_ERROR(stack.push(1));
+    CORE_TEST_ERROR(stack.push(2));
+    CORE_TEST_ERROR(stack.push(3));
+    stack.clear();
+    CORE_TEST_TRUE(stack.isEmpty());
+}
+
 template<typename T>
 static void testType(int amount) {
     testPushPopPeek<T>();
@@ -69,6 +82,7 @@ static void testType(int amount) {
     testToString2<T>();
     testToString3<T>();
     testPop<T>(amount);
+    testClear<T>();
 }
 
 void Core::testStack(bool light) {

+ 8 - 0
test/modules/ThreadTests.cpp

@@ -61,6 +61,13 @@ static void testMoveAssignment() {
     CORE_TEST_ERROR(m.join());
 }
 
+static void testMoveIntoActive() {
+    Core::Thread t;
+    CORE_TEST_ERROR(t.start([](void*) { return 0; }, nullptr));
+    Core::Thread m;
+    t = Core::move(m);
+}
+
 static void testDoubleJoin() {
     Core::Thread t;
     CORE_TEST_ERROR(t.start([](void*) { return 0; }, nullptr));
@@ -75,5 +82,6 @@ void Core::testThread() {
     testAutoJoin();
     testMove();
     testMoveAssignment();
+    testMoveIntoActive();
     testDoubleJoin();
 }

+ 36 - 3
test/modules/UniquePointerTests.cpp

@@ -4,6 +4,8 @@
 struct B final {
     static int instances;
 
+    bool b = false;
+
     B() {
         instances++;
     }
@@ -11,24 +13,38 @@ struct B final {
     ~B() {
         instances--;
     }
+
+    void test() {
+        b = true;
+    }
 };
 
 int B::instances = 0;
 
 template class Core::UniquePointer<B>;
+using UniqueB = Core::UniquePointer<B>;
 
 static void testDestroy() {
     {
-        Core::UniquePointer<B> p(new B());
+        UniqueB p(new B());
         CORE_TEST_EQUAL(1, B::instances);
     }
     CORE_TEST_EQUAL(0, B::instances);
 }
 
+static void testMoveConstructDestroys() {
+    UniqueB p1(new B());
+    CORE_TEST_EQUAL(1, B::instances);
+    UniqueB p2(Core::move(p1));
+    CORE_TEST_EQUAL(1, B::instances);
+    p2 = nullptr;
+    CORE_TEST_EQUAL(0, B::instances);
+}
+
 static void testMoveDestroys() {
     {
-        Core::UniquePointer<B> p1(new B());
-        Core::UniquePointer<B> p2(new B());
+        UniqueB p1(new B());
+        UniqueB p2(new B());
         CORE_TEST_EQUAL(2, B::instances);
         p1 = Core::move(p2);
         CORE_TEST_EQUAL(1, B::instances);
@@ -36,7 +52,24 @@ static void testMoveDestroys() {
     CORE_TEST_EQUAL(0, B::instances);
 }
 
+static void testEmpty() {
+    UniqueB p;
+    CORE_TEST_TRUE(p == nullptr);
+    CORE_TEST_TRUE(static_cast<const UniqueB&>(p) == nullptr);
+}
+
+static void testCall() {
+    UniqueB p(new B());
+    CORE_TEST_FALSE(p->b);
+    p->test();
+    CORE_TEST_TRUE(p->b);
+    CORE_TEST_TRUE(static_cast<const UniqueB&>(p)->b);
+}
+
 void Core::testUniquePointer() {
     testDestroy();
+    testMoveConstructDestroys();
     testMoveDestroys();
+    testEmpty();
+    testCall();
 }

+ 20 - 0
test/modules/UtilityTests.cpp

@@ -18,7 +18,27 @@ static void testIf() {
     CORE_TEST_TRUE((Core::IsSame<Core::If<false, int, double>, double>));
 }
 
+static void testNegativeArguments() {
+    char from[16];
+    Core::memorySet(from, 1, sizeof(from));
+    Core::memorySet(from, 0, -1);
+    char to[16];
+    Core::memorySet(to, 1, sizeof(to));
+    Core::memoryCopy(to, from, -1);
+    CORE_TEST_TRUE(Core::memoryCompare(from, to, sizeof(from)));
+}
+
+static void testNegativeRellocate() {
+    char* buffer = nullptr;
+    CORE_TEST_ERROR(Core::reallocate(buffer, 16));
+    CORE_TEST_TRUE(buffer != nullptr);
+    CORE_TEST_ERROR(Core::reallocate(buffer, -1));
+    CORE_TEST_TRUE(buffer == nullptr);
+}
+
 void Core::testUtility() {
     testPopCount();
     testIf();
+    testNegativeArguments();
+    testNegativeRellocate();
 }

+ 7 - 0
test/modules/VectorTests.cpp

@@ -197,6 +197,12 @@ static void testToString() {
     CORE_TEST_EQUAL(s2, s);
 }
 
+static void testNormalizeIntVector() {
+    I3 i(1, 2, 3);
+    i.normalize();
+    CORE_TEST_VECTOR(I3(0, 0, 1), i);
+}
+
 void Core::testVector() {
     testInitAndRead();
     testSetAngles();
@@ -218,4 +224,5 @@ void Core::testVector() {
     testNormalize();
     testCast();
     testToString();
+    testNormalizeIntVector();
 }

+ 10 - 0
test/modules/ViewTests.cpp

@@ -25,7 +25,17 @@ static void testFromQuaternion() {
     CORE_TEST_VECTOR(V3(-1.0f, 0.0f, 0.0f), v.getBack());
 }
 
+static void testUpdateMatrix() {
+    Core::View v;
+    CORE_TEST_STRING("[[0.00, 0.00, 0.00, 0.00], "
+                     "[0.00, 0.00, 0.00, 0.00], "
+                     "[0.00, 0.00, 0.00, 0.00], "
+                     "[0.00, 0.00, 0.00, 1.00]]",
+                     v.updateMatrix(Core::Vector3(1.0f, 2.0f, 3.0f)));
+}
+
 void Core::testView() {
     testFromAngles();
     testFromQuaternion();
+    testUpdateMatrix();
 }