diff --git a/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp b/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp index 6c29ac1cc24..a6a1f0499ea 100644 --- a/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp +++ b/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp @@ -355,6 +355,29 @@ private Q_SLOTS: fromLiteral(u8"Hello, World!"); // char[] in <= C++17, char8_t[] in >= C++20 } + void fromChar() const { fromCharacter('\xE4', 1); } + void fromUChar() const { fromCharacter(static_cast('\xE4'), 1); } + void fromSChar() const { fromCharacter(static_cast('\xE4'), 1); } + void fromChar16T() const { fromCharacter(u'ä', 1); } + void fromUShort() const { fromCharacter(ushort(0xE4), 1); } + void fromChar32T() const { + fromCharacter(U'ä', 1); + if (QTest::currentTestFailed()) + return; + fromCharacter(U'\x1F0A0', 2); // U+1F0A0: PLAYING CARD BACK + } + void fromWCharT() const { + ONLY_WIN(fromCharacter(L'ä', 1)); // should work on Unix, too (char32_t does) + } + void fromQChar() const { fromCharacter(QChar(u'ä'), 1); } + void fromQLatin1Char() const { fromCharacter(QLatin1Char('\xE4'), 1); } + void fromQCharSpecialCharacter() const { + fromCharacter(QChar::ReplacementCharacter, 1); + if (QTest::currentTestFailed()) + return; + fromCharacter(QChar::LastValidCodePoint, 1); + } + void fromChar16TStar() const { fromLiteral(u"Hello, World!"); } void fromWCharTStar() const { ONLY_WIN(fromLiteral(L"Hello, World!")); } @@ -391,6 +414,8 @@ private: template void fromLiteral(const Char *arg) const; template + void fromCharacter(Char arg, qsizetype expectedSize) const; + template void fromRange() const; template void fromContainer() const; @@ -671,6 +696,40 @@ void tst_QAnyStringView::fromLiteral(const Char *arg) const conversion_tests(arg); } +template +void tst_QAnyStringView::fromCharacter(Char arg, qsizetype expectedSize) const +{ + // Need to re-create a new QASV(arg) each time, QASV(Char).data() dangles + // after the end of the Full Expression: + + QCOMPARE(QAnyStringView(arg).size(), expectedSize); + + // QCOMPARE(QAnyStringView(arg), arg); // not all pairs compile, so do it manually: + + const QChar chars[] = { + QAnyStringView(arg).front(), + QAnyStringView(arg).back(), + }; + + switch (expectedSize) { + case 1: + if constexpr (std::is_same_v) // QChar doesn't have a ctor for this + QCOMPARE(chars[0], QChar(uchar(arg))); + else + QCOMPARE(chars[0], QChar(arg)); + break; + case 2: + QCOMPARE_EQ(QAnyStringView(arg), QStringView::fromArray(chars)); + if constexpr (std::is_convertible_v) + QCOMPARE_EQ(QAnyStringView(arg), QStringView(QChar::fromUcs4(arg))); + break; + default: + QFAIL("Don't know how to compare this type to QAnyStringView"); + } + + // conversion_tests() would produce dangling references +} + template void tst_QAnyStringView::fromRange() const {