summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/text/qbytearrayview/tst_qbytearrayview.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/corelib/text/qbytearrayview/tst_qbytearrayview.cpp')
-rw-r--r--tests/auto/corelib/text/qbytearrayview/tst_qbytearrayview.cpp237
1 files changed, 185 insertions, 52 deletions
diff --git a/tests/auto/corelib/text/qbytearrayview/tst_qbytearrayview.cpp b/tests/auto/corelib/text/qbytearrayview/tst_qbytearrayview.cpp
index c1d80b47dc..894f0430dd 100644
--- a/tests/auto/corelib/text/qbytearrayview/tst_qbytearrayview.cpp
+++ b/tests/auto/corelib/text/qbytearrayview/tst_qbytearrayview.cpp
@@ -1,30 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2020 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite 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 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QByteArrayView>
@@ -33,6 +8,7 @@
// for negative testing (can't convert from)
#include <deque>
#include <list>
+#include <QVarLengthArray>
template <typename T>
constexpr bool CanConvert = std::is_convertible_v<T, QByteArrayView>;
@@ -48,20 +24,20 @@ static_assert(CanConvert<char*>);
static_assert(CanConvert<const char*>);
static_assert(!CanConvert<uchar>);
-static_assert(CanConvert<uchar[1]>);
-static_assert(CanConvert<const uchar[1]>);
+static_assert(!CanConvert<uchar[1]>);
+static_assert(!CanConvert<const uchar[1]>);
static_assert(CanConvert<uchar*>);
static_assert(CanConvert<const uchar*>);
static_assert(!CanConvert<signed char>);
-static_assert(CanConvert<signed char[1]>);
-static_assert(CanConvert<const signed char[1]>);
+static_assert(!CanConvert<signed char[1]>);
+static_assert(!CanConvert<const signed char[1]>);
static_assert(CanConvert<signed char*>);
static_assert(CanConvert<const signed char*>);
static_assert(!CanConvert<std::byte>);
-static_assert(CanConvert<std::byte[1]>);
-static_assert(CanConvert<const std::byte[1]>);
+static_assert(!CanConvert<std::byte[1]>);
+static_assert(!CanConvert<const std::byte[1]>);
static_assert(CanConvert<std::byte*>);
static_assert(CanConvert<const std::byte*>);
@@ -107,9 +83,11 @@ class tst_QByteArrayView : public QObject
{
Q_OBJECT
private slots:
+ // Note: much of the shared API is tested in ../qbytearrayapisymmetry/
void constExpr() const;
void basics() const;
void literals() const;
+ void fromArray() const;
void literalsWithInternalNulls() const;
void at() const;
@@ -192,6 +170,8 @@ private slots:
}
void comparison() const;
+ void compare() const;
+ void std_stringview_conversion();
private:
template <typename Data>
@@ -217,6 +197,10 @@ void tst_QByteArrayView::constExpr() const
static_assert(bv.isEmpty());
static_assert(bv.data() == nullptr);
+ constexpr std::string_view sv = bv;
+ static_assert(sv.size() == 0);
+ static_assert(sv.data() == nullptr);
+
constexpr QByteArrayView bv2(bv.data(), bv.data() + bv.size());
static_assert(bv2.isNull());
static_assert(bv2.empty());
@@ -229,6 +213,10 @@ void tst_QByteArrayView::constExpr() const
static_assert(bv.isEmpty());
static_assert(bv.data() != nullptr);
+ constexpr std::string_view sv = bv;
+ static_assert(sv.size() == bv.size());
+ static_assert(sv.data() == bv.data());
+
constexpr QByteArrayView bv2(bv.data(), bv.data() + bv.size());
static_assert(!bv2.isNull());
static_assert(bv2.empty());
@@ -261,11 +249,23 @@ void tst_QByteArrayView::constExpr() const
static_assert(bv.rbegin() != bv.rend());
static_assert(bv.crbegin() != bv.crend());
+ constexpr std::string_view sv = bv;
+ static_assert(sv.size() == bv.size());
+ static_assert(sv.data() == bv.data());
+#ifdef AMBIGUOUS_CALL // QTBUG-108805
+ static_assert(sv == bv);
+ static_assert(bv == sv);
+#endif
+
constexpr QByteArrayView bv2(bv.data(), bv.data() + bv.size());
static_assert(!bv2.isNull());
static_assert(!bv2.empty());
static_assert(bv2.size() == 5);
}
+#if !defined(Q_CC_GNU) || defined(Q_CC_CLANG)
+ // 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.
+ // Note: Q_CC_GNU is also defined for Clang, so we need to check that too.
{
static constexpr char hello[] = "Hello";
constexpr QByteArrayView bv(hello);
@@ -282,6 +282,13 @@ void tst_QByteArrayView::constExpr() const
static_assert(bv.at(4) == 'o');
static_assert(bv.back() == 'o');
static_assert(bv.last() == 'o');
+
+ constexpr std::string_view sv = bv;
+ static_assert(bv.size() == sv.size());
+#ifdef AMBIGUOUS_CALL // QTBUG-108805
+ static_assert(bv == sv);
+ static_assert(sv == bv);
+#endif
}
{
static constexpr char hello[] = { 'H', 'e', 'l', 'l', 'o' };
@@ -299,13 +306,57 @@ void tst_QByteArrayView::constExpr() const
static_assert(bv.at(4) == 'o');
static_assert(bv.back() == 'o');
static_assert(bv.last() == 'o');
+
+ constexpr std::string_view sv = bv;
+ static_assert(bv.size() == sv.size());
+#ifdef AMBIGUOUS_CALL // QTBUG-108805
+ static_assert(bv == sv);
+ static_assert(sv == bv);
+#endif
}
+#endif
{
constexpr char *null = nullptr;
constexpr QByteArrayView bv(null);
static_assert(bv.isNull());
static_assert(bv.isEmpty());
static_assert(bv.size() == 0);
+
+ constexpr std::string_view sv = bv;
+ static_assert(sv.size() == 0);
+ static_assert(sv.data() == nullptr);
+ }
+ {
+ constexpr QByteArrayView bv(QLatin1StringView("Hello"));
+ static_assert(bv.size() == 5);
+ static_assert(!bv.empty());
+ static_assert(!bv.isEmpty());
+ static_assert(!bv.isNull());
+ static_assert(*bv.data() == 'H');
+ static_assert(bv[0] == 'H');
+ static_assert(bv.at(0) == 'H');
+ static_assert(bv.front() == 'H');
+ static_assert(bv.first() == 'H');
+ static_assert(bv[4] == 'o');
+ static_assert(bv.at(4) == 'o');
+ static_assert(bv.back() == 'o');
+ static_assert(bv.last() == 'o');
+ }
+ {
+ constexpr QByteArrayView bv(QUtf8StringView("Hello"));
+ static_assert(bv.size() == 5);
+ static_assert(!bv.empty());
+ static_assert(!bv.isEmpty());
+ static_assert(!bv.isNull());
+ static_assert(*bv.data() == 'H');
+ static_assert(bv[0] == 'H');
+ static_assert(bv.at(0) == 'H');
+ static_assert(bv.front() == 'H');
+ static_assert(bv.first() == 'H');
+ static_assert(bv[4] == 'o');
+ static_assert(bv.at(4) == 'o');
+ static_assert(bv.back() == 'o');
+ static_assert(bv.last() == 'o');
}
}
@@ -324,9 +375,11 @@ void tst_QByteArrayView::basics() const
QVERIFY(!(bv2 != bv1));
}
+// Note: initially the size would be deduced from the array literal,
+// but it caused source compatibility issues so this is currently not the case.
void tst_QByteArrayView::literals() const
{
- const char hello[] = "Hello";
+ const char hello[] = "Hello\0This shouldn't be found";
QCOMPARE(QByteArrayView(hello).size(), 5);
QCOMPARE(QByteArrayView(hello + 0).size(), 5); // forces decay to pointer
@@ -349,6 +402,44 @@ void tst_QByteArrayView::literals() const
QVERIFY(!bv2.isNull());
QVERIFY(!bv2.empty());
QCOMPARE(bv2.size(), 5);
+
+ const char abc[] = "abc";
+ bv = abc;
+ QCOMPARE(bv.size(), 3);
+
+ const char def[3] = {'d', 'e', 'f'};
+ bv = def;
+ QCOMPARE(bv.size(), 3);
+}
+
+void tst_QByteArrayView::fromArray() const
+{
+ static constexpr char hello[] = "Hello\0abc\0\0.";
+
+ constexpr QByteArrayView bv = QByteArrayView::fromArray(hello);
+ QCOMPARE(bv.size(), 13);
+ QVERIFY(!bv.empty());
+ QVERIFY(!bv.isEmpty());
+ QVERIFY(!bv.isNull());
+ QCOMPARE(*bv.data(), 'H');
+ QCOMPARE(bv[0], 'H');
+ QCOMPARE(bv.at(0), 'H');
+ QCOMPARE(bv.front(), 'H');
+ QCOMPARE(bv.first(), 'H');
+ QCOMPARE(bv[4], 'o');
+ QCOMPARE(bv.at(4), 'o');
+ QCOMPARE(bv[5], '\0');
+ QCOMPARE(bv.at(5), '\0');
+ QCOMPARE(*(bv.data() + bv.size() - 2), '.');
+ QCOMPARE(bv.back(), '\0');
+ QCOMPARE(bv.last(), '\0');
+
+ const std::byte bytes[] = {std::byte(0x0), std::byte(0x1), std::byte(0x2)};
+ QByteArrayView bbv = QByteArrayView::fromArray(bytes);
+ QCOMPARE(bbv.data(), reinterpret_cast<const char *>(bytes + 0));
+ QCOMPARE(bbv.size(), 3);
+ QCOMPARE(bbv.first(), 0x0);
+ QCOMPARE(bbv.last(), 0x2);
}
void tst_QByteArrayView::literalsWithInternalNulls() const
@@ -356,36 +447,39 @@ void tst_QByteArrayView::literalsWithInternalNulls() const
const char withnull[] = "a\0zzz";
// these are different results
- QCOMPARE(size_t(QByteArrayView(withnull).size()), sizeof(withnull)/sizeof(withnull[0]) - 1);
+ QCOMPARE(size_t(QByteArrayView::fromArray(withnull).size()), std::size(withnull));
QCOMPARE(QByteArrayView(withnull + 0).size(), 1);
- QByteArrayView nulled(withnull);
+ QByteArrayView nulled = QByteArrayView::fromArray(withnull);
+ QCOMPARE(nulled.last(), '\0');
+ nulled.chop(1); // cut off trailing \0
QCOMPARE(nulled[1], '\0');
QCOMPARE(nulled.indexOf('\0'), 1);
QCOMPARE(nulled.indexOf('z'), 2);
QCOMPARE(nulled.lastIndexOf('z'), 4);
QCOMPARE(nulled.lastIndexOf('a'), 0);
QVERIFY(nulled.startsWith("a\0z"));
- QVERIFY(!nulled.startsWith("a\0y"));
+ QVERIFY(nulled.startsWith("a\0y"));
+ QVERIFY(!nulled.startsWith(QByteArrayView("a\0y", 3)));
QVERIFY(nulled.endsWith("zz"));
QVERIFY(nulled.contains("z"));
- QVERIFY(nulled.contains("\0z"));
- QVERIFY(!nulled.contains("\0y"));
- QCOMPARE(nulled.first(5), withnull);
- QCOMPARE(nulled.last(5), withnull);
- QCOMPARE(nulled.sliced(0), withnull);
+ QVERIFY(nulled.contains(QByteArrayView("\0z", 2)));
+ QVERIFY(!nulled.contains(QByteArrayView("\0y", 2)));
+ QCOMPARE(nulled.first(5), QByteArrayView(withnull, 5));
+ QCOMPARE(nulled.last(5), QByteArrayView(withnull, 5));
+ QCOMPARE(nulled.sliced(0), QByteArrayView(withnull, 5));
QCOMPARE(nulled.sliced(2, 2), "zz");
- QCOMPARE(nulled.chopped(2), "a\0z");
- QVERIFY(nulled.chopped(2) != "a\0y");
+ QCOMPARE(nulled.chopped(2), QByteArrayView("a\0z", 3));
+ QVERIFY(nulled.chopped(2) != QByteArrayView("a\0y", 3));
QCOMPARE(nulled.count('z'), 3);
const char nullfirst[] = "\0buzz";
- QByteArrayView fromnull(nullfirst);
+ QByteArrayView fromnull = QByteArrayView::fromArray(nullfirst);
QVERIFY(!fromnull.isEmpty());
const char nullNotEnd[] = { 'b', 'o', 'w', '\0', 'a', 'f', 't', 'z' };
- QByteArrayView midNull(nullNotEnd);
- QCOMPARE(midNull.back(), 't');
+ QByteArrayView midNull = QByteArrayView::fromArray(nullNotEnd);
+ QCOMPARE(midNull.back(), 'z');
}
void tst_QByteArrayView::at() const
@@ -405,7 +499,7 @@ void tst_QByteArrayView::fromQByteArray() const
QByteArray empty = "";
QVERIFY(QByteArrayView(null).isNull());
- QVERIFY(!qToByteArrayViewIgnoringNull(null).isNull());
+ QVERIFY(qToByteArrayViewIgnoringNull(null).isNull());
QVERIFY(QByteArrayView(null).isEmpty());
QVERIFY(qToByteArrayViewIgnoringNull(null).isEmpty());
@@ -522,14 +616,14 @@ void tst_QByteArrayView::fromEmptyLiteral() const
QCOMPARE(QByteArrayView(null).size(), 0);
QCOMPARE(QByteArrayView(null).data(), nullptr);
- QCOMPARE(QByteArrayView(empty).size(), 0);
- QCOMPARE(static_cast<const void*>(QByteArrayView(empty).data()),
+ QCOMPARE(QByteArrayView::fromArray(empty).size(), 1);
+ QCOMPARE(static_cast<const void*>(QByteArrayView::fromArray(empty).data()),
static_cast<const void*>(empty));
QVERIFY(QByteArrayView(null).isNull());
QVERIFY(QByteArrayView(null).isEmpty());
- QVERIFY(QByteArrayView(empty).isEmpty());
- QVERIFY(!QByteArrayView(empty).isNull());
+ QVERIFY(!QByteArrayView::fromArray(empty).isEmpty());
+ QVERIFY(!QByteArrayView::fromArray(empty).isNull());
}
template <typename Char>
@@ -591,5 +685,44 @@ void tst_QByteArrayView::comparison() const
QVERIFY(bb > aa);
}
+void tst_QByteArrayView::compare() const
+{
+ QByteArrayView alpha = "original";
+
+ QVERIFY(alpha.compare("original", Qt::CaseSensitive) == 0);
+ QVERIFY(alpha.compare("Original", Qt::CaseSensitive) > 0);
+ QVERIFY(alpha.compare("Original", Qt::CaseInsensitive) == 0);
+ QByteArrayView beta = "unoriginal";
+ QVERIFY(alpha.compare(beta, Qt::CaseInsensitive) < 0);
+ beta = "Unoriginal";
+ QVERIFY(alpha.compare(beta, Qt::CaseInsensitive) < 0);
+ QVERIFY(alpha.compare(beta, Qt::CaseSensitive) > 0);
+}
+
+void tst_QByteArrayView::std_stringview_conversion()
+{
+ static_assert(std::is_convertible_v<QByteArrayView, std::string_view>);
+
+ QByteArrayView bav;
+ std::string_view sv(bav);
+ QCOMPARE(sv, std::string_view());
+
+ bav = "";
+ sv = bav;
+ QCOMPARE(bav.size(), 0);
+ QCOMPARE(sv.size(), size_t(0));
+ QCOMPARE(sv, std::string_view());
+
+ bav = "Hello";
+ sv = bav;
+ QCOMPARE(sv, std::string_view("Hello"));
+
+ bav = QByteArrayView::fromArray("Hello\0world");
+ sv = bav;
+ QCOMPARE(bav.size(), 12);
+ QCOMPARE(sv.size(), size_t(12));
+ QCOMPARE(sv, std::string_view("Hello\0world", 12));
+}
+
QTEST_APPLESS_MAIN(tst_QByteArrayView)
#include "tst_qbytearrayview.moc"