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 | // <algorithm> |
10 | |
11 | // template<InputIterator InIter1, InputIterator InIter2, typename OutIter> |
12 | // requires OutputIterator<OutIter, InIter1::reference> |
13 | // && OutputIterator<OutIter, InIter2::reference> |
14 | // && HasLess<InIter2::value_type, InIter1::value_type> |
15 | // && HasLess<InIter1::value_type, InIter2::value_type> |
16 | // constexpr OutIter // constexpr after C++17 |
17 | // set_difference(InIter1 first1, InIter1 last1, InIter2 first2, InIter2 last2, |
18 | // OutIter result); |
19 | |
20 | #include <algorithm> |
21 | #include <cassert> |
22 | |
23 | #include "test_macros.h" |
24 | #include "test_iterators.h" |
25 | #include "../../sortable_helpers.h" |
26 | |
27 | template<class T, class Iter1, class Iter2, class OutIter> |
28 | TEST_CONSTEXPR_CXX20 void test4() |
29 | { |
30 | const T a[] = {11, 33, 31, 41}; |
31 | const T b[] = {22, 32, 43, 42, 52}; |
32 | { |
33 | T result[20] = {}; |
34 | T expected[] = {11, 31}; |
35 | OutIter end = std::set_difference(Iter1(a), Iter1(a+4), Iter2(b), Iter2(b+5), OutIter(result)); |
36 | assert(std::lexicographical_compare(result, base(end), expected, expected+2, T::less) == 0); |
37 | for (const T *it = base(end); it != result+20; ++it) { |
38 | assert(it->value == 0); |
39 | } |
40 | } |
41 | { |
42 | T result[20] = {}; |
43 | T expected[] = {22, 42, 52}; |
44 | OutIter end = std::set_difference(Iter1(b), Iter1(b+5), Iter2(a), Iter2(a+4), OutIter(result)); |
45 | assert(std::lexicographical_compare(result, base(end), expected, expected+3, T::less) == 0); |
46 | for (const T *it = base(end); it != result+20; ++it) { |
47 | assert(it->value == 0); |
48 | } |
49 | } |
50 | } |
51 | |
52 | template<class T, class Iter1, class Iter2> |
53 | TEST_CONSTEXPR_CXX20 void test3() |
54 | { |
55 | test4<T, Iter1, Iter2, cpp17_output_iterator<T*> >(); |
56 | test4<T, Iter1, Iter2, forward_iterator<T*> >(); |
57 | test4<T, Iter1, Iter2, bidirectional_iterator<T*> >(); |
58 | test4<T, Iter1, Iter2, random_access_iterator<T*> >(); |
59 | test4<T, Iter1, Iter2, T*>(); |
60 | } |
61 | |
62 | template<class T, class Iter1> |
63 | TEST_CONSTEXPR_CXX20 void test2() |
64 | { |
65 | test3<T, Iter1, cpp17_input_iterator<const T*> >(); |
66 | test3<T, Iter1, forward_iterator<const T*> >(); |
67 | test3<T, Iter1, bidirectional_iterator<const T*> >(); |
68 | test3<T, Iter1, random_access_iterator<const T*> >(); |
69 | test3<T, Iter1, const T*>(); |
70 | } |
71 | |
72 | template<class T> |
73 | TEST_CONSTEXPR_CXX20 void test1() |
74 | { |
75 | test2<T, cpp17_input_iterator<const T*> >(); |
76 | test2<T, forward_iterator<const T*> >(); |
77 | test2<T, bidirectional_iterator<const T*> >(); |
78 | test2<T, random_access_iterator<const T*> >(); |
79 | test2<T, const T*>(); |
80 | } |
81 | |
82 | TEST_CONSTEXPR_CXX20 bool test() |
83 | { |
84 | test1<TrivialSortable>(); |
85 | test1<NonTrivialSortable>(); |
86 | return true; |
87 | } |
88 | |
89 | int main(int, char**) |
90 | { |
91 | test(); |
92 | #if TEST_STD_VER > 17 |
93 | static_assert(test()); |
94 | #endif |
95 | |
96 | return 0; |
97 | } |
98 | |