#include "../../src/ErrorSimulator.h" #include "../Tests.h" #include "core/Utility.h" static void testPopCount() { CORE_TEST_U64(4, corePopCount(0xF)); CORE_TEST_U64(0, corePopCount(0x0)); CORE_TEST_U64(2, corePopCount(0x6)); CORE_TEST_U64(7, corePopCount(0x7F)); CORE_TEST_U64(3, corePopCount(0x2A)); CORE_TEST_U64(32, corePopCount(0xFFFFFFFF)); CORE_TEST_U64(64, corePopCount(0xFFFFFFFFFFFFFFFF)); CORE_TEST_U64(44, corePopCount(0xFFFF0FFFFFFF)); } static void testZeroRellocate() { void* buffer = coreReallocate(nullptr, 16); CORE_TEST_NOT_NULL(buffer); buffer = coreReallocate(buffer, 0); CORE_TEST_NULL(buffer); } static void testMemoryInfoList() { void* a = coreAllocate(8); void* b = coreAllocate(8); void* c = coreAllocate(8); void* d = coreAllocate(8); coreFree(b); // remove middle element coreFree(a); // remove first coreFree(d); // remove last coreFree(c); // remove single coreFree(nullptr); } void coreTestUtility() { testPopCount(); testZeroRellocate(); testMemoryInfoList(); } static void outOfMemory(void*) { coreSetOutOfMemoryHandler(nullptr, nullptr); } void coreTestInvalidAllocate(void) { coreSetOutOfMemoryHandler(outOfMemory, nullptr); coreAllocate(0xFFFFFFFFFFF); CORE_TEST_TRUE(false); coreFinalizeTests(); CORE_EXIT(0); } void coreTestInvalidReallocate(void) { coreSetOutOfMemoryHandler(outOfMemory, nullptr); void* p = coreAllocate(0xFF); corePrintMemoryReport(); coreReallocate(p, 0xFFFFFFFFFFF); CORE_TEST_TRUE(false); coreFinalizeTests(); CORE_EXIT(0); } void coreTestPreCanary(void) { #ifdef CORE_CHECK_MEMORY char* p = coreAllocate(16); p[-1] = 0; coreFree(p); CORE_TEST_TRUE(false); #endif coreFinalizeTests(); CORE_EXIT(0); } void coreTestPostCanary(void) { #ifdef CORE_CHECK_MEMORY char* p = coreAllocate(16); p[17] = 0; coreFree(p); CORE_TEST_TRUE(false); #endif coreFinalizeTests(); CORE_EXIT(0); }