UtilityTests.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. void testUtility(bool light) {
  78. testPopCount();
  79. testZeroRellocate();
  80. testMemoryInfoList();
  81. testInterpolate();
  82. testRadianToDegree();
  83. testDegreeToRadian();
  84. testSleep(light ? 5'000'000 : 50'000'000);
  85. testSleep(light ? 50'000'000 : 1'300'000'000);
  86. testSwap();
  87. testFail();
  88. }
  89. static void outOfMemory(void*) {
  90. setOutOfMemoryHandler(nullptr, nullptr);
  91. }
  92. void testInvalidAllocate(void) {
  93. setOutOfMemoryHandler(outOfMemory, nullptr);
  94. coreAllocate(0xFFFFFFFFFFF);
  95. TEST_TRUE(false);
  96. finalizeTests();
  97. EXIT(0);
  98. }
  99. void testInvalidReallocate(void) {
  100. setOutOfMemoryHandler(outOfMemory, nullptr);
  101. void* p = coreAllocate(0xFF);
  102. printMemoryReport();
  103. coreReallocate(p, 0xFFFFFFFFFFF);
  104. TEST_TRUE(false);
  105. finalizeTests();
  106. EXIT(0);
  107. }
  108. [[noreturn]] void testPreCanary(void) {
  109. #ifdef CHECK_MEMORY
  110. char* p = coreAllocate(16);
  111. p[-1] = 0;
  112. coreFree(p);
  113. TEST_TRUE(false);
  114. #endif
  115. finalizeTests();
  116. EXIT(0);
  117. }
  118. [[noreturn]] void testPostCanary(void) {
  119. #ifdef CHECK_MEMORY
  120. char* p = coreAllocate(16);
  121. p[17] = 0;
  122. coreFree(p);
  123. TEST_TRUE(false);
  124. #endif
  125. finalizeTests();
  126. EXIT(0);
  127. }