summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorAndrei Golubev <andrei.golubev@qt.io>2020-07-13 12:26:36 +0300
committerAndrei Golubev <andrei.golubev@qt.io>2020-08-18 12:55:38 +0200
commit4bf8e82d413815cd8ef076e7a5c65d0651a0e27a (patch)
tree184ab55f4a166fe64e1a7002b9601c18dc9f09de /tests
parent0bd647fa4ffb3319bf3b1d044441b137910629e1 (diff)
Add QArrayDataPointer::freeSpace*() functions
Added functions that tell how much free space is available at the beginning and at the end of the storage Updated preconditions of operations to use freeSpace* functions Also, changed casts uint(this->size) to size_t(this->size) Task-number: QTBUG-84320 Change-Id: Iad94c1060a00f62068da9d1327e332a00d4f4109 Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp
index 2d17a170f8..efec562ba9 100644
--- a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp
+++ b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp
@@ -76,6 +76,8 @@ private slots:
void variadicLiterals();
void rValueReferences();
void grow();
+ void freeSpace_data();
+ void freeSpace();
#ifndef QT_NO_EXCEPTIONS
void exceptionSafetyPrimitives_constructor();
void exceptionSafetyPrimitives_destructor();
@@ -1814,6 +1816,43 @@ void tst_QArrayData::grow()
}
}
+void tst_QArrayData::freeSpace_data()
+{
+ QTest::addColumn<QArrayData::ArrayOptions>("allocationOptions");
+ QTest::addColumn<size_t>("n");
+
+ for (const size_t n : {1, 3, 5, 7, 16, 25}) {
+ QString suffix = QString::number(n) + QLatin1String("-elements");
+ QTest::newRow(qPrintable(QLatin1String("default-alloc-") + suffix))
+ << QArrayData::ArrayOptions(QArrayData::DefaultAllocationFlags) << n;
+ QTest::newRow(qPrintable(QLatin1String("grows-forward-") + suffix))
+ << QArrayData::ArrayOptions(QArrayData::GrowsForward) << n;
+ QTest::newRow(qPrintable(QLatin1String("grows-bidirectional-") + suffix))
+ << QArrayData::ArrayOptions(QArrayData::GrowsForward | QArrayData::GrowsBackwards) << n;
+ }
+}
+
+void tst_QArrayData::freeSpace()
+{
+ QFETCH(QArrayData::ArrayOptions, allocationOptions);
+ QFETCH(size_t, n);
+ const auto testFreeSpace = [] (auto dummy, auto options, size_t n) {
+ using Type = std::decay_t<decltype(dummy)>;
+ using Data = QTypedArrayData<Type>;
+ using DataPointer = QArrayDataPointer<Type>;
+ Q_UNUSED(dummy);
+ DataPointer ptr(Data::allocate(n, options));
+ const auto alloc = qsizetype(ptr.constAllocatedCapacity());
+ QVERIFY(size_t(alloc) >= n);
+ QCOMPARE(ptr.freeSpaceAtBegin() + ptr.freeSpaceAtEnd(), alloc);
+ };
+ RUN_TEST_FUNC(testFreeSpace, char(0), allocationOptions, n);
+ RUN_TEST_FUNC(testFreeSpace, char16_t(0), allocationOptions, n);
+ RUN_TEST_FUNC(testFreeSpace, int(0), allocationOptions, n);
+ RUN_TEST_FUNC(testFreeSpace, QString(), allocationOptions, n);
+ RUN_TEST_FUNC(testFreeSpace, CountedObject(), allocationOptions, n);
+}
+
#ifndef QT_NO_EXCEPTIONS
struct ThrowingTypeWatcher
{