SimpleVector: don't assert when reserving on empty

Change-Id: I09ac235085e645c8149c153653377252fef6fa3d
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
João Abecasis 2012-02-17 00:29:12 +01:00 committed by Qt by Nokia
parent a1621d235d
commit 91e20fff87
2 changed files with 65 additions and 2 deletions

View File

@ -156,9 +156,10 @@ public:
}
}
SimpleVector detached(Data::allocate(n,
SimpleVector detached(Data::allocate(qMax(n, size()),
d->detachFlags() | Data::CapacityReserved));
detached.d->copyAppend(constBegin(), constEnd());
if (size())
detached.d->copyAppend(constBegin(), constEnd());
detached.swap(*this);
}

View File

@ -72,6 +72,8 @@ private slots:
void sharedNullEmpty();
void staticData();
void simpleVector();
void simpleVectorReserve_data();
void simpleVectorReserve();
void allocate_data();
void allocate();
void alignment_data();
@ -529,6 +531,66 @@ void tst_QArrayData::simpleVector()
}
}
Q_DECLARE_METATYPE(SimpleVector<int>)
void tst_QArrayData::simpleVectorReserve_data()
{
QTest::addColumn<SimpleVector<int> >("vector");
QTest::addColumn<size_t>("capacity");
QTest::addColumn<size_t>("size");
QTest::newRow("null") << SimpleVector<int>() << size_t(0) << size_t(0);
QTest::newRow("empty") << SimpleVector<int>(0, 42) << size_t(0) << size_t(0);
QTest::newRow("non-empty") << SimpleVector<int>(5, 42) << size_t(5) << size_t(5);
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 = {
static_cast<QTypedArrayData<int> *>(
const_cast<QArrayData *>(&array.header)) };
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);
}
void tst_QArrayData::simpleVectorReserve()
{
QFETCH(SimpleVector<int>, vector);
QFETCH(size_t, capacity);
QFETCH(size_t, size);
QVERIFY(!capacity || capacity >= size);
QCOMPARE(vector.capacity(), capacity);
QCOMPARE(vector.size(), size);
const SimpleVector<int> copy(vector);
vector.reserve(0);
QCOMPARE(vector.capacity(), capacity);
QCOMPARE(vector.size(), size);
vector.reserve(10);
// zero-capacity (immutable) resets with detach
if (!capacity)
capacity = size;
QCOMPARE(vector.capacity(), qMax(size_t(10), capacity));
QCOMPARE(vector.size(), size);
vector.reserve(20);
QCOMPARE(vector.capacity(), size_t(20));
QCOMPARE(vector.size(), size);
vector.reserve(30);
QCOMPARE(vector.capacity(), size_t(30));
QCOMPARE(vector.size(), size);
QVERIFY(vector == copy);
}
struct Deallocator
{
Deallocator(size_t objectSize, size_t alignment)