Macros.h 13 KB

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