Przeglądaj źródła

Memory allocation failure terminates

Kajetan Johannes Hammerle 2 tygodni temu
rodzic
commit
2c3274dbe6

+ 1 - 0
.gitignore

@@ -5,3 +5,4 @@ profile
 compiler
 .cache
 *.swp
+*.swo

+ 6 - 7
include/core/data/ArrayList.hpp

@@ -1,6 +1,8 @@
 #ifndef CORE_ARRAYLIST_HPP
 #define CORE_ARRAYLIST_HPP
 
+#include <assert.h>
+
 #include "core/utils/AlignedData.hpp"
 #include "core/utils/ArrayString.hpp"
 
@@ -94,20 +96,17 @@ namespace Core {
             length = 0;
         }
 
-        check_return Error removeBySwap(size_t index) {
-            if(index >= length) {
-                return ErrorCode::INVALID_INDEX;
-            }
+        void removeBySwap(size_t index) {
+            assert(index < length);
             length--;
             if(index != length) {
                 begin()[index] = Core::move(begin()[length]);
             }
             begin()[length].~T();
-            return ErrorCode::NONE;
         }
 
-        check_return Error removeLast() {
-            return removeBySwap(length - 1);
+        void removeLast() {
+            removeBySwap(length - 1);
         }
 
         template<typename String>

+ 13 - 17
include/core/data/Components.hpp

@@ -2,6 +2,7 @@
 #define CORE_COMPONENTS_HPP
 
 #include "core/data/HashMap.hpp"
+#include "core/utils/Error.hpp"
 
 namespace Core {
     using Entity = int;
@@ -63,16 +64,9 @@ namespace Core {
             size_t index = components.getLength();
             size_t* indexP = nullptr;
             CORE_RETURN_ERROR(entityToIndex.tryEmplace(indexP, ent, index));
-            Error e = ErrorCode::NONE;
-            if(checkError(e, indexToEntity.add(ent))) {
-                (void)entityToIndex.remove(ent);
-                return e;
-            }
-            if(checkError(e, components.put(t, Core::forward<Args>(args)...))) {
-                (void)entityToIndex.remove(ent);
-                (void)indexToEntity.removeLast();
-            }
-            return e;
+            indexToEntity.add(ent);
+            t = &components.put(Core::forward<Args>(args)...);
+            return ErrorCode::NONE;
         }
 
         template<typename... Args>
@@ -81,21 +75,23 @@ namespace Core {
             return put(t, e, Core::forward<Args>(args)...);
         }
 
-        check_return Error remove(Entity ent) {
+        bool remove(Entity ent) {
             size_t* indexP = entityToIndex.search(ent);
             if(indexP == nullptr) {
-                return ErrorCode::NOT_FOUND;
+                return false;
             }
             size_t lastIndex = components.getLength() - 1;
             size_t index = *indexP;
-            CORE_RETURN_ERROR(entityToIndex.remove(ent));
-            CORE_RETURN_ERROR(components.removeBySwap(index));
+            entityToIndex.remove(ent);
+            components.removeBySwap(index);
             if(index == lastIndex) {
-                return indexToEntity.removeBySwap(index);
+                indexToEntity.removeBySwap(index);
+                return true;
             }
             Entity other = indexToEntity[lastIndex];
-            CORE_RETURN_ERROR(indexToEntity.removeBySwap(index));
-            return entityToIndex.add(other, index);
+            indexToEntity.removeBySwap(index);
+            entityToIndex.add(other, index);
+            return true;
         }
 
         T* search(Entity e) {

+ 24 - 39
include/core/data/HashMap.hpp

@@ -120,89 +120,74 @@ namespace Core {
         check_return Error copyFrom(const HashMap& other) {
             HashMap copy;
             for(const auto& en : other) {
-                CORE_RETURN_ERROR(copy.add(en.getKey(), en.value));
+                copy.add(en.getKey(), en.value);
             }
             swap(copy.nodes, nodes);
             swap(copy.nodePointers, nodePointers);
             return ErrorCode::NONE;
         }
 
-        check_return Error rehash(size_t minCapacity) {
+        void rehash(size_t minCapacity) {
             if(minCapacity <= nodePointers.getLength()) {
-                return ErrorCode::NONE;
+                return;
             }
             HashMap<K, V> map;
-            size_t shifts =
-                Math::roundUpLog2(Core::Math::max(minCapacity, 8lu));
-            if(shifts >= 48) {
-                return ErrorCode::CAPACITY_REACHED;
-            }
-            size_t l = 1lu << shifts;
-            CORE_RETURN_ERROR(map.nodePointers.resize(l));
+            size_t l =
+                1lu << Math::roundUpLog2(Core::Math::max(minCapacity, 8lu));
+            map.nodePointers.resize(l);
             for(NodePointerList& list : nodePointers) {
                 for(NodePointer& n : list) {
                     size_t h = map.hashIndex(n->data.key);
-                    CORE_RETURN_ERROR(map.nodePointers[h].add(n));
+                    map.nodePointers[h].add(n);
                 }
             }
             Core::swap(map.nodePointers, nodePointers);
-            return ErrorCode::NONE;
         }
 
         template<typename... Args>
         check_return Error tryEmplace(V*& v, const K& key, Args&&... args) {
-            CORE_RETURN_ERROR(rehash(nodes.getLength() + 1));
+            rehash(nodes.getLength() + 1);
             size_t h = hashIndex(key);
             v = searchList(key, h);
             if(v != nullptr) {
                 return ErrorCode::EXISTING_KEY;
             }
-            NodePointer np = nullptr;
-            CORE_RETURN_ERROR(nodes.put(np, key, Core::forward<Args>(args)...));
-            Error e = ErrorCode::NONE;
-            if(checkError(e, nodePointers[h].add(np))) {
-                nodes.remove(np);
-                return e;
-            }
+            NodePointer np = nodes.put(key, Core::forward<Args>(args)...);
+            nodePointers[h].add(np);
             v = &(np->data.value);
             return ErrorCode::NONE;
         }
 
         template<typename VA>
-        check_return Error put(V*& v, const K& key, VA&& value) {
-            CORE_RETURN_ERROR(rehash(nodes.getLength() + 1));
+        V& put(const K& key, VA&& value) {
+            rehash(nodes.getLength() + 1);
             size_t h = hashIndex(key);
-            v = searchList(key, h);
+            V* v = searchList(key, h);
             if(v != nullptr) {
                 *v = Core::forward<VA>(value);
-                return ErrorCode::NONE;
+                return *v;
             }
-            NodePointer np = nullptr;
-            CORE_RETURN_ERROR(nodes.put(np, key, Core::forward<VA>(value)));
-            Error e = ErrorCode::NONE;
-            if(checkError(e, nodePointers[h].add(np))) {
-                nodes.remove(np);
-                return e;
-            }
-            v = &(np->data.value);
-            return ErrorCode::NONE;
+            NodePointer np = nodes.put(key, Core::forward<VA>(value));
+            nodePointers[h].add(np);
+            return np->data.value;
         }
 
         template<typename VA>
-        check_return Error add(const K& key, VA&& value) {
-            V* v = nullptr;
-            return put(v, key, Core::forward<VA>(value));
+        HashMap& add(const K& key, VA&& value) {
+            put(key, Core::forward<VA>(value));
+            return *this;
         }
 
-        check_return Error remove(const K& key) {
+        bool remove(const K& key) {
             NodePointerList& list = nodePointers[hashIndex(key)];
             for(size_t i = 0; i < list.getLength(); i++) {
                 if(list[i]->data.key == key) {
                     nodes.remove(list[i]);
-                    return list.removeBySwap(i);
+                    list.removeBySwap(i);
+                    return true;
                 }
             }
-            return ErrorCode::NOT_FOUND;
+            return false;
         }
 
         const V* search(const K& key) const {

+ 13 - 23
include/core/data/LinkedList.hpp

@@ -54,7 +54,11 @@ namespace Core {
         LinkedList() : first(nullptr), last(nullptr), length(0) {
         }
 
-        LinkedList(const LinkedList& other) = delete;
+        LinkedList(const LinkedList& other) : LinkedList() {
+            for(const T& t : other) {
+                add(t);
+            }
+        }
 
         LinkedList(LinkedList&& other) : LinkedList() {
             swap(other);
@@ -64,44 +68,30 @@ namespace Core {
             clear();
         }
 
-        LinkedList& operator=(const LinkedList& other) = delete;
-
-        LinkedList& operator=(LinkedList&& other) {
+        LinkedList& operator=(LinkedList other) {
             swap(other);
             return *this;
         }
 
-        check_return Error copyFrom(const LinkedList& other) {
-            LinkedList copy;
-            for(const T& t : other) {
-                CORE_RETURN_ERROR(copy.add(t));
-            }
-            swap(copy);
-            return ErrorCode::NONE;
-        }
-
         template<typename... Args>
-        check_return Error put(Node*& n, Args&&... args) {
-            n = new(noThrow) Node(Core::forward<Args>(args)...);
-            if(n == nullptr) {
-                return ErrorCode::OUT_OF_MEMORY;
-            }
+        Node* put(Args&&... args) {
+            Node* n = new(noThrow) Node(Core::forward<Args>(args)...);
             length++;
             if(first == nullptr) {
                 first = n;
                 last = n;
-                return ErrorCode::NONE;
+                return n;
             }
             last->next = n;
             n->previous = last;
             last = n;
-            return ErrorCode::NONE;
+            return n;
         }
 
         template<typename... Args>
-        check_return Error add(Args&&... args) {
-            Node* n = nullptr;
-            return put(n, Core::forward<Args>(args)...);
+        LinkedList& add(Args&&... args) {
+            put(Core::forward<Args>(args)...);
+            return *this;
         }
 
         Iterator begin() {

+ 42 - 63
include/core/data/List.hpp

@@ -1,10 +1,11 @@
 #ifndef CORE_LIST_HPP
 #define CORE_LIST_HPP
 
+#include <assert.h>
+
 #include "core/math/Math.hpp"
 #include "core/utils/AlignedData.hpp"
 #include "core/utils/ArrayString.hpp"
-#include "core/utils/Error.hpp"
 #include "core/utils/New.hpp"
 
 namespace Core {
@@ -18,7 +19,13 @@ namespace Core {
         List() : length(0), capacity(0), data(nullptr) {
         }
 
-        List(const List& other) = delete;
+        List(const List& other)
+            : length(0), capacity(other.capacity),
+              data(allocate(other.capacity)) {
+            for(const T& t : other) {
+                unsafeAdd(t);
+            }
+        }
 
         List(List&& other) : List() {
             swap(other);
@@ -29,24 +36,11 @@ namespace Core {
             delete[] reinterpret_cast<AlignedType<T>*>(data);
         }
 
-        List& operator=(const List& other) = delete;
-
-        List& operator=(List&& other) {
+        List& operator=(List other) {
             swap(other);
             return *this;
         }
 
-        check_return Error copyFrom(const List& other) {
-            List copy;
-            CORE_RETURN_ERROR(allocate(copy.data, other.capacity));
-            copy.capacity = other.capacity;
-            for(size_t i = 0; i < other.length; i++) {
-                copy.unsafeAdd(other[i]);
-            }
-            swap(copy);
-            return ErrorCode::NONE;
-        }
-
         T* begin() {
             return data;
         }
@@ -63,23 +57,21 @@ namespace Core {
             return data + length;
         }
 
-        check_return Error reserve(size_t n) {
-            if(n <= capacity) {
-                return ErrorCode::NONE;
+        void reserve(size_t n) {
+            if(n > capacity) {
+                setSize(n);
             }
-            return setSize(n);
         }
 
-        check_return Error shrink() {
-            if(length == capacity) {
-                return ErrorCode::NONE;
+        void shrink() {
+            if(length != capacity) {
+                setSize(length);
             }
-            return setSize(length);
         }
 
-        check_return Error resize(size_t n, const T& t) {
+        void resize(size_t n, const T& t) {
             if(length < n) {
-                CORE_RETURN_ERROR(reserve(n));
+                reserve(n);
                 for(size_t i = n - length; i != 0; i--) {
                     unsafeAdd(t);
                 }
@@ -89,12 +81,11 @@ namespace Core {
                 }
                 length = n;
             }
-            return ErrorCode::NONE;
         }
 
-        check_return Error resize(size_t n) {
+        void resize(size_t n) {
             if(length < n) {
-                CORE_RETURN_ERROR(reserve(n));
+                reserve(n);
                 for(size_t i = n - length; i != 0; i--) {
                     unsafeAdd(T());
                 }
@@ -104,20 +95,18 @@ namespace Core {
                 }
                 length = n;
             }
-            return ErrorCode::NONE;
         }
 
         template<typename... Args>
-        check_return Error put(T*& t, Args&&... args) {
-            CORE_RETURN_ERROR(ensureCapacity());
-            t = unsafeAdd(Core::forward<Args>(args)...);
-            return ErrorCode::NONE;
+        T& put(Args&&... args) {
+            ensureCapacity();
+            return *unsafeAdd(Core::forward<Args>(args)...);
         }
 
         template<typename... Args>
-        check_return Error add(Args&&... args) {
-            T* t = nullptr;
-            return put(t, Core::forward<Args>(args)...);
+        List& add(Args&&... args) {
+            put(Core::forward<Args>(args)...);
+            return *this;
         }
 
         T& operator[](size_t index) {
@@ -143,22 +132,17 @@ namespace Core {
             length = 0;
         }
 
-        check_return Error removeBySwap(size_t index) {
-            if(index >= length) {
-                return ErrorCode::INVALID_INDEX;
-            }
+        void removeBySwap(size_t index) {
+            assert(index < length);
             length--;
             if(index != length) {
                 data[index] = Core::move(data[length]);
             }
             data[length].~T();
-            return ErrorCode::NONE;
         }
 
-        check_return Error remove(size_t index) {
-            if(index >= length) {
-                return ErrorCode::INVALID_INDEX;
-            }
+        void remove(size_t index) {
+            assert(index < length);
             length--;
             T* currentT = begin() + index;
             T* endT = end();
@@ -168,11 +152,10 @@ namespace Core {
                 currentT = nextT;
             }
             endT->~T();
-            return ErrorCode::NONE;
         }
 
-        check_return Error removeLast() {
-            return removeBySwap(length - 1);
+        void removeLast() {
+            removeBySwap(length - 1);
         }
 
         template<typename String>
@@ -187,20 +170,17 @@ namespace Core {
         }
 
     private:
-        static Error allocate(T*& t, size_t n) {
-            if(n > (1lu << 48)) {
-                return ErrorCode::CAPACITY_REACHED;
-            } else if(n <= 0) {
-                return ErrorCode::NONE;
+        static T* allocate(size_t n) {
+            if(n <= 0) {
+                return nullptr;
             }
-            t = reinterpret_cast<T*>(new(noThrow) AlignedType<T>[n]);
-            return t == nullptr ? ErrorCode::OUT_OF_MEMORY : ErrorCode::NONE;
+            return reinterpret_cast<T*>(new(noThrow) AlignedType<T>[n]);
         }
 
-        check_return Error ensureCapacity() {
-            return length < capacity
-                       ? ErrorCode::NONE
-                       : reserve(capacity + Math::max(4lu, capacity / 4));
+        void ensureCapacity() {
+            if(length >= capacity) {
+                reserve(capacity + Math::max(4lu, capacity / 4));
+            }
         }
 
         // does not check for capacity
@@ -209,15 +189,14 @@ namespace Core {
             return new(data + length++) T(Core::forward<Args>(args)...);
         }
 
-        check_return Error setSize(size_t n) {
+        void setSize(size_t n) {
             List copy;
-            CORE_RETURN_ERROR(allocate(copy.data, n));
+            copy.data = allocate(n);
             copy.capacity = n;
             for(size_t i = 0; i < length; i++) {
                 copy.unsafeAdd(Core::move(data[i]));
             }
             swap(copy);
-            return ErrorCode::NONE;
         }
     };
 }

+ 50 - 54
include/core/data/ProbingHashMap.hpp

@@ -7,6 +7,7 @@
 #include "core/utils/Error.hpp"
 #include "core/utils/HashCode.hpp"
 #include "core/utils/Logger.hpp"
+#include "core/utils/Meta.hpp"
 #include "core/utils/New.hpp"
 #include "core/utils/Types.hpp"
 
@@ -27,7 +28,7 @@ namespace Core {
             }
 
             template<typename String>
-            check_return Error toString(String& s) const {
+            CError toString(String& s) const {
                 CORE_RETURN_ERROR(s.append(key));
                 CORE_RETURN_ERROR(s.append(" = "));
                 return s.append(value);
@@ -123,17 +124,21 @@ namespace Core {
             IteratorAdapter<const ProbingHashMap, ConstKeyIterator>;
 
     private:
-        List<K> keys;
-        V* values;
-        size_t entries;
+        List<K> keys{};
+        V* values = nullptr;
+        V* invalidKeyValue = nullptr;
+        size_t entries = 0;
 
     public:
-        ProbingHashMap() : keys(), values(nullptr), entries(0) {
-        }
+        ProbingHashMap() = default;
 
-        ProbingHashMap(const ProbingHashMap& other) = delete;
+        ProbingHashMap(const ProbingHashMap& other) {
+            for(const auto& e : other) {
+                add(e.getKey(), e.value);
+            }
+        }
 
-        ProbingHashMap(ProbingHashMap&& other) : ProbingHashMap() {
+        ProbingHashMap(ProbingHashMap&& other) {
             swap(other);
         }
 
@@ -144,72 +149,62 @@ namespace Core {
                 }
             }
             delete[] reinterpret_cast<AlignedType<V>*>(values);
+            delete invalidKeyValue;
         }
 
-        ProbingHashMap& operator=(const ProbingHashMap& other) = delete;
-
-        ProbingHashMap& operator=(ProbingHashMap&& other) {
+        ProbingHashMap& operator=(ProbingHashMap other) {
             swap(other);
             return *this;
         }
 
-        check_return Error copyFrom(const ProbingHashMap& other) {
-            ProbingHashMap copy;
-            for(const auto& e : other) {
-                CORE_RETURN_ERROR(copy.add(e.getKey(), e.value));
-            }
-            swap(copy);
-            return ErrorCode::NONE;
-        }
-
-        check_return Error rehash(size_t minCapacity) {
+        void rehash(size_t minCapacity) {
             if(minCapacity <= keys.getLength()) {
-                return ErrorCode::NONE;
+                return;
             }
             ProbingHashMap<K, V> map;
-            size_t shifts =
-                Math::roundUpLog2(Core::Math::max(minCapacity, 8lu));
-            if(shifts >= 48) {
-                return ErrorCode::CAPACITY_REACHED;
-            }
-            size_t l = 1lu << shifts;
-            CORE_RETURN_ERROR(map.keys.resize(l, emptyValue<K>()));
+            size_t l =
+                1lu << Math::roundUpLog2(Core::Math::max(minCapacity, 8lu));
+            map.keys.resize(l, emptyValue<K>());
             map.values = reinterpret_cast<V*>(new(noThrow) AlignedType<V>[l]);
-            if(map.values == nullptr) {
-                return ErrorCode::OUT_OF_MEMORY;
-            }
             for(size_t i = 0; i < keys.getLength(); i++) {
                 if(keys[i] != emptyValue<K>()) {
-                    CORE_RETURN_ERROR(map.add(keys[i], values[i]));
+                    map.add(keys[i], values[i]);
                 }
             }
             swap(map);
-            return ErrorCode::NONE;
         }
 
         template<typename... Args>
-        check_return Error tryEmplace(V*& v, const K& key, Args&&... args) {
+        bool tryEmplace(V*& v, const K& key, Args&&... args) {
             if(key == emptyValue<K>()) {
-                return ErrorCode::INVALID_ARGUMENT;
+                if(invalidKeyValue != nullptr) {
+                    return false;
+                }
+                invalidKeyValue = new(noThrow) V(Core::forward<Args>(args)...);
+                v = invalidKeyValue;
+                return true;
             }
-            size_t index = 0;
-            CORE_RETURN_ERROR(searchSlot(index, key));
+            size_t index = searchSlot(key);
             if(keys[index] == key) {
-                return ErrorCode::EXISTING_KEY;
+                return false;
             }
             keys[index] = key;
             v = new(values + index) V(Core::forward<Args>(args)...);
             entries++;
-            return ErrorCode::NONE;
+            return true;
         }
 
         template<typename VA>
-        check_return Error put(V*& v, const K& key, VA&& value) {
+        V& put(const K& key, VA&& value) {
             if(key == emptyValue<K>()) {
-                return ErrorCode::INVALID_ARGUMENT;
+                if(invalidKeyValue != nullptr) {
+                    *invalidKeyValue = Core::forward<VA>(value);
+                } else {
+                    invalidKeyValue = new(noThrow) V(Core::forward<VA>(value));
+                }
+                return *invalidKeyValue;
             }
-            size_t index = 0;
-            CORE_RETURN_ERROR(searchSlot(index, key));
+            size_t index = searchSlot(key);
             if(keys[index] == key) {
                 values[index] = Core::forward<VA>(value);
             } else {
@@ -217,14 +212,13 @@ namespace Core {
                 entries++;
             }
             keys[index] = key;
-            v = reinterpret_cast<V*>(values + index);
-            return ErrorCode::NONE;
+            return values[index];
         }
 
         template<typename VA>
-        check_return Error add(const K& key, VA&& value) {
-            V* v = nullptr;
-            return put(v, key, Core::forward<VA>(value));
+        ProbingHashMap& add(const K& key, VA&& value) {
+            put(key, Core::forward<VA>(value));
+            return *this;
         }
 
         const V* search(const K& key) const {
@@ -285,18 +279,17 @@ namespace Core {
         }
 
     private:
-        check_return Error searchSlot(size_t& slot, const K& key) {
+        size_t searchSlot(const K& key) {
             size_t rehashFactor = 2;
             while(true) {
-                CORE_RETURN_ERROR(rehash(entries * rehashFactor + 1));
+                rehash(entries * rehashFactor + 1);
                 size_t baseHash = hashCode(key) * 514685581u;
                 size_t end = keys.getLength() - 1;
                 // rehash on bad clustering
                 for(size_t i = 0; i <= 5; i++) {
                     size_t hash = (baseHash + i) & end;
                     if(keys[hash] == emptyValue<K>() || keys[hash] == key) {
-                        slot = hash;
-                        return Core::ErrorCode::NONE;
+                        return hash;
                     }
                 }
                 rehashFactor *= 2;
@@ -305,9 +298,12 @@ namespace Core {
 
         template<typename Value>
         Value* searchValue(const K& key) const {
+            if(key == Core::emptyValue<K>()) {
+                return invalidKeyValue;
+            }
             if(keys.getLength() != 0) {
                 size_t baseHash = hashCode(key) * 514685581u;
-                size_t end = static_cast<u64>(keys.getLength()) - 1;
+                size_t end = keys.getLength() - 1;
                 for(size_t i = 0; i <= end; i++) [[unlikely]] {
                     size_t hash = (baseHash + i) & end;
                     if(keys[hash] == key) [[likely]] {

+ 13 - 7
include/core/data/Stack.hpp

@@ -3,6 +3,8 @@
 
 #include "core/data/ArrayList.hpp"
 #include "core/data/List.hpp"
+#include "core/utils/Error.hpp"
+#include "core/utils/Meta.hpp"
 
 namespace Core {
     namespace Internal {
@@ -13,19 +15,21 @@ namespace Core {
         public:
             template<typename... Args>
             check_return Error push(Args&&... args) {
-                return data.add(Core::forward<Args>(args)...);
+                if constexpr(Core::IsSame<S, List<T>>) {
+                    data.add(Core::forward<Args>(args)...);
+                    return ErrorCode::NONE;
+                } else {
+                    return data.add(Core::forward<Args>(args)...);
+                }
             }
 
             void clear() {
                 data.clear();
             }
 
-            check_return Error pop() {
-                size_t index = data.getLength();
-                if(index <= 0) {
-                    return ErrorCode::INVALID_STATE;
-                }
-                return data.removeBySwap(index - 1);
+            void pop() {
+                assert(data.getLength() > 0);
+                data.removeBySwap(data.getLength() - 1);
             }
 
             bool isEmpty() const {
@@ -33,10 +37,12 @@ namespace Core {
             }
 
             T& peek() {
+                assert(data.getLength() > 0);
                 return data[data.getLength() - 1];
             }
 
             const T& peek() const {
+                assert(data.getLength() > 0);
                 return data[data.getLength() - 1];
             }
 

+ 4 - 4
include/core/math/MatrixStack.hpp

@@ -14,10 +14,8 @@ namespace Core {
             (void)stack.add(Matrix());
         }
 
-        check_return Error pop() {
-            if(stack.getLength() <= 1) {
-                return ErrorCode::INVALID_STATE;
-            }
+        void pop() {
+            assert(stack.getLength() > 0);
             return stack.removeLast();
         }
 
@@ -26,10 +24,12 @@ namespace Core {
         }
 
         Matrix& peek() {
+            assert(stack.getLength() > 0);
             return stack[stack.getLength() - 1];
         }
 
         const Matrix& peek() const {
+            assert(stack.getLength() > 0);
             return stack[stack.getLength() - 1];
         }
 

+ 5 - 1
include/core/utils/Utility.hpp

@@ -40,7 +40,11 @@ namespace Core {
     void print(int c);
     void print(const char* s);
     void printLine(const char* s);
-    CError reallocate(char*& p, size_t n);
+
+    using OutOfMemoryHandler = void (*)(void*);
+    void setOutOfMemoryHandler(OutOfMemoryHandler h, void* data);
+    void* allocate(size_t n);
+    void* reallocate(void* p, size_t n);
 }
 
 #endif

+ 2 - 2
performance/Main.cpp

@@ -52,7 +52,7 @@ static Nanos testEmptySearch(const Map& m) {
 template<typename Map>
 static void fillOrder(Map& m) {
     for(int i = 0; i < 10000; i++) {
-        CORE_TEST_ERROR(m.add(i, i * i));
+        m.add(i, i * i);
     }
 }
 
@@ -61,7 +61,7 @@ static void fillChaos(Map& m) {
     Core::Random random(0);
     for(int i = 0; i < 10000; i++) {
         int r = random.nextI32(0, 9999);
-        CORE_TEST_ERROR(m.add(r, r * r));
+        m.add(r, r * r);
     }
 }
 

+ 0 - 3
src/BitArray.cpp

@@ -142,9 +142,6 @@ check_return Core::Error Core::BitArray::resize(u64 newLength, u64 newBits) {
     }
     u64 arrayLength = getArrayLength(newLength, newBits);
     u64* newData = new(noThrow) u64[arrayLength];
-    if(newData == nullptr) {
-        return ErrorCode::OUT_OF_MEMORY;
-    }
     memset(newData, 0, arrayLength * sizeof(int));
 
     u64 end = Math::min(getLength(), newLength);

+ 1 - 1
src/Buffer.cpp

@@ -33,7 +33,7 @@ Core::Error Core::Buffer::add(const void* data, size_t size) {
         while(length + size > capacity) {
             capacity += Math::max<size_t>(4, capacity / 4);
         }
-        CORE_RETURN_ERROR(reallocate(buffer, capacity));
+        buffer = static_cast<char*>(reallocate(buffer, capacity));
     }
     memcpy(buffer + length, data, size);
     length += size;

+ 1 - 11
src/ErrorSimulator.cpp

@@ -1,18 +1,8 @@
 #ifdef ERROR_SIMULATOR
 #include "ErrorSimulator.hpp"
 
-#include <stdlib.h>
-
-#include "core/utils/Utility.hpp"
-
-bool Core::Fail::realloc = false;
+bool Core::Fail::alloc = false;
 bool Core::Fail::fileClose = false;
 bool Core::Fail::timeGet = false;
-int Core::Fail::leftAllocations = -1;
-
-bool Core::Fail::freeAndReturn(void* p) {
-    free(p);
-    return true;
-}
 
 #endif

+ 3 - 7
src/ErrorSimulator.hpp

@@ -3,19 +3,15 @@
 
 #ifdef ERROR_SIMULATOR
 namespace Core::Fail {
-    extern bool realloc;
+    extern bool alloc;
     extern bool fileClose;
     extern bool timeGet;
-    extern int leftAllocations;
-
-    bool freeAndReturn(void* p);
 }
-#define CORE_REALLOC_FAIL(pointer)                                             \
-    (Core::Fail::realloc && Core::Fail::freeAndReturn(pointer))
+#define CORE_ALLOC_FAIL Core::Fail::alloc
 #define CORE_FILE_CLOSE_FAIL Core::Fail::fileClose
 #define CORE_TIME_GET_FAIL Core::Fail::timeGet
 #else
-#define CORE_REALLOC_FAIL(...) false
+#define CORE_ALLOC_FAIL(...) false
 #define CORE_FILE_CLOSE_FAIL false
 #define CORE_TIME_GET_FAIL false
 #endif

+ 3 - 17
src/New.cpp

@@ -2,28 +2,14 @@
 
 #include <stdlib.h>
 
-#include "ErrorSimulator.hpp"
+#include "core/utils/Utility.hpp"
 
 void* operator new(size_t bytes, const NoThrow&) noexcept {
-#ifdef ERROR_SIMULATOR
-    if(Core::Fail::leftAllocations > 0) {
-        Core::Fail::leftAllocations--;
-    } else if(Core::Fail::leftAllocations == 0) {
-        return nullptr;
-    }
-#endif
-    return malloc(bytes);
+    return Core::allocate(bytes);
 }
 
 void* operator new[](size_t bytes, const NoThrow&) noexcept {
-#ifdef ERROR_SIMULATOR
-    if(Core::Fail::leftAllocations > 0) {
-        Core::Fail::leftAllocations--;
-    } else if(Core::Fail::leftAllocations == 0) {
-        return nullptr;
-    }
-#endif
-    return malloc(bytes);
+    return Core::allocate(bytes);
 }
 
 void operator delete(void* p) noexcept {

+ 44 - 8
src/Utility.cpp

@@ -10,6 +10,8 @@
 
 static Core::ExitHandler exitHandler = nullptr;
 static void* exitData = nullptr;
+static Core::OutOfMemoryHandler outOfMemoryHandler = nullptr;
+static void* outOfMemoryData = nullptr;
 
 void Core::exitWithHandler(const char* file, int line, int value) {
     if(value != 0) {
@@ -68,16 +70,50 @@ void Core::printLine(const char* s) {
     }
 }
 
-Core::Error Core::reallocate(char*& p, size_t n) {
-    if(n <= 0) {
+void Core::setOutOfMemoryHandler(OutOfMemoryHandler h, void* data) {
+    outOfMemoryHandler = h;
+    outOfMemoryData = data;
+}
+
+void* Core::allocate(size_t n) {
+    void* p = malloc(n);
+#ifdef ERROR_SIMULATOR
+    if(CORE_ALLOC_FAIL && p != nullptr) {
         free(p);
         p = nullptr;
-        return ErrorCode::NONE;
     }
-    char* newP = static_cast<char*>(realloc(p, n));
-    if(newP == nullptr || CORE_REALLOC_FAIL(newP)) {
-        return ErrorCode::OUT_OF_MEMORY;
+#endif
+    while(p == nullptr && outOfMemoryHandler != nullptr) {
+        outOfMemoryHandler(outOfMemoryData);
+        p = malloc(n);
+    }
+    if(p == nullptr) {
+        CORE_EXIT(static_cast<int>(ErrorCode::OUT_OF_MEMORY));
+    }
+    return p;
+}
+
+void* Core::reallocate(void* oldP, size_t n) {
+    if(n <= 0) {
+        free(oldP);
+        return nullptr;
+    }
+    void* p = realloc(oldP, n);
+#ifdef ERROR_SIMULATOR
+    if(CORE_ALLOC_FAIL && p != nullptr) {
+        oldP = p;
+        p = nullptr;
+    }
+#endif
+    // this double check is to prevent the compiler from complaining
+    if(p == nullptr) {
+        while(p == nullptr && outOfMemoryHandler != nullptr) {
+            outOfMemoryHandler(outOfMemoryData);
+            p = realloc(oldP, n);
+        }
+    }
+    if(p == nullptr) {
+        CORE_EXIT(static_cast<int>(ErrorCode::OUT_OF_MEMORY));
     }
-    p = newP;
-    return ErrorCode::NONE;
+    return p;
 }

BIN
test/.Test.hpp.swo


+ 5 - 8
test/Main.cpp

@@ -21,37 +21,34 @@ static void onExit(int code, void* data) {
 int main(int argAmount, const char** args) {
     setlocale(LC_ALL, "en_US.utf8");
     bool light = false;
-    bool outOfMemoryTest = true;
     for(int i = 0; i < argAmount; i++) {
         if(strcmp(args[i], "light") == 0) {
             light = true;
-        } else if(strcmp(args[i], "valgrind") == 0) {
-            outOfMemoryTest = false;
         }
     }
     Core::testArrayList(light);
     Core::testArrayString();
     Core::testArray();
-    Core::testBitArray(outOfMemoryTest);
+    Core::testBitArray();
     Core::testBox();
     Core::testBuffer(light);
     Core::testBufferedValue();
     Core::testClock(light);
     Core::testColor();
-    Core::testComponents(outOfMemoryTest);
+    Core::testComponents();
     Core::testError();
     Core::testFileReader();
     Core::testFrustum();
     Core::testHashedString();
-    Core::testHashMap(light, outOfMemoryTest);
-    Core::testLinkedList(light, outOfMemoryTest);
+    Core::testHashMap(light);
+    Core::testLinkedList(light);
     Core::testList(light);
     Core::testMath();
     Core::testMatrixStack(light);
     Core::testMatrix();
     Core::testNew();
     Core::testPlane();
-    Core::testProbingHashMap(light, outOfMemoryTest);
+    Core::testProbingHashMap(light);
     Core::testQuaternion();
     Core::testRandom(light);
     Core::testRingBuffer();

+ 5 - 5
test/Tests.hpp

@@ -7,26 +7,26 @@ namespace Core {
     void testArrayList(bool light);
     void testArrayString();
     void testArray();
-    void testBitArray(bool outOfMemoryTest);
+    void testBitArray();
     void testBox();
     void testBuffer(bool light);
     void testBufferedValue();
     void testClock(bool light);
     void testColor();
-    void testComponents(bool outOfMemoryTest);
+    void testComponents();
     void testError();
     void testFileReader();
     void testFrustum();
     void testHashedString();
-    void testHashMap(bool light, bool outOfMemoryTest);
-    void testLinkedList(bool light, bool outOfMemoryTest);
+    void testHashMap(bool light);
+    void testLinkedList(bool light);
     void testList(bool light);
     void testMath();
     void testMatrixStack(bool light);
     void testMatrix();
     void testNew();
     void testPlane();
-    void testProbingHashMap(bool light, bool outOfMemoryTest);
+    void testProbingHashMap(bool light);
     void testQuaternion();
     void testRandom(bool light);
     void testRingBuffer();

+ 3 - 9
test/modules/ArrayListTests.cpp

@@ -132,20 +132,14 @@ static void testRemove() {
     CORE_TEST_ERROR(list.add(4u));
     CORE_TEST_ERROR(list.add(3u));
     CORE_TEST_ERROR(list.add(2u));
-    CORE_TEST_ERROR(list.removeBySwap(0));
+    list.removeBySwap(0);
     CORE_TEST_EQUAL(2, list[0]);
     CORE_TEST_EQUAL(3, list[1]);
     CORE_TEST_EQUAL(2, list.getLength());
-    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_INDEX, list.removeBySwap(4));
-    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_INDEX, list.removeBySwap(3654));
-    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_INDEX, list.removeBySwap(756756));
-    CORE_TEST_EQUAL(2, list[0]);
-    CORE_TEST_EQUAL(3, list[1]);
-    CORE_TEST_EQUAL(2, list.getLength());
-    CORE_TEST_ERROR(list.removeBySwap(1));
+    list.removeBySwap(1);
     CORE_TEST_EQUAL(2, list[0]);
     CORE_TEST_EQUAL(1, list.getLength());
-    CORE_TEST_ERROR(list.removeBySwap(0));
+    list.removeBySwap(0);
     CORE_TEST_EQUAL(0, list.getLength());
 }
 

+ 1 - 20
test/modules/BitArrayTests.cpp

@@ -143,22 +143,7 @@ static void testInvalidArgument() {
     CORE_TEST_EQUAL(Core::ErrorCode::INVALID_ARGUMENT, bits.resize(5, 68));
 }
 
-static void testOutOfMemory() {
-#ifdef ERROR_SIMULATOR
-    Core::BitArray bits;
-    Core::Fail::leftAllocations = 0;
-    CORE_TEST_EQUAL(Core::ErrorCode::OUT_OF_MEMORY, bits.resize(8, 4));
-    Core::Fail::leftAllocations = -1;
-#endif
-}
-
-static void testOutOfMemory2() {
-    Core::BitArray bits;
-    CORE_TEST_EQUAL(Core::ErrorCode::OUT_OF_MEMORY,
-                    bits.resize(0x01FF'FFFF'FFFF'FFFF, 64));
-}
-
-void Core::testBitArray(bool outOfMemoryTest) {
+void Core::testBitArray() {
     testSetRead();
     testOutOfBoundsSetRead();
     testBigSetRead();
@@ -171,8 +156,4 @@ void Core::testBitArray(bool outOfMemoryTest) {
     testResizeExact();
     testMoveAssignment();
     testInvalidArgument();
-    if(outOfMemoryTest) {
-        testOutOfMemory();
-    }
-    testOutOfMemory2();
 }

+ 7 - 27
test/modules/ComponentsTests.cpp

@@ -145,12 +145,12 @@ static void testRemove() {
     CORE_TEST_ERROR(c.add(5, 20));
     CORE_TEST_ERROR(c.add(10, 30));
 
-    CORE_TEST_EQUAL(Core::ErrorCode::NOT_FOUND, c.remove(20));
-    CORE_TEST_ERROR(c.remove(5));
-    CORE_TEST_EQUAL(Core::ErrorCode::NOT_FOUND, c.remove(30));
+    CORE_TEST_FALSE(c.remove(20));
+    CORE_TEST_TRUE(c.remove(5));
+    CORE_TEST_FALSE(c.remove(30));
 
     CORE_TEST_ERROR(c.add(20, 40));
-    CORE_TEST_ERROR(c.remove(20));
+    CORE_TEST_TRUE(c.remove(20));
 
     int* i1 = c.search(1);
     int* i2 = c.search(5);
@@ -165,7 +165,7 @@ static void testRemove() {
         CORE_TEST_EQUAL(30, *i3);
     }
 
-    CORE_TEST_ERROR(c.remove(10));
+    CORE_TEST_TRUE(c.remove(10));
 
     i1 = c.search(1);
     i2 = c.search(5);
@@ -179,30 +179,13 @@ static void testRemove() {
         CORE_TEST_EQUAL(10, *i1);
     }
 
-    CORE_TEST_ERROR(c.remove(1));
+    CORE_TEST_TRUE(c.remove(1));
 
     CORE_TEST_NULL(c.search(1));
     CORE_TEST_NULL(c.search(5));
     CORE_TEST_NULL(c.search(10));
 }
 
-static void testOutOfMemory() {
-#ifdef ERROR_SIMULATOR
-    IntComponent c;
-    int* i1 = nullptr;
-    int memFails = 0;
-    for(int i = 0; i < 40; i++) {
-        Core::Fail::leftAllocations = 2;
-        Core::Error e = c.put(i1, 1, 10);
-        if(e == Core::ErrorCode::OUT_OF_MEMORY) {
-            memFails++;
-        }
-    }
-    Core::Fail::leftAllocations = -1;
-    CORE_TEST_TRUE(memFails != 0);
-#endif
-}
-
 static void testConstSearch() {
     IntComponent c;
     int* i = nullptr;
@@ -215,13 +198,10 @@ static void testConstSearch() {
     CORE_TEST_NULL(cc.search(2));
 }
 
-void Core::testComponents(bool outOfMemoryTest) {
+void Core::testComponents() {
     testAddForEach();
     testAddConstForEach();
     testAddComponentForEach();
     testRemove();
-    if(outOfMemoryTest) {
-        testOutOfMemory();
-    }
     testConstSearch();
 }

+ 18 - 62
test/modules/HashMapTests.cpp

@@ -7,7 +7,7 @@ using IntMap = Core::HashMap<int, int>;
 
 static void testAdd() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(5, 4));
+    map.add(5, 4);
     int* value = map.search(5);
     if(CORE_TEST_NOT_NULL(value)) {
         CORE_TEST_EQUAL(4, *value);
@@ -16,9 +16,7 @@ static void testAdd() {
 
 static void testMultipleAdd() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(5, 4));
-    CORE_TEST_ERROR(map.add(10, 3));
-    CORE_TEST_ERROR(map.add(15, 2));
+    map.add(5, 4).add(10, 3).add(15, 2);
     CORE_TEST_TRUE(map.contains(5));
     CORE_TEST_TRUE(map.contains(10));
     CORE_TEST_TRUE(map.contains(15));
@@ -42,8 +40,7 @@ static void testSearch() {
 
 static void testAddReplace() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(5, 4));
-    CORE_TEST_ERROR(map.add(5, 10));
+    map.add(5, 4).add(5, 10);
     CORE_TEST_TRUE(map.contains(5));
     int* a = map.search(5);
     if(CORE_TEST_NOT_NULL(a)) {
@@ -53,8 +50,7 @@ static void testAddReplace() {
 
 static void testClear() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(5, 4));
-    CORE_TEST_ERROR(map.add(4, 10));
+    map.add(5, 4).add(4, 10);
     map.clear();
     CORE_TEST_FALSE(map.contains(5));
     CORE_TEST_FALSE(map.contains(4));
@@ -64,7 +60,7 @@ static void testOverflow(bool light) {
     IntMap map;
     int limit = light ? 10000 : 100000;
     for(int i = 0; i < limit; i++) {
-        CORE_TEST_ERROR(map.add(i, i));
+        map.add(i, i);
     }
     for(int i = 0; i < limit; i++) {
         CORE_TEST_TRUE(map.contains(i));
@@ -127,15 +123,13 @@ static void testEmplace() {
 
 static void testToString1() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(1, 3));
-    CORE_TEST_ERROR(map.add(2, 4));
-    CORE_TEST_ERROR(map.add(3, 5));
+    map.add(1, 3).add(2, 4).add(3, 5);
     CORE_TEST_STRING("[1 = 3, 2 = 4, 3 = 5]", map);
 }
 
 static void testToString2() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(1, 3));
+    map.add(1, 3);
     CORE_TEST_STRING("[1 = 3]", map);
 }
 
@@ -146,9 +140,7 @@ static void testToString3() {
 
 static void testCopy() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(1, 3));
-    CORE_TEST_ERROR(map.add(2, 4));
-    CORE_TEST_ERROR(map.add(3, 5));
+    map.add(1, 3).add(2, 4).add(3, 5);
     IntMap copy;
     CORE_TEST_ERROR(copy.copyFrom(map));
 
@@ -165,9 +157,7 @@ static void testCopy() {
 
 static void testMove() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(1, 3));
-    CORE_TEST_ERROR(map.add(2, 4));
-    CORE_TEST_ERROR(map.add(3, 5));
+    map.add(1, 3).add(2, 4).add(3, 5);
     IntMap move(Core::move(map));
 
     int* a = move.search(1);
@@ -187,9 +177,7 @@ static void testMove() {
 
 static void testMoveAssignment() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(1, 3));
-    CORE_TEST_ERROR(map.add(2, 4));
-    CORE_TEST_ERROR(map.add(3, 5));
+    map.add(1, 3).add(2, 4).add(3, 5);
 
     IntMap move;
     move = Core::move(map);
@@ -211,12 +199,10 @@ static void testMoveAssignment() {
 
 static void testRemove() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(1, 3));
-    CORE_TEST_ERROR(map.add(2, 4));
-    CORE_TEST_ERROR(map.add(3, 5));
+    map.add(1, 3).add(2, 4).add(3, 5);
 
-    CORE_TEST_ERROR(map.remove(2));
-    CORE_TEST_EQUAL(Core::ErrorCode::NOT_FOUND, map.remove(7));
+    CORE_TEST_TRUE(map.remove(2));
+    CORE_TEST_FALSE(map.remove(7));
 
     int* a = map.search(1);
     int* b = map.search(2);
@@ -234,9 +220,7 @@ static void testRemove() {
 
 static void testEntryForEach() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(5, 4));
-    CORE_TEST_ERROR(map.add(10, 3));
-    CORE_TEST_ERROR(map.add(15, 2));
+    map.add(5, 4).add(10, 3).add(15, 2);
 
     int counter = 0;
     for(auto& entry : map) {
@@ -254,9 +238,7 @@ static void testEntryForEach() {
 
 static void testKeyForEach() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(5, 4));
-    CORE_TEST_ERROR(map.add(10, 3));
-    CORE_TEST_ERROR(map.add(15, 2));
+    map.add(5, 4).add(10, 3).add(15, 2);
 
     int counter = 0;
     for(const int& key : map.getKeys()) {
@@ -274,9 +256,7 @@ static void testKeyForEach() {
 
 static void testValueForEach() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(5, 4));
-    CORE_TEST_ERROR(map.add(10, 3));
-    CORE_TEST_ERROR(map.add(15, 2));
+    map.add(5, 4).add(10, 3).add(15, 2);
 
     int counter = 0;
     for(int& value : map.getValues()) {
@@ -295,7 +275,7 @@ static void testValueForEach() {
 template<typename T>
 static void testType() {
     Core::HashMap<T, int> m;
-    CORE_TEST_ERROR(m.add(T(), 3));
+    m.add(T(), 3);
 }
 
 static void testTypes() {
@@ -312,28 +292,7 @@ static void testTypes() {
     testType<unsigned long long>();
 }
 
-static void testOutOfMemory() {
-#ifdef ERROR_SIMULATOR
-    IntMap map;
-    int memFails = 0;
-    for(int i = 0; i < 40; i++) {
-        Core::Fail::leftAllocations = 2;
-        int* v = nullptr;
-        Core::Error e = map.put(v, 1, 1);
-        if(e == Core::ErrorCode::OUT_OF_MEMORY) {
-            memFails++;
-        }
-    }
-    int* found = map.search(1);
-    if(CORE_TEST_NOT_NULL(found)) {
-        CORE_TEST_EQUAL(1, *found);
-    }
-    Core::Fail::leftAllocations = -1;
-    CORE_TEST_TRUE(memFails != 0);
-#endif
-}
-
-void Core::testHashMap(bool light, bool outOfMemoryTest) {
+void Core::testHashMap(bool light) {
     testAdd();
     testMultipleAdd();
     testSearch();
@@ -352,7 +311,4 @@ void Core::testHashMap(bool light, bool outOfMemoryTest) {
     testKeyForEach();
     testValueForEach();
     testTypes();
-    if(outOfMemoryTest) {
-        testOutOfMemory();
-    }
 }

+ 1 - 3
test/modules/HashedStringTests.cpp

@@ -45,9 +45,7 @@ static void testHashCode() {
 
 static void testAsHashMapKey() {
     Core::HashMap<HString, int> map;
-    CORE_TEST_ERROR(map.add("wusi", 3));
-    CORE_TEST_ERROR(map.add("hiThere", 7));
-    CORE_TEST_ERROR(map.add("baum123", 5));
+    map.add("wusi", 3).add("hiThere", 7).add("baum123", 5);
 
     int* a = map.search("wusi");
     int* b = map.search("hiThere");

+ 18 - 62
test/modules/LinkedListTests.cpp

@@ -17,12 +17,12 @@ struct LinkedListTester final {
 
 static void testWithoutCopyOrMove() {
     Core::LinkedList<LinkedListTester> list;
-    CORE_TEST_ERROR(list.add(3));
+    list.add(3);
 }
 
 static void testAdd() {
     IntList list;
-    CORE_TEST_ERROR(list.add(5u));
+    list.add(5u);
     auto iter = list.begin();
     if(CORE_TEST_TRUE(iter != list.end())) {
         CORE_TEST_EQUAL(5, *iter);
@@ -32,9 +32,7 @@ static void testAdd() {
 
 static void testMultipleAdd() {
     IntList list;
-    CORE_TEST_ERROR(list.add(4u));
-    CORE_TEST_ERROR(list.add(3u));
-    CORE_TEST_ERROR(list.add(2u));
+    list.add(4u).add(3u).add(2u);
     auto iter = list.begin();
     CORE_TEST_EQUAL(4, *iter);
     CORE_TEST_EQUAL(3, *(++iter));
@@ -44,8 +42,7 @@ static void testMultipleAdd() {
 
 static void testClear() {
     IntList list;
-    CORE_TEST_ERROR(list.add(5u));
-    CORE_TEST_ERROR(list.add(4u));
+    list.add(5u).add(4u);
     list.clear();
     CORE_TEST_EQUAL(0, list.getLength());
     CORE_TEST_FALSE(list.begin() != list.end());
@@ -55,7 +52,7 @@ static void testBigAdd(bool light) {
     size_t limit = light ? 10000 : 100000;
     IntList list;
     for(size_t i = 0; i < limit; i++) {
-        CORE_TEST_ERROR(list.add(i));
+        list.add(i);
     }
     auto iter = list.begin();
     for(size_t i = 0; i < list.getLength(); i++) {
@@ -67,12 +64,9 @@ static void testBigAdd(bool light) {
 
 static void testCopy() {
     IntList list;
-    CORE_TEST_ERROR(list.add(1u));
-    CORE_TEST_ERROR(list.add(2u));
-    CORE_TEST_ERROR(list.add(3u));
+    list.add(1u).add(2u).add(3u);
 
-    IntList copy;
-    CORE_TEST_ERROR(copy.copyFrom(list));
+    IntList copy = list;
     CORE_TEST_EQUAL(list.getLength(), copy.getLength());
     auto iterA = list.begin();
     auto iterB = copy.begin();
@@ -85,9 +79,7 @@ static void testCopy() {
 
 static void testMove() {
     IntList list;
-    CORE_TEST_ERROR(list.add(1u));
-    CORE_TEST_ERROR(list.add(2u));
-    CORE_TEST_ERROR(list.add(3u));
+    list.add(1u).add(2u).add(3u);
 
     const IntList move(Core::move(list));
     CORE_TEST_EQUAL(0, list.getLength());
@@ -100,9 +92,7 @@ static void testMove() {
 
 static void testMoveAssignment() {
     IntList list;
-    CORE_TEST_ERROR(list.add(1u));
-    CORE_TEST_ERROR(list.add(2u));
-    CORE_TEST_ERROR(list.add(3u));
+    list.add(1u).add(2u).add(3u);
 
     IntList move;
     move = Core::move(list);
@@ -116,15 +106,13 @@ static void testMoveAssignment() {
 
 static void testToString1() {
     IntList list;
-    CORE_TEST_ERROR(list.add(1u));
-    CORE_TEST_ERROR(list.add(243u));
-    CORE_TEST_ERROR(list.add(423u));
+    list.add(1u).add(243u).add(423u);
     CORE_TEST_STRING("[1, 243, 423]", list);
 }
 
 static void testToString2() {
     IntList list;
-    CORE_TEST_ERROR(list.add(1u));
+    list.add(1u);
     CORE_TEST_STRING("[1]", list);
 }
 
@@ -135,14 +123,10 @@ static void testToString3() {
 
 static void testRemove() {
     IntList list;
-    IntList::Node* a = nullptr;
-    IntList::Node* b = nullptr;
-    IntList::Node* c = nullptr;
-    IntList::Node* d = nullptr;
-    CORE_TEST_ERROR(list.put(a, 4u));
-    CORE_TEST_ERROR(list.put(b, 3u));
-    CORE_TEST_ERROR(list.put(c, 2u));
-    CORE_TEST_ERROR(list.put(d, 1u));
+    IntList::Node* a = list.put(4u);
+    IntList::Node* b = list.put(3u);
+    IntList::Node* c = list.put(2u);
+    IntList::Node* d = list.put(1u);
     CORE_TEST_NOT_NULL(a);
     CORE_TEST_NOT_NULL(b);
     CORE_TEST_NOT_NULL(c);
@@ -178,10 +162,7 @@ static void testRemove() {
 
 static void testRemoveFirst() {
     IntList list;
-    CORE_TEST_ERROR(list.add(4u));
-    CORE_TEST_ERROR(list.add(3u));
-    CORE_TEST_ERROR(list.add(2u));
-    CORE_TEST_ERROR(list.add(1u));
+    list.add(4u).add(3u).add(2u).add(1u);
 
     list.removeFirst();
     auto iter = list.begin();
@@ -227,10 +208,7 @@ static void testRemoveFirst() {
 
 static void testRemoveLast() {
     IntList list;
-    CORE_TEST_ERROR(list.add(4u));
-    CORE_TEST_ERROR(list.add(3u));
-    CORE_TEST_ERROR(list.add(2u));
-    CORE_TEST_ERROR(list.add(1u));
+    list.add(4u).add(3u).add(2u).add(1u);
 
     list.removeLast();
     auto iter = list.begin();
@@ -274,26 +252,7 @@ static void testRemoveLast() {
     CORE_TEST_EQUAL(0, list.getLength());
 }
 
-static void testOutOfMemory() {
-#ifdef ERROR_SIMULATOR
-    IntList list;
-    int memFails = 0;
-    for(int i = 0; i < 40; i++) {
-        Core::Fail::leftAllocations = i;
-        Core::Error e = list.add(1u);
-        if(e == Core::ErrorCode::OUT_OF_MEMORY) {
-            memFails++;
-        }
-    }
-    if(CORE_TEST_TRUE(list.getLength() > 0)) {
-        CORE_TEST_EQUAL(1, *list.begin());
-    }
-    Core::Fail::leftAllocations = -1;
-    CORE_TEST_TRUE(memFails != 0);
-#endif
-}
-
-void Core::testLinkedList(bool light, bool outOfMemoryTest) {
+void Core::testLinkedList(bool light) {
     testWithoutCopyOrMove();
     testAdd();
     testMultipleAdd();
@@ -308,7 +267,4 @@ void Core::testLinkedList(bool light, bool outOfMemoryTest) {
     testRemove();
     testRemoveFirst();
     testRemoveLast();
-    if(outOfMemoryTest) {
-        testOutOfMemory();
-    }
 }

+ 32 - 60
test/modules/ListTests.cpp

@@ -8,17 +8,14 @@ using IntList = Core::List<size_t>;
 
 static void testAdd() {
     IntList list;
-    if(CORE_TEST_ERROR(list.add(5u))) {
-        CORE_TEST_EQUAL(5, list[0]);
-    }
+    list.add(5u);
+    CORE_TEST_EQUAL(5, list[0]);
     CORE_TEST_EQUAL(1, list.getLength());
 }
 
 static void testMultipleAdd() {
     IntList list;
-    CORE_TEST_ERROR(list.add(4u));
-    CORE_TEST_ERROR(list.add(3u));
-    CORE_TEST_ERROR(list.add(2u));
+    list.add(4u).add(3u).add(2u);
     CORE_TEST_EQUAL(4, list[0]);
     CORE_TEST_EQUAL(3, list[1]);
     CORE_TEST_EQUAL(2, list[2]);
@@ -27,27 +24,23 @@ static void testMultipleAdd() {
 
 static void testAddReplace() {
     IntList list;
-    if(CORE_TEST_ERROR(list.add(5u))) {
-        list[0] = 3;
-    }
+    list.add(5u);
+    list[0] = 3;
     CORE_TEST_EQUAL(3, list[0]);
 }
 
 static void testClear() {
     IntList list;
-    CORE_TEST_ERROR(list.add(5u));
-    CORE_TEST_ERROR(list.add(4u));
+    list.add(5u).add(4u);
     list.clear();
     CORE_TEST_EQUAL(0, list.getLength());
 }
 
 static void testShrink() {
     IntList list;
-    CORE_TEST_ERROR(list.add(5u));
-    CORE_TEST_ERROR(list.add(4u));
-    CORE_TEST_ERROR(list.add(3u));
+    list.add(5u).add(4u).add(3u);
     CORE_TEST_TRUE(list.getCapacity() >= 3);
-    CORE_TEST_ERROR(list.shrink());
+    list.shrink();
     CORE_TEST_TRUE(list.getLength() == 3);
     CORE_TEST_TRUE(list.getCapacity() == 3);
     CORE_TEST_EQUAL(5, list[0]);
@@ -59,7 +52,7 @@ static void testBigAdd(bool light) {
     size_t limit = light ? 10000 : 100000;
     IntList list;
     for(size_t i = 0; i < limit; i++) {
-        CORE_TEST_ERROR(list.add(i));
+        list.add(i);
     }
     for(size_t i = 0; i < list.getLength(); i++) {
         CORE_TEST_EQUAL(i, list[i]);
@@ -69,12 +62,9 @@ static void testBigAdd(bool light) {
 
 static void testCopy() {
     IntList list;
-    CORE_TEST_ERROR(list.add(1u));
-    CORE_TEST_ERROR(list.add(2u));
-    CORE_TEST_ERROR(list.add(3u));
+    list.add(1u).add(2u).add(3u);
 
-    IntList copy;
-    CORE_TEST_ERROR(copy.copyFrom(list));
+    IntList copy = list;
     CORE_TEST_EQUAL(list.getLength(), copy.getLength());
     size_t limit = Core::Math::min(copy.getLength(), list.getLength());
     for(size_t i = 0; i < limit; i++) {
@@ -84,9 +74,7 @@ static void testCopy() {
 
 static void testMove() {
     IntList list;
-    CORE_TEST_ERROR(list.add(1u));
-    CORE_TEST_ERROR(list.add(2u));
-    CORE_TEST_ERROR(list.add(3u));
+    list.add(1u).add(2u).add(3u);
 
     IntList move(Core::move(list));
     CORE_TEST_EQUAL(0, list.getLength());
@@ -98,9 +86,7 @@ static void testMove() {
 
 static void testMoveAssignment() {
     IntList list;
-    CORE_TEST_ERROR(list.add(1u));
-    CORE_TEST_ERROR(list.add(2u));
-    CORE_TEST_ERROR(list.add(3u));
+    list.add(1u).add(2u).add(3u);
 
     IntList move;
     move = Core::move(list);
@@ -113,15 +99,13 @@ static void testMoveAssignment() {
 
 static void testToString1() {
     IntList list;
-    CORE_TEST_ERROR(list.add(1u));
-    CORE_TEST_ERROR(list.add(243u));
-    CORE_TEST_ERROR(list.add(423u));
+    list.add(1u).add(243u).add(423u);
     CORE_TEST_STRING("[1, 243, 423]", list);
 }
 
 static void testToString2() {
     IntList list;
-    CORE_TEST_ERROR(list.add(1u));
+    list.add(1u);
     CORE_TEST_STRING("[1]", list);
 }
 
@@ -132,46 +116,35 @@ static void testToString3() {
 
 static void testRemoveBySwap() {
     IntList list;
-    CORE_TEST_ERROR(list.add(4u));
-    CORE_TEST_ERROR(list.add(3u));
-    CORE_TEST_ERROR(list.add(2u));
-    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_INDEX, list.removeBySwap(534));
-    CORE_TEST_ERROR(list.removeBySwap(0));
-    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_INDEX, list.removeBySwap(2));
+    list.add(4u).add(3u).add(2u);
+    list.removeBySwap(0);
     CORE_TEST_EQUAL(2, list[0]);
     CORE_TEST_EQUAL(3, list[1]);
     CORE_TEST_EQUAL(2, list.getLength());
-    CORE_TEST_ERROR(list.removeBySwap(1));
+    list.removeBySwap(1);
     CORE_TEST_EQUAL(2, list[0]);
     CORE_TEST_EQUAL(1, list.getLength());
-    CORE_TEST_ERROR(list.removeBySwap(0));
+    list.removeBySwap(0);
     CORE_TEST_EQUAL(0, list.getLength());
-    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_INDEX, list.removeBySwap(0));
 }
 
 static void testRemove() {
     IntList list;
-    CORE_TEST_ERROR(list.add(4u));
-    CORE_TEST_ERROR(list.add(3u));
-    CORE_TEST_ERROR(list.add(2u));
-    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_INDEX, list.remove(5356));
-    CORE_TEST_ERROR(list.remove(0));
-    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_INDEX, list.remove(2));
+    list.add(4u).add(3u).add(2u);
+    list.remove(0);
     CORE_TEST_EQUAL(3, list[0]);
     CORE_TEST_EQUAL(2, list[1]);
-    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_INDEX, list.remove(2));
     CORE_TEST_EQUAL(2, list.getLength());
-    CORE_TEST_ERROR(list.remove(1));
+    list.remove(1);
     CORE_TEST_EQUAL(3, list[0]);
     CORE_TEST_EQUAL(1, list.getLength());
-    CORE_TEST_ERROR(list.remove(0));
+    list.remove(0);
     CORE_TEST_EQUAL(0, list.getLength());
-    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_INDEX, list.remove(0));
 }
 
 static void testResize() {
     IntList list;
-    CORE_TEST_ERROR(list.resize(5, 10));
+    list.resize(5, 10);
     CORE_TEST_EQUAL(5, list.getLength());
     for(size_t i = 0; i < 5; i++) {
         CORE_TEST_EQUAL(10, list[i]);
@@ -180,7 +153,7 @@ static void testResize() {
 
 static void testDefaultResize() {
     IntList list;
-    CORE_TEST_ERROR(list.resize(5));
+    list.resize(5);
     CORE_TEST_EQUAL(5, list.getLength());
     for(size_t i = 0; i < 5; i++) {
         CORE_TEST_EQUAL(0, list[i]);
@@ -189,26 +162,25 @@ static void testDefaultResize() {
 
 static void testInvalidReserve() {
     IntList list;
-    CORE_TEST_ERROR(list.reserve(0));
+    list.reserve(0);
 }
 
 static void testShrinkExact() {
     IntList list;
-    CORE_TEST_ERROR(list.resize(50));
-    CORE_TEST_ERROR(list.shrink());
+    list.resize(50);
+    list.shrink();
 }
 
 static void testShrinkResize() {
     IntList list;
-    CORE_TEST_ERROR(list.resize(50));
-    CORE_TEST_ERROR(list.resize(20, 5));
-    CORE_TEST_ERROR(list.resize(10));
+    list.resize(50);
+    list.resize(20, 5);
+    list.resize(10);
 }
 
 static void testCopyEmpty() {
     IntList list;
-    IntList copy;
-    CORE_TEST_ERROR(copy.copyFrom(list));
+    IntList copy = list;
 }
 
 void Core::testList(bool light) {

+ 1 - 7
test/modules/MatrixStackTests.cpp

@@ -16,7 +16,7 @@ static void testPop() {
     Matrices stack;
     CORE_TEST_ERROR(stack.push());
     stack.peek().scale(2.0f);
-    CORE_TEST_ERROR(stack.pop());
+    stack.pop();
     Core::Matrix m;
     for(int i = 0; i < 16; i++) {
         CORE_TEST_FLOAT(m.getValues()[i], stack.peek().getValues()[i], 0.0f);
@@ -99,11 +99,6 @@ static void testToString3() {
                      stack);
 }
 
-static void testInvalidPop() {
-    Matrices stack;
-    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_STATE, stack.pop());
-}
-
 static void testConstPeek() {
     const Matrices stack;
     CORE_TEST_STRING("[[1.00, 0.00, 0.00, 0.00], "
@@ -121,6 +116,5 @@ void Core::testMatrixStack(bool light) {
     testToString1();
     testToString2();
     testToString3();
-    testInvalidPop();
     testConstPeek();
 }

+ 25 - 80
test/modules/ProbingHashMapTests.cpp

@@ -8,7 +8,7 @@ using IntMap = Core::ProbingHashMap<int, int>;
 
 static void testAdd() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(5, 4));
+    map.add(5, 4);
     int* value = map.search(5);
     CORE_TEST_NOT_NULL(value);
     if(value != nullptr) {
@@ -18,9 +18,7 @@ static void testAdd() {
 
 static void testMultipleAdd() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(5, 4));
-    CORE_TEST_ERROR(map.add(10, 3));
-    CORE_TEST_ERROR(map.add(15, 2));
+    map.add(5, 4).add(10, 3).add(15, 2);
     CORE_TEST_TRUE(map.contains(5));
     CORE_TEST_TRUE(map.contains(10));
     CORE_TEST_TRUE(map.contains(15));
@@ -40,16 +38,13 @@ static void testMultipleAdd() {
 static void testSearch() {
     IntMap map;
     CORE_TEST_NULL(map.search(6));
-    CORE_TEST_ERROR(map.add(5, 4));
-    CORE_TEST_ERROR(map.add(10, 3));
-    CORE_TEST_ERROR(map.add(15, 2));
+    map.add(5, 4).add(10, 3).add(15, 2);
     CORE_TEST_NULL(map.search(6));
 }
 
 static void testAddReplace() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(5, 4));
-    CORE_TEST_ERROR(map.add(5, 10));
+    map.add(5, 4).add(5, 10);
     CORE_TEST_TRUE(map.contains(5));
     int* a = map.search(5);
     CORE_TEST_NOT_NULL(a);
@@ -60,8 +55,7 @@ static void testAddReplace() {
 
 static void testClear() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(5, 4));
-    CORE_TEST_ERROR(map.add(4, 10));
+    map.add(5, 4).add(4, 10);
     map.clear();
     CORE_TEST_FALSE(map.contains(5));
     CORE_TEST_FALSE(map.contains(4));
@@ -71,7 +65,7 @@ static void testOverflow(bool light) {
     int limit = light ? 10000 : 100000;
     IntMap map;
     for(int i = 0; i < limit; i++) {
-        CORE_TEST_ERROR(map.add(i, i));
+        map.add(i, i);
     }
     for(int i = 0; i < limit; i++) {
         CORE_TEST_TRUE(map.contains(i));
@@ -125,13 +119,11 @@ static void testEmplace() {
         Core::ProbingHashMap<int, ProbingHashMapTestStruct> map;
 
         ProbingHashMapTestStruct* ar = nullptr;
-        CORE_TEST_ERROR(map.tryEmplace(ar, 0, 3, 4));
-        CORE_TEST_ERROR(map.tryEmplace(ar, 3, 4, 5));
-        CORE_TEST_ERROR(map.tryEmplace(ar, 20, 5, 6));
-        CORE_TEST_EQUAL(Core::ErrorCode::EXISTING_KEY,
-                        map.tryEmplace(ar, 3, 6, 7));
-        CORE_TEST_EQUAL(Core::ErrorCode::EXISTING_KEY,
-                        map.tryEmplace(ar, 20, 7, 8));
+        CORE_TEST_TRUE(map.tryEmplace(ar, 0, 3, 4));
+        CORE_TEST_TRUE(map.tryEmplace(ar, 3, 4, 5));
+        CORE_TEST_TRUE(map.tryEmplace(ar, 20, 5, 6));
+        CORE_TEST_FALSE(map.tryEmplace(ar, 3, 6, 7));
+        CORE_TEST_FALSE(map.tryEmplace(ar, 20, 7, 8));
 
         ProbingHashMapTestStruct* a = map.search(0);
         ProbingHashMapTestStruct* b = map.search(3);
@@ -152,15 +144,13 @@ static void testEmplace() {
 
 static void testToString1() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(1, 3));
-    CORE_TEST_ERROR(map.add(2, 4));
-    CORE_TEST_ERROR(map.add(3, 5));
+    map.add(1, 3).add(2, 4).add(3, 5);
     CORE_TEST_STRING("[2 = 4, 1 = 3, 3 = 5]", map);
 }
 
 static void testToString2() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(1, 3));
+    map.add(1, 3);
     CORE_TEST_STRING("[1 = 3]", map);
 }
 
@@ -171,11 +161,8 @@ static void testToString3() {
 
 static void testCopy() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(1, 3));
-    CORE_TEST_ERROR(map.add(2, 4));
-    CORE_TEST_ERROR(map.add(3, 5));
-    IntMap copy;
-    CORE_TEST_ERROR(copy.copyFrom(map));
+    map.add(1, 3).add(2, 4).add(3, 5);
+    IntMap copy = map;
 
     int* a[6] = {map.search(1),  map.search(2),  map.search(3),
                  copy.search(1), copy.search(2), copy.search(3)};
@@ -190,9 +177,7 @@ static void testCopy() {
 
 static void testMove() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(1, 3));
-    CORE_TEST_ERROR(map.add(2, 4));
-    CORE_TEST_ERROR(map.add(3, 5));
+    map.add(1, 3).add(2, 4).add(3, 5);
     IntMap move(Core::move(map));
 
     int* a = move.search(1);
@@ -212,9 +197,7 @@ static void testMove() {
 
 static void testMoveAssignment() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(1, 3));
-    CORE_TEST_ERROR(map.add(2, 4));
-    CORE_TEST_ERROR(map.add(3, 5));
+    map.add(1, 3).add(2, 4).add(3, 5);
 
     IntMap move;
     move = Core::move(map);
@@ -236,9 +219,7 @@ static void testMoveAssignment() {
 
 static void testEntryForEach() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(5, 4));
-    CORE_TEST_ERROR(map.add(10, 3));
-    CORE_TEST_ERROR(map.add(15, 2));
+    map.add(5, 4).add(10, 3).add(15, 2);
 
     int counter = 0;
     for(auto entry : map) {
@@ -256,9 +237,7 @@ static void testEntryForEach() {
 
 static void testKeyForEach() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(5, 4));
-    CORE_TEST_ERROR(map.add(10, 3));
-    CORE_TEST_ERROR(map.add(15, 2));
+    map.add(5, 4).add(10, 3).add(15, 2);
 
     int counter = 0;
     for(const int& key : map.getKeys()) {
@@ -276,9 +255,7 @@ static void testKeyForEach() {
 
 static void testValueForEach() {
     IntMap map;
-    CORE_TEST_ERROR(map.add(5, 4));
-    CORE_TEST_ERROR(map.add(10, 3));
-    CORE_TEST_ERROR(map.add(15, 2));
+    map.add(5, 4).add(10, 3).add(15, 2);
 
     int counter = 0;
     for(int& value : map.getValues()) {
@@ -297,7 +274,7 @@ static void testValueForEach() {
 template<typename T>
 static void testType() {
     Core::ProbingHashMap<T, int> m;
-    CORE_TEST_ERROR(m.add(T(), 3));
+    m.add(T(), 3);
 }
 
 static void testTypes() {
@@ -314,49 +291,21 @@ static void testTypes() {
     testType<unsigned long long>();
 }
 
-static void testOutOfMemory() {
-#ifdef ERROR_SIMULATOR
-    IntMap map;
-    int memFails = 0;
-    for(int i = 0; i < 40; i++) {
-        Core::Fail::leftAllocations = i;
-        int* v = nullptr;
-        Core::Error e = map.put(v, 1, 1);
-        if(e == Core::ErrorCode::OUT_OF_MEMORY) {
-            memFails++;
-        }
-    }
-    int* found = map.search(1);
-    if(CORE_TEST_NOT_NULL(found)) {
-        CORE_TEST_EQUAL(1, *found);
-    }
-    Core::Fail::leftAllocations = -1;
-    CORE_TEST_TRUE(memFails != 0);
-#endif
-}
-
 static void testInsertInvalid() {
     IntMap map;
     int* v;
-    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_ARGUMENT,
-                    map.tryEmplace(v, Core::emptyValue<int>(), 2));
-    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_ARGUMENT,
-                    map.put(v, Core::emptyValue<int>(), 2));
+    CORE_TEST_TRUE(map.tryEmplace(v, Core::emptyValue<int>(), 2));
+    CORE_TEST_EQUAL(3, map.put(Core::emptyValue<int>(), 3));
 }
 
 static void testAddCollisions() {
     IntMap map;
     for(int i = 0; i < 8; i++) {
-        CORE_TEST_ERROR(map.add(i * 16, i));
+        map.add(i * 16, i);
     }
 }
 
-static void testLargeRehash() {
-    IntMap map;
-    CORE_TEST_EQUAL(Core::ErrorCode::CAPACITY_REACHED, map.rehash(1llu << 50));
-}
-
-void Core::testProbingHashMap(bool light, bool outOfMemoryTest) {
+void Core::testProbingHashMap(bool light) {
     testAdd();
     testMultipleAdd();
     testSearch();
@@ -374,10 +323,6 @@ void Core::testProbingHashMap(bool light, bool outOfMemoryTest) {
     testKeyForEach();
     testValueForEach();
     testTypes();
-    if(outOfMemoryTest) {
-        testOutOfMemory();
-    }
     testInsertInvalid();
     testAddCollisions();
-    testLargeRehash();
 }

+ 4 - 13
test/modules/StackTests.cpp

@@ -12,13 +12,13 @@ static void testPushPopPeek() {
     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());
+    stack.pop();
     CORE_TEST_EQUAL(2, stack.peek());
     CORE_TEST_EQUAL(2, static_cast<const T&>(stack).peek());
-    CORE_TEST_ERROR(stack.pop());
+    stack.pop();
     CORE_TEST_EQUAL(1, stack.peek());
     CORE_TEST_EQUAL(1, static_cast<const T&>(stack).peek());
-    CORE_TEST_ERROR(stack.pop());
+    stack.pop();
     CORE_TEST_TRUE(stack.isEmpty());
 }
 
@@ -29,7 +29,7 @@ static void testBigPushPop(int amount) {
         CORE_TEST_ERROR(stack.push(i));
     }
     for(int i = 0; i < amount; i++) {
-        CORE_TEST_ERROR(stack.pop());
+        stack.pop();
     }
     CORE_TEST_TRUE(stack.isEmpty());
 }
@@ -56,14 +56,6 @@ static void testToString3() {
     CORE_TEST_STRING("[]", stack);
 }
 
-template<typename T>
-static void testPop(int amount) {
-    T stack;
-    for(int i = 0; i < amount; i++) {
-        CORE_TEST_EQUAL(Core::ErrorCode::INVALID_STATE, stack.pop());
-    }
-}
-
 template<typename T>
 static void testClear() {
     T stack;
@@ -81,7 +73,6 @@ static void testType(int amount) {
     testToString1<T>();
     testToString2<T>();
     testToString3<T>();
-    testPop<T>(amount);
     testClear<T>();
 }
 

+ 34 - 11
test/modules/UtilityTests.cpp

@@ -1,3 +1,5 @@
+#include <stdlib.h>
+
 #include "../../src/ErrorSimulator.hpp"
 #include "../Tests.hpp"
 #include "core/utils/Utility.hpp"
@@ -19,28 +21,49 @@ static void testIf() {
     CORE_TEST_TRUE((Core::IsSame<Core::If<false, int, double>, double>));
 }
 
+static void onOutOfMemory(void* p) {
+    *static_cast<bool*>(p) = true;
+}
+
+static void testAllocateFail() {
+#ifdef ERROR_SIMULATOR
+    Core::Fail::alloc = true;
+    bool check = false;
+    Core::setOutOfMemoryHandler(onOutOfMemory, &check);
+    void* p = Core::allocate(16);
+    CORE_TEST_NOT_NULL(p);
+    CORE_TEST_TRUE(check);
+    Core::Fail::alloc = false;
+    Core::setOutOfMemoryHandler(nullptr, nullptr);
+    free(p);
+#endif
+}
+
 static void testZeroRellocate() {
-    char* buffer = nullptr;
-    CORE_TEST_ERROR(Core::reallocate(buffer, 16));
-    CORE_TEST_TRUE(buffer != nullptr);
-    CORE_TEST_ERROR(Core::reallocate(buffer, 0));
-    CORE_TEST_TRUE(buffer == nullptr);
+    void* buffer = Core::reallocate(nullptr, 16);
+    CORE_TEST_NOT_NULL(buffer);
+    buffer = Core::reallocate(buffer, 0);
+    CORE_TEST_NULL(buffer);
 }
 
 static void testReallocateFail() {
 #ifdef ERROR_SIMULATOR
-    char* buffer = nullptr;
-    Core::Fail::realloc = true;
-    CORE_TEST_EQUAL(Core::ErrorCode::OUT_OF_MEMORY,
-                    Core::reallocate(buffer, 16));
-    CORE_TEST_TRUE(buffer == nullptr);
-    Core::Fail::realloc = false;
+    Core::Fail::alloc = true;
+    bool check = false;
+    Core::setOutOfMemoryHandler(onOutOfMemory, &check);
+    void* p = Core::reallocate(nullptr, 16);
+    CORE_TEST_NOT_NULL(p);
+    CORE_TEST_TRUE(check);
+    Core::Fail::alloc = false;
+    Core::setOutOfMemoryHandler(nullptr, nullptr);
+    free(p);
 #endif
 }
 
 void Core::testUtility() {
     testPopCount();
     testIf();
+    testAllocateFail();
     testZeroRellocate();
     testReallocateFail();
 }