From f31c37a1312fc4e21a58906e2b883099629fb9e1 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 7 Feb 2025 10:43:42 +0100 Subject: [PATCH] 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.9 6.8 6.5 6.2 Change-Id: I30ac2d1199da5a03fe6fee4c9d5ea2a209a003d6 Reviewed-by: Ahmad Samir --- .../qanystringview/tst_qanystringview.cpp | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp b/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp index d8706b8a873..8c129a93c71 100644 --- a/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp +++ b/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp @@ -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 void fromLiteral(const Char *arg) const; template - void fromCharacter(Char arg, qsizetype expectedSize) const; + void fromCharacter(Char arg, qsizetype expectedSize, bool expectConversion = false) const; template void fromRange() const; template @@ -747,8 +747,11 @@ void tst_QAnyStringView::fromLiteral(const Char *arg) const conversion_tests(arg); } +template +const void *as_const_void_star(T *p) { return p; } + template -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: