From: Liang Qi Date: Tue, 10 May 2011 09:38:53 +0000 (+0200) Subject: Respect capacity in QVector::append(). X-Git-Tag: qt-v5.0.0-alpha1~4299^2~4 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=44b7877c879faabe83310df697d1ec2f013ee8b5;p=profile%2Fivi%2Fqtbase.git Respect capacity in QVector::append(). Fix a bug in QVector::append(), it should use the capacity for new size, when it is implicit shared and capacity is bigger than the new size. Autotest included. Task-number: QTBUG-11763 Reviewed-by: joao Reviewed-by: Olivier Goffart --- diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 9418642..6f068dc 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -574,8 +574,9 @@ void QVector::append(const T &t) { if (d->ref != 1 || d->size + 1 > d->alloc) { const T copy(t); - realloc(d->size, QVectorData::grow(sizeOfTypedData(), d->size + 1, sizeof(T), - QTypeInfo::isStatic)); + realloc(d->size, (d->size + 1 > d->alloc) ? + QVectorData::grow(sizeOfTypedData(), d->size + 1, sizeof(T), QTypeInfo::isStatic) + : d->alloc); if (QTypeInfo::isComplex) new (p->array + d->size) T(copy); else diff --git a/tests/auto/qvector/tst_qvector.cpp b/tests/auto/qvector/tst_qvector.cpp index dde59d7..1669c24 100644 --- a/tests/auto/qvector/tst_qvector.cpp +++ b/tests/auto/qvector/tst_qvector.cpp @@ -89,6 +89,8 @@ private slots: void outOfMemory(); void QTBUG6416_reserve(); + void QTBUG11763_data(); + void QTBUG11763(); void initializeList(); }; @@ -847,6 +849,83 @@ void tst_QVector::QTBUG6416_reserve() QCOMPARE(fooCtor, fooDtor); } +void tst_QVector::QTBUG11763_data() +{ + QTest::addColumn("capacity"); + QTest::addColumn("fill_size"); + QTest::addColumn("func_id"); + QTest::addColumn("result1"); + QTest::addColumn("result2"); + QTest::addColumn("result3"); + QTest::addColumn("result4"); + + int result1, result2, result3, result4; + int fill_size; + for (int i = 70; i <= 100; i += 10) { + fill_size = i - 20; + for (int j = 0; j <= 3; j++) { + if (j == 0) { // append + result1 = i; + result2 = i; + result3 = i - 19; + result4 = i - 20; + } else if (j == 1) { // insert(0) + result1 = i; + result2 = i; + result3 = i - 19; + result4 = i - 20; + } else if (j == 2) { // insert(20) + result1 = i; + result2 = i; + result3 = i - 19; + result4 = i - 20; + } else if (j == 3) { // insert(0, 10) + result1 = i; + result2 = i; + result3 = i - 10; + result4 = i - 20; + } + QTest::newRow(qPrintable(QString("QTBUG11763:%1").arg(i))) << i << fill_size << j << result1 << result2 << result3 << result4; + } + } +} + +void tst_QVector::QTBUG11763() +{ + QFETCH(int, capacity); + QFETCH(int, fill_size); + QFETCH(int, func_id); + QFETCH(int, result1); + QFETCH(int, result2); + QFETCH(int, result3); + QFETCH(int, result4); + + QVector v1; + QVector v2; + + v1.reserve(capacity); + v1.resize(0); + v1.fill(qreal(1.0), fill_size); + + v2 = v1; + + // no need to test begin() and end(), there is a detach() in them + if (func_id == 0) { + v1.append(qreal(1.0)); //push_back is same as append + } else if (func_id == 1) { + v1.insert(0, qreal(1.0)); //push_front is same as prepend, insert(0) + } else if (func_id == 2) { + v1.insert(20, qreal(1.0)); + } else if (func_id == 3) { + v1.insert(0, 10, qreal(1.0)); + } + + QCOMPARE(v1.capacity(), result1); + QCOMPARE(v2.capacity(), result2); + QCOMPARE(v1.size(), result3); + QCOMPARE(v2.size(), result4); +} + void tst_QVector::initializeList() { #ifdef Q_COMPILER_INITIALIZER_LISTS