testCommon.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include <functional> // IWYU pragma: export
  5. #include <initializer_list> // IWYU pragma: export
  6. #include <iostream> // IWYU pragma: export
  7. #define ASSERT_TRUE(x) \
  8. do { \
  9. if (!(x)) { \
  10. std::cout << "ASSERT_TRUE(" #x ") failed on line " << __LINE__ << '\n'; \
  11. return false; \
  12. } \
  13. } while (false)
  14. #define ASSERT_EQUAL(actual, expected) \
  15. do { \
  16. if (!((actual) == (expected))) { \
  17. std::cout << "ASSERT_EQUAL(" #actual ", " #expected ") failed on line " \
  18. << __LINE__ << '\n'; \
  19. std::cout << " Actual: '" << (actual) << "'\n"; \
  20. std::cout << "Expected: '" << (expected) << "'\n"; \
  21. return false; \
  22. } \
  23. } while (false)
  24. #define BOOL_STRING(b) ((b) ? "TRUE" : "FALSE")
  25. namespace {
  26. inline int runTests(std::initializer_list<std::function<bool()>> tests,
  27. bool const fail_fast = true)
  28. {
  29. int result = 0;
  30. for (auto const& test : tests) {
  31. if (!test()) {
  32. result = 1;
  33. if (fail_fast) {
  34. break;
  35. }
  36. }
  37. }
  38. if (result == 0) {
  39. std::cout << "Passed\n";
  40. }
  41. return result;
  42. }
  43. }