UtilityTests.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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(0xFFFFFFFF));
  12. TEST_U64(64, popCount(0xFFFFFFFFFFFFFFFF));
  13. TEST_U64(44, popCount(0xFFFF0FFFFFFF));
  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 testInterpolate() {
  33. TEST_FLOAT(7.5f, interpolate(5.0f, 10.0f, 0.5f), eps);
  34. TEST_FLOAT(-2.0, interpolate(-10.0f, 10.0f, 0.4f), eps);
  35. TEST_FLOAT(10.0f, interpolate(-3.0f, 10.0f, 1.0f), eps);
  36. TEST_FLOAT(7.0f, interpolate(7.0f, 10.0f, 0.0f), eps);
  37. TEST_FLOAT(6.0f, interpolate(0.0f, 10.0f, 0.6f), eps);
  38. }
  39. static void testRadianToDegree() {
  40. TEST_FLOAT(45.0f, radianToDegree(PI * 0.25f), eps);
  41. TEST_FLOAT(90.0f, radianToDegree(PI * 0.5f), eps);
  42. TEST_FLOAT(180.0f, radianToDegree(PI), eps);
  43. TEST_FLOAT(360.0f, radianToDegree(PI * 2.0f), eps);
  44. }
  45. static void testDegreeToRadian() {
  46. TEST_FLOAT(PI * 0.25f, degreeToRadian(45.0f), eps);
  47. TEST_FLOAT(PI * 0.5f, degreeToRadian(90.0f), eps);
  48. TEST_FLOAT(PI, degreeToRadian(180.0f), eps);
  49. TEST_FLOAT(PI * 2.0f, degreeToRadian(360.0f), eps);
  50. }
  51. static void testSleep(i64 nanos) {
  52. i64 time = -getNanos();
  53. TEST_FALSE(sleepNanos(nanos));
  54. time += getNanos();
  55. TEST_TRUE(time >= nanos && time <= (nanos * 13) / 10);
  56. }
  57. typedef struct {
  58. int i;
  59. i64 d;
  60. } SwapTest;
  61. static void testSwap() {
  62. SwapTest a = {3, 20};
  63. SwapTest b = {7, 30};
  64. swap(&a, &b);
  65. TEST_INT(7, a.i);
  66. TEST_INT(3, b.i);
  67. TEST_I64(30, a.d);
  68. TEST_I64(20, b.d);
  69. }
  70. static void testFail() {
  71. #ifdef ERROR_SIMULATOR
  72. failTimeGet = true;
  73. TEST_I64(-1, getNanos());
  74. failTimeGet = false;
  75. #endif
  76. }
  77. BUBBLE_SORT(size_t, Size)
  78. #define greateThanSize(a, b) ((a) > (b))
  79. BUBBLE_SORT_SOURCE(size_t, Size, greateThanSize)
  80. static void testSort() {
  81. size_t data[] = {9, 0, 3, 1, 8, 4, 6, 2, 5, 7};
  82. size_t n = ARRAY_LENGTH(data);
  83. bubbleSortSize(data, n);
  84. bubbleSortSize(data, n);
  85. bubbleSortSize(data, 0);
  86. for(size_t i = 0; i < n; i++) {
  87. TEST_SIZE(data[i], i);
  88. }
  89. }
  90. void testUtility(bool light) {
  91. testPopCount();
  92. testZeroRellocate();
  93. testMemoryInfoList();
  94. testInterpolate();
  95. testRadianToDegree();
  96. testDegreeToRadian();
  97. testSleep(light ? 5'000'000 : 50'000'000);
  98. testSleep(light ? 50'000'000 : 1'300'000'000);
  99. testSwap();
  100. testFail();
  101. testSort();
  102. }
  103. static void outOfMemory(void*) {
  104. setOutOfMemoryHandler(nullptr, nullptr);
  105. }
  106. void testInvalidAllocate(void) {
  107. setOutOfMemoryHandler(outOfMemory, nullptr);
  108. coreAllocate(0xFFFFFFFFFFF);
  109. TEST_TRUE(false);
  110. finalizeTests();
  111. EXIT(0);
  112. }
  113. void testInvalidReallocate(void) {
  114. setOutOfMemoryHandler(outOfMemory, nullptr);
  115. void* p = coreAllocate(0xFF);
  116. printMemoryReport();
  117. coreReallocate(p, 0xFFFFFFFFFFF);
  118. TEST_TRUE(false);
  119. finalizeTests();
  120. EXIT(0);
  121. }
  122. [[noreturn]] void testPreCanary(void) {
  123. #ifdef CHECK_MEMORY
  124. char* p = coreAllocate(16);
  125. p[-1] = 0;
  126. coreFree(p);
  127. TEST_TRUE(false);
  128. #endif
  129. finalizeTests();
  130. EXIT(0);
  131. }
  132. [[noreturn]] void testPostCanary(void) {
  133. #ifdef CHECK_MEMORY
  134. char* p = coreAllocate(16);
  135. p[17] = 0;
  136. coreFree(p);
  137. TEST_TRUE(false);
  138. #endif
  139. finalizeTests();
  140. EXIT(0);
  141. }