diff --git a/AK/StdLibExtras.h b/AK/StdLibExtras.h index 4b38a7764c..cf13cfbf7a 100644 --- a/AK/StdLibExtras.h +++ b/AK/StdLibExtras.h @@ -40,7 +40,6 @@ inline constexpr unsigned round_up_to_power_of_two(unsigned value, unsigned powe namespace AK { - template constexpr SizeType array_size(T (&)[N]) { @@ -501,6 +500,9 @@ template using CopyConst = typename Conditional::value, typename AddConst::Type, typename RemoveConst::Type>::Type; +template +using Void = void; + } using AK::AddConst; @@ -523,3 +525,4 @@ using AK::min; using AK::move; using AK::RemoveConst; using AK::swap; +using AK::Void; diff --git a/AK/TestSuite.h b/AK/TestSuite.h index 6098c3b42f..797a7b4219 100644 --- a/AK/TestSuite.h +++ b/AK/TestSuite.h @@ -220,6 +220,35 @@ void TestSuite::run(const NonnullRefPtrVector& tests) << m_testtime << " tests, " << m_benchtime << " benchmarks, " << (global_timer.elapsed_milliseconds() - (m_testtime + m_benchtime)) << " other)"; } +// Use SFINAE to print if we can. +// This trick is good enough for TestSuite.h, but not flexible enough to be put into LogStream.h. +template +struct MaybeStream { + static const Stream& call(const Stream& stream, const LHS&, const RHS&) + { + return stream; + } +}; +template +struct MaybeStream(0) << "" << *reinterpret_cast(0) << "" << *reinterpret_cast(0) << "")>> { + static const Stream& call(const Stream& stream, const LHS& lhs, const RHS& rhs) + { + return stream << ": LHS=\"" << lhs << "\", RHS=\"" << rhs << "\""; + } +}; +template +static const Stream& maybe_print_rhs_lhs(const Stream& stream, const LHS& lhs, const RHS& rhs) +{ + return MaybeStream::call(stream, lhs, rhs); +} +template +static const Stream& force_print_rhs_lhs(const Stream& stream, const LHS& lhs, const RHS& rhs) +{ + using _ = decltype(*reinterpret_cast(0) << "" << *reinterpret_cast(0) << "" << *reinterpret_cast(0) << ""); + (void)sizeof(_); + return MaybeStream::call(stream, lhs, rhs); +} + } using AK::TestCase; @@ -263,10 +292,22 @@ using AK::TestSuite; TestSuite::release(); \ } -#define EXPECT_EQ(a, b) \ - { \ - if ((a) != (b)) \ - warn() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": EXPECT_EQ(" #a ", " #b ") failed"; \ +#define EXPECT_EQ(a, b) \ + { \ + auto lhs = (a); \ + auto rhs = (b); \ + if (lhs != rhs) \ + AK::maybe_print_rhs_lhs(warn() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": EXPECT_EQ(" #a ", " #b ") failed", lhs, rhs); \ + } + +// If you're stuck and `EXPECT_EQ` seems to refuse to print anything useful, +// try this: It'll spit out a nice compiler error telling you why it doesn't print. +#define EXPECT_EQ_FORCE(a, b) \ + { \ + auto lhs = (a); \ + auto rhs = (b); \ + if (lhs != rhs) \ + AK::force_print_rhs_lhs(warn() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": EXPECT_EQ(" #a ", " #b ") failed", lhs, rhs); \ } #define EXPECT(x) \