UtilityTests.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 = cReallocate(nullptr, 16);
  17. TEST_NOT_NULL(buffer);
  18. buffer = cReallocate(buffer, 0);
  19. TEST_NULL(buffer);
  20. }
  21. static void testMemoryInfoList() {
  22. void* a = cAllocate(8);
  23. void* b = cAllocate(8);
  24. void* c = cAllocate(8);
  25. void* d = cAllocate(8);
  26. cFree(b); // remove middle element
  27. cFree(a); // remove first
  28. cFree(d); // remove last
  29. cFree(c); // remove single
  30. cFree(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. static void testFail() {
  58. #ifdef ERROR_SIMULATOR
  59. failTimeGet = true;
  60. TEST_I64(-1, getNanos());
  61. failTimeGet = false;
  62. #endif
  63. }
  64. void testUtility(bool light) {
  65. testPopCount();
  66. testZeroRellocate();
  67. testMemoryInfoList();
  68. testInterpolate();
  69. testRadianToDegree();
  70. testDegreeToRadian();
  71. testSleep(light ? 5'000'000 : 50'000'000);
  72. testSleep(light ? 50'000'000 : 1'300'000'000);
  73. testFail();
  74. }
  75. static void outOfMemory(void*) {
  76. setOutOfMemoryHandler(nullptr, nullptr);
  77. }
  78. void testInvalidAllocate(void) {
  79. setOutOfMemoryHandler(outOfMemory, nullptr);
  80. cAllocate(0xFFFFFFFFFFF);
  81. TEST_TRUE(false);
  82. finalizeTests();
  83. EXIT(0);
  84. }
  85. void testInvalidReallocate(void) {
  86. setOutOfMemoryHandler(outOfMemory, nullptr);
  87. void* p = cAllocate(0xFF);
  88. printMemoryReport();
  89. cReallocate(p, 0xFFFFFFFFFFF);
  90. TEST_TRUE(false);
  91. finalizeTests();
  92. EXIT(0);
  93. }
  94. [[noreturn]] void testPreCanary(void) {
  95. #ifdef CHECK_MEMORY
  96. char* p = cAllocate(16);
  97. p[-1] = 0;
  98. cFree(p);
  99. TEST_TRUE(false);
  100. #endif
  101. finalizeTests();
  102. EXIT(0);
  103. }
  104. [[noreturn]] void testPostCanary(void) {
  105. #ifdef CHECK_MEMORY
  106. char* p = cAllocate(16);
  107. p[17] = 0;
  108. cFree(p);
  109. TEST_TRUE(false);
  110. #endif
  111. finalizeTests();
  112. EXIT(0);
  113. }