#include "../../src/ErrorSimulator.h" #include "../Tests.h" #include "core/Utility.h" static const float eps = 0.0001f; 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); } static void testInterpolate() { CORE_TEST_FLOAT(7.5f, coreInterpolate(5.0f, 10.0f, 0.5f), eps); CORE_TEST_FLOAT(-2.0, coreInterpolate(-10.0f, 10.0f, 0.4f), eps); CORE_TEST_FLOAT(10.0f, coreInterpolate(-3.0f, 10.0f, 1.0f), eps); CORE_TEST_FLOAT(7.0f, coreInterpolate(7.0f, 10.0f, 0.0f), eps); CORE_TEST_FLOAT(6.0f, coreInterpolate(0.0f, 10.0f, 0.6f), eps); } static void testRadianToDegree() { CORE_TEST_FLOAT(45.0f, coreRadianToDegree(CORE_PI * 0.25f), eps); CORE_TEST_FLOAT(90.0f, coreRadianToDegree(CORE_PI * 0.5f), eps); CORE_TEST_FLOAT(180.0f, coreRadianToDegree(CORE_PI), eps); CORE_TEST_FLOAT(360.0f, coreRadianToDegree(CORE_PI * 2.0f), eps); } static void testDegreeToRadian() { CORE_TEST_FLOAT(CORE_PI * 0.25f, coreDegreeToRadian(45.0f), eps); CORE_TEST_FLOAT(CORE_PI * 0.5f, coreDegreeToRadian(90.0f), eps); CORE_TEST_FLOAT(CORE_PI, coreDegreeToRadian(180.0f), eps); CORE_TEST_FLOAT(CORE_PI * 2.0f, coreDegreeToRadian(360.0f), eps); } static void testSleep(i64 nanos) { i64 time = -coreNanos(); CORE_TEST_FALSE(coreSleep(nanos)); time += coreNanos(); CORE_TEST_TRUE(time >= nanos && time <= (nanos * 13) / 10); } static void testFail() { #ifdef ERROR_SIMULATOR coreFailTimeGet = true; CORE_TEST_I64(-1, coreNanos()); coreFailTimeGet = false; #endif } void coreTestUtility(bool light) { testPopCount(); testZeroRellocate(); testMemoryInfoList(); testInterpolate(); testRadianToDegree(); testDegreeToRadian(); testSleep(light ? 5'000'000 : 50'000'000); testSleep(light ? 50'000'000 : 1'300'000'000); testFail(); } 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); } [[noreturn]] 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); } [[noreturn]] 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); }