testTools.h 6.4 KB

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