UtilityTests.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. TEST_U64(4, popCount(0xF));
  7. TEST_U64(0, popCount(0x0));
  8. TEST_U64(2, popCount(0x6));
  9. TEST_U64(7, popCount(0x7F));
  10. TEST_U64(3, popCount(0x2A));
  11. TEST_U64(32, popCount(0xFFFF'FFFF));
  12. TEST_U64(64, popCount(0xFFFF'FFFF'FFFF'FFFF));
  13. TEST_U64(44, popCount(0xFFFF'0FFF'FFFF));
  14. }
  15. static void testZeroRellocate() {
  16. void* buffer = coreReallocate(nullptr, 16);
  17. TEST_NOT_NULL(buffer);
  18. buffer = coreReallocate(buffer, 0);
  19. 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 testZeroAllocate() {
  33. constexpr size_t n = 1024 * 1024;
  34. void* a = coreAllocate(n);
  35. memset(a, 0, n);
  36. void* b = coreZeroAllocate(n);
  37. TEST_TRUE(memcmp(a, b, n) == 0);
  38. coreFree(a);
  39. coreFree(b);
  40. }
  41. static void testInterpolate() {
  42. TEST_FLOAT(7.5f, interpolate(5.0f, 10.0f, 0.5f), eps);
  43. TEST_FLOAT(-2.0, interpolate(-10.0f, 10.0f, 0.4f), eps);
  44. TEST_FLOAT(10.0f, interpolate(-3.0f, 10.0f, 1.0f), eps);
  45. TEST_FLOAT(7.0f, interpolate(7.0f, 10.0f, 0.0f), eps);
  46. TEST_FLOAT(6.0f, interpolate(0.0f, 10.0f, 0.6f), eps);
  47. }
  48. static void testRadianToDegree() {
  49. TEST_FLOAT(45.0f, radianToDegree(PI * 0.25f), eps);
  50. TEST_FLOAT(90.0f, radianToDegree(PI * 0.5f), eps);
  51. TEST_FLOAT(180.0f, radianToDegree(PI), eps);
  52. TEST_FLOAT(360.0f, radianToDegree(PI * 2.0f), eps);
  53. }
  54. static void testDegreeToRadian() {
  55. TEST_FLOAT(PI * 0.25f, degreeToRadian(45.0f), eps);
  56. TEST_FLOAT(PI * 0.5f, degreeToRadian(90.0f), eps);
  57. TEST_FLOAT(PI, degreeToRadian(180.0f), eps);
  58. TEST_FLOAT(PI * 2.0f, degreeToRadian(360.0f), eps);
  59. }
  60. static void testSleep(i64 nanos) {
  61. i64 time = -getNanos();
  62. TEST_FALSE(sleepNanos(nanos));
  63. time += getNanos();
  64. TEST_TRUE(time >= nanos && time <= (nanos * 13) / 10);
  65. }
  66. static void testSleepMillis(i64 millis) {
  67. i64 time = -getNanos();
  68. TEST_FALSE(sleepMillis(millis));
  69. time += getNanos();
  70. i64 nanos = millis * 1'000'000;
  71. TEST_TRUE(time >= nanos && time <= (nanos * 13) / 10);
  72. }
  73. typedef struct {
  74. int i;
  75. i64 d;
  76. } SwapTest;
  77. static void testSwap() {
  78. SwapTest a = {3, 20};
  79. SwapTest b = {7, 30};
  80. swap(&a, &b);
  81. TEST_INT(7, a.i);
  82. TEST_INT(3, b.i);
  83. TEST_I64(30, a.d);
  84. TEST_I64(20, b.d);
  85. }
  86. static void testFail() {
  87. #ifdef ERROR_SIMULATOR
  88. failTimeGet = true;
  89. TEST_I64(-1, getNanos());
  90. failTimeGet = false;
  91. #endif
  92. }
  93. BUBBLE_SORT(size_t, Size)
  94. #define greateThanSize(a, b) ((a) > (b))
  95. BUBBLE_SORT_SOURCE(size_t, Size, greateThanSize)
  96. static void testSort() {
  97. size_t data[] = {9, 0, 3, 1, 8, 4, 6, 2, 5, 7};
  98. size_t n = ARRAY_LENGTH(data);
  99. bubbleSortSize(data, n);
  100. bubbleSortSize(data, n);
  101. bubbleSortSize(data, 0);
  102. for(size_t i = 0; i < n; i++) {
  103. TEST_SIZE(data[i], i);
  104. }
  105. }
  106. static void testMinMax() {
  107. TEST_SIZE(5, minSize(5, 7));
  108. TEST_SIZE(7, maxSize(5, 7));
  109. TEST_SIZE(5, clampSize(3, 5, 7));
  110. TEST_SIZE(7, clampSize(9, 5, 7));
  111. TEST_SIZE(6, clampSize(6, 5, 7));
  112. TEST_U32(4, minU32(4, 6));
  113. TEST_U32(6, maxU32(4, 6));
  114. TEST_SIZE(4, clampU32(2, 4, 6));
  115. TEST_SIZE(6, clampU32(8, 4, 6));
  116. TEST_SIZE(5, clampU32(5, 4, 6));
  117. }
  118. void testUtility(bool light) {
  119. testPopCount();
  120. testZeroRellocate();
  121. testMemoryInfoList();
  122. testZeroAllocate();
  123. testInterpolate();
  124. testRadianToDegree();
  125. testDegreeToRadian();
  126. testSleep(light ? 5'000'000 : 50'000'000);
  127. testSleep(light ? 50'000'000 : 1'300'000'000);
  128. testSleepMillis(light ? 5 : 50);
  129. testSleepMillis(light ? 50 : 1300);
  130. testSwap();
  131. testFail();
  132. testSort();
  133. testMinMax();
  134. }
  135. static void outOfMemory(void*) {
  136. setOutOfMemoryHandler(nullptr, nullptr);
  137. }
  138. void testInvalidAllocate(void) {
  139. setOutOfMemoryHandler(outOfMemory, nullptr);
  140. coreAllocate(0xFFF'FFFF'FFFF);
  141. TEST_TRUE(false);
  142. finalizeTests();
  143. EXIT(0);
  144. }
  145. void testInvalidReallocate(void) {
  146. setOutOfMemoryHandler(outOfMemory, nullptr);
  147. void* p = coreAllocate(0xFF);
  148. printMemoryReport();
  149. coreReallocate(p, 0xFFF'FFFF'FFFF);
  150. TEST_TRUE(false);
  151. finalizeTests();
  152. EXIT(0);
  153. }
  154. [[noreturn]] void testPreCanary(void) {
  155. #ifdef CHECK_MEMORY
  156. char* p = coreAllocate(16);
  157. p[-1] = 0;
  158. coreFree(p);
  159. TEST_TRUE(false);
  160. #endif
  161. finalizeTests();
  162. EXIT(0);
  163. }
  164. [[noreturn]] void testPostCanary(void) {
  165. #ifdef CHECK_MEMORY
  166. char* p = coreAllocate(16);
  167. p[17] = 0;
  168. coreFree(p);
  169. TEST_TRUE(false);
  170. #endif
  171. finalizeTests();
  172. EXIT(0);
  173. }