testTools.h 6.5 KB

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