testTools.h 5.6 KB

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