diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2012-06-14 15:05:34 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2019-12-08 10:29:42 +0100 |
commit | add048bc4eee8e4422fe2b434b4b817f56693d33 (patch) | |
tree | c83275a8046a64b38ce474a1f9a6f2297526aab5 /tests | |
parent | f2569c0ff75eb9a8418bb065c33c318f0a44c8ed (diff) |
Start moving QArrayData's size and data pointer to the main class
This requires that the allocation functions return two pointers: the d
pointer and the pointer to the actual data.
Ported QArrayDataPointer & SimpleVector to the inlined size & data.
For now, the size and offset members are not yet removed from
QArrayData, to let QVector, QByteArray and QString compile unmodified.
Change-Id: I8489300976723d75b8fd5831427b1e2bba486196
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/tools/qarraydata/simplevector.h | 25 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp | 175 |
2 files changed, 74 insertions, 126 deletions
diff --git a/tests/auto/corelib/tools/qarraydata/simplevector.h b/tests/auto/corelib/tools/qarraydata/simplevector.h index aa791b600c..94cee5d887 100644 --- a/tests/auto/corelib/tools/qarraydata/simplevector.h +++ b/tests/auto/corelib/tools/qarraydata/simplevector.h @@ -76,12 +76,23 @@ public: { } - explicit SimpleVector(Data *ptr) - : d(ptr) + template <size_t N> + explicit SimpleVector(QStaticArrayData<T, N> &ptr) + : d(static_cast<Data *>(&ptr.header), ptr.data, N) + { + } + + SimpleVector(Data *header, T *data, size_t len = 0) + : d(header, data, len) + { + } + + explicit SimpleVector(QPair<Data*, T*> ptr, size_t len = 0) + : d(ptr, len) { } - bool empty() const { return d->size == 0; } + bool empty() const { return d.size == 0; } bool isNull() const { return d.isNull(); } bool isEmpty() const { return this->empty(); } @@ -89,8 +100,8 @@ public: bool isShared() const { return d->isShared(); } bool isSharedWith(const SimpleVector &other) const { return d == other.d; } - size_t size() const { return d->size; } - size_t capacity() const { return d->allocatedCapacity(); } + size_t size() const { return d.size; } + size_t capacity() const { return d->constAllocatedCapacity(); } iterator begin() { detach(); return d->begin(); } iterator end() { detach(); return d->end(); } @@ -139,10 +150,10 @@ public: return; if (n <= capacity()) { - if (d->flags & Data::CapacityReserved) + if (d->flags() & Data::CapacityReserved) return; if (!d->isShared()) { - d->flags |= Data::CapacityReserved; + d->flags() |= Data::CapacityReserved; return; } } diff --git a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp index 44066e29a1..3a19f944d3 100644 --- a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp +++ b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp @@ -56,7 +56,6 @@ class tst_QArrayData : public QObject private slots: void referenceCounting(); void sharedNullEmpty(); - void staticData(); void simpleVector(); void simpleVectorReserve_data(); void simpleVectorReserve(); @@ -127,7 +126,8 @@ void tst_QArrayData::referenceCounting() void tst_QArrayData::sharedNullEmpty() { QArrayData *null = const_cast<QArrayData *>(QArrayData::shared_null); - QArrayData *empty = QArrayData::allocate(1, alignof(QArrayData), 0); + QArrayData *empty; + QArrayData::allocate(&empty, 1, alignof(QArrayData), 0); QVERIFY(null->isStatic()); QVERIFY(null->isShared()); @@ -149,37 +149,11 @@ void tst_QArrayData::sharedNullEmpty() QVERIFY(null != empty); - QCOMPARE(null->size, 0); QCOMPARE(null->allocatedCapacity(), size_t(0)); - QCOMPARE(empty->size, 0); QCOMPARE(empty->allocatedCapacity(), size_t(0)); } -void tst_QArrayData::staticData() -{ - QStaticArrayData<char, 10> charArray = { - Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(char, 10), - { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' } - }; - QStaticArrayData<int, 10> intArray = { - Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(int, 10), - { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } - }; - QStaticArrayData<double, 10> doubleArray = { - Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(double, 10), - { 0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f } - }; - - QCOMPARE(charArray.header.size, 10); - QCOMPARE(intArray.header.size, 10); - QCOMPARE(doubleArray.header.size, 10); - - QCOMPARE(charArray.header.data(), reinterpret_cast<void *>(&charArray.data)); - QCOMPARE(intArray.header.data(), reinterpret_cast<void *>(&intArray.data)); - QCOMPARE(doubleArray.header.data(), reinterpret_cast<void *>(&doubleArray.data)); -} - void tst_QArrayData::simpleVector() { QArrayData data0 = { Q_REFCOUNT_INITIALIZE_STATIC, QArrayData::StaticDataFlags, 0, 0, 0 }; @@ -192,10 +166,10 @@ void tst_QArrayData::simpleVector() SimpleVector<int> v1; SimpleVector<int> v2(v1); - SimpleVector<int> v3(static_cast<QTypedArrayData<int> *>(&data0)); - SimpleVector<int> v4(static_cast<QTypedArrayData<int> *>(&data1.header)); - SimpleVector<int> v5(static_cast<QTypedArrayData<int> *>(&data0)); - SimpleVector<int> v6(static_cast<QTypedArrayData<int> *>(&data1.header)); + SimpleVector<int> v3(static_cast<QTypedArrayData<int> *>(&data0), 0, 0); + SimpleVector<int> v4(data1); + SimpleVector<int> v5(static_cast<QTypedArrayData<int> *>(&data0), 0, 0); + SimpleVector<int> v6(data1); SimpleVector<int> v7(10, 5); SimpleVector<int> v8(array, array + sizeof(array)/sizeof(*array)); @@ -457,9 +431,11 @@ void tst_QArrayData::simpleVectorReserve_data() static const QStaticArrayData<int, 15> array = { Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(int, 15), { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } }; - QArrayDataPointerRef<int> p = { + const QArrayDataPointerRef<int> p = { static_cast<QTypedArrayData<int> *>( - const_cast<QArrayData *>(&array.header)) }; + const_cast<QArrayData *>(&array.header)), + const_cast<int *>(array.data), + sizeof(array.data) / sizeof(array.data[0]) }; QTest::newRow("static") << SimpleVector<int>(p) << size_t(0) << size_t(15); QTest::newRow("raw-data") << SimpleVector<int>::fromRawData(array.data, 15) << size_t(0) << size_t(15); @@ -542,7 +518,8 @@ void tst_QArrayData::allocate_data() { "void *", sizeof(void *), alignof(void *) } }; - QArrayData *shared_empty = QArrayData::allocate(0, alignof(QArrayData), 0); + QArrayData *shared_empty; + QArrayData::allocate(&shared_empty, 1, alignof(QArrayData), 0); QVERIFY(shared_empty); struct { @@ -580,18 +557,20 @@ void tst_QArrayData::allocate() size_t minAlignment = qMax(alignment, alignof(QArrayData)); // Shared Empty - QCOMPARE(QArrayData::allocate(objectSize, minAlignment, 0, - QArrayData::ArrayOptions(allocateOptions)), commonEmpty); + QArrayData *empty; + QCOMPARE((QArrayData::allocate(&empty, objectSize, minAlignment, 0, + QArrayData::ArrayOptions(allocateOptions)), empty), commonEmpty); Deallocator keeper(objectSize, minAlignment); keeper.headers.reserve(1024); for (int capacity = 1; capacity <= 1024; capacity <<= 1) { - QArrayData *data = QArrayData::allocate(objectSize, minAlignment, + QArrayData *data; + void *dataPointer = QArrayData::allocate(&data, objectSize, minAlignment, capacity, QArrayData::ArrayOptions(allocateOptions)); + keeper.headers.append(data); - QCOMPARE(data->size, 0); if (allocateOptions & QArrayData::GrowsForward) QVERIFY(data->allocatedCapacity() > uint(capacity)); else @@ -600,7 +579,7 @@ void tst_QArrayData::allocate() // Check that the allocated array can be used. Best tested with a // memory checker, such as valgrind, running. - ::memset(data->data(), 'A', objectSize * capacity); + ::memset(dataPointer, 'A', objectSize * capacity); } } @@ -621,22 +600,23 @@ void tst_QArrayData::reallocate() int capacity = 10; Deallocator keeper(objectSize, minAlignment); - QArrayData *data = QArrayData::allocate(objectSize, minAlignment, capacity, - QArrayData::ArrayOptions(allocateOptions) & ~QArrayData::GrowsForward); + QArrayData *data; + void *dataPointer = QArrayData::allocate(&data, objectSize, minAlignment, capacity, + QArrayData::ArrayOptions(allocateOptions) & ~QArrayData::GrowsForward); keeper.headers.append(data); - memset(data->data(), 'A', objectSize * capacity); - data->size = capacity; + memset(dataPointer, 'A', objectSize * capacity); // now try to reallocate int newCapacity = 40; - data = QArrayData::reallocateUnaligned(data, objectSize, newCapacity, - QArrayData::ArrayOptions(allocateOptions)); + auto pair = QArrayData::reallocateUnaligned(data, dataPointer, objectSize, newCapacity, + QArrayData::ArrayOptions(allocateOptions)); + data = pair.first; + dataPointer = pair.second; QVERIFY(data); keeper.headers.clear(); keeper.headers.append(data); - QCOMPARE(data->size, capacity); if (allocateOptions & QArrayData::GrowsForward) QVERIFY(data->allocatedCapacity() > size_t(newCapacity)); else @@ -644,7 +624,7 @@ void tst_QArrayData::reallocate() QCOMPARE(!(data->flags & QArrayData::CapacityReserved), !isCapacityReserved); for (int i = 0; i < capacity; ++i) - QCOMPARE(static_cast<char *>(data->data())[i], 'A'); + QCOMPARE(static_cast<char *>(dataPointer)[i], 'A'); } class Unaligned @@ -674,89 +654,46 @@ void tst_QArrayData::alignment() keeper.headers.reserve(100); for (int i = 0; i < 100; ++i) { - QArrayData *data = QArrayData::allocate(sizeof(Unaligned), + QArrayData *data; + void *dataPointer = QArrayData::allocate(&data, sizeof(Unaligned), minAlignment, 8, QArrayData::DefaultAllocationFlags); keeper.headers.append(data); QVERIFY(data); - QCOMPARE(data->size, 0); QVERIFY(data->allocatedCapacity() >= uint(8)); // These conditions should hold as long as header and array are // allocated together - QVERIFY(data->offset >= qptrdiff(sizeof(QArrayData))); - QVERIFY(data->offset <= qptrdiff(sizeof(QArrayData) + qptrdiff offset = reinterpret_cast<char *>(dataPointer) - + reinterpret_cast<char *>(data); + QVERIFY(offset >= qptrdiff(sizeof(QArrayData))); + QVERIFY(offset <= qptrdiff(sizeof(QArrayData) + minAlignment - alignof(QArrayData))); // Data is aligned - QCOMPARE(quintptr(quintptr(data->data()) % alignment), quintptr(0u)); + QCOMPARE(quintptr(quintptr(dataPointer) % alignment), quintptr(0u)); // Check that the allocated array can be used. Best tested with a // memory checker, such as valgrind, running. - ::memset(data->data(), 'A', sizeof(Unaligned) * 8); + ::memset(dataPointer, 'A', sizeof(Unaligned) * 8); } } void tst_QArrayData::typedData() { - QStaticArrayData<int, 10> data = { - Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(int, 10), - { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } - }; - QCOMPARE(data.header.size, 10); - - { - QTypedArrayData<int> *array = - static_cast<QTypedArrayData<int> *>(&data.header); - QCOMPARE(array->data(), data.data); - - int j = 0; - for (QTypedArrayData<int>::iterator iter = array->begin(); - iter != array->end(); ++iter, ++j) - QCOMPARE((const int *)iter, data.data + j); - QCOMPARE(j, 10); - } - - { - const QTypedArrayData<int> *array = - static_cast<const QTypedArrayData<int> *>(&data.header); - - QCOMPARE(array->data(), data.data); - - int j = 0; - for (QTypedArrayData<int>::const_iterator iter = array->begin(); - iter != array->end(); ++iter, ++j) - QCOMPARE((const int *)iter, data.data + j); - QCOMPARE(j, 10); - } - - { - QTypedArrayData<int> *null = QTypedArrayData<int>::sharedNull(); - QTypedArrayData<int> *empty = QTypedArrayData<int>::allocate(0); - - QVERIFY(null != empty); - - QCOMPARE(null->size, 0); - QCOMPARE(empty->size, 0); - - QCOMPARE(null->begin(), null->end()); - QCOMPARE(empty->begin(), empty->end()); - } - - { Deallocator keeper(sizeof(char), alignof(QTypedArrayData<char>::AlignmentDummy)); - QArrayData *array = QTypedArrayData<char>::allocate(10); + QPair<QTypedArrayData<char> *, char *> pair = QTypedArrayData<char>::allocate(10); + QArrayData *array = pair.first; keeper.headers.append(array); QVERIFY(array); - QCOMPARE(array->size, 0); QCOMPARE(array->allocatedCapacity(), size_t(10)); // Check that the allocated array can be used. Best tested with a // memory checker, such as valgrind, running. - ::memset(array->data(), 0, 10 * sizeof(char)); + ::memset(pair.second, 0, 10 * sizeof(char)); keeper.headers.clear(); QTypedArrayData<short>::deallocate(array); @@ -767,16 +704,16 @@ void tst_QArrayData::typedData() { Deallocator keeper(sizeof(short), alignof(QTypedArrayData<short>::AlignmentDummy)); - QArrayData *array = QTypedArrayData<short>::allocate(10); + QPair<QTypedArrayData<short> *, short *> pair = QTypedArrayData<short>::allocate(10); + QArrayData *array = pair.first; keeper.headers.append(array); QVERIFY(array); - QCOMPARE(array->size, 0); QCOMPARE(array->allocatedCapacity(), size_t(10)); // Check that the allocated array can be used. Best tested with a // memory checker, such as valgrind, running. - ::memset(array->data(), 0, 10 * sizeof(short)); + ::memset(pair.second, 0, 10 * sizeof(short)); keeper.headers.clear(); QTypedArrayData<short>::deallocate(array); @@ -787,16 +724,16 @@ void tst_QArrayData::typedData() { Deallocator keeper(sizeof(double), alignof(QTypedArrayData<double>::AlignmentDummy)); - QArrayData *array = QTypedArrayData<double>::allocate(10); + QPair<QTypedArrayData<double> *, double *> pair = QTypedArrayData<double>::allocate(10); + QArrayData *array = pair.first; keeper.headers.append(array); QVERIFY(array); - QCOMPARE(array->size, 0); QCOMPARE(array->allocatedCapacity(), size_t(10)); // Check that the allocated array can be used. Best tested with a // memory checker, such as valgrind, running. - ::memset(array->data(), 0, 10 * sizeof(double)); + ::memset(pair.second, 0, 10 * sizeof(double)); keeper.headers.clear(); QTypedArrayData<double>::deallocate(array); @@ -1292,17 +1229,17 @@ void tst_QArrayData::literals() { { QArrayDataPointer<char> d = Q_ARRAY_LITERAL(char, "ABCDEFGHIJ"); - QCOMPARE(d->size, 10 + 1); + QCOMPARE(d.size, 10u + 1u); for (int i = 0; i < 10; ++i) - QCOMPARE(d->data()[i], char('A' + i)); + QCOMPARE(d.data()[i], char('A' + i)); } { // wchar_t is not necessarily 2-bytes QArrayDataPointer<wchar_t> d = Q_ARRAY_LITERAL(wchar_t, L"ABCDEFGHIJ"); - QCOMPARE(d->size, 10 + 1); + QCOMPARE(d.size, 10u + 1u); for (int i = 0; i < 10; ++i) - QCOMPARE(d->data()[i], wchar_t('A' + i)); + QCOMPARE(d.data()[i], wchar_t('A' + i)); } { @@ -1340,26 +1277,26 @@ void tst_QArrayData::variadicLiterals() { QArrayDataPointer<int> d = Q_ARRAY_LITERAL(int, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9); - QCOMPARE(d->size, 10); + QCOMPARE(d.size, 10u); for (int i = 0; i < 10; ++i) - QCOMPARE(d->data()[i], i); + QCOMPARE(d.data()[i], i); } { QArrayDataPointer<char> d = Q_ARRAY_LITERAL(char, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'); - QCOMPARE(d->size, 10); + QCOMPARE(d.size, 10u); for (int i = 0; i < 10; ++i) - QCOMPARE(d->data()[i], char('A' + i)); + QCOMPARE(d.data()[i], char('A' + i)); } { QArrayDataPointer<const char *> d = Q_ARRAY_LITERAL(const char *, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"); - QCOMPARE(d->size, 10); + QCOMPARE(d.size, 10u); for (int i = 0; i < 10; ++i) { - QCOMPARE(d->data()[i][0], char('A' + i)); - QCOMPARE(d->data()[i][1], '\0'); + QCOMPARE(d.data()[i][0], char('A' + i)); + QCOMPARE(d.data()[i][1], '\0'); } } |