testTools.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #ifndef TEST_TOOLS
  2. #define TEST_TOOLS
  3. #include "../DFPSR/includeEssentials.h"
  4. #include "../DFPSR/api/algorithmAPI.h"
  5. #include "../DFPSR/math/FVector.h"
  6. #include <cstdlib>
  7. #include <csignal>
  8. #include <stdio.h>
  9. using namespace dsr;
  10. static bool beginsWith(const ReadableString &message, const ReadableString &prefix) {
  11. // Reading a character out of bound safely returns \0 by value, so we can rely
  12. // on the null character to exit early if message is storter than prefix.
  13. for (int c = 0; c < string_length(prefix); c++) {
  14. if (message[c] != prefix[c]) {
  15. return false;
  16. }
  17. }
  18. return true;
  19. }
  20. static thread_local String ExpectedErrorPrefix;
  21. #define OP_EQUALS(A, B) ((A) == (B))
  22. #define OP_NOT_EQUALS(A, B) ((A) != (B))
  23. #define OP_LESSER(A, B) ((A) < (B))
  24. #define OP_LESSER_OR_EQUAL(A, B) ((A) <= (B))
  25. #define OP_GREATER(A, B) ((A) > (B))
  26. #define OP_GREATER_OR_EQUAL(A, B) ((A) >= (B))
  27. inline bool OP_NEAR(float a, float b) {
  28. return a > b - 0.0001f && a < b + 0.0001f;
  29. }
  30. inline bool OP_NEAR(const FVector2D& a, const FVector2D& b) {
  31. return OP_NEAR(a.x, b.x) && OP_NEAR(a.y, b.y);
  32. }
  33. inline bool OP_NEAR(const FVector3D& a, const FVector3D& b) {
  34. return OP_NEAR(a.x, b.x) && OP_NEAR(a.y, b.y) && OP_NEAR(a.z, b.z);
  35. }
  36. inline bool OP_NEAR(const FVector4D& a, const FVector4D& b) {
  37. return OP_NEAR(a.x, b.x) && OP_NEAR(a.y, b.y) && OP_NEAR(a.z, b.z) && OP_NEAR(a.w, b.w);
  38. }
  39. static void messageHandler(const ReadableString &message, MessageType type) {
  40. if (type == MessageType::Error) {
  41. if (string_length(ExpectedErrorPrefix) == 0) {
  42. // Unexpected error!
  43. string_sendMessage_default(message, MessageType::Error);
  44. } else {
  45. // Expected error.
  46. if (!beginsWith(message, ExpectedErrorPrefix)) {
  47. string_sendMessage_default(string_combine(U"Unexpected message in error!\n\nMessage:\n", message, U"\n\nExpected prefix:\n", ExpectedErrorPrefix, U"\n\n"), MessageType::Error);
  48. } else {
  49. string_sendMessage_default(U"*", MessageType::StandardPrinting);
  50. }
  51. }
  52. } else {
  53. // Forward everything to the default message handler.
  54. string_sendMessage_default(message, type);
  55. }
  56. }
  57. static void handleArguments(const List<String> &args) {
  58. for (int i = 1; i < args.length(); i++) {
  59. String key = string_upperCase(args[i]);
  60. String value;
  61. if (i + 1 < args.length()) {
  62. value = args[i + 1];
  63. }
  64. if (string_match(key, U"-P") || string_match(key, U"--PATH")) {
  65. file_setCurrentPath(value);
  66. }
  67. }
  68. }
  69. static thread_local String testName = U"Uninitialized test\n";
  70. static thread_local String stateName = U"New thread\n";
  71. static bool failed = false;
  72. #define START_TEST(NAME) \
  73. DSR_MAIN_CALLER(dsrMain) \
  74. void dsrMain(List<String> args) \
  75. { \
  76. testName = #NAME; \
  77. stateName = U"While Assigning message handler"; \
  78. std::signal(SIGSEGV, [](int signal) { failed = true; throwError(U"Segmentation fault from ", testName, U"! ", stateName); }); \
  79. string_assignMessageHandler(&messageHandler); \
  80. stateName = U"While handling arguments\n"; \
  81. handleArguments(args); \
  82. stateName = U"Test start\n"; \
  83. printText(U"Running test \"", #NAME, "\":\n "); \
  84. intptr_t startAllocationCount = heap_getAllocationCount(); \
  85. heap_forAllHeapAllocations([](AllocationHeader * header, void * allocation) { \
  86. heap_setAllocationCustomFlags(header, 1u); \
  87. }); \
  88. {
  89. #define END_TEST \
  90. } \
  91. printText(U" (done)\n"); \
  92. stateName = U"After test end\n"; \
  93. if (failed) { \
  94. heap_terminatingApplication(); \
  95. exit(1); \
  96. } \
  97. }
  98. // These can be used instead of ASSERT_CRASH to handle multiple template arguments that are not enclosed within ().
  99. #define BEGIN_CRASH(PREFIX) \
  100. ExpectedErrorPrefix = PREFIX; \
  101. stateName = string_combine(U"During expected crash starting with ", PREFIX, U"\n");
  102. #define END_CRASH \
  103. ExpectedErrorPrefix = "";
  104. // Prefix is the expected start of the error message.
  105. // Just enough to know that we triggered the right error message.
  106. #define ASSERT_CRASH(A, PREFIX) \
  107. BEGIN_CRASH(PREFIX); \
  108. (void)(A); \
  109. END_CRASH \
  110. stateName = string_combine(U"After expected crash starting with ", PREFIX, U"\n");
  111. #define ASSERT(CONDITION) \
  112. { \
  113. stateName = string_combine(U"While evaluating condition ", #CONDITION, U"\n"); \
  114. if (CONDITION) { \
  115. printText(U"*"); \
  116. } else { \
  117. printText( \
  118. U"\n\n", \
  119. U"_______________________________ FAIL _______________________________\n", \
  120. U"\n", \
  121. U"Failed assertion!\nCondition: ", #CONDITION, U"\n", \
  122. U"____________________________________________________________________\n" \
  123. ); \
  124. failed = true; \
  125. } \
  126. }
  127. #define ASSERT_COMP(A, B, OP, OP_NAME) \
  128. { \
  129. stateName = string_combine(U"While evaluating condition ", #A, " ", OP_NAME, U" ", #B, U"\n"); \
  130. auto lhs = A; \
  131. auto rhs = B; \
  132. if (OP(lhs, rhs)) { \
  133. printText(U"*"); \
  134. } else { \
  135. printText( \
  136. U"\n\n", \
  137. U"_______________________________ FAIL _______________________________\n", \
  138. U"\n", \
  139. U"Condition: ", #A, " ", OP_NAME, U" ", #B, U"\n", \
  140. lhs, " ", OP_NAME, " ", rhs, U" is false.\n", \
  141. U"____________________________________________________________________\n" \
  142. ); \
  143. failed = true; \
  144. } \
  145. }
  146. #define ASSERT_EQUAL(A, B) ASSERT_COMP(A, B, OP_EQUALS, "==")
  147. #define ASSERT_NOT_EQUAL(A, B) ASSERT_COMP(A, B, OP_NOT_EQUALS, "!=")
  148. #define ASSERT_LESSER(A, B) ASSERT_COMP(A, B, OP_LESSER, "<")
  149. #define ASSERT_LESSER_OR_EQUAL(A, B) ASSERT_COMP(A, B, OP_LESSER_OR_EQUAL, "<=")
  150. #define ASSERT_GREATER(A, B) ASSERT_COMP(A, B, OP_GREATER, ">")
  151. #define ASSERT_GREATER_OR_EQUAL(A, B) ASSERT_COMP(A, B, OP_GREATER_OR_EQUAL, ">=")
  152. #define ASSERT_NEAR(A, B) ASSERT_COMP(A, B, OP_NEAR, "==")
  153. #define ASSERT_HEAP_DEPTH(DEPTH) { \
  154. intptr_t currentAllocationCount = heap_getAllocationCount(); \
  155. intptr_t expectedAllocationCount = startAllocationCount + (DEPTH); \
  156. if (currentAllocationCount != expectedAllocationCount) { \
  157. printf( \
  158. "\n\n" \
  159. "_______________________________ FAIL _______________________________\n" \
  160. "\n" \
  161. "Expected allocation count %i but found %i allocations instead.\n" \
  162. "____________________________________________________________________\n" \
  163. , (int)expectedAllocationCount, (int)currentAllocationCount); \
  164. failed = true; \
  165. } \
  166. }
  167. const String inputPath = string_combine(U"test", file_separator(), U"input", file_separator());
  168. const String expectedPath = string_combine(U"test", file_separator(), U"expected", file_separator());
  169. #endif