1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: libcpp-no-concepts
11 
12 // template<class I1, class I2, class Out,
13 //     class R = ranges::less, class P1 = identity, class P2 = identity>
14 //   concept mergeable = see below;                           // since C++20
15 
16 #include <iterator>
17 
18 #include <functional>
19 
20 #include "test_iterators.h"
21 #include "test_macros.h"
22 
23 using CompDefault = std::ranges::less;
24 using CompInt = bool(*)(int, int);
25 using ProjDefault = std::identity;
26 
27 using Input = cpp20_input_iterator<int*>;
28 static_assert( std::input_iterator<Input>);
29 using InputLong = cpp20_input_iterator<long*>;
30 static_assert( std::input_iterator<InputLong>);
31 
32 using Output = cpp17_output_iterator<int*>;
33 static_assert( std::weakly_incrementable<Output>);
34 
35 static_assert( std::indirectly_copyable<Input, Output>);
36 static_assert( std::indirectly_copyable<InputLong, Output>);
37 static_assert( std::indirect_strict_weak_order<CompDefault, Input, Input>);
38 static_assert( std::indirect_strict_weak_order<CompInt, Input, Input>);
39 static_assert( std::indirect_strict_weak_order<CompDefault, Input, InputLong>);
40 static_assert( std::indirect_strict_weak_order<CompInt, Input, InputLong>);
41 
42 // All requirements satisfied.
43 static_assert( std::mergeable<Input, Input, Output>);
44 static_assert( std::mergeable<Input, Input, Output, CompInt>);
45 static_assert( std::mergeable<Input, Input, Output, CompInt, ProjDefault>);
46 
47 // Non-default projections.
48 struct Foo {};
49 using ProjFooToInt = int(*)(Foo);
50 using ProjFooToLong = long(*)(Foo);
51 static_assert( std::indirect_strict_weak_order<CompDefault,
52     std::projected<Foo*, ProjFooToInt>, std::projected<Foo*, ProjFooToLong>>);
53 static_assert( std::mergeable<Foo*, Foo*, Foo*, CompDefault, ProjFooToInt, ProjFooToLong>);
54 static_assert( std::indirect_strict_weak_order<CompInt,
55     std::projected<Foo*, ProjFooToInt>, std::projected<Foo*, ProjFooToLong>>);
56 static_assert( std::mergeable<Foo*, Foo*, Foo*, CompInt, ProjFooToInt, ProjFooToLong>);
57 
58 // I1 or I2 is not an input iterator.
59 static_assert(!std::input_iterator<Output>);
60 static_assert(!std::mergeable<Output, Input, Output>);
61 static_assert(!std::mergeable<Input, Output, Output>);
62 
63 // O is not weakly incrementable.
64 struct NotWeaklyIncrementable {
65   int& operator*() const;
66 };
67 
68 static_assert(!std::weakly_incrementable<NotWeaklyIncrementable>);
69 static_assert( std::indirectly_copyable<Input, NotWeaklyIncrementable>);
70 static_assert( std::indirect_strict_weak_order<CompDefault, Input, Input>);
71 static_assert(!std::mergeable<Input, Input, NotWeaklyIncrementable>);
72 
73 // I1 or I2 is not indirectly copyable into O.
74 struct AssignableOnlyFromInt {
75   AssignableOnlyFromInt& operator=(int);
76   template <class T>
77   AssignableOnlyFromInt& operator=(T) = delete;
78 };
79 using OutputOnlyInt = cpp17_output_iterator<AssignableOnlyFromInt*>;
80 static_assert( std::weakly_incrementable<OutputOnlyInt>);
81 
82 static_assert( std::indirectly_copyable<Input, OutputOnlyInt>);
83 static_assert(!std::indirectly_copyable<InputLong, OutputOnlyInt>);
84 static_assert( std::indirect_strict_weak_order<CompDefault, Input, InputLong>);
85 static_assert( std::mergeable<Input, Input, OutputOnlyInt>);
86 static_assert(!std::mergeable<Input, InputLong, OutputOnlyInt>);
87 static_assert(!std::mergeable<InputLong, Input, OutputOnlyInt>);
88 
89 // No indirect strict weak order between I1 and I2 (bad comparison functor).
90 using GoodComp = bool(*)(int, int);
91 static_assert( std::indirect_strict_weak_order<GoodComp, Input, Input>);
92 static_assert( std::mergeable<Input, Input, Output, GoodComp>);
93 using BadComp = bool(*)(int*, int*);
94 static_assert(!std::indirect_strict_weak_order<BadComp, Input, Input>);
95 static_assert(!std::mergeable<Input, Input, Output, BadComp>);
96 
97 // No indirect strict weak order between I1 and I2 (bad projection).
98 using ToInt = int(*)(int);
99 using ToPtr = int*(*)(int);
100 static_assert( std::mergeable<Input, Input, Output, GoodComp, std::identity, std::identity>);
101 static_assert( std::mergeable<Input, Input, Output, GoodComp, ToInt, ToInt>);
102 static_assert(!std::mergeable<Input, Input, Output, GoodComp, ToPtr, ToInt>);
103 static_assert(!std::mergeable<Input, Input, Output, GoodComp, ToInt, ToPtr>);
104 static_assert(!std::mergeable<Input, Input, Output, bool(*)(int*, int), ToPtr, ToInt>);
105 static_assert(!std::mergeable<Input, Input, Output, bool(*)(int, int*), ToInt, ToPtr>);
106 
107 // A projection that only supports non-const references and has a non-const `operator()` still has to work.
108 struct ProjectionOnlyMutable {
109   int operator()(int&);
110   int operator()(int&&) const = delete;
111 };
112 static_assert( std::mergeable<Input, Input, Output, CompDefault, ProjectionOnlyMutable, ProjectionOnlyMutable>);
113 
114 // The output is weakly incrementable but not an output iterator.
115 struct WeaklyIncrementable {
116   using value_type = int;
117   using difference_type = int;
118 
119   int& operator*() const;
120   WeaklyIncrementable& operator++();
121   // `output_iterator` requires `i++` to return an iterator,
122   // while `weakly_incrementable` requires only that `i++` be well-formed.
123   void operator++(int);
124 };
125 static_assert( std::weakly_incrementable<WeaklyIncrementable>);
126 static_assert( std::indirectly_copyable<int*, WeaklyIncrementable>);
127 static_assert(!std::output_iterator<WeaklyIncrementable, int>);
128 static_assert( std::mergeable<Input, Input, WeaklyIncrementable>);
129