aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorEike Ziller <eike.ziller@qt.io>2017-12-15 13:52:47 +0100
committerEike Ziller <eike.ziller@qt.io>2017-12-20 09:58:27 +0000
commitf62b24c4755b7488661df87039d66a77183ebb9d (patch)
tree05186775689e853f90cf1b56dd3c7a4c0f20ea53 /tests
parent91e40e12dbb94283cd804405411effc7f9903ed4 (diff)
Utils::transform: Support containers without reserve()
Support containers without reserve() as result container, for example std::set/deque/list Change-Id: Ia96b834c67d5ee74bfb9de2cf6b86639f6b3d5d7 Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/algorithm/tst_algorithm.cpp36
1 files changed, 34 insertions, 2 deletions
diff --git a/tests/auto/algorithm/tst_algorithm.cpp b/tests/auto/algorithm/tst_algorithm.cpp
index 70088d8a20..b5ce21c775 100644
--- a/tests/auto/algorithm/tst_algorithm.cpp
+++ b/tests/auto/algorithm/tst_algorithm.cpp
@@ -27,6 +27,7 @@
#include <array>
#include <deque>
+#include <list>
#include <memory>
#include <unordered_map>
#include <valarray>
@@ -339,6 +340,25 @@ void tst_Algorithm::transform()
return s.getMember();
});
}
+ // target containers without reserve(...)
+ {
+ // std::vector -> std::deque
+ const std::vector<int> v({1, 2, 3, 4});
+ const std::deque<int> trans = Utils::transform<std::deque>(v, [](int i) { return i + 1; });
+ QCOMPARE(trans, std::deque<int>({2, 3, 4, 5}));
+ }
+ {
+ // std::vector -> std::list
+ const std::vector<int> v({1, 2, 3, 4});
+ const std::list<int> trans = Utils::transform<std::list>(v, [](int i) { return i + 1; });
+ QCOMPARE(trans, std::list<int>({2, 3, 4, 5}));
+ }
+ {
+ // std::vector -> std::set
+ const std::vector<int> v({1, 2, 3, 4});
+ 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}));
+ }
}
void tst_Algorithm::sort()
@@ -473,9 +493,10 @@ void tst_Algorithm::toRawPointer()
// different result container
const std::vector<Struct *> x2 = Utils::toRawPointer<std::vector>(v);
const QVector<Struct *> x3 = Utils::toRawPointer<QVector>(v);
+ const std::list<Struct *> x4 = Utils::toRawPointer<std::list>(v);
// different fully specified result container
- const std::vector<BaseStruct *> x4 = Utils::toRawPointer<std::vector<BaseStruct *>>(v);
- const QVector<BaseStruct *> x5 = Utils::toRawPointer<QVector<BaseStruct *>>(v);
+ const std::vector<BaseStruct *> x5 = Utils::toRawPointer<std::vector<BaseStruct *>>(v);
+ const QVector<BaseStruct *> x6 = Utils::toRawPointer<QVector<BaseStruct *>>(v);
}
void tst_Algorithm::toReferences()
@@ -496,6 +517,11 @@ void tst_Algorithm::toReferences()
std::vector<Struct> v;
const QList<std::reference_wrapper<Struct>> x = Utils::toReferences<QList>(v);
}
+ {
+ // std::vector -> std::list
+ std::vector<Struct> v;
+ const std::list<std::reference_wrapper<Struct>> x = Utils::toReferences<std::list>(v);
+ }
// toConstReference
{
// std::vector -> std::vector
@@ -513,6 +539,12 @@ void tst_Algorithm::toReferences()
const std::vector<Struct> v;
const QList<std::reference_wrapper<const Struct>> x = Utils::toConstReferences<QList>(v);
}
+ {
+ // std::vector -> std::list
+ const std::vector<Struct> v;
+ const std::list<std::reference_wrapper<const Struct>> x
+ = Utils::toConstReferences<std::list>(v);
+ }
}
QTEST_MAIN(tst_Algorithm)