tst_qstringapisymmetry: check expected overloads are selected

Check that the expected overloads are selected in
QString/QAnyStringView overload sets.

Pick-to: 6.3
Change-Id: I38148c20a72eb60cf86844a39fe0ed419d2fa562
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2022-03-07 11:51:44 +01:00
parent ed5978effe
commit 5235b1bdc8

View File

@ -30,6 +30,7 @@
#undef QT_NO_CAST_FROM_ASCII #undef QT_NO_CAST_FROM_ASCII
#undef QT_NO_CAST_TO_ASCII #undef QT_NO_CAST_TO_ASCII
#undef QT_ASCII_CAST_WARNINGS #undef QT_ASCII_CAST_WARNINGS
#define QT_USE_QSTRINGBUILDER
#include <QChar> #include <QChar>
#include <QLatin1String> #include <QLatin1String>
@ -168,6 +169,7 @@ private Q_SLOTS:
void overload_vector_char16_t() { overload<std::vector<char16_t>>(); } void overload_vector_char16_t() { overload<std::vector<char16_t>>(); }
void overload_vector_QChar() { overload<std::vector<QChar>>(); } void overload_vector_QChar() { overload<std::vector<QChar>>(); }
void overload_special();
private: private:
// //
// Mixed UTF-16, UTF-8, Latin-1 checks: // Mixed UTF-16, UTF-8, Latin-1 checks:
@ -943,27 +945,27 @@ template <> constexpr qsizetype size(const char16_t&) { return 1; }
namespace { namespace {
void overload_s_a(const QString &) {} auto overload_s_a(const QString &s) { return s; }
Q_WEAK_OVERLOAD Q_WEAK_OVERLOAD
void overload_s_a(QAnyStringView) {} auto overload_s_a(QAnyStringView s) { return s; }
void overload_sr_a(QString &&) {} auto overload_sr_a(QString &&s) { return std::move(s); }
Q_WEAK_OVERLOAD Q_WEAK_OVERLOAD
void overload_sr_a(QAnyStringView) {} auto overload_sr_a(QAnyStringView s) { return s; }
Q_WEAK_OVERLOAD Q_WEAK_OVERLOAD
void overload_a_s(const QString &) {} auto overload_a_s(const QString &s) { return s; }
void overload_a_s(QAnyStringView) {} auto overload_a_s(QAnyStringView s) { return s; }
Q_WEAK_OVERLOAD Q_WEAK_OVERLOAD
void overload_a_sr(QString &&) {} auto overload_a_sr(QString &&s) { return std::move(s); }
void overload_a_sr(QAnyStringView) {} auto overload_a_sr(QAnyStringView s) { return s; }
void overload_s_v(const QString &) {} auto overload_s_v(const QString &s) { return s; }
void overload_s_v(QStringView) {} auto overload_s_v(QStringView s) { return s; }
void overload_sr_v(QString &&) {} auto overload_sr_v(QString &&s) { return std::move(s); }
void overload_sr_v(QStringView) {} auto overload_sr_v(QStringView s) { return s; }
} // unnamed namespace } // unnamed namespace
@ -1025,6 +1027,51 @@ void tst_QStringApiSymmetry::overload()
} }
} }
void tst_QStringApiSymmetry::overload_special()
{
auto check = [](auto result, auto expected) {
static_assert(std::is_same_v<decltype(result), decltype(expected)>);
};
{
#define rvalue QStringLiteral("hello")
auto lvalue = rvalue;
auto builder = [&] { return lvalue % ""; };
// check that QString/Builder go to the QString overload in a_s(r):
check(overload_a_s(lvalue), QString());
check(overload_a_s(rvalue), QString());
check(overload_a_s(builder()), QAnyStringView()); // weak overloads must match exactly
check(overload_a_s(QString(builder())), QString());
check(overload_a_sr(lvalue), QAnyStringView()); // lvalue can't bind to rvalue ref
check(overload_a_sr(rvalue), QString());
check(overload_a_sr(builder()), QAnyStringView());
check(overload_a_sr(QString(builder())), QString());
// check that everything goes to the QString overload in s(r)_a:
// exception: u""
check(overload_s_a(lvalue), QString());
check(overload_s_a(rvalue), QString());
check(overload_s_a(builder()), QString());
check(overload_s_a(""), QString());
check(overload_s_a(u""), QAnyStringView());
check(overload_s_a(u8""), QString());
check(overload_s_a(QLatin1String("")), QString());
check(overload_sr_a(lvalue), QAnyStringView()); // lvalues don't bind to rvalue refs
check(overload_sr_a(rvalue), QString());
check(overload_sr_a(builder()), QString());
check(overload_sr_a(""), QString());
check(overload_sr_a(u""), QAnyStringView());
check(overload_sr_a(u8""), QString());
check(overload_sr_a(QLatin1String("")), QString());
#undef rvalue
}
}
void tst_QStringApiSymmetry::compare_data(bool hasConceptOfNullAndEmpty) void tst_QStringApiSymmetry::compare_data(bool hasConceptOfNullAndEmpty)
{ {
QTest::addColumn<QStringView>("lhsUnicode"); QTest::addColumn<QStringView>("lhsUnicode");