diff options
Diffstat (limited to 'tests/auto/corelib/text/qstringview')
-rw-r--r-- | tests/auto/corelib/text/qstringview/CMakeLists.txt | 13 | ||||
-rw-r--r-- | tests/auto/corelib/text/qstringview/qstringview.pro | 7 | ||||
-rw-r--r-- | tests/auto/corelib/text/qstringview/tst_qstringview.cpp | 192 |
3 files changed, 103 insertions, 109 deletions
diff --git a/tests/auto/corelib/text/qstringview/CMakeLists.txt b/tests/auto/corelib/text/qstringview/CMakeLists.txt index aefcf6a1ee..ba5f540838 100644 --- a/tests/auto/corelib/text/qstringview/CMakeLists.txt +++ b/tests/auto/corelib/text/qstringview/CMakeLists.txt @@ -1,12 +1,21 @@ -# Generated from qstringview.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_qstringview Test: ##################################################################### -qt_add_test(tst_qstringview +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qstringview LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qstringview SOURCES tst_qstringview.cpp + LIBRARIES + Qt::CorePrivate ) ## Scopes: diff --git a/tests/auto/corelib/text/qstringview/qstringview.pro b/tests/auto/corelib/text/qstringview/qstringview.pro deleted file mode 100644 index e6d610e980..0000000000 --- a/tests/auto/corelib/text/qstringview/qstringview.pro +++ /dev/null @@ -1,7 +0,0 @@ -CONFIG += testcase -TARGET = tst_qstringview -QT = core testlib -contains(QT_CONFIG, c++14):CONFIG *= c++14 -contains(QT_CONFIG, c++1z):CONFIG *= c++1z -contains(QT_CONFIG, c++2a):CONFIG *= c++2a -SOURCES += tst_qstringview.cpp diff --git a/tests/auto/corelib/text/qstringview/tst_qstringview.cpp b/tests/auto/corelib/text/qstringview/tst_qstringview.cpp index 55e23570d7..df3ef94371 100644 --- a/tests/auto/corelib/text/qstringview/tst_qstringview.cpp +++ b/tests/auto/corelib/text/qstringview/tst_qstringview.cpp @@ -1,38 +1,17 @@ -/**************************************************************************** -** -** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// 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 #include <QStringView> #include <QStringTokenizer> #include <QString> #include <QChar> -#include <QStringRef> #include <QVarLengthArray> #include <QList> +#if QT_CONFIG(cpp_winrt) +# include <private/qt_winrtbase_p.h> +#endif +#include <private/qxmlstream_p.h> + #include <QTest> @@ -72,12 +51,6 @@ static_assert(CanConvert<const QString >::value); static_assert(CanConvert< QString&>::value); static_assert(CanConvert<const QString&>::value); -static_assert(CanConvert< QStringRef >::value); -static_assert(CanConvert<const QStringRef >::value); -static_assert(CanConvert< QStringRef&>::value); -static_assert(CanConvert<const QStringRef&>::value); - - // // ushort // @@ -122,11 +95,13 @@ 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 // -Q_CONSTEXPR bool CanConvertFromWCharT = +constexpr bool CanConvertFromWCharT = #ifdef Q_OS_WIN true #else @@ -156,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 @@ -164,22 +152,22 @@ private Q_SLOTS: void constExpr() const; void basics() const; void literals() const; + void fromArray() const; void at() const; void arg() const; void fromQString() const; - void fromQStringRef() const; void fromQCharStar() const { - const QChar str[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!', 0 }; + const QChar str[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0' }; fromLiteral(str); } void fromUShortStar() const { - const ushort str[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!', 0 }; + const ushort str[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0' }; fromLiteral(str); } @@ -270,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; @@ -288,7 +278,6 @@ private: void tst_QStringView::constExpr() const { // compile-time checks -#ifdef Q_COMPILER_CONSTEXPR { constexpr QStringView sv; static_assert(sv.size() == 0); @@ -370,7 +359,6 @@ void tst_QStringView::constExpr() const static_assert(sv3.isEmpty()); static_assert(sv3.size() == 0); } -#endif } void tst_QStringView::basics() const @@ -433,8 +421,39 @@ void tst_QStringView::literals() const } // these are different results - QCOMPARE(size_t(QStringView(withnull).size()), sizeof(withnull)/sizeof(withnull[0]) - 1); - QCOMPARE(QStringView(withnull + 0).size(), 1); + QCOMPARE(size_t(QStringView(withnull).size()), size_t(1)); + QCOMPARE(size_t(QStringView::fromArray(withnull).size()), sizeof(withnull)/sizeof(withnull[0])); + QCOMPARE(QStringView(withnull + 0).size(), qsizetype(1)); +} + +void tst_QStringView::fromArray() const +{ + static constexpr char16_t hello[] = u"Hello\0abc\0\0."; + + constexpr QStringView sv = QStringView::fromArray(hello); + QCOMPARE(sv.size(), 13); + QVERIFY(!sv.empty()); + QVERIFY(!sv.isEmpty()); + QVERIFY(!sv.isNull()); + QCOMPARE(*sv.data(), 'H'); + QCOMPARE(sv[0], 'H'); + QCOMPARE(sv.at(0), 'H'); + QCOMPARE(sv.front(), 'H'); + QCOMPARE(sv.first(), 'H'); + QCOMPARE(sv[4], 'o'); + QCOMPARE(sv.at(4), 'o'); + QCOMPARE(sv[5], '\0'); + QCOMPARE(sv.at(5), '\0'); + QCOMPARE(*(sv.data() + sv.size() - 2), '.'); + QCOMPARE(sv.back(), '\0'); + QCOMPARE(sv.last(), '\0'); + + const char16_t bytes[] = {u'a', u'b', u'c'}; + QStringView sv2 = QStringView::fromArray(bytes); + QCOMPARE(sv2.data(), reinterpret_cast<const QChar *>(bytes + 0)); + QCOMPARE(sv2.size(), 3); + QCOMPARE(sv2.first(), u'a'); + QCOMPARE(sv2.last(), u'c'); } void tst_QStringView::at() const @@ -450,6 +469,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); \ @@ -502,20 +525,6 @@ void tst_QStringView::fromQString() const conversion_tests(QString("Hello World!")); } -void tst_QStringView::fromQStringRef() const -{ - QStringRef null; - QString emptyS = ""; - QStringRef empty(&emptyS); - - QVERIFY( QStringView(null).isNull()); - QVERIFY( QStringView(null).isEmpty()); - QVERIFY( QStringView(empty).isEmpty()); - QVERIFY(!QStringView(empty).isNull()); - - conversion_tests(QString("Hello World!").midRef(6)); -} - void tst_QStringView::tokenize_data() const { // copied from tst_QString @@ -544,13 +553,11 @@ void tst_QStringView::tokenize() const QFETCH(const QStringList, result); // lvalue QString -#ifdef __cpp_deduction_guides { auto rit = result.cbegin(); for (auto sv : QStringTokenizer{str, sep}) QCOMPARE(sv, *rit++); } -#endif { auto rit = result.cbegin(); for (auto sv : QStringView{str}.tokenize(sep)) @@ -558,41 +565,23 @@ void tst_QStringView::tokenize() const } // rvalue QString -#ifdef __cpp_deduction_guides { auto rit = result.cbegin(); for (auto sv : QStringTokenizer{str, QString{sep}}) QCOMPARE(sv, *rit++); } -#endif { auto rit = result.cbegin(); for (auto sv : QStringView{str}.tokenize(QString{sep})) QCOMPARE(sv, *rit++); } - // (rvalue) QStringRef -#ifdef __cpp_deduction_guides - { - auto rit = result.cbegin(); - for (auto sv : QStringTokenizer{str, sep.midRef(0)}) - QCOMPARE(sv, *rit++); - } -#endif - { - auto rit = result.cbegin(); - for (auto sv : QStringView{str}.tokenize(sep.midRef(0))) - QCOMPARE(sv, *rit++); - } - // (rvalue) QChar -#ifdef __cpp_deduction_guides if (sep.size() == 1) { auto rit = result.cbegin(); for (auto sv : QStringTokenizer{str, sep.front()}) QCOMPARE(sv, *rit++); } -#endif if (sep.size() == 1) { auto rit = result.cbegin(); for (auto sv : QStringView{str}.tokenize(sep.front())) @@ -600,13 +589,11 @@ void tst_QStringView::tokenize() const } // (rvalue) char16_t -#ifdef __cpp_deduction_guides if (sep.size() == 1) { auto rit = result.cbegin(); for (auto sv : QStringTokenizer{str, *qToStringViewIgnoringNull(sep).utf16()}) QCOMPARE(sv, *rit++); } -#endif if (sep.size() == 1) { auto rit = result.cbegin(); for (auto sv : QStringView{str}.tokenize(*qToStringViewIgnoringNull(sep).utf16())) @@ -621,20 +608,17 @@ void tst_QStringView::tokenize() const return literal; }; const std::unique_ptr<const char16_t[]> literal = make_literal(sep); -#ifdef __cpp_deduction_guides { auto rit = result.cbegin(); for (auto sv : QStringTokenizer{str, literal.get()}) QCOMPARE(sv, *rit++); } -#endif { auto rit = result.cbegin(); for (auto sv : QStringView{str}.tokenize(literal.get())) QCOMPARE(sv, *rit++); } -#ifdef __cpp_deduction_guides #ifdef __cpp_lib_ranges // lvalue QString { @@ -654,15 +638,6 @@ void tst_QStringView::tokenize() const QCOMPARE(result, actual); } - // (rvalue) QStringRef - { - QStringList actual; - const QStringTokenizer tok{str, sep.midRef(0)}; - std::ranges::transform(tok, std::back_inserter(actual), - [](auto sv) { return sv.toString(); }); - QCOMPARE(result, actual); - } - // (rvalue) QChar if (sep.size() == 1) { QStringList actual; @@ -672,14 +647,13 @@ void tst_QStringView::tokenize() const QCOMPARE(result, actual); } #endif // __cpp_lib_ranges -#endif // __cpp_deduction_guides } template <typename Char> void tst_QStringView::fromLiteral(const Char *arg) const { const Char *null = nullptr; - const Char empty[] = { 0 }; + const Char empty[] = { Char{} }; QCOMPARE(QStringView(null).size(), qsizetype(0)); QCOMPARE(QStringView(null).data(), nullptr); @@ -888,13 +862,6 @@ void tst_QStringView::overloadResolution() QStringViewOverloadResolution::test(ushortPointer); } - { - QStringRef stringRef; - QStringViewOverloadResolution::test(stringRef); - QStringViewOverloadResolution::test(qAsConst(stringRef)); - QStringViewOverloadResolution::test(std::move(stringRef)); - } - #if defined(Q_OS_WIN) { wchar_t wchartArray[42] = {}; @@ -913,10 +880,35 @@ void tst_QStringView::overloadResolution() { std::u16string string; QStringViewOverloadResolution::test(string); - QStringViewOverloadResolution::test(qAsConst(string)); + QStringViewOverloadResolution::test(std::as_const(string)); QStringViewOverloadResolution::test(std::move(string)); } } +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" |