diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-11-12 12:30:33 +0100 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-11-17 11:47:28 +0100 |
commit | 30597cfc0ef299356a2d200a5628612d4d0b222c (patch) | |
tree | f48fec9d857958e0ef99719881f4a52553664a7a /tests/auto | |
parent | 21bce8925725bfea1abc371301e943d8970c71fc (diff) |
Clean up emplace implementations
Avoid duplicated code paths for GrowsForward vs
GrowsBackward. Special case emplaceing at the
beginning or end of the awrray where we can
avoid creating a temporary copy.
Change-Id: I2218ffd8d38cfa22e8faca75ebeadb79a682d3cf
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp | 154 |
1 files changed, 0 insertions, 154 deletions
diff --git a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp index 7ab93d636a..3bad143af5 100644 --- a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp +++ b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp @@ -84,9 +84,6 @@ private slots: void dataPointerAllocate(); void selfEmplaceBackwards(); void selfEmplaceForward(); -#ifndef QT_NO_EXCEPTIONS - void exceptionSafetyPrimitives_displacer(); -#endif }; template <class T> const T &const_(const T &t) { return t; } @@ -2278,156 +2275,5 @@ void tst_QArrayData::selfEmplaceForward() RUN_TEST_FUNC(testSelfEmplace, MyComplexQString(), 4, complexObjs); } -#ifndef QT_NO_EXCEPTIONS -struct ThrowingTypeWatcher -{ - std::vector<int> destroyedIds; - bool watch = false; - void destroyed(int id) - { - if (watch) - destroyedIds.push_back(id); - } -}; -ThrowingTypeWatcher& throwingTypeWatcher() { static ThrowingTypeWatcher global; return global; } - -struct ThrowingType -{ - static unsigned int throwOnce; - static constexpr char throwString[] = "Requested to throw"; - static constexpr char throwStringDtor[] = "Requested to throw in dtor"; - void checkThrow() { - // deferred throw - if (throwOnce > 0) { - --throwOnce; - if (throwOnce == 0) { - throw std::runtime_error(throwString); - } - } - return; - } - int id = 0; - - ThrowingType(int val = 0) noexcept(false) : id(val) - { - checkThrow(); - } - ThrowingType(const ThrowingType &other) noexcept(false) : id(other.id) - { - checkThrow(); - } - ThrowingType& operator=(const ThrowingType &other) noexcept(false) - { - id = other.id; - checkThrow(); - return *this; - } - ~ThrowingType() - { - throwingTypeWatcher().destroyed(id); // notify global watcher - id = -1; - - } -}; -unsigned int ThrowingType::throwOnce = 0; -bool operator==(const ThrowingType &a, const ThrowingType &b) { - return a.id == b.id; -} -QT_BEGIN_NAMESPACE -Q_DECLARE_TYPEINFO(ThrowingType, Q_RELOCATABLE_TYPE); -QT_END_NAMESPACE - -template<typename T> // T must be constructible from a single int parameter -static QArrayDataPointer<T> createDataPointer(qsizetype capacity, qsizetype initSize) -{ - QArrayDataPointer<T> adp(QTypedArrayData<T>::allocate(capacity)); - adp->appendInitialize(initSize); - // assign unique values - int i = 0; - std::generate(adp.begin(), adp.end(), [&i] () { return T(i++); }); - return adp; -} - -void tst_QArrayData::exceptionSafetyPrimitives_displacer() -{ - QVERIFY(QTypeInfo<ThrowingType>::isRelocatable); - using Prims = QtPrivate::QArrayExceptionSafetyPrimitives<ThrowingType>; - const auto doDisplace = [] (auto &dataPointer, auto start, auto finish, qsizetype diff) { - typename Prims::Displacer displace(start, finish, diff); - new (start) ThrowingType(42); - ++dataPointer.size; - displace.commit(); - }; - - // successful operation with displace to the right - { - auto data = createDataPointer<ThrowingType>(20, 10); - auto reference = createDataPointer<ThrowingType>(20, 10); - reference->insert(reference.size - 1, 1, ThrowingType(42)); - - auto where = data.end() - 1; - doDisplace(data, where, data.end(), 1); - - QCOMPARE(data.size, reference.size); - for (qsizetype i = 0; i < data.size; ++i) - QCOMPARE(data.data()[i], reference.data()[i]); - } - - // failed operation with displace to the right - { - auto data = createDataPointer<ThrowingType>(20, 10); - auto reference = createDataPointer<ThrowingType>(20, 10); - try { - ThrowingType::throwOnce = 1; - doDisplace(data, data.end() - 1, data.end(), 1); - QFAIL("Unreachable line!"); - } catch (const std::exception &e) { - QCOMPARE(std::string(e.what()), ThrowingType::throwString); - QCOMPARE(data.size, reference.size); - for (qsizetype i = 0; i < data.size; ++i) - QCOMPARE(data.data()[i], reference.data()[i]); - } - } - - // successful operation with displace to the left - { - auto data = createDataPointer<ThrowingType>(20, 10); - auto reference = createDataPointer<ThrowingType>(20, 10); - reference.data()[0] = reference.data()[1]; - reference.data()[1] = ThrowingType(42); - - data.begin()->~ThrowingType(); // free space at begin - --data.size; - auto where = data.begin() + 1; - doDisplace(data, where, where + 1, -1); - - QCOMPARE(data.size, reference.size); - for (qsizetype i = 0; i < data.size; ++i) - QCOMPARE(data.data()[i], reference.data()[i]); - } - - // failed operation with displace to the left - { - auto data = createDataPointer<ThrowingType>(20, 10); - auto reference = createDataPointer<ThrowingType>(20, 10); - reference->erase(reference.begin(), reference.begin() + 1); - - try { - data.begin()->~ThrowingType(); // free space at begin - --data.size; - ThrowingType::throwOnce = 1; - auto where = data.begin() + 1; - doDisplace(data, where, where + 1, -1); - QFAIL("Unreachable line!"); - } catch (const std::exception &e) { - QCOMPARE(std::string(e.what()), ThrowingType::throwString); - QCOMPARE(data.size, reference.size); - for (qsizetype i = 0; i < data.size; ++i) - QCOMPARE(data.data()[i + 1], reference.data()[i]); - } - } -} -#endif // QT_NO_EXCEPTIONS - QTEST_APPLESS_MAIN(tst_QArrayData) #include "tst_qarraydata.moc" |