UtilityTests.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include <cstring>
  2. #include "../../src/ErrorSimulator.hpp"
  3. #include "../Tests.hpp"
  4. #include "core/ToString.hpp"
  5. #include "core/Utility.hpp"
  6. static const float eps = 0.0001f;
  7. static void testPopCount() {
  8. TEST(4lu, popCount(0xF));
  9. TEST(0lu, popCount(0x0));
  10. TEST(2lu, popCount(0x6));
  11. TEST(7lu, popCount(0x7F));
  12. TEST(3lu, popCount(0x2A));
  13. TEST(32lu, popCount(0xFFFF'FFFF));
  14. TEST(64lu, popCount(0xFFFF'FFFF'FFFF'FFFF));
  15. TEST(44lu, popCount(0xFFFF'0FFF'FFFF));
  16. }
  17. static void testZeroRellocate() {
  18. void* buffer = coreReallocate(nullptr, 16);
  19. TEST_NOT_NULL(buffer);
  20. buffer = coreReallocate(buffer, 0);
  21. TEST_NULL(buffer);
  22. }
  23. static void testMemoryInfoList() {
  24. void* a = coreAllocate(8);
  25. void* b = coreAllocate(8);
  26. void* c = coreAllocate(8);
  27. void* d = coreAllocate(8);
  28. coreFree(b); // remove middle element
  29. coreFree(a); // remove first
  30. coreFree(d); // remove last
  31. coreFree(c); // remove single
  32. coreFree(nullptr);
  33. }
  34. static void testZeroAllocate() {
  35. constexpr size_t n = 1024 * 1024;
  36. void* a = coreAllocate(n);
  37. memset(a, 0, n);
  38. void* b = coreZeroAllocate(n);
  39. TEST_TRUE(memcmp(a, b, n) == 0);
  40. coreFree(a);
  41. coreFree(b);
  42. }
  43. static void testInterpolate() {
  44. TEST_FLOAT(7.5f, interpolate(5.0f, 10.0f, 0.5f), eps);
  45. TEST_FLOAT(-2.0, interpolate(-10.0f, 10.0f, 0.4f), eps);
  46. TEST_FLOAT(10.0f, interpolate(-3.0f, 10.0f, 1.0f), eps);
  47. TEST_FLOAT(7.0f, interpolate(7.0f, 10.0f, 0.0f), eps);
  48. TEST_FLOAT(6.0f, interpolate(0.0f, 10.0f, 0.6f), eps);
  49. }
  50. static void testRadianToDegree() {
  51. TEST_FLOAT(45.0f, radianToDegree(PI * 0.25f), eps);
  52. TEST_FLOAT(90.0f, radianToDegree(PI * 0.5f), eps);
  53. TEST_FLOAT(180.0f, radianToDegree(PI), eps);
  54. TEST_FLOAT(360.0f, radianToDegree(PI * 2.0f), eps);
  55. }
  56. static void testDegreeToRadian() {
  57. TEST_FLOAT(PI * 0.25f, degreeToRadian(45.0f), eps);
  58. TEST_FLOAT(PI * 0.5f, degreeToRadian(90.0f), eps);
  59. TEST_FLOAT(PI, degreeToRadian(180.0f), eps);
  60. TEST_FLOAT(PI * 2.0f, degreeToRadian(360.0f), eps);
  61. }
  62. static void testSleep(i64 nanos) {
  63. i64 time = -getNanos();
  64. TEST_FALSE(sleepNanos(nanos));
  65. time += getNanos();
  66. TEST_TRUE(time >= nanos && time <= (nanos * 13) / 10);
  67. }
  68. static void testSleepMillis(i64 millis) {
  69. i64 time = -getNanos();
  70. TEST_FALSE(sleepMillis(millis));
  71. time += getNanos();
  72. i64 nanos = millis * 1'000'000;
  73. TEST_TRUE(time >= nanos && time <= (nanos * 13) / 10);
  74. }
  75. typedef struct {
  76. int i;
  77. i64 d;
  78. } SwapTest;
  79. static void testSwap() {
  80. SwapTest a = {3, 20};
  81. SwapTest b = {7, 30};
  82. Core::swap(a, b);
  83. TEST(7, a.i);
  84. TEST(3, b.i);
  85. TEST(30l, a.d);
  86. TEST(20l, b.d);
  87. }
  88. static void testFail() {
  89. #ifdef ERROR_SIMULATOR
  90. failStepThrow = 1;
  91. TEST(-1l, getNanos());
  92. failStepThrow = 1;
  93. TEST_TRUE(sleepMillis(5));
  94. failStepThrow = 0;
  95. #endif
  96. }
  97. static void testSort() {
  98. size_t data[] = {9, 0, 3, 1, 8, 4, 6, 2, 5, 7};
  99. size_t n = ARRAY_LENGTH(data);
  100. bubbleSort(data, n);
  101. bubbleSort(data, n);
  102. bubbleSort(data, 0);
  103. for(size_t i = 0; i < n; i++) {
  104. TEST(data[i], i);
  105. }
  106. }
  107. static void testMinMax() {
  108. TEST(5, min<int>(5, 7));
  109. TEST(7, max<int>(5, 7));
  110. TEST(5, clamp<int>(3, 5, 7));
  111. TEST(7, clamp<int>(9, 5, 7));
  112. TEST(6, clamp<int>(6, 5, 7));
  113. }
  114. static void testToString() {
  115. char buffer[512];
  116. formatBuffer(buffer, sizeof(buffer), "aa##a", 1, 2, 3);
  117. TEST_STRING("aa#a123", buffer);
  118. }
  119. void testUtility(bool light) {
  120. testPopCount();
  121. testZeroRellocate();
  122. testMemoryInfoList();
  123. testZeroAllocate();
  124. testInterpolate();
  125. testRadianToDegree();
  126. testDegreeToRadian();
  127. testSleep(light ? 5'000'000 : 50'000'000);
  128. testSleep(light ? 50'000'000 : 1'300'000'000);
  129. testSleepMillis(light ? 5 : 50);
  130. testSleepMillis(light ? 50 : 1300);
  131. testSwap();
  132. testFail();
  133. testSort();
  134. testMinMax();
  135. testToString();
  136. }
  137. static void outOfMemory(void*) {
  138. setOutOfMemoryHandler(nullptr, nullptr);
  139. }
  140. void testInvalidAllocate(void) {
  141. setOutOfMemoryHandler(outOfMemory, nullptr);
  142. coreAllocate(0xFFF'FFFF'FFFF);
  143. TEST_TRUE(false);
  144. finalizeTests();
  145. EXIT(0);
  146. }
  147. void testInvalidReallocate(void) {
  148. setOutOfMemoryHandler(outOfMemory, nullptr);
  149. void* p = coreAllocate(0xFF);
  150. printMemoryReport();
  151. coreReallocate(p, 0xFFF'FFFF'FFFF);
  152. TEST_TRUE(false);
  153. finalizeTests();
  154. EXIT(0);
  155. }
  156. [[noreturn]] void testPreCanary(void) {
  157. #ifdef CHECK_MEMORY
  158. char* p = static_cast<char*>(coreAllocate(16));
  159. p[-1] = 0;
  160. coreFree(p);
  161. TEST_TRUE(false);
  162. #endif
  163. finalizeTests();
  164. EXIT(0);
  165. }
  166. [[noreturn]] void testPreCanaryNew(void) {
  167. #ifdef CHECK_MEMORY
  168. volatile char* p2 = new char[3];
  169. volatile char* p = coreNewN(char, 16);
  170. delete[] p2;
  171. coreDeleteN(p);
  172. p = coreNewN(char, 16);
  173. p[-1] = 0;
  174. coreDeleteN(p);
  175. TEST_TRUE(false);
  176. #endif
  177. finalizeTests();
  178. EXIT(0);
  179. }
  180. [[noreturn]] void testPreCanaryNewArray(void) {
  181. #ifdef CHECK_MEMORY
  182. volatile char* p = coreNew(char);
  183. p[-1] = 0;
  184. coreDelete(p);
  185. TEST_TRUE(false);
  186. #endif
  187. finalizeTests();
  188. EXIT(0);
  189. }
  190. [[noreturn]] void testPostCanary(void) {
  191. #ifdef CHECK_MEMORY
  192. char* p = static_cast<char*>(coreAllocate(16));
  193. p[17] = 0;
  194. coreFree(p);
  195. TEST_TRUE(false);
  196. #endif
  197. finalizeTests();
  198. EXIT(0);
  199. }