From 1b8b8024554f9f36bc6642c2dfe830765e932df5 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 9 Jun 2022 09:52:58 +0200 Subject: [PATCH] tst_QAnyStringView: explicitly check the spaceship operator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We implicitly checked it, because, in C++20 builds, the non-equality relational operators are synthesized from it by the compiler, and we test those, but we didn't check that <=> returns strong_ordering. We now do. Pick-to: 6.4 6.3 6.2 Task-number: QTBUG-104108 Change-Id: Ieb19a2d4cb2d600d884f4e2e89e98c6187e23872 Reviewed-by: Thiago Macieira Reviewed-by: Qt CI Bot Reviewed-by: MÃ¥rten Nordheim --- .../qanystringview/tst_qanystringview.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp b/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp index fe7083502e..4433b8efbc 100644 --- a/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp +++ b/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp @@ -42,6 +42,14 @@ # define ONLY_WIN(expr) QSKIP("This is a Windows-only test") #endif +#ifdef __cpp_impl_three_way_comparison +# define ONLY_3WAY(expr) expr +#else +# define ONLY_3WAY(expr) \ + QSKIP("This test requires C++20 spaceship operator (<=>) " \ + "support enabled in the standard library.") +#endif + using namespace Qt::StringLiterals; template @@ -292,6 +300,7 @@ private Q_SLOTS: void fromQStringBuilder_QString_QString() const { fromQStringBuilder(u"1"_s % u"2"_s, u"12"); } void comparison(); + void compare3way(); private: template @@ -629,5 +638,27 @@ void tst_QAnyStringView::comparison() QVERIFY(QAnyStringView::compare(bb, aa) > 0); } +void tst_QAnyStringView::compare3way() +{ +#define COMPARE_3WAY(lhs, rhs, res) \ + do { \ + const auto qt_3way_cmp_res = (lhs) <=> (rhs); \ + static_assert(std::is_same_v); \ + QCOMPARE(std::is_eq(qt_3way_cmp_res), std::is_eq(res)); \ + QCOMPARE(std::is_lt(qt_3way_cmp_res), std::is_lt(res)); \ + QCOMPARE(std::is_gt(qt_3way_cmp_res), std::is_gt(res)); \ + } while (false) + + ONLY_3WAY( + const QAnyStringView aa = u"aa"; + const QAnyStringView upperAa = u"AA"; + const QAnyStringView bb = u"bb"; + COMPARE_3WAY(aa, aa, std::strong_ordering::equal); + COMPARE_3WAY(aa, bb, std::strong_ordering::less); + COMPARE_3WAY(bb, aa, std::strong_ordering::greater) + ); +#undef COMPARE_3WAY +} + QTEST_APPLESS_MAIN(tst_QAnyStringView) #include "tst_qanystringview.moc"