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.cpp104
1 files changed, 99 insertions, 5 deletions
diff --git a/tests/auto/corelib/text/qbytearrayview/tst_qbytearrayview.cpp b/tests/auto/corelib/text/qbytearrayview/tst_qbytearrayview.cpp
index 1a77750246..702e1840da 100644
--- a/tests/auto/corelib/text/qbytearrayview/tst_qbytearrayview.cpp
+++ b/tests/auto/corelib/text/qbytearrayview/tst_qbytearrayview.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2020 The Qt Company Ltd.
-// 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 <QByteArrayView>
@@ -171,6 +171,7 @@ private slots:
void comparison() const;
void compare() const;
+ void std_stringview_conversion();
private:
template <typename Data>
@@ -196,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());
@@ -208,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());
@@ -240,15 +249,22 @@ 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)
+#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.
- // 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);
@@ -265,6 +281,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' };
@@ -282,6 +305,16 @@ void tst_QByteArrayView::constExpr() const
static_assert(bv.at(4) == 'o');
static_assert(bv.back() == 'o');
static_assert(bv.last() == 'o');
+
+ constexpr auto bv2 = QByteArrayView::fromArray(hello);
+ QCOMPARE_EQ(bv, bv2);
+
+ 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
{
@@ -290,6 +323,42 @@ void tst_QByteArrayView::constExpr() const
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');
}
}
@@ -349,7 +418,7 @@ void tst_QByteArrayView::fromArray() const
{
static constexpr char hello[] = "Hello\0abc\0\0.";
- constexpr QByteArrayView bv = QByteArrayView::fromArray(hello);
+ const QByteArrayView bv = QByteArrayView::fromArray(hello);
QCOMPARE(bv.size(), 13);
QVERIFY(!bv.empty());
QVERIFY(!bv.isEmpty());
@@ -432,7 +501,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());
@@ -632,5 +701,30 @@ void tst_QByteArrayView::compare() const
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"