Browse Source

Fix release build again

Kajetan Johannes Hammerle 2 months ago
parent
commit
9ab253e168

+ 13 - 3
CMakeLists.txt

@@ -67,9 +67,19 @@ set(SRC_PERFORMANCE
     "test/Test.cpp"
 )
 
+set(DEBUG_COMPILE "")
+set(DEBUG_LINK "")
+set(LOG_LEVEL 4)
+
+if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
+    set(DEBUG_COMPILE --coverage)
+    set(DEBUG_LINK gcov)
+    set(LOG_LEVEL 4)
+endif()
+
 add_library(core STATIC ${SRC})
 target_compile_options(core PUBLIC
-    --coverage
+    ${DEBUG_COMPILE}
     -fdiagnostics-color=always
     -fno-exceptions
     -fno-rtti
@@ -167,12 +177,12 @@ target_compile_options(core PUBLIC
     -Wzero-as-null-pointer-constant
 )
 target_compile_definitions(core 
-    PUBLIC CORE_LOG_LEVEL=4
+    PUBLIC CORE_LOG_LEVEL=${LOG_LEVEL}
     PRIVATE ERROR_SIMULATOR=true
 )
 target_link_libraries(core 
     PUBLIC -nodefaultlibs c m
-    PRIVATE gcov
+    PRIVATE ${DEBUG_LINK}
 )
 target_sources(core PUBLIC 
     FILE_SET HEADERS

+ 13 - 16
include/core/data/List.hpp

@@ -65,28 +65,14 @@ namespace Core {
             if(n <= capacity) {
                 return Error::NONE;
             }
-            List copy;
-            CORE_RETURN_ERROR(allocate(copy.data, n));
-            copy.capacity = n;
-            for(T& t : *this) {
-                copy.unsafeAdd(Core::move(t));
-            }
-            swap(copy);
-            return Error::NONE;
+            return setSize(n);
         }
 
         check_return Error shrink() {
             if(length == capacity) {
                 return Error::NONE;
             }
-            List copy;
-            CORE_RETURN_ERROR(allocate(copy.data, length));
-            copy.capacity = length;
-            for(int i = 0; i < length; i++) {
-                copy.unsafeAdd(Core::move(data[i]));
-            }
-            swap(copy);
-            return Error::NONE;
+            return setSize(length);
         }
 
         check_return Error resize(int n, const T& t) {
@@ -219,6 +205,17 @@ namespace Core {
         T* unsafeAdd(Args&&... args) {
             return new(data + length++) T(Core::forward<Args>(args)...);
         }
+
+        check_return Error setSize(int n) {
+            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]));
+            }
+            swap(copy);
+            return Error::NONE;
+        }
     };
 }
 

+ 10 - 9
include/core/data/ProbingHashMap.hpp

@@ -164,7 +164,8 @@ namespace Core {
                 return Error::NONE;
             }
             ProbingHashMap<K, V> map;
-            int l = Core::Math::max(1 << Math::roundUpLog2(minCapacity), 8);
+            int l = static_cast<int>(Core::Math::max(
+                1u << static_cast<u32>(Math::roundUpLog2(minCapacity)), 8u));
             CORE_RETURN_ERROR(map.keys.resize(l, emptyValue<K>()));
             map.values = reinterpret_cast<V*>(new AlignedType<V>[l]);
             if(map.values == nullptr) {
@@ -281,11 +282,11 @@ namespace Core {
             int rehashFactor = 2;
             while(true) {
                 CORE_RETURN_ERROR(rehash(entries * rehashFactor + 1));
-                int baseHash = static_cast<int>(hashCode(key) * 514685581u);
-                int end = keys.getLength() - 1;
+                u32 baseHash = hashCode(key) * 514685581u;
+                u32 end = static_cast<u32>(keys.getLength()) - 1;
                 // rehash on bad clustering
-                for(int i = 0; i <= 5; i++) {
-                    int hash = (baseHash + i) & end;
+                for(u32 i = 0; i <= 5; i++) {
+                    int hash = static_cast<int>((baseHash + i) & end);
                     if(keys[hash] == emptyValue<K>() || keys[hash] == key) {
                         slot = hash;
                         return Core::Error::NONE;
@@ -297,10 +298,10 @@ namespace Core {
 
         template<typename Value>
         Value* searchValue(const K& key) const {
-            int baseHash = static_cast<int>(hashCode(key) * 514685581u);
-            int end = keys.getLength() - 1;
-            for(int i = 0; i <= end; i++) [[unlikely]] {
-                int hash = (baseHash + i) & end;
+            u32 baseHash = hashCode(key) * 514685581u;
+            u32 end = static_cast<u32>(keys.getLength()) - 1;
+            for(u32 i = 0; i <= end; i++) [[unlikely]] {
+                int hash = static_cast<int>((baseHash + i) & end);
                 if(keys[hash] == key) [[likely]] {
                     return values + hash;
                 } else if(keys[hash] == emptyValue<K>()) {

+ 7 - 0
src/ErrorSimulator.cpp

@@ -2,9 +2,16 @@
 
 #ifdef ERROR_SIMULATOR
 
+#include "core/utils/Utility.hpp"
+
 bool Core::Fail::realloc = false;
 bool Core::Fail::fileClose = false;
 bool Core::Fail::timeGet = false;
 int Core::Fail::leftAllocations = -1;
 
+bool Core::Fail::freeAndReturn(void* p) {
+    Core::free(p);
+    return true;
+}
+
 #endif

+ 1 - 6
src/ErrorSimulator.hpp

@@ -1,8 +1,6 @@
 #ifndef CORE_ERROR_SIMULATOR_HPP
 #define CORE_ERROR_SIMULATOR_HPP
 
-#include "core/utils/Utility.hpp"
-
 #ifdef ERROR_SIMULATOR
 namespace Core::Fail {
     extern bool realloc;
@@ -10,10 +8,7 @@ namespace Core::Fail {
     extern bool timeGet;
     extern int leftAllocations;
 
-    inline bool freeAndReturn(void* p) {
-        Core::free(p);
-        return true;
-    }
+    bool freeAndReturn(void* p);
 }
 #define CORE_REALLOC_FAIL(pointer)                                             \
     (Core::Fail::realloc && Core::Fail::freeAndReturn(pointer))

+ 0 - 2
tasks

@@ -18,8 +18,6 @@ printHelpExit() {
     exit 0
 }
 
-# TODO gcovr https://gcovr.com/en/5.0/guide.html
-
 task=$1
 if [ -z "$task" ]; then
     printHelpExit

+ 3 - 1
test/Test.hpp

@@ -92,7 +92,9 @@ namespace Core::Test {
 #define CORE_TEST_TRUE(actual) CORE_TEST_EQUAL(true, actual)
 #define CORE_TEST_ERROR(actual) CORE_TEST_EQUAL(Core::Error::NONE, actual)
 #define CORE_TEST_NULL(actual) CORE_TEST_EQUAL(true, actual == nullptr)
-#define CORE_TEST_NOT_NULL(actual) CORE_TEST_EQUAL(true, actual != nullptr)
+// double check to make the null check clear for the compiler
+#define CORE_TEST_NOT_NULL(actual)                                             \
+    (CORE_TEST_EQUAL(true, (actual) != nullptr) && (actual) != nullptr)
 #define CORE_TEST_FLOAT(wanted, actual, error)                                 \
     Core::Test::Internal::checkFloat(__FILE__, __LINE__, wanted, actual, error)
 #define CORE_TEST_VECTOR(wanted, actual)                                       \

+ 3 - 1
test/modules/ListTests.cpp

@@ -1,6 +1,7 @@
 #include "../../src/ErrorSimulator.hpp"
 #include "../Tests.hpp"
 #include "core/data/List.hpp"
+#include "core/math/Math.hpp"
 
 template class Core::List<int>;
 using IntList = Core::List<int>;
@@ -75,7 +76,8 @@ static void testCopy() {
     IntList copy;
     CORE_TEST_ERROR(copy.copyFrom(list));
     CORE_TEST_EQUAL(list.getLength(), copy.getLength());
-    for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
+    int limit = Core::Math::min(copy.getLength(), list.getLength());
+    for(int i = 0; i < limit; i++) {
         CORE_TEST_EQUAL(list[i], copy[i]);
     }
 }