Macros.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, const Parameters&...);
  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. // If you're stuck and `EXPECT_EQ` seems to refuse to print anything useful,
  50. // try this: It'll spit out a nice compiler error telling you why it doesn't print.
  51. #define EXPECT_EQ_FORCE(a, b) \
  52. do { \
  53. auto lhs = (a); \
  54. auto rhs = (b); \
  55. if (lhs != rhs) { \
  56. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_EQ({}, {}) failed with lhs={} and rhs={}", __FILE__, __LINE__, #a, #b, lhs, rhs); \
  57. ::Test::current_test_case_did_fail(); \
  58. } \
  59. } while (false)
  60. #define EXPECT_NE(a, b) \
  61. do { \
  62. auto lhs = (a); \
  63. auto rhs = (b); \
  64. if (lhs == rhs) { \
  65. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_NE({}, {}) failed with lhs={} and rhs={}", __FILE__, __LINE__, #a, #b, FormatIfSupported { lhs }, FormatIfSupported { rhs }); \
  66. ::Test::current_test_case_did_fail(); \
  67. } \
  68. } while (false)
  69. #define EXPECT(x) \
  70. do { \
  71. if (!(x)) { \
  72. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT({}) failed", __FILE__, __LINE__, #x); \
  73. ::Test::current_test_case_did_fail(); \
  74. } \
  75. } while (false)
  76. #define EXPECT_APPROXIMATE(a, b) \
  77. do { \
  78. auto expect_close_lhs = a; \
  79. auto expect_close_rhs = b; \
  80. auto expect_close_diff = static_cast<double>(expect_close_lhs) - static_cast<double>(expect_close_rhs); \
  81. if (AK::fabs(expect_close_diff) > 0.0000005) { \
  82. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_APPROXIMATE({}, {})" \
  83. " failed with lhs={}, rhs={}, (lhs-rhs)={}", \
  84. __FILE__, __LINE__, #a, #b, expect_close_lhs, expect_close_rhs, expect_close_diff); \
  85. ::Test::current_test_case_did_fail(); \
  86. } \
  87. } while (false)
  88. #define FAIL(message) \
  89. do { \
  90. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: {}", __FILE__, __LINE__, message); \
  91. ::Test::current_test_case_did_fail(); \
  92. } while (false)
  93. // To use, specify the lambda to execute in a sub process and verify it exits:
  94. // EXPECT_CRASH("This should fail", []{
  95. // return Test::Crash::Failure::DidNotCrash;
  96. // });
  97. #define EXPECT_CRASH(test_message, test_func) \
  98. do { \
  99. Test::Crash crash(test_message, test_func); \
  100. if (!crash.run()) \
  101. ::Test::current_test_case_did_fail(); \
  102. } while (false)