test_helper.h 1.5 KB

123456789101112131415161718192021222324252627282930313233
  1. #pragma once
  2. #include <exception>
  3. #include <iostream>
  4. class TestFailedException : public std::exception
  5. {
  6. };
  7. // Asserts the comparison specified by CMP is true, or fails the current unit test
  8. #define UASSERTCMP(CMP, actual, expected) \
  9. do { \
  10. const auto &a = (actual); \
  11. const auto &e = (expected); \
  12. if (!CMP(a, e)) { \
  13. std::cout \
  14. << "Test assertion failed: " << #actual << " " << #CMP << " " \
  15. << #expected << std::endl \
  16. << " at " << __FILE__ << ":" << __LINE__ << std::endl \
  17. << " actual: " << a << std::endl \
  18. << " expected: " \
  19. << e << std::endl; \
  20. throw TestFailedException(); \
  21. } \
  22. } while (0)
  23. #define CMPEQ(a, e) (a == e)
  24. #define CMPTRUE(a, e) (a)
  25. #define CMPNE(a, e) (a != e)
  26. #define UASSERTEQ(actual, expected) UASSERTCMP(CMPEQ, actual, expected)
  27. #define UASSERTNE(actual, nexpected) UASSERTCMP(CMPNE, actual, nexpected)
  28. #define UASSERT(actual) UASSERTCMP(CMPTRUE, actual, true)