tst_QAnyStringView: test which chars magically convert

QAnyStringView is designed to replace overload sets containing QString
and QChar, among others, so it needs to accept types that merely
implicitly convert to QString and QChar. It achieves this by
maintaining a hidden container to hold the converted QString or QChar
for the duration of the full-expression, much like the compiler would
do if the function took QString/QChar.

This patch adds a check for the single-char ctors to determine whether
they convert or not, by comparing QASV::data() to the address of the
QASV's argument. While it would be interesting to have the same test
for QString, that is outside the scope of this patch.

Task-number: QTBUG-126054
Pick-to: 6.8 6.5 6.2
Change-Id: I30ac2d1199da5a03fe6fee4c9d5ea2a209a003d6
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
(cherry picked from commit f31c37a1312fc4e21a58906e2b883099629fb9e1)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2025-02-07 10:43:42 +01:00 committed by Qt Cherry-pick Bot
parent f8b100c136
commit 18b56bbae2

View File

@ -410,17 +410,17 @@ private Q_SLOTS:
void fromChar16T() const { fromCharacter(u'ä', 1); }
void fromUShort() const { fromCharacter(ushort(0xE4), 1); }
void fromChar32T() const {
fromCharacter(U'ä', 1);
fromCharacter(U'\x1F0A0', 2); // U+1F0A0: PLAYING CARD BACK
fromCharacter(U'ä', 1, true);
fromCharacter(U'\x1F0A0', 2, true); // 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 fromQLatin1Char() const { fromCharacter(QLatin1Char('\xE4'), 1, true); }
void fromQCharSpecialCharacter() const {
fromCharacter(QChar::ReplacementCharacter, 1);
fromCharacter(QChar::LastValidCodePoint, 1);
fromCharacter(QChar::ReplacementCharacter, 1, true);
fromCharacter(QChar::LastValidCodePoint, 1, true);
}
void fromCharacterSpecial() const;
@ -463,7 +463,7 @@ private:
template <typename Char>
void fromLiteral(const Char *arg) const;
template <typename Char>
void fromCharacter(Char arg, qsizetype expectedSize) const;
void fromCharacter(Char arg, qsizetype expectedSize, bool expectConversion = false) const;
template <typename Char>
void fromRange() const;
template <typename Char, typename Container>
@ -747,8 +747,11 @@ void tst_QAnyStringView::fromLiteral(const Char *arg) const
conversion_tests(arg);
}
template <typename T>
const void *as_const_void_star(T *p) { return p; }
template<typename Char>
void tst_QAnyStringView::fromCharacter(Char arg, qsizetype expectedSize) const
void tst_QAnyStringView::fromCharacter(Char arg, qsizetype expectedSize, bool expectConversion) const
{
// Need to re-create a new QASV(arg) each time, QASV(Char).data() dangles
// after the end of the Full Expression:
@ -758,6 +761,11 @@ void tst_QAnyStringView::fromCharacter(Char arg, qsizetype expectedSize) const
QCOMPARE(QAnyStringView(arg).size(), expectedSize);
if (expectConversion)
QCOMPARE_NE(QAnyStringView(arg).data(), as_const_void_star(std::addressof(arg)));
else
QCOMPARE_EQ(QAnyStringView(arg).data(), as_const_void_star(std::addressof(arg)));
// QCOMPARE(QAnyStringView(arg), arg); // not all pairs compile, so do it manually:
// Check implicit conversion: