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
This commit is contained in:
Liang Qi 2011-05-10 11:38:53 +02:00
parent 9398867209
commit 44b7877c87
2 changed files with 82 additions and 2 deletions

View File

@ -574,8 +574,9 @@ void QVector<T>::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<T>::isStatic));
realloc(d->size, (d->size + 1 > d->alloc) ?
QVectorData::grow(sizeOfTypedData(), d->size + 1, sizeof(T), QTypeInfo<T>::isStatic)
: d->alloc);
if (QTypeInfo<T>::isComplex)
new (p->array + d->size) T(copy);
else

View File

@ -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<int>("capacity");
QTest::addColumn<int>("fill_size");
QTest::addColumn<int>("func_id");
QTest::addColumn<int>("result1");
QTest::addColumn<int>("result2");
QTest::addColumn<int>("result3");
QTest::addColumn<int>("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<qreal> v1;
QVector<qreal> 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