|
15 | 15 | #include <QTest>
|
16 | 16 | #include <QVector>
|
17 | 17 | #include <algorithm>
|
| 18 | +#include <array> |
18 | 19 | #include <deque>
|
19 | 20 | #include <forward_list>
|
20 | 21 | #include <iostream>
|
@@ -182,6 +183,7 @@ private Q_SLOTS:
|
182 | 183 | void multi_partitioned();
|
183 | 184 | void multi_partitioned_with_function_taking_a_value();
|
184 | 185 | void sub_range();
|
| 186 | + void product(); |
185 | 187 | };
|
186 | 188 |
|
187 | 189 | void TestAlgorithms::copy()
|
@@ -2929,6 +2931,68 @@ void TestAlgorithms::sub_range()
|
2929 | 2931 | #endif
|
2930 | 2932 | }
|
2931 | 2933 |
|
| 2934 | +void TestAlgorithms::product() |
| 2935 | +{ |
| 2936 | + { // Base case |
| 2937 | + std::vector<int> v{1, 2, 3}; |
| 2938 | + std::vector<std::tuple<int>> expected{{1}, {2}, {3}}; |
| 2939 | + auto result = kdalgorithms::cartesian_product(v); |
| 2940 | + QCOMPARE(result, expected); |
| 2941 | + } |
| 2942 | + |
| 2943 | + { // Two lists |
| 2944 | + std::vector<int> v1{1, 2}; |
| 2945 | + std::vector<int> v2{3, 4}; |
| 2946 | + std::vector<std::tuple<int, int>> expected{{1, 3}, {1, 4}, {2, 3}, {2, 4}}; |
| 2947 | + auto result = kdalgorithms::cartesian_product(v1, v2); |
| 2948 | + QCOMPARE(result, expected); |
| 2949 | + } |
| 2950 | + |
| 2951 | + { // Three lists |
| 2952 | + std::vector<int> v1{1, 2}; |
| 2953 | + std::vector<int> v2{3, 4}; |
| 2954 | + std::vector<int> v3{5, 6, 7}; |
| 2955 | + std::vector<std::tuple<int, int, int>> expected{{1, 3, 5}, {1, 3, 6}, {1, 3, 7}, {1, 4, 5}, |
| 2956 | + {1, 4, 6}, {1, 4, 7}, {2, 3, 5}, {2, 3, 6}, |
| 2957 | + {2, 3, 7}, {2, 4, 5}, {2, 4, 6}, {2, 4, 7}}; |
| 2958 | + auto result = kdalgorithms::cartesian_product(v1, v2, v3); |
| 2959 | + QCOMPARE(result, expected); |
| 2960 | + } |
| 2961 | + |
| 2962 | + { // Different types |
| 2963 | + std::vector<int> v1{1, 2}; |
| 2964 | + std::vector<bool> v2{true, false}; |
| 2965 | + std::vector<std::tuple<int, bool>> expected{{1, true}, {1, false}, {2, true}, {2, false}}; |
| 2966 | + auto result = kdalgorithms::cartesian_product(v1, v2); |
| 2967 | + QCOMPARE(result, expected); |
| 2968 | + } |
| 2969 | + |
| 2970 | + { // Different container types |
| 2971 | + // Example from https://en.cppreference.com/w/cpp/ranges/cartesian_product_view |
| 2972 | + const std::array<char, 2> x = {'A', 'B'}; |
| 2973 | + const std::vector<int> y = {1, 2, 3}; |
| 2974 | + const std::list<std::string> z = {"α", "β", "γ", "δ"}; |
| 2975 | + |
| 2976 | + const auto expected = std::vector<std::tuple<char, int, std::string>>{ |
| 2977 | + {'A', 1, "α"}, {'A', 1, "β"}, {'A', 1, "γ"}, {'A', 1, "δ"}, {'A', 2, "α"}, |
| 2978 | + {'A', 2, "β"}, {'A', 2, "γ"}, {'A', 2, "δ"}, {'A', 3, "α"}, {'A', 3, "β"}, |
| 2979 | + {'A', 3, "γ"}, {'A', 3, "δ"}, {'B', 1, "α"}, {'B', 1, "β"}, {'B', 1, "γ"}, |
| 2980 | + {'B', 1, "δ"}, {'B', 2, "α"}, {'B', 2, "β"}, {'B', 2, "γ"}, {'B', 2, "δ"}, |
| 2981 | + {'B', 3, "α"}, {'B', 3, "β"}, {'B', 3, "γ"}, {'B', 3, "δ"}, |
| 2982 | + }; |
| 2983 | + auto result = kdalgorithms::cartesian_product(x, y, z); |
| 2984 | + QCOMPARE(result, expected); |
| 2985 | + } |
| 2986 | + |
| 2987 | + { // Result Type given |
| 2988 | + std::vector<int> v1{1, 2}; |
| 2989 | + std::list<bool> v2{true, false}; |
| 2990 | + std::deque<std::tuple<int, bool>> expected{{1, true}, {1, false}, {2, true}, {2, false}}; |
| 2991 | + auto result = kdalgorithms::cartesian_product<std::deque>(v1, v2); |
| 2992 | + QCOMPARE(result, expected); |
| 2993 | + } |
| 2994 | +} |
| 2995 | + |
2932 | 2996 | QTEST_MAIN(TestAlgorithms)
|
2933 | 2997 |
|
2934 | 2998 | #include "tst_kdalgorithms.moc"
|
0 commit comments