tst_QAnyStringView: add fromX for various char-like types

These were not covered by the existing tests.

Pick-to: 6.5 6.2
Change-Id: I909ea3aa5b676904dc72ecf8ce32b73cca1b6af7
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
(cherry picked from commit 39189190fee872d2e6fcaca0f5c10d0ba6a70ea6)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-05-24 14:50:59 +02:00 committed by Qt Cherry-pick Bot
parent fd6f9e92d8
commit 7d16ea588b

View File

@ -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<unsigned char>('\xE4'), 1); }
void fromSChar() const { fromCharacter(static_cast<signed char>('\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 <typename Char>
void fromLiteral(const Char *arg) const;
template <typename Char>
void fromCharacter(Char arg, qsizetype expectedSize) const;
template <typename Char>
void fromRange() const;
template <typename Char, typename Container>
void fromContainer() const;
@ -671,6 +696,40 @@ void tst_QAnyStringView::fromLiteral(const Char *arg) const
conversion_tests(arg);
}
template<typename Char>
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<Char, signed char>) // 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<Char, char32_t>)
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 <typename Char>
void tst_QAnyStringView::fromRange() const
{