diff options
Diffstat (limited to 'tests/auto/corelib/text/qstringview/tst_qstringview.cpp')
-rw-r--r-- | tests/auto/corelib/text/qstringview/tst_qstringview.cpp | 79 |
1 files changed, 77 insertions, 2 deletions
diff --git a/tests/auto/corelib/text/qstringview/tst_qstringview.cpp b/tests/auto/corelib/text/qstringview/tst_qstringview.cpp index 9eab2f6966..3f87ffc9fc 100644 --- a/tests/auto/corelib/text/qstringview/tst_qstringview.cpp +++ b/tests/auto/corelib/text/qstringview/tst_qstringview.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QStringView> #include <QStringTokenizer> @@ -7,6 +7,11 @@ #include <QChar> #include <QVarLengthArray> #include <QList> +#if QT_CONFIG(cpp_winrt) +# include <private/qt_winrtbase_p.h> +#endif +#include <private/qxmlstream_p.h> + #include <QTest> @@ -90,6 +95,8 @@ static_assert(CanConvert<std::array<char16_t, 123>>::value); static_assert(!CanConvert<std::deque<char16_t>>::value); static_assert(!CanConvert<std::list<char16_t>>::value); +static_assert(CanConvert<QtPrivate::XmlStringRef>::value); + // // wchar_t // @@ -124,6 +131,19 @@ static_assert(CanConvert<std::array<wchar_t, 123>>::value == CanConvertFromWChar static_assert(!CanConvert<std::deque<wchar_t>>::value); static_assert(!CanConvert<std::list<wchar_t>>::value); +#if QT_CONFIG(cpp_winrt) + +// +// winrt::hstring (QTBUG-111886) +// + +static_assert(CanConvert< winrt::hstring >::value); +static_assert(CanConvert<const winrt::hstring >::value); +static_assert(CanConvert< winrt::hstring&>::value); +static_assert(CanConvert<const winrt::hstring&>::value); + +#endif // QT_CONFIG(cpp_winrt) + class tst_QStringView : public QObject { Q_OBJECT @@ -238,6 +258,8 @@ private Q_SLOTS: void tokenize_data() const; void tokenize() const; + void std_stringview_conversion(); + private: template <typename String> void conversion_tests(String arg) const; @@ -337,6 +359,30 @@ void tst_QStringView::constExpr() const static_assert(sv3.isEmpty()); static_assert(sv3.size() == 0); } +#if !defined(Q_CC_GNU_ONLY) || !defined(QT_SANITIZE_UNDEFINED) + // Below checks are disabled because of a compilation issue with GCC and + // -fsanitize=undefined. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71962. + { + static constexpr char16_t hello[] = u"Hello"; + constexpr QStringView sv(hello); + static_assert(sv.size() == 5); + static_assert(!sv.empty()); + static_assert(!sv.isEmpty()); + static_assert(!sv.isNull()); + static_assert(*sv.utf16() == 'H'); + static_assert(sv[0] == QLatin1Char('H')); + static_assert(sv.at(0) == QLatin1Char('H')); + static_assert(sv.front() == QLatin1Char('H')); + static_assert(sv.first() == QLatin1Char('H')); + static_assert(sv[4] == QLatin1Char('o')); + static_assert(sv.at(4) == QLatin1Char('o')); + static_assert(sv.back() == QLatin1Char('o')); + static_assert(sv.last() == QLatin1Char('o')); + + constexpr auto sv2 = QStringView::fromArray(hello); + QCOMPARE_EQ(sv, sv2.chopped(1)); + } +#endif // -fsanitize=undefined } void tst_QStringView::basics() const @@ -408,7 +454,7 @@ void tst_QStringView::fromArray() const { static constexpr char16_t hello[] = u"Hello\0abc\0\0."; - constexpr QStringView sv = QStringView::fromArray(hello); + const QStringView sv = QStringView::fromArray(hello); QCOMPARE(sv.size(), 13); QVERIFY(!sv.empty()); QVERIFY(!sv.isEmpty()); @@ -447,6 +493,10 @@ void tst_QStringView::at() const void tst_QStringView::arg() const { + // nullness checks + QCOMPARE(QStringView().arg(QStringView()), ""); + QCOMPARE(QStringView(u"%1").arg(QStringView()), ""); + #define CHECK1(pattern, arg1, expected) \ do { \ auto p = QStringView(u"" pattern); \ @@ -859,5 +909,30 @@ void tst_QStringView::overloadResolution() } } +void tst_QStringView::std_stringview_conversion() +{ + static_assert(std::is_convertible_v<QStringView, std::u16string_view>); + + QStringView s; + std::u16string_view sv(s); + QCOMPARE(sv, std::u16string_view()); + + s = u""; + sv = s; + QCOMPARE(s.size(), 0); + QCOMPARE(sv.size(), size_t(0)); + QCOMPARE(sv, std::u16string_view()); + + s = u"Hello"; + sv = s; + QCOMPARE(sv, std::u16string_view(u"Hello")); + + s = QStringView::fromArray(u"Hello\0world"); + sv = s; + QCOMPARE(s.size(), 12); + QCOMPARE(sv.size(), size_t(12)); + QCOMPARE(sv, std::u16string_view(u"Hello\0world\0", 12)); +} + QTEST_APPLESS_MAIN(tst_QStringView) #include "tst_qstringview.moc" |