UtilityTests.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "../../src/ErrorSimulator.h"
  2. #include "../Tests.h"
  3. #include "core/Utility.h"
  4. static const float eps = 0.0001f;
  5. static void testPopCount() {
  6. CORE_TEST_U64(4, corePopCount(0xF));
  7. CORE_TEST_U64(0, corePopCount(0x0));
  8. CORE_TEST_U64(2, corePopCount(0x6));
  9. CORE_TEST_U64(7, corePopCount(0x7F));
  10. CORE_TEST_U64(3, corePopCount(0x2A));
  11. CORE_TEST_U64(32, corePopCount(0xFFFFFFFF));
  12. CORE_TEST_U64(64, corePopCount(0xFFFFFFFFFFFFFFFF));
  13. CORE_TEST_U64(44, corePopCount(0xFFFF0FFFFFFF));
  14. }
  15. static void testZeroRellocate() {
  16. void* buffer = coreReallocate(nullptr, 16);
  17. CORE_TEST_NOT_NULL(buffer);
  18. buffer = coreReallocate(buffer, 0);
  19. CORE_TEST_NULL(buffer);
  20. }
  21. static void testMemoryInfoList() {
  22. void* a = coreAllocate(8);
  23. void* b = coreAllocate(8);
  24. void* c = coreAllocate(8);
  25. void* d = coreAllocate(8);
  26. coreFree(b); // remove middle element
  27. coreFree(a); // remove first
  28. coreFree(d); // remove last
  29. coreFree(c); // remove single
  30. coreFree(nullptr);
  31. }
  32. static void testInterpolate() {
  33. CORE_TEST_FLOAT(7.5f, coreInterpolate(5.0f, 10.0f, 0.5f), eps);
  34. CORE_TEST_FLOAT(-2.0, coreInterpolate(-10.0f, 10.0f, 0.4f), eps);
  35. CORE_TEST_FLOAT(10.0f, coreInterpolate(-3.0f, 10.0f, 1.0f), eps);
  36. CORE_TEST_FLOAT(7.0f, coreInterpolate(7.0f, 10.0f, 0.0f), eps);
  37. CORE_TEST_FLOAT(6.0f, coreInterpolate(0.0f, 10.0f, 0.6f), eps);
  38. }
  39. static void testRadianToDegree() {
  40. CORE_TEST_FLOAT(45.0f, coreRadianToDegree(CORE_PI * 0.25f), eps);
  41. CORE_TEST_FLOAT(90.0f, coreRadianToDegree(CORE_PI * 0.5f), eps);
  42. CORE_TEST_FLOAT(180.0f, coreRadianToDegree(CORE_PI), eps);
  43. CORE_TEST_FLOAT(360.0f, coreRadianToDegree(CORE_PI * 2.0f), eps);
  44. }
  45. static void testDegreeToRadian() {
  46. CORE_TEST_FLOAT(CORE_PI * 0.25f, coreDegreeToRadian(45.0f), eps);
  47. CORE_TEST_FLOAT(CORE_PI * 0.5f, coreDegreeToRadian(90.0f), eps);
  48. CORE_TEST_FLOAT(CORE_PI, coreDegreeToRadian(180.0f), eps);
  49. CORE_TEST_FLOAT(CORE_PI * 2.0f, coreDegreeToRadian(360.0f), eps);
  50. }
  51. static void testSleep(i64 nanos) {
  52. i64 time = -coreNanos();
  53. CORE_TEST_FALSE(coreSleep(nanos));
  54. time += coreNanos();
  55. CORE_TEST_TRUE(time >= nanos && time <= (nanos * 13) / 10);
  56. }
  57. static void testFail() {
  58. #ifdef ERROR_SIMULATOR
  59. coreFailTimeGet = true;
  60. CORE_TEST_I64(-1, coreNanos());
  61. coreFailTimeGet = false;
  62. #endif
  63. }
  64. void coreTestUtility(bool light) {
  65. testPopCount();
  66. testZeroRellocate();
  67. testMemoryInfoList();
  68. testInterpolate();
  69. testRadianToDegree();
  70. testDegreeToRadian();
  71. testSleep(light ? 5'000'000 : 50'000'000);
  72. testSleep(light ? 50'000'000 : 1'300'000'000);
  73. testFail();
  74. }
  75. static void outOfMemory(void*) {
  76. coreSetOutOfMemoryHandler(nullptr, nullptr);
  77. }
  78. void coreTestInvalidAllocate(void) {
  79. coreSetOutOfMemoryHandler(outOfMemory, nullptr);
  80. coreAllocate(0xFFFFFFFFFFF);
  81. CORE_TEST_TRUE(false);
  82. coreFinalizeTests();
  83. CORE_EXIT(0);
  84. }
  85. void coreTestInvalidReallocate(void) {
  86. coreSetOutOfMemoryHandler(outOfMemory, nullptr);
  87. void* p = coreAllocate(0xFF);
  88. corePrintMemoryReport();
  89. coreReallocate(p, 0xFFFFFFFFFFF);
  90. CORE_TEST_TRUE(false);
  91. coreFinalizeTests();
  92. CORE_EXIT(0);
  93. }
  94. [[noreturn]] void coreTestPreCanary(void) {
  95. #ifdef CORE_CHECK_MEMORY
  96. char* p = coreAllocate(16);
  97. p[-1] = 0;
  98. coreFree(p);
  99. CORE_TEST_TRUE(false);
  100. #endif
  101. coreFinalizeTests();
  102. CORE_EXIT(0);
  103. }
  104. [[noreturn]] void coreTestPostCanary(void) {
  105. #ifdef CORE_CHECK_MEMORY
  106. char* p = coreAllocate(16);
  107. p[17] = 0;
  108. coreFree(p);
  109. CORE_TEST_TRUE(false);
  110. #endif
  111. coreFinalizeTests();
  112. CORE_EXIT(0);
  113. }