testTools.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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(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. #define ASSERT(CONDITION) \
  28. if (CONDITION) { \
  29. printText("*"); \
  30. } else { \
  31. printText("\n\n"); \
  32. printText("_______________________________ FAIL _______________________________\n"); \
  33. printText("\n"); \
  34. printText("Failed assertion!\nCondition: ", #CONDITION, "\n"); \
  35. printText("____________________________________________________________________\n"); \
  36. return FAILED; \
  37. }
  38. #define ASSERT_COMP(A, B, OP, OP_NAME) \
  39. if (OP(A, B)) { \
  40. printText("*"); \
  41. } else { \
  42. printText("\n\n"); \
  43. printText("_______________________________ FAIL _______________________________\n"); \
  44. printText("\n"); \
  45. printText("Condition: ", #A, " ", OP_NAME, " ", #B, "\n"); \
  46. printText((A), " ", OP_NAME, " ", (B), " is false.\n"); \
  47. printText("____________________________________________________________________\n"); \
  48. printText("\n\n"); \
  49. return FAILED; \
  50. }
  51. #define ASSERT_EQUAL(A, B) ASSERT_COMP(A, B, OP_EQUALS, "==")
  52. #define ASSERT_NOT_EQUAL(A, B) ASSERT_COMP(A, B, OP_NOT_EQUALS, "!=")
  53. #define ASSERT_LESSER(A, B) ASSERT_COMP(A, B, OP_LESSER, "<")
  54. #define ASSERT_LESSER_OR_EQUAL(A, B) ASSERT_COMP(A, B, OP_LESSER_OR_EQUAL, "<=")
  55. #define ASSERT_GREATER(A, B) ASSERT_COMP(A, B, OP_GREATER, ">")
  56. #define ASSERT_GREATER_OR_EQUAL(A, B) ASSERT_COMP(A, B, OP_GREATER_OR_EQUAL, ">=")
  57. #define ASSERT_CRASH(A) \
  58. try { \
  59. (void)(A); \
  60. return FAILED; \
  61. } catch(...) {}
  62. #define ASSERT_NEAR(A, B) \
  63. if (nearValue(A, B)) { \
  64. printText("*"); \
  65. } else { \
  66. printText("\n\n"); \
  67. printText("_______________________________ FAIL _______________________________\n"); \
  68. printText("\n"); \
  69. printText("Condition: ", #A, " ≈ ", #B, "\n"); \
  70. printText((A), " is not close enough to ", (B), "\n"); \
  71. printText("____________________________________________________________________\n"); \
  72. printText("\n\n"); \
  73. return FAILED; \
  74. }
  75. const dsr::String inputPath = dsr::string_combine(U"test", file_separator(), U"input", file_separator());
  76. const dsr::String expectedPath = dsr::string_combine(U"test", file_separator(), U"expected", file_separator());
  77. #endif