Macros.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Andrew Kaster <akaster@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Assertions.h>
  9. #include <AK/CheckedFormatString.h>
  10. #include <AK/Math.h>
  11. #include <LibTest/CrashTest.h>
  12. namespace AK {
  13. template<typename... Parameters>
  14. void warnln(CheckedFormatString<Parameters...>&& fmtstr, Parameters const&...);
  15. }
  16. namespace Test {
  17. // Declare a helper so that we can call it from VERIFY in included headers
  18. void current_test_case_did_fail();
  19. }
  20. #undef VERIFY
  21. #define VERIFY(x) \
  22. do { \
  23. if (!(x)) { \
  24. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: VERIFY({}) failed", __FILE__, __LINE__, #x); \
  25. ::Test::current_test_case_did_fail(); \
  26. } \
  27. } while (false)
  28. #undef VERIFY_NOT_REACHED
  29. #define VERIFY_NOT_REACHED() \
  30. do { \
  31. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: VERIFY_NOT_REACHED() called", __FILE__, __LINE__); \
  32. ::abort(); \
  33. } while (false)
  34. #undef TODO
  35. #define TODO() \
  36. do { \
  37. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: TODO() called", __FILE__, __LINE__); \
  38. ::abort(); \
  39. } while (false)
  40. #define EXPECT_EQ(a, b) \
  41. do { \
  42. auto lhs = (a); \
  43. auto rhs = (b); \
  44. if (lhs != rhs) { \
  45. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_EQ({}, {}) failed with lhs={} and rhs={}", __FILE__, __LINE__, #a, #b, FormatIfSupported { lhs }, FormatIfSupported { rhs }); \
  46. ::Test::current_test_case_did_fail(); \
  47. } \
  48. } while (false)
  49. #define EXPECT_EQ_TRUTH(a, b) \
  50. do { \
  51. auto lhs = (a); \
  52. auto rhs = (b); \
  53. bool ltruth = static_cast<bool>(lhs); \
  54. bool rtruth = static_cast<bool>(rhs); \
  55. if (ltruth != rtruth) { \
  56. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_EQ_TRUTH({}, {}) failed with lhs={} ({}) and rhs={} ({})", \
  57. __FILE__, __LINE__, #a, #b, FormatIfSupported { lhs }, ltruth, FormatIfSupported { rhs }, rtruth); \
  58. ::Test::current_test_case_did_fail(); \
  59. } \
  60. } while (false)
  61. // If you're stuck and `EXPECT_EQ` seems to refuse to print anything useful,
  62. // try this: It'll spit out a nice compiler error telling you why it doesn't print.
  63. #define EXPECT_EQ_FORCE(a, b) \
  64. do { \
  65. auto lhs = (a); \
  66. auto rhs = (b); \
  67. if (lhs != rhs) { \
  68. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_EQ({}, {}) failed with lhs={} and rhs={}", __FILE__, __LINE__, #a, #b, lhs, rhs); \
  69. ::Test::current_test_case_did_fail(); \
  70. } \
  71. } while (false)
  72. #define EXPECT_NE(a, b) \
  73. do { \
  74. auto lhs = (a); \
  75. auto rhs = (b); \
  76. if (lhs == rhs) { \
  77. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_NE({}, {}) failed with lhs={} and rhs={}", __FILE__, __LINE__, #a, #b, FormatIfSupported { lhs }, FormatIfSupported { rhs }); \
  78. ::Test::current_test_case_did_fail(); \
  79. } \
  80. } while (false)
  81. #define EXPECT(x) \
  82. do { \
  83. if (!(x)) { \
  84. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT({}) failed", __FILE__, __LINE__, #x); \
  85. ::Test::current_test_case_did_fail(); \
  86. } \
  87. } while (false)
  88. #define EXPECT_APPROXIMATE(a, b) \
  89. do { \
  90. auto expect_close_lhs = a; \
  91. auto expect_close_rhs = b; \
  92. auto expect_close_diff = static_cast<double>(expect_close_lhs) - static_cast<double>(expect_close_rhs); \
  93. if (AK::fabs(expect_close_diff) > 0.0000005) { \
  94. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_APPROXIMATE({}, {})" \
  95. " failed with lhs={}, rhs={}, (lhs-rhs)={}", \
  96. __FILE__, __LINE__, #a, #b, expect_close_lhs, expect_close_rhs, expect_close_diff); \
  97. ::Test::current_test_case_did_fail(); \
  98. } \
  99. } while (false)
  100. #define FAIL(message) \
  101. do { \
  102. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: {}", __FILE__, __LINE__, message); \
  103. ::Test::current_test_case_did_fail(); \
  104. } while (false)
  105. // To use, specify the lambda to execute in a sub process and verify it exits:
  106. // EXPECT_CRASH("This should fail", []{
  107. // return Test::Crash::Failure::DidNotCrash;
  108. // });
  109. #define EXPECT_CRASH(test_message, test_func) \
  110. do { \
  111. Test::Crash crash(test_message, test_func); \
  112. if (!crash.run()) \
  113. ::Test::current_test_case_did_fail(); \
  114. } while (false)
  115. #define EXPECT_CRASH_WITH_SIGNAL(test_message, signal, test_func) \
  116. do { \
  117. Test::Crash crash(test_message, test_func, (signal)); \
  118. if (!crash.run()) \
  119. ::Test::current_test_case_did_fail(); \
  120. } while (false)
  121. #define EXPECT_NO_CRASH(test_message, test_func) \
  122. do { \
  123. Test::Crash crash(test_message, test_func, 0); \
  124. if (!crash.run()) \
  125. ::Test::current_test_case_did_fail(); \
  126. } while (false)
  127. #define TRY_OR_FAIL(expression) \
  128. ({ \
  129. /* Ignore -Wshadow to allow nesting the macro. */ \
  130. AK_IGNORE_DIAGNOSTIC("-Wshadow", \
  131. auto&& _temporary_result = (expression)); \
  132. static_assert(!::AK::Detail::IsLvalueReference<decltype(_temporary_result.release_value())>, \
  133. "Do not return a reference from a fallible expression"); \
  134. if (_temporary_result.is_error()) [[unlikely]] { \
  135. FAIL(_temporary_result.release_error()); \
  136. return; \
  137. } \
  138. _temporary_result.release_value(); \
  139. })