diff --git a/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp b/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp index fe7083502ee..4433b8efbcd 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"