#include "../../src/ErrorSimulator.h" #include "../Tests.h" #include "core/Utility.h" static const float eps = 0.0001f; static void testPopCount() { CORE_TEST_U64(4, popCount(0xF)); CORE_TEST_U64(0, popCount(0x0)); CORE_TEST_U64(2, popCount(0x6)); CORE_TEST_U64(7, popCount(0x7F)); CORE_TEST_U64(3, popCount(0x2A)); CORE_TEST_U64(32, popCount(0xFFFFFFFF)); CORE_TEST_U64(64, popCount(0xFFFFFFFFFFFFFFFF)); CORE_TEST_U64(44, popCount(0xFFFF0FFFFFFF)); } static void testZeroRellocate() { void* buffer = cReallocate(nullptr, 16); CORE_TEST_NOT_NULL(buffer); buffer = cReallocate(buffer, 0); CORE_TEST_NULL(buffer); } static void testMemoryInfoList() { void* a = cAllocate(8); void* b = cAllocate(8); void* c = cAllocate(8); void* d = cAllocate(8); cFree(b); // remove middle element cFree(a); // remove first cFree(d); // remove last cFree(c); // remove single cFree(nullptr); } static void testInterpolate() { CORE_TEST_FLOAT(7.5f, interpolate(5.0f, 10.0f, 0.5f), eps); CORE_TEST_FLOAT(-2.0, interpolate(-10.0f, 10.0f, 0.4f), eps); CORE_TEST_FLOAT(10.0f, interpolate(-3.0f, 10.0f, 1.0f), eps); CORE_TEST_FLOAT(7.0f, interpolate(7.0f, 10.0f, 0.0f), eps); CORE_TEST_FLOAT(6.0f, interpolate(0.0f, 10.0f, 0.6f), eps); } static void testRadianToDegree() { CORE_TEST_FLOAT(45.0f, radianToDegree(CORE_PI * 0.25f), eps); CORE_TEST_FLOAT(90.0f, radianToDegree(CORE_PI * 0.5f), eps); CORE_TEST_FLOAT(180.0f, radianToDegree(CORE_PI), eps); CORE_TEST_FLOAT(360.0f, radianToDegree(CORE_PI * 2.0f), eps); } static void testDegreeToRadian() { CORE_TEST_FLOAT(CORE_PI * 0.25f, degreeToRadian(45.0f), eps); CORE_TEST_FLOAT(CORE_PI * 0.5f, degreeToRadian(90.0f), eps); CORE_TEST_FLOAT(CORE_PI, degreeToRadian(180.0f), eps); CORE_TEST_FLOAT(CORE_PI * 2.0f, degreeToRadian(360.0f), eps); } static void testSleep(i64 nanos) { i64 time = -getNanos(); CORE_TEST_FALSE(sleepNanos(nanos)); time += getNanos(); CORE_TEST_TRUE(time >= nanos && time <= (nanos * 13) / 10); } static void testFail() { #ifdef ERROR_SIMULATOR coreFailTimeGet = true; CORE_TEST_I64(-1, getNanos()); 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*) { setOutOfMemoryHandler(nullptr, nullptr); } void coreTestInvalidAllocate(void) { setOutOfMemoryHandler(outOfMemory, nullptr); cAllocate(0xFFFFFFFFFFF); CORE_TEST_TRUE(false); coreFinalizeTests(); CORE_EXIT(0); } void coreTestInvalidReallocate(void) { setOutOfMemoryHandler(outOfMemory, nullptr); void* p = cAllocate(0xFF); printMemoryReport(); cReallocate(p, 0xFFFFFFFFFFF); CORE_TEST_TRUE(false); coreFinalizeTests(); CORE_EXIT(0); } [[noreturn]] void coreTestPreCanary(void) { #ifdef CORE_CHECK_MEMORY char* p = cAllocate(16); p[-1] = 0; cFree(p); CORE_TEST_TRUE(false); #endif coreFinalizeTests(); CORE_EXIT(0); } [[noreturn]] void coreTestPostCanary(void) { #ifdef CORE_CHECK_MEMORY char* p = cAllocate(16); p[17] = 0; cFree(p); CORE_TEST_TRUE(false); #endif coreFinalizeTests(); CORE_EXIT(0); }