diff options
author | Eike Ziller <eike.ziller@qt.io> | 2017-11-28 16:30:49 +0100 |
---|---|---|
committer | Tobias Hunger <tobias.hunger@qt.io> | 2017-11-29 08:59:52 +0000 |
commit | c90e5b50dc27c67c7ac67d3eb9fc3d6f22ee0e6c (patch) | |
tree | df5721bb532c0655b668e9327877f38409b1fdee /tests/auto/algorithm/tst_algorithm.cpp | |
parent | e2baa116cad487d12f2ebccd96f16e99995fcb90 (diff) | |
download | qt-creator-c90e5b50dc27c67c7ac67d3eb9fc3d6f22ee0e6c.tar.gz |
Algorithm: Support std::vector<std::unique_ptr> with contains and findOr
Change-Id: I01cb71c06b405f071498ed8752f9acac44d2b223
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.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/tests/auto/algorithm/tst_algorithm.cpp b/tests/auto/algorithm/tst_algorithm.cpp index be3f07b070..9692c55c11 100644 --- a/tests/auto/algorithm/tst_algorithm.cpp +++ b/tests/auto/algorithm/tst_algorithm.cpp @@ -37,6 +37,9 @@ class tst_Algorithm : public QObject private slots: void transform(); void sort(); + void contains(); + void findOr(); + void findOrDefault(); }; @@ -50,6 +53,8 @@ struct Struct { Struct(int m) : member(m) {} bool operator==(const Struct &other) const { return member == other.member; } + bool isOdd() const { return member % 2 == 1; } + bool isEven() const { return !isOdd(); } int member; }; @@ -168,6 +173,64 @@ void tst_Algorithm::sort() QCOMPARE(valarray[i], valarrayResult[i]); } +void tst_Algorithm::contains() +{ + std::vector<int> v1{1, 2, 3, 4}; + QVERIFY(Utils::contains(v1, [](int i) { return i == 2; })); + QVERIFY(!Utils::contains(v1, [](int i) { return i == 5; })); + std::vector<std::unique_ptr<int>> v2; + v2.emplace_back(std::make_unique<int>(1)); + v2.emplace_back(std::make_unique<int>(2)); + v2.emplace_back(std::make_unique<int>(3)); + v2.emplace_back(std::make_unique<int>(4)); + QVERIFY(Utils::contains(v2, [](const std::unique_ptr<int> &ip) { return *ip == 2; })); + QVERIFY(!Utils::contains(v2, [](const std::unique_ptr<int> &ip) { return *ip == 5; })); +} + +void tst_Algorithm::findOr() +{ + std::vector<int> v1{1, 2, 3, 4}; + QCOMPARE(Utils::findOr(v1, 6, [](int i) { return i == 2; }), 2); + QCOMPARE(Utils::findOr(v1, 6, [](int i) { return i == 5; }), 6); + std::vector<std::unique_ptr<int>> v2; + v2.emplace_back(std::make_unique<int>(1)); + v2.emplace_back(std::make_unique<int>(2)); + v2.emplace_back(std::make_unique<int>(3)); + v2.emplace_back(std::make_unique<int>(4)); + int def = 6; + QCOMPARE(Utils::findOr(v2, &def, [](const std::unique_ptr<int> &ip) { return *ip == 2; }), v2.at(1).get()); + QCOMPARE(Utils::findOr(v2, &def, [](const std::unique_ptr<int> &ip) { return *ip == 5; }), &def); + std::vector<std::unique_ptr<Struct>> v3; + v3.emplace_back(std::make_unique<Struct>(1)); + v3.emplace_back(std::make_unique<Struct>(3)); + v3.emplace_back(std::make_unique<Struct>(5)); + v3.emplace_back(std::make_unique<Struct>(7)); + Struct defS(6); + QCOMPARE(Utils::findOr(v3, &defS, &Struct::isOdd), v3.at(0).get()); + QCOMPARE(Utils::findOr(v3, &defS, &Struct::isEven), &defS); +} + +void tst_Algorithm::findOrDefault() +{ + std::vector<int> v1{1, 2, 3, 4}; + QCOMPARE(Utils::findOrDefault(v1, [](int i) { return i == 2; }), 2); + QCOMPARE(Utils::findOrDefault(v1, [](int i) { return i == 5; }), 0); + std::vector<std::unique_ptr<int>> v2; + v2.emplace_back(std::make_unique<int>(1)); + v2.emplace_back(std::make_unique<int>(2)); + v2.emplace_back(std::make_unique<int>(3)); + v2.emplace_back(std::make_unique<int>(4)); + QCOMPARE(Utils::findOrDefault(v2, [](const std::unique_ptr<int> &ip) { return *ip == 2; }), v2.at(1).get()); + QCOMPARE(Utils::findOrDefault(v2, [](const std::unique_ptr<int> &ip) { return *ip == 5; }), nullptr); + std::vector<std::unique_ptr<Struct>> v3; + v3.emplace_back(std::make_unique<Struct>(1)); + v3.emplace_back(std::make_unique<Struct>(3)); + v3.emplace_back(std::make_unique<Struct>(5)); + v3.emplace_back(std::make_unique<Struct>(7)); + QCOMPARE(Utils::findOrDefault(v3, &Struct::isOdd), v3.at(0).get()); + QCOMPARE(Utils::findOrDefault(v3, &Struct::isEven), nullptr); +} + QTEST_MAIN(tst_Algorithm) #include "tst_algorithm.moc" |