Macros.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <LibTest/CrashTest.h>
  11. namespace AK {
  12. template<typename... Parameters>
  13. void warnln(CheckedFormatString<Parameters...>&& fmtstr, const Parameters&...);
  14. }
  15. namespace Test {
  16. // Declare a helper so that we can call it from VERIFY in included headers
  17. void current_test_case_did_fail();
  18. }
  19. #undef VERIFY
  20. #define VERIFY(x) \
  21. do { \
  22. if (!(x)) { \
  23. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: VERIFY({}) failed", __FILE__, __LINE__, #x); \
  24. ::Test::current_test_case_did_fail(); \
  25. } \
  26. } while (false)
  27. #undef VERIFY_NOT_REACHED
  28. #define VERIFY_NOT_REACHED() \
  29. do { \
  30. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: VERIFY_NOT_REACHED() called", __FILE__, __LINE__); \
  31. ::abort(); \
  32. } while (false)
  33. #undef TODO
  34. #define TODO() \
  35. do { \
  36. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: TODO() called", __FILE__, __LINE__); \
  37. ::abort(); \
  38. } while (false)
  39. #define EXPECT_EQ(a, b) \
  40. do { \
  41. auto lhs = (a); \
  42. auto rhs = (b); \
  43. if (lhs != rhs) { \
  44. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_EQ({}, {}) failed with lhs={} and rhs={}", __FILE__, __LINE__, #a, #b, FormatIfSupported { lhs }, FormatIfSupported { rhs }); \
  45. ::Test::current_test_case_did_fail(); \
  46. } \
  47. } while (false)
  48. // If you're stuck and `EXPECT_EQ` seems to refuse to print anything useful,
  49. // try this: It'll spit out a nice compiler error telling you why it doesn't print.
  50. #define EXPECT_EQ_FORCE(a, b) \
  51. do { \
  52. auto lhs = (a); \
  53. auto rhs = (b); \
  54. if (lhs != rhs) { \
  55. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_EQ({}, {}) failed with lhs={} and rhs={}", __FILE__, __LINE__, #a, #b, lhs, rhs); \
  56. ::Test::current_test_case_did_fail(); \
  57. } \
  58. } while (false)
  59. #define EXPECT_NE(a, b) \
  60. do { \
  61. auto lhs = (a); \
  62. auto rhs = (b); \
  63. if (lhs == rhs) { \
  64. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_NE({}, {}) failed with lhs={} and rhs={}", __FILE__, __LINE__, #a, #b, FormatIfSupported { lhs }, FormatIfSupported { rhs }); \
  65. ::Test::current_test_case_did_fail(); \
  66. } \
  67. } while (false)
  68. #define EXPECT(x) \
  69. do { \
  70. if (!(x)) { \
  71. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT({}) failed", __FILE__, __LINE__, #x); \
  72. ::Test::current_test_case_did_fail(); \
  73. } \
  74. } while (false)
  75. #define EXPECT_APPROXIMATE(a, b) \
  76. do { \
  77. auto expect_close_lhs = a; \
  78. auto expect_close_rhs = b; \
  79. auto expect_close_diff = static_cast<double>(expect_close_lhs) - static_cast<double>(expect_close_rhs); \
  80. if (fabs(expect_close_diff) > 0.0000005) { \
  81. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_APPROXIMATE({}, {})" \
  82. " failed with lhs={}, rhs={}, (lhs-rhs)={}", \
  83. __FILE__, __LINE__, #a, #b, expect_close_lhs, expect_close_rhs, expect_close_diff); \
  84. ::Test::current_test_case_did_fail(); \
  85. } \
  86. } while (false)
  87. #define FAIL(message) \
  88. do { \
  89. ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: {}", __FILE__, __LINE__, message); \
  90. ::Test::current_test_case_did_fail(); \
  91. } while (false)
  92. // To use, specify the lambda to execute in a sub process and verify it exits:
  93. // EXPECT_CRASH("This should fail", []{
  94. // return Test::Crash::Failure::DidNotCrash;
  95. // });
  96. #define EXPECT_CRASH(test_message, test_func) \
  97. do { \
  98. Test::Crash crash(test_message, test_func); \
  99. if (!crash.run()) \
  100. ::Test::current_test_case_did_fail(); \
  101. } while (false)