summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/tools/qvarlengtharray.h3
-rw-r--r--tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp27
2 files changed, 29 insertions, 1 deletions
diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h
index 90be3b3299..6ecc22213f 100644
--- a/src/corelib/tools/qvarlengtharray.h
+++ b/src/corelib/tools/qvarlengtharray.h
@@ -621,7 +621,8 @@ Q_OUTOFLINE_TEMPLATE auto QVarLengthArray<T, Prealloc>::emplace(const_iterator b
Q_ASSERT_X(isValidIterator(before), "QVarLengthArray::insert", "The specified const_iterator argument 'before' is invalid");
qsizetype offset = qsizetype(before - ptr);
- reserve(s + 1);
+ if (s == a)
+ reserve(s * 2);
if (!QTypeInfo<T>::isRelocatable) {
T *b = ptr + offset;
T *i = ptr + s;
diff --git a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp
index 6e95577655..1d30ca3d03 100644
--- a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp
+++ b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp
@@ -180,6 +180,33 @@ void tst_QVarLengthArray::emplace()
QCOMPARE(&r, &strings.back());
QCOMPARE(strings.size(), 3);
QCOMPARE(strings.back(), QString(42, u'y'));
+
+ // test growing from empty arrays
+ QVarLengthArray<QString> emptyArrDefaultPrealloc;
+ QCOMPARE(emptyArrDefaultPrealloc.size(), 0);
+ emptyArrDefaultPrealloc.emplace_back();
+ QCOMPARE(emptyArrDefaultPrealloc.size(), 1);
+ emptyArrDefaultPrealloc.resize(1024);
+ QCOMPARE(emptyArrDefaultPrealloc.size(), 1024);
+ emptyArrDefaultPrealloc.resize(0);
+ QCOMPARE(emptyArrDefaultPrealloc.size(), 0);
+ emptyArrDefaultPrealloc.squeeze();
+ QCOMPARE(emptyArrDefaultPrealloc.size(), 0);
+ emptyArrDefaultPrealloc.emplace_back();
+ QCOMPARE(emptyArrDefaultPrealloc.size(), 1);
+
+ QVarLengthArray<QString, 1> emptyArrSmallPrealloc;
+ QCOMPARE(emptyArrSmallPrealloc.size(), 0);
+ emptyArrSmallPrealloc.emplace_back();
+ QCOMPARE(emptyArrSmallPrealloc.size(), 1);
+ emptyArrSmallPrealloc.resize(1024);
+ QCOMPARE(emptyArrSmallPrealloc.size(), 1024);
+ emptyArrSmallPrealloc.resize(0);
+ QCOMPARE(emptyArrSmallPrealloc.size(), 0);
+ emptyArrSmallPrealloc.squeeze();
+ QCOMPARE(emptyArrSmallPrealloc.size(), 0);
+ emptyArrSmallPrealloc.emplace_back();
+ QCOMPARE(emptyArrSmallPrealloc.size(), 1);
}
}