Browse Source

Generate test coverage, light test mode

Kajetan Johannes Hammerle 2 months ago
parent
commit
9d798ff19f

+ 5 - 1
CMakeLists.txt

@@ -65,6 +65,7 @@ set(SRC_PERFORMANCE
 
 add_library(core STATIC ${SRC})
 target_compile_options(core PUBLIC
+    --coverage
     -fdiagnostics-color=always
     -fno-exceptions
     -fno-rtti
@@ -162,7 +163,10 @@ target_compile_options(core PUBLIC
     -Wzero-as-null-pointer-constant
 )
 target_compile_definitions(core PUBLIC CORE_LOG_LEVEL=4)
-target_link_libraries(core PUBLIC -nodefaultlibs c m)
+target_link_libraries(core 
+    PUBLIC -nodefaultlibs c m
+    PRIVATE gcov
+)
 target_sources(core PUBLIC 
     FILE_SET HEADERS
     BASE_DIRS include

+ 13 - 1
tasks

@@ -8,6 +8,7 @@ printHelpExit() {
     echo "$0 build       | build everything"
     echo "$0 install     | move build results into the install folder"
     echo "$0 test        | run the tests"
+    echo "$0 coverage    | generate code coverage"
     echo "$0 performance | run the performance tests"
     echo "$0 final       | find classes / structs which are not final"
     echo "$0 macro       | find macros without CORE" 
@@ -16,6 +17,8 @@ printHelpExit() {
     exit 0
 }
 
+# TODO gcovr https://gcovr.com/en/5.0/guide.html
+
 task=$1
 if [ -z "$task" ]; then
     printHelpExit
@@ -27,6 +30,7 @@ test=false
 performance=false
 time=false
 install=false
+coverage=false
 
 # parsing
 if [ "$task" = "clean" ]; then
@@ -37,6 +41,10 @@ elif [ "$task" = "build" ]; then
 elif [ "$task" = "install" ]; then
     build=true
     install=true
+elif [ "$task" = "coverage" ]; then
+    build=true
+    coverage=true
+    test=true
 elif [ "$task" = "test" ]; then
     build=true
     test=true
@@ -73,7 +81,8 @@ if $install; then
 fi
 if $test; then
     cd build
-    ./test
+    ./test || true
+    cd ..
 fi
 if $performance; then
     cd build
@@ -100,4 +109,7 @@ if $time; then
         i=$(expr $(expr $i + 1) % 5) && true
     done
     printf "$output" | sort -n
+fi
+if $coverage; then
+    gcovr -r . build -e test -e performance
 fi

+ 12 - 11
test/Main.cpp

@@ -12,31 +12,32 @@ static void onExit(int code, void* data) {
     Core::Test::finalize();
 }
 
-int main() {
-    Core::testArrayList();
+int main(int argAmount, const char**) {
+    bool light = argAmount <= 1;
+    Core::testArrayList(light);
     Core::testArrayString();
     Core::testArray();
     Core::testBitArray();
     Core::testBox();
-    Core::testBuffer();
+    Core::testBuffer(light);
     Core::testBufferedValue();
-    Core::testClock();
+    Core::testClock(light);
     Core::testColor();
     Core::testComponents();
     Core::testFileReader();
     Core::testFrustum();
-    Core::testHashMap();
-    Core::testLinkedList();
-    Core::testList();
+    Core::testHashMap(light);
+    Core::testLinkedList(light);
+    Core::testList(light);
     Core::testMath();
-    Core::testMatrixStack();
+    Core::testMatrixStack(light);
     Core::testMatrix();
     Core::testPlane();
-    Core::testProbingHashMap();
+    Core::testProbingHashMap(light);
     Core::testQuaternion();
-    Core::testRandom();
+    Core::testRandom(light);
     Core::testRingBuffer();
-    Core::testStack();
+    Core::testStack(light);
     Core::testThread();
     Core::testUniquePointer();
     Core::testUtility();

+ 10 - 10
test/Tests.hpp

@@ -4,30 +4,30 @@
 #include "Test.hpp"
 
 namespace Core {
-    void testArrayList();
+    void testArrayList(bool light);
     void testArrayString();
     void testArray();
     void testBitArray();
     void testBox();
-    void testBuffer();
+    void testBuffer(bool light);
     void testBufferedValue();
-    void testClock();
+    void testClock(bool light);
     void testColor();
     void testComponents();
     void testFileReader();
     void testFrustum();
-    void testHashMap();
-    void testLinkedList();
-    void testList();
+    void testHashMap(bool light);
+    void testLinkedList(bool light);
+    void testList(bool light);
     void testMath();
-    void testMatrixStack();
+    void testMatrixStack(bool light);
     void testMatrix();
     void testPlane();
-    void testProbingHashMap();
+    void testProbingHashMap(bool light);
     void testQuaternion();
-    void testRandom();
+    void testRandom(bool light);
     void testRingBuffer();
-    void testStack();
+    void testStack(bool light);
     void testThread();
     void testUniquePointer();
     void testUtility();

+ 5 - 4
test/modules/ArrayListTests.cpp

@@ -37,12 +37,13 @@ static void testClear() {
     CORE_TEST_EQUAL(0, list.getLength());
 }
 
-static void testOverflow() {
+static void testOverflow(bool light) {
     IntList list;
     for(int i = 0; i < 20; i++) {
         CORE_TEST_ERROR(list.add(i));
     }
-    for(int i = 0; i < 100000; i++) {
+    int limit = light ? 1000 : 100000;
+    for(int i = 0; i < limit; i++) {
         CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, list.add(i));
     }
     for(int i = 0; i < list.getLength(); i++) {
@@ -141,12 +142,12 @@ static void testRemove() {
     CORE_TEST_EQUAL(0, list.getLength());
 }
 
-void Core::testArrayList() {
+void Core::testArrayList(bool light) {
     testAdd();
     testMultipleAdd();
     testAddReplace();
     testClear();
-    testOverflow();
+    testOverflow(light);
     testCopy();
     testCopyAssignment();
     testMove();

+ 2 - 0
test/modules/ArrayTests.cpp

@@ -1,6 +1,8 @@
 #include "../Tests.hpp"
 #include "core/data/Array.hpp"
 
+template class Core::Array<int, 3>;
+
 static void testToString1() {
     Core::Array<int, 3> a;
     a[0] = 1;

+ 6 - 5
test/modules/BufferTests.cpp

@@ -4,15 +4,16 @@
 static constexpr int SIZE_TYPES =
     sizeof(int) + sizeof(long) + sizeof(float) + sizeof(double);
 
-static void testAdd() {
+static void testAdd(bool light) {
     Core::Buffer buffer(10);
-    for(int i = 0; i < 100000; i++) {
+    int limit = light ? 1000 : 100000;
+    for(int i = 0; i < limit; i++) {
         CORE_TEST_ERROR(buffer.add(5));
         CORE_TEST_ERROR(buffer.add(5L));
         CORE_TEST_ERROR(buffer.add(5.0f));
         CORE_TEST_ERROR(buffer.add(5.0));
     }
-    CORE_TEST_EQUAL(SIZE_TYPES * 100000, buffer.getLength());
+    CORE_TEST_EQUAL(SIZE_TYPES * limit, buffer.getLength());
 }
 
 static void testCopy() {
@@ -60,8 +61,8 @@ static void testMove() {
     CORE_TEST_EQUAL(SIZE_TYPES * 10, buffer2.getLength());
 }
 
-void Core::testBuffer() {
-    testAdd();
+void Core::testBuffer(bool light) {
+    testAdd(light);
     testCopy();
     testMoveConstruct();
     testMove();

+ 4 - 4
test/modules/ClockTests.cpp

@@ -33,12 +33,12 @@ static void testWait(Core::Clock::Nanos wait) {
     CORE_TEST_ERROR(c.wait(wait));
     Core::Clock::Nanos n2 = 0;
     CORE_TEST_ERROR(c.update(n2));
-    CORE_TEST_TRUE(n2 >= wait && n2 <= wait * 11 / 10);
+    CORE_TEST_TRUE(n2 >= wait && n2 <= wait * 12 / 10);
 }
 
-void Core::testClock() {
+void Core::testClock(bool light) {
     testUpdate();
     testUpdatesPerSecond();
-    testWait(50'000'000);
-    testWait(1'300'000'000);
+    testWait(light ? 5'000'000 : 50'000'000);
+    testWait(light ? 50'000'000 : 1'300'000'000);
 }

+ 6 - 5
test/modules/HashMapTests.cpp

@@ -60,12 +60,13 @@ static void testClear() {
     CORE_TEST_FALSE(map.contains(4));
 }
 
-static void testOverflow() {
+static void testOverflow(bool light) {
     IntMap map;
-    for(int i = 0; i < 100000; i++) {
+    int limit = light ? 10000 : 100000;
+    for(int i = 0; i < limit; i++) {
         CORE_TEST_ERROR(map.add(i, i));
     }
-    for(int i = 0; i < 100000; i++) {
+    for(int i = 0; i < limit; i++) {
         CORE_TEST_TRUE(map.contains(i));
     }
 }
@@ -310,13 +311,13 @@ static void testTypes() {
     testType<unsigned long long>();
 }
 
-void Core::testHashMap() {
+void Core::testHashMap(bool light) {
     testAdd();
     testMultipleAdd();
     testSearch();
     testAddReplace();
     testClear();
-    testOverflow();
+    testOverflow(light);
     testEmplace();
     testToString1();
     testToString2();

+ 6 - 5
test/modules/LinkedListTests.cpp

@@ -49,9 +49,10 @@ static void testClear() {
     CORE_TEST_FALSE(list.begin() != list.end());
 }
 
-static void testBigAdd() {
+static void testBigAdd(bool light) {
+    int limit = light ? 10000 : 100000;
     IntList list;
-    for(int i = 0; i < 100000; i++) {
+    for(int i = 0; i < limit; i++) {
         CORE_TEST_ERROR(list.add(i));
     }
     auto iter = list.begin();
@@ -59,7 +60,7 @@ static void testBigAdd() {
         CORE_TEST_EQUAL(i, *iter);
         ++iter;
     }
-    CORE_TEST_EQUAL(100000, list.getLength());
+    CORE_TEST_EQUAL(limit, list.getLength());
 }
 
 static void testCopy() {
@@ -271,12 +272,12 @@ static void testRemoveLast() {
     CORE_TEST_EQUAL(0, list.getLength());
 }
 
-void Core::testLinkedList() {
+void Core::testLinkedList(bool light) {
     testWithoutCopyOrMove();
     testAdd();
     testMultipleAdd();
     testClear();
-    testBigAdd();
+    testBigAdd(light);
     testCopy();
     testMove();
     testMoveAssignment();

+ 6 - 5
test/modules/ListTests.cpp

@@ -52,15 +52,16 @@ static void testShrink() {
     CORE_TEST_EQUAL(3, list[2]);
 }
 
-static void testBigAdd() {
+static void testBigAdd(bool light) {
+    int limit = light ? 10000 : 100000;
     IntList list;
-    for(int i = 0; i < 100000; i++) {
+    for(int i = 0; i < limit; i++) {
         CORE_TEST_ERROR(list.add(i));
     }
     for(int i = 0; i < list.getLength(); i++) {
         CORE_TEST_EQUAL(i, list[i]);
     }
-    CORE_TEST_EQUAL(100000, list.getLength());
+    CORE_TEST_EQUAL(limit, list.getLength());
 }
 
 static void testCopy() {
@@ -181,13 +182,13 @@ static void testDefaultResize() {
     }
 }
 
-void Core::testList() {
+void Core::testList(bool light) {
     testAdd();
     testMultipleAdd();
     testAddReplace();
     testClear();
     testShrink();
-    testBigAdd();
+    testBigAdd(light);
     testCopy();
     testMove();
     testMoveAssignment();

+ 5 - 4
test/modules/MatrixStackTests.cpp

@@ -22,12 +22,13 @@ static void testPop() {
     }
 }
 
-static void testPush() {
+static void testPush(bool light) {
     Matrices stack;
     for(int i = 0; i < 4; i++) {
         CORE_TEST_ERROR(stack.push());
     }
-    for(int i = 0; i < 1000000; i++) {
+    int limit = light ? 10000 : 1000000;
+    for(int i = 0; i < limit; i++) {
         CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, stack.push());
     }
 }
@@ -97,10 +98,10 @@ static void testToString3() {
                      stack);
 }
 
-void Core::testMatrixStack() {
+void Core::testMatrixStack(bool light) {
     testInit();
     testPop();
-    testPush();
+    testPush(light);
     testClear();
     testToString1();
     testToString2();

+ 6 - 5
test/modules/ProbingHashMapTests.cpp

@@ -62,12 +62,13 @@ static void testClear() {
     CORE_TEST_FALSE(map.contains(4));
 }
 
-static void testOverflow() {
+static void testOverflow(bool light) {
+    int limit = light ? 10000 : 100000;
     IntMap map;
-    for(int i = 0; i < 100000; i++) {
+    for(int i = 0; i < limit; i++) {
         CORE_TEST_ERROR(map.add(i, i));
     }
-    for(int i = 0; i < 100000; i++) {
+    for(int i = 0; i < limit; i++) {
         CORE_TEST_TRUE(map.contains(i));
     }
 }
@@ -307,13 +308,13 @@ static void testTypes() {
     testType<unsigned long long>();
 }
 
-void Core::testProbingHashMap() {
+void Core::testProbingHashMap(bool light) {
     testAdd();
     testMultipleAdd();
     testSearch();
     testAddReplace();
     testClear();
-    testOverflow();
+    testOverflow(light);
     testEmplace();
     testToString1();
     testToString2();

+ 39 - 25
test/modules/RandomTests.cpp

@@ -2,75 +2,89 @@
 #include "core/data/Array.hpp"
 #include "core/utils/Random.hpp"
 
-static void testAverage() {
+static void testAverage(bool light) {
+    int limit = light ? 100000 : 1000000;
     Core::Random r(553);
     float sum = 0;
-    for(int i = 0; i < 1000000; i++) {
+    for(int i = 0; i < limit; i++) {
         sum += static_cast<float>(r.next(2, 10));
     }
-    sum /= 1000000.0f;
+    sum /= static_cast<float>(limit);
     CORE_TEST_FLOAT(6.0f, sum, 0.01f);
 }
 
-static void testCoin() {
+static void testCoin(bool light) {
+    int limit = light ? 100000 : 1000000;
     Core::Random r(553);
     Core::Array<int, 2> c(0);
-    for(int i = 0; i < 1000000; i++) {
+    for(int i = 0; i < limit; i++) {
         c[r.nextBool()]++;
     }
-    CORE_TEST_FLOAT(0.0f, static_cast<float>(c[0] - c[1]) / 1000000.0f, 0.001f);
+    CORE_TEST_FLOAT(0.0f,
+                    static_cast<float>(c[0] - c[1]) / static_cast<float>(limit),
+                    0.003f);
 }
 
-static void testDistribution() {
+static void testDistribution(bool light) {
+    int limit = light ? 100000 : 1000000;
     Core::Random r(553);
     Core::Array<int, 102> c(0);
-    for(int i = 0; i < 1000000; i++) {
+    for(int i = 0; i < limit; i++) {
         c[r.next(1, c.getLength() - 2)]++;
     }
     CORE_TEST_EQUAL(0, c[0]);
     CORE_TEST_EQUAL(0, c[c.getLength() - 1]);
     for(int i = 1; i < c.getLength() - 1; i++) {
-        CORE_TEST_FLOAT(0.01f, static_cast<float>(c[i]) / 1000000.0f, 0.001f);
+        CORE_TEST_FLOAT(0.01f,
+                        static_cast<float>(c[i]) / static_cast<float>(limit),
+                        0.001f);
     }
 }
 
-static void testFloatAverage() {
+static void testFloatAverage(bool light) {
+    int limit = light ? 100000 : 1000000;
     Core::Random r(553);
     float sum = 0;
-    for(int i = 0; i < 1000000; i++) {
+    for(int i = 0; i < limit; i++) {
         sum += r.nextFloat();
     }
-    sum /= 1000000.0f;
+    sum /= static_cast<float>(limit);
     CORE_TEST_FLOAT(0.5f, sum, 0.001f);
 }
 
-static void testFloatCoin() {
+static void testFloatCoin(bool light) {
+    int limit = light ? 100000 : 1000000;
     Core::Random r(553);
     Core::Array<int, 2> c(0);
-    for(int i = 0; i < 1000000; i++) {
+    for(int i = 0; i < limit; i++) {
         c[r.nextFloat() < 0.5f]++;
     }
-    CORE_TEST_FLOAT(0.0f, static_cast<float>(c[0] - c[1]) / 1000000.0f, 0.001f);
+    CORE_TEST_FLOAT(0.0f,
+                    static_cast<float>(c[0] - c[1]) / static_cast<float>(limit),
+                    0.003f);
 }
 
-static void testFloatDistribution() {
+static void testFloatDistribution(bool light) {
+    int limit = light ? 100000 : 1000000;
     Core::Random r(553);
     Core::Array<int, 102> c(0);
-    for(int i = 0; i < 1000000; i++) {
+    for(int i = 0; i < limit; i++) {
         c[static_cast<int>(r.nextFloat(1.0f, c.getLength() - 1))]++;
     }
     CORE_TEST_EQUAL(0, c[0]);
     CORE_TEST_EQUAL(0, c[c.getLength() - 1]);
     for(int i = 1; i < c.getLength() - 1; i++) {
-        CORE_TEST_FLOAT(0.01f, static_cast<float>(c[i]) / 1000000.0f, 0.001f);
+        CORE_TEST_FLOAT(0.01f,
+                        static_cast<float>(c[i]) / static_cast<float>(limit),
+                        0.003f);
     }
 }
 
-void Core::testRandom() {
-    testAverage();
-    testCoin();
-    testDistribution();
-    testFloatAverage();
-    testFloatCoin();
-    testFloatDistribution();
+void Core::testRandom(bool light) {
+    testAverage(light);
+    testCoin(light);
+    testDistribution(light);
+    testFloatAverage(light);
+    testFloatCoin(light);
+    testFloatDistribution(light);
 }

+ 5 - 5
test/modules/StackTests.cpp

@@ -51,9 +51,9 @@ static void testToString3() {
 }
 
 template<typename T>
-static void testPop() {
+static void testPop(int amount) {
     T stack;
-    for(int i = 0; i < 100000; i++) {
+    for(int i = 0; i < amount; i++) {
         CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, stack.pop());
     }
 }
@@ -65,10 +65,10 @@ static void testType(int amount) {
     testToString1<T>();
     testToString2<T>();
     testToString3<T>();
-    testPop<T>();
+    testPop<T>(amount);
 }
 
-void Core::testStack() {
-    testType<Core::ListStack<int>>(100000);
+void Core::testStack(bool light) {
+    testType<Core::ListStack<int>>(light ? 10000 : 100000);
     testType<Core::ArrayStack<int, 100>>(100);
 }