UtilityTests.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #define IMPORT_CORE
  2. #include "../../src/ErrorSimulator.h"
  3. #include "../Tests.h"
  4. #include "core/Utility.h"
  5. static const float eps = 0.0001f;
  6. static void testPopCount() {
  7. TEST_U64(4, popCount(0xF));
  8. TEST_U64(0, popCount(0x0));
  9. TEST_U64(2, popCount(0x6));
  10. TEST_U64(7, popCount(0x7F));
  11. TEST_U64(3, popCount(0x2A));
  12. TEST_U64(32, popCount(0xFFFFFFFF));
  13. TEST_U64(64, popCount(0xFFFFFFFFFFFFFFFF));
  14. TEST_U64(44, popCount(0xFFFF0FFFFFFF));
  15. }
  16. static void testZeroRellocate() {
  17. void* buffer = cReallocate(nullptr, 16);
  18. TEST_NOT_NULL(buffer);
  19. buffer = cReallocate(buffer, 0);
  20. TEST_NULL(buffer);
  21. }
  22. static void testMemoryInfoList() {
  23. void* a = cAllocate(8);
  24. void* b = cAllocate(8);
  25. void* c = cAllocate(8);
  26. void* d = cAllocate(8);
  27. cFree(b); // remove middle element
  28. cFree(a); // remove first
  29. cFree(d); // remove last
  30. cFree(c); // remove single
  31. cFree(nullptr);
  32. }
  33. static void testInterpolate() {
  34. TEST_FLOAT(7.5f, interpolate(5.0f, 10.0f, 0.5f), eps);
  35. TEST_FLOAT(-2.0, interpolate(-10.0f, 10.0f, 0.4f), eps);
  36. TEST_FLOAT(10.0f, interpolate(-3.0f, 10.0f, 1.0f), eps);
  37. TEST_FLOAT(7.0f, interpolate(7.0f, 10.0f, 0.0f), eps);
  38. TEST_FLOAT(6.0f, interpolate(0.0f, 10.0f, 0.6f), eps);
  39. }
  40. static void testRadianToDegree() {
  41. TEST_FLOAT(45.0f, radianToDegree(PI * 0.25f), eps);
  42. TEST_FLOAT(90.0f, radianToDegree(PI * 0.5f), eps);
  43. TEST_FLOAT(180.0f, radianToDegree(PI), eps);
  44. TEST_FLOAT(360.0f, radianToDegree(PI * 2.0f), eps);
  45. }
  46. static void testDegreeToRadian() {
  47. TEST_FLOAT(PI * 0.25f, degreeToRadian(45.0f), eps);
  48. TEST_FLOAT(PI * 0.5f, degreeToRadian(90.0f), eps);
  49. TEST_FLOAT(PI, degreeToRadian(180.0f), eps);
  50. TEST_FLOAT(PI * 2.0f, degreeToRadian(360.0f), eps);
  51. }
  52. static void testSleep(i64 nanos) {
  53. i64 time = -getNanos();
  54. TEST_FALSE(sleepNanos(nanos));
  55. time += getNanos();
  56. TEST_TRUE(time >= nanos && time <= (nanos * 13) / 10);
  57. }
  58. typedef struct {
  59. int i;
  60. i64 d;
  61. } SwapTest;
  62. static void testSwap() {
  63. SwapTest a = {3, 20};
  64. SwapTest b = {7, 30};
  65. swap(&a, &b);
  66. TEST_INT(7, a.i);
  67. TEST_INT(3, b.i);
  68. TEST_I64(30, a.d);
  69. TEST_I64(20, b.d);
  70. }
  71. static void testFail() {
  72. #ifdef ERROR_SIMULATOR
  73. failTimeGet = true;
  74. TEST_I64(-1, getNanos());
  75. failTimeGet = false;
  76. #endif
  77. }
  78. void testUtility(bool light) {
  79. testPopCount();
  80. testZeroRellocate();
  81. testMemoryInfoList();
  82. testInterpolate();
  83. testRadianToDegree();
  84. testDegreeToRadian();
  85. testSleep(light ? 5'000'000 : 50'000'000);
  86. testSleep(light ? 50'000'000 : 1'300'000'000);
  87. testSwap();
  88. testFail();
  89. }
  90. static void outOfMemory(void*) {
  91. setOutOfMemoryHandler(nullptr, nullptr);
  92. }
  93. void testInvalidAllocate(void) {
  94. setOutOfMemoryHandler(outOfMemory, nullptr);
  95. cAllocate(0xFFFFFFFFFFF);
  96. TEST_TRUE(false);
  97. finalizeTests();
  98. EXIT(0);
  99. }
  100. void testInvalidReallocate(void) {
  101. setOutOfMemoryHandler(outOfMemory, nullptr);
  102. void* p = cAllocate(0xFF);
  103. printMemoryReport();
  104. cReallocate(p, 0xFFFFFFFFFFF);
  105. TEST_TRUE(false);
  106. finalizeTests();
  107. EXIT(0);
  108. }
  109. [[noreturn]] void testPreCanary(void) {
  110. #ifdef CORE_CHECK_MEMORY
  111. char* p = cAllocate(16);
  112. p[-1] = 0;
  113. cFree(p);
  114. TEST_TRUE(false);
  115. #endif
  116. finalizeTests();
  117. EXIT(0);
  118. }
  119. [[noreturn]] void testPostCanary(void) {
  120. #ifdef CORE_CHECK_MEMORY
  121. char* p = cAllocate(16);
  122. p[17] = 0;
  123. cFree(p);
  124. TEST_TRUE(false);
  125. #endif
  126. finalizeTests();
  127. EXIT(0);
  128. }