summaryrefslogtreecommitdiff
path: root/tests/auto/algorithm/tst_algorithm.cpp
diff options
context:
space:
mode:
authorEike Ziller <eike.ziller@qt.io>2017-12-17 15:41:56 +0100
committerEike Ziller <eike.ziller@qt.io>2017-12-20 09:58:50 +0000
commit8944ba039166ea594f5eb5e30dd0d0265c3b3e43 (patch)
tree8a0588b93e47fae825fa1be0a48b95b9414abec7 /tests/auto/algorithm/tst_algorithm.cpp
parentf62b24c4755b7488661df87039d66a77183ebb9d (diff)
downloadqt-creator-8944ba039166ea594f5eb5e30dd0d0265c3b3e43.tar.gz
Utils::transform: Add support for various map types as result
Add support for output as std::map/unordered_map/set and QMap and QHash, when giving the full result type as template argument. For std:: (unordered_)map, the function must return a std::pair<Key,Value>, for QMap and QHash it can also be QPair<Key,Value>. Change-Id: If3dff17ab6aa5d1b11abc244813fd885d10c75a4 Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
Diffstat (limited to 'tests/auto/algorithm/tst_algorithm.cpp')
-rw-r--r--tests/auto/algorithm/tst_algorithm.cpp65
1 files changed, 65 insertions, 0 deletions
diff --git a/tests/auto/algorithm/tst_algorithm.cpp b/tests/auto/algorithm/tst_algorithm.cpp
index b5ce21c775..87e3b7ecbd 100644
--- a/tests/auto/algorithm/tst_algorithm.cpp
+++ b/tests/auto/algorithm/tst_algorithm.cpp
@@ -30,6 +30,7 @@
#include <list>
#include <memory>
#include <unordered_map>
+#include <unordered_set>
#include <valarray>
// must get included after the containers above or gcc4.9 will have a problem using
@@ -359,6 +360,70 @@ void tst_Algorithm::transform()
const std::set<int> trans = Utils::transform<std::set<int>>(v, [](int i) { return i + 1; });
QCOMPARE(trans, std::set<int>({2, 3, 4, 5}));
}
+ // various map/set/hash without push_back
+ {
+ // std::vector -> std::map
+ const std::vector<int> v({1, 2, 3, 4});
+ const std::map<int, int> trans = Utils::transform<std::map<int, int>>(v, [](int i) {
+ return std::make_pair(i, i + 1);
+ });
+ const std::map<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}});
+ QCOMPARE(trans, expected);
+ }
+ {
+ // std::vector -> std::unordered_set
+ const std::vector<int> v({1, 2, 3, 4});
+ const std::unordered_set<int> trans = Utils::transform<std::unordered_set<int>>(v, [](int i) {
+ return i + 1;
+ });
+ QCOMPARE(trans, std::unordered_set<int>({2, 3, 4, 5}));
+ }
+ {
+ // std::vector -> std::unordered_map
+ const std::vector<int> v({1, 2, 3, 4});
+ const std::unordered_map<int, int> trans
+ = Utils::transform<std::unordered_map<int, int>>(v, [](int i) {
+ return std::make_pair(i, i + 1);
+ });
+ const std::unordered_map<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}});
+ QCOMPARE(trans, expected);
+ }
+ {
+ // std::vector -> QMap using std::pair
+ const std::vector<int> v({1, 2, 3, 4});
+ const QMap<int, int> trans = Utils::transform<QMap<int, int>>(v, [](int i) {
+ return std::make_pair(i, i + 1);
+ });
+ const QMap<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}});
+ QCOMPARE(trans, expected);
+ }
+ {
+ // std::vector -> QMap using QPair
+ const std::vector<int> v({1, 2, 3, 4});
+ const QMap<int, int> trans = Utils::transform<QMap<int, int>>(v, [](int i) {
+ return qMakePair(i, i + 1);
+ });
+ const QMap<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}});
+ QCOMPARE(trans, expected);
+ }
+ {
+ // std::vector -> QHash using std::pair
+ const std::vector<int> v({1, 2, 3, 4});
+ const QHash<int, int> trans = Utils::transform<QHash<int, int>>(v, [](int i) {
+ return std::make_pair(i, i + 1);
+ });
+ const QHash<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}});
+ QCOMPARE(trans, expected);
+ }
+ {
+ // std::vector -> QHash using QPair
+ const std::vector<int> v({1, 2, 3, 4});
+ const QHash<int, int> trans = Utils::transform<QHash<int, int>>(v, [](int i) {
+ return qMakePair(i, i + 1);
+ });
+ const QHash<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}});
+ QCOMPARE(trans, expected);
+ }
}
void tst_Algorithm::sort()