diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-02-07 12:29:59 +0100 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-04-09 20:03:32 +0200 |
commit | 0375757bfb3852ace3bf3237a7de0ed2dbb371d8 (patch) | |
tree | da0241b0cb4870b42a72cd572e7b6845a4d8700e /tests/auto/corelib/tools/qhash | |
parent | c6cdf38e752c22babdbe645366bdfb7ce51d01ff (diff) |
Implement emplace() for QHash and QMultiHash
At the same time use the opportunity to refactor the
insertion code inside the implementation of QHash to
avoid copy and move constructors as much as possible
and always construct nodes in place.
Change-Id: I951b4cf2c77a17f7db825c6a776aae38c2662d23
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'tests/auto/corelib/tools/qhash')
-rw-r--r-- | tests/auto/corelib/tools/qhash/tst_qhash.cpp | 85 |
1 files changed, 79 insertions, 6 deletions
diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp index da1cb15cd8..c3e5d8d083 100644 --- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp +++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp @@ -70,6 +70,8 @@ private slots: void equal_range(); void insert_hash(); + void emplace(); + void badHashFunction(); }; @@ -96,26 +98,53 @@ int Foo::count = 0; class MyClass { public: - MyClass() { ++count; + MyClass() + { + ++count; + } + MyClass( const QString& c) + { + count++; + str = c; } - MyClass( const QString& c) { - count++; str = c; + MyClass(const QString &a, const QString &b) + { + count++; + str = a + b; } ~MyClass() { count--; } MyClass( const MyClass& c ) { - count++; str = c.str; + count++; + ++copies; + str = c.str; } MyClass &operator =(const MyClass &o) { - str = o.str; return *this; + str = o.str; + ++copies; + return *this; + } + MyClass(MyClass &&c) { + count++; + ++moves; + str = c.str; + } + MyClass &operator =(MyClass &&o) { + str = o.str; + ++moves; + return *this; } QString str; static int count; + static int copies; + static int moves; }; -int MyClass::count = 0; +int MyClass::count = 0; +int MyClass::copies = 0; +int MyClass::moves = 0; typedef QHash<QString, MyClass> MyMap; @@ -1650,6 +1679,50 @@ void tst_QHash::insert_hash() } } +void tst_QHash::emplace() +{ + { + QHash<QString, MyClass> hash; + MyClass::copies = 0; + MyClass::moves = 0; + + hash.emplace(QString("a"), QString("a")); + QCOMPARE(hash["a"].str, "a"); + QCOMPARE(MyClass::copies, 0); + QCOMPARE(MyClass::moves, 0); + hash.emplace(QString("ab"), QString("ab")); + QCOMPARE(hash["ab"].str, "ab"); + QCOMPARE(MyClass::copies, 0); + QCOMPARE(MyClass::moves, 0); + hash.emplace(QString("ab"), QString("abc")); + QCOMPARE(hash["ab"].str, "abc"); + QCOMPARE(MyClass::copies, 0); + QCOMPARE(MyClass::moves, 1); + } + { + QMultiHash<QString, MyClass> hash; + MyClass::copies = 0; + MyClass::moves = 0; + + hash.emplace(QString("a"), QString("a")); + QCOMPARE(hash["a"].str, "a"); + QCOMPARE(MyClass::copies, 0); + QCOMPARE(MyClass::moves, 0); + hash.emplace(QString("ab"), QString("ab")); + QCOMPARE(hash["ab"].str, "ab"); + QCOMPARE(MyClass::copies, 0); + QCOMPARE(MyClass::moves, 0); + hash.emplace(QString("ab"), QString("abc")); + QCOMPARE(hash["ab"].str, "abc"); + QCOMPARE(MyClass::copies, 0); + QCOMPARE(MyClass::moves, 0); + hash.emplaceReplace(QString("ab"), QString("abcd")); + QCOMPARE(hash["ab"].str, "abcd"); + QCOMPARE(MyClass::copies, 0); + QCOMPARE(MyClass::moves, 1); + } +} + struct BadKey { int k; BadKey(int i) : k(i) {} |