summaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorMårten Nordheim <marten.nordheim@qt.io>2020-09-16 12:04:31 +0200
committerMårten Nordheim <marten.nordheim@qt.io>2020-09-18 23:34:52 +0200
commit107ff4c1d6b5da2cb11c65b2bd9106817f7fdb02 (patch)
treee0c79bac217c10fd6d25fa9c4155f2003f6636df /tests/auto
parentbbe7570ddcc6fcce707a355b76d2d0024c44ea38 (diff)
Q(Any|Utf8)StringView: move array size deduction feature to fromArray
The constructor taking an array literal will now stop at the first null-terminator encountered. And fromArray is introduced which only supports array literals. Constructs a view of the full size. Explicit so it shouldn't be surprising. Change-Id: I1497c33a5c12453a95e87c990abe6335b2817081 Reviewed-by: Andrei Golubev <andrei.golubev@qt.io> Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/corelib/text/qstringview/tst_qstringview.cpp34
1 files changed, 33 insertions, 1 deletions
diff --git a/tests/auto/corelib/text/qstringview/tst_qstringview.cpp b/tests/auto/corelib/text/qstringview/tst_qstringview.cpp
index bf3454e028..b08d9ce8d9 100644
--- a/tests/auto/corelib/text/qstringview/tst_qstringview.cpp
+++ b/tests/auto/corelib/text/qstringview/tst_qstringview.cpp
@@ -157,6 +157,7 @@ private Q_SLOTS:
void constExpr() const;
void basics() const;
void literals() const;
+ void fromArray() const;
void at() const;
void arg() const;
@@ -425,10 +426,41 @@ void tst_QStringView::literals() const
}
// these are different results
- QCOMPARE(size_t(QStringView(withnull).size()), sizeof(withnull)/sizeof(withnull[0]) - 1);
+ QCOMPARE(size_t(QStringView(withnull).size()), 1);
+ QCOMPARE(size_t(QStringView::fromArray(withnull).size()), sizeof(withnull)/sizeof(withnull[0]));
QCOMPARE(QStringView(withnull + 0).size(), 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
{
QString hello("Hello");