diff options
Diffstat (limited to 'tests/auto/corelib/text/qstringview')
-rw-r--r-- | tests/auto/corelib/text/qstringview/CMakeLists.txt | 18 | ||||
-rw-r--r-- | tests/auto/corelib/text/qstringview/tst_qstringview.cpp | 120 |
2 files changed, 96 insertions, 42 deletions
diff --git a/tests/auto/corelib/text/qstringview/CMakeLists.txt b/tests/auto/corelib/text/qstringview/CMakeLists.txt index 34c145a498..b541cdd0ed 100644 --- a/tests/auto/corelib/text/qstringview/CMakeLists.txt +++ b/tests/auto/corelib/text/qstringview/CMakeLists.txt @@ -1,13 +1,29 @@ -# Generated from qstringview.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_qstringview Test: ##################################################################### +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 ) +if(QT_FEATURE_sanitize_undefined) + qt_internal_extend_target(tst_qstringview + DEFINES + QT_SANITIZE_UNDEFINED # GCC (in)famously doesn't provide a predefined macro for this + ) +endif() + ## Scopes: ##################################################################### diff --git a/tests/auto/corelib/text/qstringview/tst_qstringview.cpp b/tests/auto/corelib/text/qstringview/tst_qstringview.cpp index a78004f04d..3f87ffc9fc 100644 --- a/tests/auto/corelib/text/qstringview/tst_qstringview.cpp +++ b/tests/auto/corelib/text/qstringview/tst_qstringview.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** 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> @@ -32,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> @@ -115,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 // @@ -149,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 @@ -263,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; @@ -362,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 @@ -433,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()); @@ -472,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); \ @@ -552,13 +577,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)) @@ -566,13 +589,11 @@ 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})) @@ -580,13 +601,11 @@ void tst_QStringView::tokenize() const } // (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())) @@ -594,13 +613,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())) @@ -615,20 +632,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 { @@ -657,7 +671,6 @@ void tst_QStringView::tokenize() const QCOMPARE(result, actual); } #endif // __cpp_lib_ranges -#endif // __cpp_deduction_guides } template <typename Char> @@ -891,10 +904,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" |