xref: /llvm-project/libcxx/test/std/algorithms/ranges_robust_against_differing_projections.pass.cpp (revision 0218ea4aaa5478d4a55c3f73735d90968c8cbace)
1964aeb71SKonstantin Varlamov //===----------------------------------------------------------------------===//
2964aeb71SKonstantin Varlamov //
3964aeb71SKonstantin Varlamov // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4964aeb71SKonstantin Varlamov // See https://llvm.org/LICENSE.txt for license information.
5964aeb71SKonstantin Varlamov // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6964aeb71SKonstantin Varlamov //
7964aeb71SKonstantin Varlamov //===----------------------------------------------------------------------===//
8964aeb71SKonstantin Varlamov 
9964aeb71SKonstantin Varlamov // UNSUPPORTED: c++03, c++11, c++14, c++17
10964aeb71SKonstantin Varlamov 
11964aeb71SKonstantin Varlamov // <algorithm>
12964aeb71SKonstantin Varlamov //
13964aeb71SKonstantin Varlamov // Range algorithms should support the case where the ranges they operate on have different value types and the given
14964aeb71SKonstantin Varlamov // projection functors are different (each projection applies to a different value type).
15964aeb71SKonstantin Varlamov 
16964aeb71SKonstantin Varlamov #include <algorithm>
17964aeb71SKonstantin Varlamov 
18964aeb71SKonstantin Varlamov #include <array>
19964aeb71SKonstantin Varlamov #include <functional>
20964aeb71SKonstantin Varlamov #include <iterator>
21964aeb71SKonstantin Varlamov #include <ranges>
22964aeb71SKonstantin Varlamov #include <utility>
2320517557Szijunzhao #include "test_macros.h"
24964aeb71SKonstantin Varlamov 
25964aeb71SKonstantin Varlamov // (in1, in2, ...)
26964aeb71SKonstantin Varlamov template <class Func, std::ranges::range Input1, std::ranges::range Input2, class ...Args>
test(Func && func,Input1 & in1,Input2 & in2,Args &&...args)27964aeb71SKonstantin Varlamov constexpr void test(Func&& func, Input1& in1, Input2& in2, Args&& ...args) {
28660b2431SNikolas Klauser   (void)func(in1.begin(), in1.end(), in2.begin(), in2.end(), std::forward<Args>(args)...);
29660b2431SNikolas Klauser   (void)func(in1, in2, std::forward<Args>(args)...);
30964aeb71SKonstantin Varlamov }
31964aeb71SKonstantin Varlamov 
test_all()32964aeb71SKonstantin Varlamov constexpr bool test_all() {
33964aeb71SKonstantin Varlamov   struct A {
34964aeb71SKonstantin Varlamov     int x = 0;
35964aeb71SKonstantin Varlamov 
36964aeb71SKonstantin Varlamov     constexpr A() = default;
37964aeb71SKonstantin Varlamov     constexpr A(int value) : x(value) {}
38964aeb71SKonstantin Varlamov     constexpr operator int() const { return x; }
39964aeb71SKonstantin Varlamov 
40964aeb71SKonstantin Varlamov     constexpr auto operator<=>(const A&) const = default;
41964aeb71SKonstantin Varlamov   };
42964aeb71SKonstantin Varlamov 
43964aeb71SKonstantin Varlamov   std::array in = {1, 2, 3};
44964aeb71SKonstantin Varlamov   std::array in2 = {A{4}, A{5}, A{6}};
45964aeb71SKonstantin Varlamov 
46964aeb71SKonstantin Varlamov   std::array output = {7, 8, 9, 10, 11, 12};
47964aeb71SKonstantin Varlamov   auto out = output.begin();
48964aeb71SKonstantin Varlamov   std::array output2 = {A{7}, A{8}, A{9}, A{10}, A{11}, A{12}};
49964aeb71SKonstantin Varlamov   auto out2 = output2.begin();
50964aeb71SKonstantin Varlamov 
51964aeb71SKonstantin Varlamov   std::ranges::equal_to eq;
52964aeb71SKonstantin Varlamov   std::ranges::less less;
53964aeb71SKonstantin Varlamov   auto sum = [](int lhs, A rhs) { return lhs + rhs.x; };
54964aeb71SKonstantin Varlamov   auto proj1 = [](int x) { return x * -1; };
55964aeb71SKonstantin Varlamov   auto proj2 = [](A a) { return a.x * -1; };
56964aeb71SKonstantin Varlamov 
57*0218ea4aSZijun Zhao #if TEST_STD_VER >= 23
58*0218ea4aSZijun Zhao   test(std::ranges::ends_with, in, in2, eq, proj1, proj2);
59*0218ea4aSZijun Zhao #endif
60964aeb71SKonstantin Varlamov   test(std::ranges::equal, in, in2, eq, proj1, proj2);
61964aeb71SKonstantin Varlamov   test(std::ranges::lexicographical_compare, in, in2, eq, proj1, proj2);
624038c859SNikolas Klauser   test(std::ranges::is_permutation, in, in2, eq, proj1, proj2);
63964aeb71SKonstantin Varlamov   test(std::ranges::includes, in, in2, less, proj1, proj2);
64964aeb71SKonstantin Varlamov   test(std::ranges::find_first_of, in, in2, eq, proj1, proj2);
65964aeb71SKonstantin Varlamov   test(std::ranges::mismatch, in, in2, eq, proj1, proj2);
66964aeb71SKonstantin Varlamov   test(std::ranges::search, in, in2, eq, proj1, proj2);
67964aeb71SKonstantin Varlamov   test(std::ranges::find_end, in, in2, eq, proj1, proj2);
68964aeb71SKonstantin Varlamov   test(std::ranges::transform, in, in2, out, sum, proj1, proj2);
69964aeb71SKonstantin Varlamov   test(std::ranges::transform, in, in2, out2, sum, proj1, proj2);
70db7d7959SKonstantin Varlamov   test(std::ranges::partial_sort_copy, in, in2, less, proj1, proj2);
71964aeb71SKonstantin Varlamov   test(std::ranges::merge, in, in2, out, less, proj1, proj2);
72964aeb71SKonstantin Varlamov   test(std::ranges::merge, in, in2, out2, less, proj1, proj2);
73964aeb71SKonstantin Varlamov   test(std::ranges::set_intersection, in, in2, out, less, proj1, proj2);
74964aeb71SKonstantin Varlamov   test(std::ranges::set_intersection, in, in2, out2, less, proj1, proj2);
75964aeb71SKonstantin Varlamov   test(std::ranges::set_difference, in, in2, out, less, proj1, proj2);
76964aeb71SKonstantin Varlamov   test(std::ranges::set_difference, in, in2, out2, less, proj1, proj2);
77964aeb71SKonstantin Varlamov   test(std::ranges::set_symmetric_difference, in, in2, out, less, proj1, proj2);
78964aeb71SKonstantin Varlamov   test(std::ranges::set_symmetric_difference, in, in2, out2, less, proj1, proj2);
79964aeb71SKonstantin Varlamov   test(std::ranges::set_union, in, in2, out, less, proj1, proj2);
80964aeb71SKonstantin Varlamov   test(std::ranges::set_union, in, in2, out2, less, proj1, proj2);
8120517557Szijunzhao #if TEST_STD_VER > 20
8220517557Szijunzhao   test(std::ranges::starts_with, in, in2, eq, proj1, proj2);
8320517557Szijunzhao #endif
84964aeb71SKonstantin Varlamov 
85964aeb71SKonstantin Varlamov   return true;
86964aeb71SKonstantin Varlamov }
87964aeb71SKonstantin Varlamov 
main(int,char **)88964aeb71SKonstantin Varlamov int main(int, char**) {
89964aeb71SKonstantin Varlamov   test_all();
90964aeb71SKonstantin Varlamov   static_assert(test_all());
91964aeb71SKonstantin Varlamov 
92964aeb71SKonstantin Varlamov   return 0;
93964aeb71SKonstantin Varlamov }
94