testTools.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifndef TEST_TOOLS
  2. #define TEST_TOOLS
  3. #include "../DFPSR/includeFramework.h"
  4. using namespace dsr;
  5. static const int PASSED = 0;
  6. static const int FAILED = 1;
  7. inline bool nearValue(float a, float b) {
  8. return fabs(a - b) < 0.0001f;
  9. }
  10. inline bool nearValue(const FVector2D& a, const FVector2D& b) {
  11. return nearValue(a.x, b.x) && nearValue(a.y, b.y);
  12. }
  13. inline bool nearValue(const FVector3D& a, const FVector3D& b) {
  14. return nearValue(a.x, b.x) && nearValue(a.y, b.y) && nearValue(a.z, b.z);
  15. }
  16. inline bool nearValue(const FVector4D& a, const FVector4D& b) {
  17. return nearValue(a.x, b.x) && nearValue(a.y, b.y) && nearValue(a.z, b.z) && nearValue(a.w, b.w);
  18. }
  19. #define START_TEST(NAME) int main() { printText("Running test \"", #NAME, "\": ");
  20. #define END_TEST printText(" (done)\n"); return PASSED; }
  21. #define OP_EQUALS_STRING(A, B) (string_match(A, B))
  22. #define OP_EQUALS(A, B) ((A) == (B))
  23. #define OP_NOT_EQUALS(A, B) ((A) != (B))
  24. #define OP_LESSER(A, B) ((A) < (B))
  25. #define OP_LESSER_OR_EQUAL(A, B) ((A) <= (B))
  26. #define OP_GREATER(A, B) ((A) > (B))
  27. #define OP_GREATER_OR_EQUAL(A, B) ((A) >= (B))
  28. #define ASSERT(CONDITION) \
  29. if (CONDITION) { \
  30. printText("*"); \
  31. } else { \
  32. printText("\n\n"); \
  33. printText("_______________________________ FAIL _______________________________\n"); \
  34. printText("\n"); \
  35. printText("Failed assertion!\nCondition: ", #CONDITION, "\n"); \
  36. printText("____________________________________________________________________\n"); \
  37. return FAILED; \
  38. }
  39. #define ASSERT_COMP(A, B, OP, OP_NAME) \
  40. if (OP(A, B)) { \
  41. printText("*"); \
  42. } else { \
  43. printText("\n\n"); \
  44. printText("_______________________________ FAIL _______________________________\n"); \
  45. printText("\n"); \
  46. printText("Condition: ", #A, " ", OP_NAME, " ", #B, "\n"); \
  47. printText((A), " ", OP_NAME, " ", (B), " is false.\n"); \
  48. printText("____________________________________________________________________\n"); \
  49. printText("\n\n"); \
  50. return FAILED; \
  51. }
  52. #define ASSERT_MATCH(A, B) ASSERT_COMP(A, B, OP_EQUALS_STRING, "matches")
  53. #define ASSERT_EQUAL(A, B) ASSERT_COMP(A, B, OP_EQUALS, "==")
  54. #define ASSERT_NOT_EQUAL(A, B) ASSERT_COMP(A, B, OP_NOT_EQUALS, "!=")
  55. #define ASSERT_LESSER(A, B) ASSERT_COMP(A, B, OP_LESSER, "<")
  56. #define ASSERT_LESSER_OR_EQUAL(A, B) ASSERT_COMP(A, B, OP_LESSER_OR_EQUAL, "<=")
  57. #define ASSERT_GREATER(A, B) ASSERT_COMP(A, B, OP_GREATER, ">")
  58. #define ASSERT_GREATER_OR_EQUAL(A, B) ASSERT_COMP(A, B, OP_GREATER_OR_EQUAL, ">=")
  59. #define ASSERT_CRASH(A) \
  60. try { \
  61. (void)(A); \
  62. return FAILED; \
  63. } catch(...) {}
  64. #define ASSERT_NEAR(A, B) \
  65. if (nearValue(A, B)) { \
  66. printText("*"); \
  67. } else { \
  68. printText("\n\n"); \
  69. printText("_______________________________ FAIL _______________________________\n"); \
  70. printText("\n"); \
  71. printText("Condition: ", #A, " ≈ ", #B, "\n"); \
  72. printText((A), " is not close enough to ", (B), "\n"); \
  73. printText("____________________________________________________________________\n"); \
  74. printText("\n\n"); \
  75. return FAILED; \
  76. }
  77. const dsr::String inputPath = dsr::string_combine(U"test", file_separator(), U"input", file_separator());
  78. const dsr::String expectedPath = dsr::string_combine(U"test", file_separator(), U"expected", file_separator());
  79. #endif