Browse Source

Allow setting build profile, fix release build errors

Kajetan Johannes Hammerle 2 months ago
parent
commit
6eff64c7ae

+ 2 - 1
.gitignore

@@ -1,3 +1,4 @@
 .vscode
 build
-install
+install
+profile

+ 6 - 5
include/core/data/BitArray.hpp

@@ -35,12 +35,13 @@ namespace Core {
         template<typename String>
         check_return Error toString(String& s) const {
             CORE_RETURN_ERROR(s.append("["));
-            for(int i = 0; i < length - 1; i++) {
-                CORE_RETURN_ERROR(s.append(get(i)));
-                CORE_RETURN_ERROR(s.append(", "));
-            }
             if(length > 0) {
-                CORE_RETURN_ERROR(s.append(get(length - 1)));
+                int end = length - 1;
+                for(int i = 0; i < end; i++) {
+                    CORE_RETURN_ERROR(s.append(get(i)));
+                    CORE_RETURN_ERROR(s.append(", "));
+                }
+                CORE_RETURN_ERROR(s.append(get(end)));
             }
             return s.append("]");
         }

+ 11 - 7
include/core/data/List.hpp

@@ -68,8 +68,8 @@ namespace Core {
             List copy;
             CORE_RETURN_ERROR(allocate(copy.data, n));
             copy.capacity = n;
-            for(int i = 0; i < length; i++) {
-                copy.unsafeAdd(Core::move(data[i]));
+            for(T& t : *this) {
+                copy.unsafeAdd(Core::move(t));
             }
             swap(copy);
             return Error::NONE;
@@ -92,7 +92,7 @@ namespace Core {
         check_return Error resize(int n, const T& t) {
             if(length < n) {
                 CORE_RETURN_ERROR(reserve(n));
-                for(int i = length; i < n; i++) {
+                for(int i = n - length; i != 0; i--) {
                     unsafeAdd(t);
                 }
             } else if(length > n) {
@@ -107,7 +107,7 @@ namespace Core {
         check_return Error resize(int n) {
             if(length < n) {
                 CORE_RETURN_ERROR(reserve(n));
-                for(int i = length; i < n; i++) {
+                for(int i = n - length; i != 0; i--) {
                     unsafeAdd(T());
                 }
             } else if(length > n) {
@@ -172,10 +172,14 @@ namespace Core {
                 return Error::INVALID_INDEX;
             }
             length--;
-            for(int i = index; i < length; i++) {
-                data[i] = Core::move(data[i + 1]);
+            T* currentT = begin() + index;
+            T* endT = end();
+            while(currentT != endT) {
+                T* nextT = currentT + 1;
+                *currentT = Core::move(*nextT);
+                currentT = nextT;
             }
-            data[length].~T();
+            endT->~T();
             return Error::NONE;
         }
 

+ 8 - 6
include/core/data/RingBuffer.hpp

@@ -103,12 +103,14 @@ namespace Core {
         template<typename String>
         check_return Error toString(String& s) const {
             CORE_RETURN_ERROR(s.append("["));
-            for(int i = 0; i < getLength() - 1; i++) {
-                CORE_RETURN_ERROR(s.append((*this)[i]));
-                CORE_RETURN_ERROR(s.append(", "));
-            }
-            if(getLength() > 0) {
-                CORE_RETURN_ERROR(s.append((*this)[getLength() - 1]));
+            int end = getLength();
+            if(end > 0) {
+                end--;
+                for(int i = 0; i < end; i++) {
+                    CORE_RETURN_ERROR(s.append((*this)[i]));
+                    CORE_RETURN_ERROR(s.append(", "));
+                }
+                CORE_RETURN_ERROR(s.append((*this)[end]));
             }
             return s.append("]");
         }

+ 5 - 1
include/core/data/Stack.hpp

@@ -21,7 +21,11 @@ namespace Core {
             }
 
             check_return Error pop() {
-                return data.removeBySwap(data.getLength() - 1);
+                int index = data.getLength();
+                if(index <= 0) {
+                    return Error::INVALID_STATE;
+                }
+                return data.removeBySwap(index - 1);
             }
 
             bool isEmpty() const {

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

@@ -261,7 +261,7 @@ namespace Core {
         template<int L>
         bool startsWidth(const ArrayString<L, CharType>& other,
                          int from = 0) const {
-            if(from + other.getLength() > length) {
+            if(from > length - other.getLength()) {
                 return false;
             }
             for(int i = 0; i < other.getLength(); i++) {

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

@@ -1,7 +1,7 @@
 #ifndef CORE_NEW_HPP
 #define CORE_NEW_HPP
 
-#include <stddef.h>
+using size_t = decltype(sizeof(0));
 
 void* operator new(size_t bytes) noexcept;
 void* operator new[](size_t bytes) noexcept;

+ 1 - 0
src/BitArray.cpp

@@ -1,6 +1,7 @@
 #include "core/data/BitArray.hpp"
 
 #include "core/math/Math.hpp"
+#include "core/utils/New.hpp"
 
 static int roundUpDivide(int a, int b) {
     if(a % b == 0) {

+ 7 - 5
tasks

@@ -31,6 +31,7 @@ performance=false
 time=false
 install=false
 coverage=false
+profile=$(cat profile)
 
 # parsing
 if [ "$task" = "clean" ]; then
@@ -55,14 +56,14 @@ elif [ "$task" = "time" ]; then
     build=true
     time=true
 elif [ "$task" = "final" ]; then
-    grep -r " class" src | grep -v -E 'final|enum|.git' || true
-    grep -r " struct" src | grep -v -E 'final|enum|.git' || true
+    grep -r " class" src include | grep -v -E 'final|enum|.git' || true
+    grep -r " struct" src include | grep -v -E 'final|enum|.git' || true
     exit 0
 elif [ "$task" = "macro" ]; then
-    grep -r "#define" src | grep -v " CORE" || true
+    grep -r "#define" src include | grep -v " CORE" || true
     exit 0
 elif [ "$task" = "include" ]; then
-    grep -r "#include <" src | grep "\.hpp" || true
+    grep -r "#include <" src include | grep "\.hpp" || true
     exit 0
 else
     echo "unknown task"
@@ -72,7 +73,8 @@ fi
 # task execution
 if $build; then
     if [ ! -e build ]; then 
-        cmake -B build -S . -G Ninja -DCMAKE_INSTALL_PREFIX=./install
+        cmake -B build -S . -G Ninja -DCMAKE_INSTALL_PREFIX=./install \
+            -DCMAKE_BUILD_TYPE=$profile
     fi
     ninja -C build
 fi

+ 1 - 1
test/modules/ComponentsTests.cpp

@@ -198,7 +198,7 @@ static void testOutOfMemory() {
         }
     }
     Core::Fail::leftAllocations = -1;
-    CORE_TEST_TRUE(memFails > 0);
+    CORE_TEST_TRUE(memFails != 0);
 }
 
 static void testConstSearch() {

+ 3 - 5
test/modules/HashMapTests.cpp

@@ -9,8 +9,7 @@ static void testAdd() {
     IntMap map;
     CORE_TEST_ERROR(map.add(5, 4));
     int* value = map.search(5);
-    CORE_TEST_NOT_NULL(value);
-    if(value != nullptr) {
+    if(CORE_TEST_NOT_NULL(value)) {
         CORE_TEST_EQUAL(4, *value);
     }
 }
@@ -47,8 +46,7 @@ static void testAddReplace() {
     CORE_TEST_ERROR(map.add(5, 10));
     CORE_TEST_TRUE(map.contains(5));
     int* a = map.search(5);
-    CORE_TEST_NOT_NULL(a);
-    if(a != nullptr) {
+    if(CORE_TEST_NOT_NULL(a)) {
         CORE_TEST_EQUAL(10, *a);
     }
 }
@@ -329,7 +327,7 @@ static void testOutOfMemory() {
         CORE_TEST_EQUAL(1, *found);
     }
     Core::Fail::leftAllocations = -1;
-    CORE_TEST_TRUE(memFails > 0);
+    CORE_TEST_TRUE(memFails != 0);
 }
 
 void Core::testHashMap(bool light) {

+ 1 - 1
test/modules/LinkedListTests.cpp

@@ -288,7 +288,7 @@ static void testOutOfMemory() {
         CORE_TEST_EQUAL(1, *list.begin());
     }
     Core::Fail::leftAllocations = -1;
-    CORE_TEST_TRUE(memFails > 0);
+    CORE_TEST_TRUE(memFails != 0);
 }
 
 void Core::testLinkedList(bool light) {

+ 1 - 0
test/modules/ListTests.cpp

@@ -157,6 +157,7 @@ static void testRemove() {
     CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.remove(2));
     CORE_TEST_EQUAL(3, list[0]);
     CORE_TEST_EQUAL(2, list[1]);
+    CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.remove(2));
     CORE_TEST_EQUAL(2, list.getLength());
     CORE_TEST_ERROR(list.remove(1));
     CORE_TEST_EQUAL(3, list[0]);

+ 1 - 1
test/modules/ProbingHashMapTests.cpp

@@ -328,7 +328,7 @@ static void testOutOfMemory() {
         CORE_TEST_EQUAL(1, *found);
     }
     Core::Fail::leftAllocations = -1;
-    CORE_TEST_TRUE(memFails > 0);
+    CORE_TEST_TRUE(memFails != 0);
 }
 
 static void testInsertInvalid() {

+ 1 - 1
test/modules/StackTests.cpp

@@ -60,7 +60,7 @@ template<typename T>
 static void testPop(int amount) {
     T stack;
     for(int i = 0; i < amount; i++) {
-        CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, stack.pop());
+        CORE_TEST_EQUAL(Core::Error::INVALID_STATE, stack.pop());
     }
 }