SimpleVector: don't assert when reserving on empty
authorJoão Abecasis <joao.abecasis@nokia.com>
Thu, 16 Feb 2012 23:29:12 +0000 (00:29 +0100)
committerQt by Nokia <qt-info@nokia.com>
Wed, 22 Feb 2012 20:18:11 +0000 (21:18 +0100)
Change-Id: I09ac235085e645c8149c153653377252fef6fa3d
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
tests/auto/corelib/tools/qarraydata/simplevector.h
tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp

index b16025b..a0a9b5f 100644 (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);
     }
 
index 561491d..0112d71 100644 (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)