#include #include "../../src/ErrorSimulator.hpp" #include "../Tests.hpp" #include "core/utils/Utility.hpp" static void testPopCount() { CORE_TEST_EQUAL(4, Core::popCount(0xF)); CORE_TEST_EQUAL(0, Core::popCount(0x0)); CORE_TEST_EQUAL(2, Core::popCount(0x6)); CORE_TEST_EQUAL(7, Core::popCount(0x7F)); CORE_TEST_EQUAL(3, Core::popCount(0x2A)); CORE_TEST_EQUAL(32, Core::popCount(0xFFFFFFFF)); CORE_TEST_EQUAL(64, Core::popCount(0xFFFFFFFFFFFFFFFFL)); CORE_TEST_EQUAL(44, Core::popCount(0xFFFF0FFFFFFF)); CORE_TEST_EQUAL(32, Core::popCount(-1)); } static void testIf() { CORE_TEST_TRUE((Core::IsSame, int>)); CORE_TEST_TRUE((Core::IsSame, double>)); } #ifdef ERROR_SIMULATOR static void onOutOfMemory(void* p) { *static_cast(p) = true; } #endif 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() { 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 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(); }