Add test for QVector::setSharable
authorJoão Abecasis <joao.abecasis@nokia.com>
Fri, 25 Nov 2011 12:16:24 +0000 (13:16 +0100)
committerQt by Nokia <qt-info@nokia.com>
Thu, 5 Jan 2012 17:14:52 +0000 (18:14 +0100)
Change-Id: Id31761bfb642d4ce515768c1ffe1e3088d883353
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
tests/auto/corelib/tools/qvector/tst_qvector.cpp

index acf3756..704d75b 100644 (file)
@@ -85,6 +85,8 @@ private slots:
     void initializeList();
 
     void const_shared_null();
+    void setSharable_data();
+    void setSharable();
 };
 
 void tst_QVector::constructors() const
@@ -946,5 +948,97 @@ void tst_QVector::const_shared_null()
     QVERIFY(!v2.isDetached());
 }
 
+Q_DECLARE_METATYPE(QVector<int>);
+
+void tst_QVector::setSharable_data()
+{
+    QTest::addColumn<QVector<int> >("vector");
+    QTest::addColumn<int>("size");
+    QTest::addColumn<int>("capacity");
+    QTest::addColumn<bool>("isCapacityReserved");
+
+    QVector<int> null;
+    QVector<int> empty(0, 5);
+    QVector<int> emptyReserved;
+    QVector<int> nonEmpty;
+    QVector<int> nonEmptyReserved;
+
+    emptyReserved.reserve(10);
+    nonEmptyReserved.reserve(15);
+
+    nonEmpty << 0 << 1 << 2 << 3 << 4;
+    nonEmptyReserved << 0 << 1 << 2 << 3 << 4 << 5 << 6;
+
+    QVERIFY(emptyReserved.capacity() >= 10);
+    QVERIFY(nonEmptyReserved.capacity() >= 15);
+
+    QTest::newRow("null") << null << 0 << 0 << false;
+    QTest::newRow("empty") << empty << 0 << 0 << false;
+    QTest::newRow("empty, Reserved") << emptyReserved << 0 << 10 << true;
+    QTest::newRow("non-empty") << nonEmpty << 5 << 0 << false;
+    QTest::newRow("non-empty, Reserved") << nonEmptyReserved << 7 << 15 << true;
+}
+
+void tst_QVector::setSharable()
+{
+    QFETCH(QVector<int>, vector);
+    QFETCH(int, size);
+    QFETCH(int, capacity);
+    QFETCH(bool, isCapacityReserved);
+
+    QVERIFY(!vector.isDetached()); // Shared with QTest
+
+    vector.setSharable(true);
+
+    QCOMPARE(vector.size(), size);
+    if (isCapacityReserved)
+        QVERIFY2(vector.capacity() >= capacity,
+                qPrintable(QString("Capacity is %1, expected at least %2.")
+                    .arg(vector.capacity())
+                    .arg(capacity)));
+
+    {
+        QVector<int> copy(vector);
+
+        QVERIFY(!copy.isDetached());
+        QVERIFY(copy.isSharedWith(vector));
+    }
+
+    vector.setSharable(false);
+    QVERIFY(vector.isDetached() || vector.isSharedWith(QVector<int>()));
+
+    {
+        QVector<int> copy(vector);
+
+        QVERIFY(copy.isDetached() || copy.isSharedWith(QVector<int>()));
+        QCOMPARE(copy.size(), size);
+        if (isCapacityReserved)
+            QVERIFY2(copy.capacity() >= capacity,
+                    qPrintable(QString("Capacity is %1, expected at least %2.")
+                        .arg(vector.capacity())
+                        .arg(capacity)));
+        QCOMPARE(copy, vector);
+    }
+
+    vector.setSharable(true);
+
+    {
+        QVector<int> copy(vector);
+
+        QVERIFY(!copy.isDetached());
+        QVERIFY(copy.isSharedWith(vector));
+    }
+
+    for (int i = 0; i < vector.size(); ++i)
+        QCOMPARE(vector[i], i);
+
+    QCOMPARE(vector.size(), size);
+    if (isCapacityReserved)
+        QVERIFY2(vector.capacity() >= capacity,
+                qPrintable(QString("Capacity is %1, expected at least %2.")
+                    .arg(vector.capacity())
+                    .arg(capacity)));
+}
+
 QTEST_APPLESS_MAIN(tst_QVector)
 #include "tst_qvector.moc"