Macros.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include <LibTest/Randomized/RandomnessSource.h>
  13. #include <LibTest/TestResult.h>
  14. namespace AK {
  15. template<typename... Parameters>
  16. void warnln(CheckedFormatString<Parameters...>&& fmtstr, Parameters const&...);
  17. }
  18. namespace Test {
  19. // Declare helpers so that we can call them from VERIFY in included headers
  20. // the setter for TestResult is already declared in TestResult.h
  21. TestResult current_test_result();
  22. Randomized::RandomnessSource& randomness_source();
  23. void set_randomness_source(Randomized::RandomnessSource);
  24. bool is_reporting_enabled();
  25. void enable_reporting();
  26. void disable_reporting();
  27. }
  28. #define EXPECT_EQ(a, b) \
  29. do { \
  30. auto lhs = (a); \
  31. auto rhs = (b); \
  32. if (lhs != rhs) { \
  33. if (::Test::is_reporting_enabled()) \
  34. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_EQ({}, {}) failed with lhs={} and rhs={}", \
  35. __FILE__, __LINE__, #a, #b, FormatIfSupported { lhs }, FormatIfSupported { rhs }); \
  36. ::Test::set_current_test_result(::Test::TestResult::Failed); \
  37. } \
  38. } while (false)
  39. #define EXPECT_EQ_TRUTH(a, b) \
  40. do { \
  41. auto lhs = (a); \
  42. auto rhs = (b); \
  43. bool ltruth = static_cast<bool>(lhs); \
  44. bool rtruth = static_cast<bool>(rhs); \
  45. if (ltruth != rtruth) { \
  46. if (::Test::is_reporting_enabled()) \
  47. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_EQ_TRUTH({}, {}) failed with lhs={} ({}) and rhs={} ({})", \
  48. __FILE__, __LINE__, #a, #b, FormatIfSupported { lhs }, ltruth, FormatIfSupported { rhs }, rtruth); \
  49. ::Test::set_current_test_result(::Test::TestResult::Failed); \
  50. } \
  51. } while (false)
  52. // If you're stuck and `EXPECT_EQ` seems to refuse to print anything useful,
  53. // try this: It'll spit out a nice compiler error telling you why it doesn't print.
  54. #define EXPECT_EQ_FORCE(a, b) \
  55. do { \
  56. auto lhs = (a); \
  57. auto rhs = (b); \
  58. if (lhs != rhs) { \
  59. if (::Test::is_reporting_enabled()) \
  60. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_EQ({}, {}) failed with lhs={} and rhs={}", \
  61. __FILE__, __LINE__, #a, #b, lhs, rhs); \
  62. ::Test::set_current_test_result(::Test::TestResult::Failed); \
  63. } \
  64. } while (false)
  65. #define EXPECT_NE(a, b) \
  66. do { \
  67. auto lhs = (a); \
  68. auto rhs = (b); \
  69. if (lhs == rhs) { \
  70. if (::Test::is_reporting_enabled()) \
  71. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_NE({}, {}) failed with lhs={} and rhs={}", \
  72. __FILE__, __LINE__, #a, #b, FormatIfSupported { lhs }, FormatIfSupported { rhs }); \
  73. ::Test::set_current_test_result(::Test::TestResult::Failed); \
  74. } \
  75. } while (false)
  76. #define EXPECT(x) \
  77. do { \
  78. if (!(x)) { \
  79. if (::Test::is_reporting_enabled()) \
  80. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT({}) failed", __FILE__, __LINE__, #x); \
  81. ::Test::set_current_test_result(::Test::TestResult::Failed); \
  82. } \
  83. } while (false)
  84. #define EXPECT_APPROXIMATE_WITH_ERROR(a, b, err) \
  85. do { \
  86. auto expect_close_lhs = a; \
  87. auto expect_close_rhs = b; \
  88. auto expect_close_diff = static_cast<double>(expect_close_lhs) - static_cast<double>(expect_close_rhs); \
  89. if (AK::fabs(expect_close_diff) > (err)) { \
  90. if (::Test::is_reporting_enabled()) \
  91. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_APPROXIMATE({}, {})" \
  92. " failed with lhs={}, rhs={}, (lhs-rhs)={}", \
  93. __FILE__, __LINE__, #a, #b, expect_close_lhs, expect_close_rhs, expect_close_diff); \
  94. ::Test::set_current_test_result(::Test::TestResult::Failed); \
  95. } \
  96. } while (false)
  97. #define EXPECT_APPROXIMATE(a, b) EXPECT_APPROXIMATE_WITH_ERROR(a, b, 0.0000005)
  98. #define REJECT(message) \
  99. do { \
  100. if (::Test::is_reporting_enabled()) \
  101. ::AK::warnln("\033[31;1mREJECTED\033[0m: {}:{}: {}", \
  102. __FILE__, __LINE__, #message); \
  103. ::Test::set_current_test_result(::Test::TestResult::Rejected); \
  104. } while (false)
  105. #define ASSUME(x) \
  106. do { \
  107. if (!(x)) { \
  108. if (::Test::is_reporting_enabled()) \
  109. ::AK::warnln("\033[31;1mREJECTED\033[0m: {}:{}: Couldn't generate random value satisfying ASSUME({})", \
  110. __FILE__, __LINE__, #x); \
  111. ::Test::set_current_test_result(::Test::TestResult::Rejected); \
  112. return; \
  113. } \
  114. } while (false)
  115. #define FAIL(message) \
  116. do { \
  117. if (::Test::is_reporting_enabled()) \
  118. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: {}", __FILE__, __LINE__, message); \
  119. ::Test::set_current_test_result(::Test::TestResult::Failed); \
  120. } while (false)
  121. // To use, specify the lambda to execute in a sub process and verify it exits:
  122. // EXPECT_CRASH("This should fail", []{
  123. // return Test::Crash::Failure::DidNotCrash;
  124. // });
  125. #define EXPECT_CRASH(test_message, test_func) \
  126. do { \
  127. Test::Crash crash(test_message, test_func); \
  128. if (!crash.run()) \
  129. ::Test::set_current_test_result(::Test::TestResult::Failed); \
  130. } while (false)
  131. #define EXPECT_CRASH_WITH_SIGNAL(test_message, signal, test_func) \
  132. do { \
  133. Test::Crash crash(test_message, test_func, (signal)); \
  134. if (!crash.run()) \
  135. ::Test::set_current_test_result(::Test::TestResult::Failed); \
  136. } while (false)
  137. #define EXPECT_NO_CRASH(test_message, test_func) \
  138. do { \
  139. Test::Crash crash(test_message, test_func, 0); \
  140. if (!crash.run()) \
  141. ::Test::set_current_test_result(::Test::TestResult::Failed); \
  142. } while (false)
  143. #define TRY_OR_FAIL(expression) \
  144. ({ \
  145. /* Ignore -Wshadow to allow nesting the macro. */ \
  146. AK_IGNORE_DIAGNOSTIC("-Wshadow", \
  147. auto&& _temporary_result = (expression)); \
  148. static_assert(!::AK::Detail::IsLvalueReference<decltype(_temporary_result.release_value())>, \
  149. "Do not return a reference from a fallible expression"); \
  150. if (_temporary_result.is_error()) [[unlikely]] { \
  151. FAIL(_temporary_result.release_error()); \
  152. return; \
  153. } \
  154. _temporary_result.release_value(); \
  155. })