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 Iter1, InputIterator Iter2, CopyConstructible Compare> |
12 | // requires Predicate<Compare, Iter1::value_type, Iter2::value_type> |
13 | // && Predicate<Compare, Iter2::value_type, Iter1::value_type> |
14 | // constexpr bool // constexpr after C++17 |
15 | // lexicographical_compare(Iter1 first1, Iter1 last1, |
16 | // Iter2 first2, Iter2 last2, Compare comp); |
17 | |
18 | #include <algorithm> |
19 | #include <functional> |
20 | #include <cassert> |
21 | |
22 | #include "test_macros.h" |
23 | #include "test_iterators.h" |
24 | |
25 | #if TEST_STD_VER > 17 |
26 | TEST_CONSTEXPR bool test_constexpr() { |
27 | int ia[] = {1, 2, 3}; |
28 | int ib[] = {1, 3, 5, 2, 4, 6}; |
29 | |
30 | std::greater<int> pred{}; |
31 | return !std::lexicographical_compare(std::begin(ia), std::end(ia), std::begin(ib), std::end(ib), pred) |
32 | && std::lexicographical_compare(std::begin(ib), std::end(ib), std::begin(ia), std::end(ia), pred) |
33 | ; |
34 | } |
35 | #endif |
36 | |
37 | template <class Iter1, class Iter2> |
38 | void |
39 | test() |
40 | { |
41 | int ia[] = {1, 2, 3, 4}; |
42 | const unsigned sa = sizeof(ia)/sizeof(ia[0]); |
43 | int ib[] = {1, 2, 3}; |
44 | typedef std::greater<int> C; |
45 | C c; |
46 | assert(!std::lexicographical_compare(Iter1(ia), Iter1(ia+sa), Iter2(ib), Iter2(ib+2), c)); |
47 | assert( std::lexicographical_compare(Iter1(ib), Iter1(ib+2), Iter2(ia), Iter2(ia+sa), c)); |
48 | assert(!std::lexicographical_compare(Iter1(ia), Iter1(ia+sa), Iter2(ib), Iter2(ib+3), c)); |
49 | assert( std::lexicographical_compare(Iter1(ib), Iter1(ib+3), Iter2(ia), Iter2(ia+sa), c)); |
50 | assert(!std::lexicographical_compare(Iter1(ia), Iter1(ia+sa), Iter2(ib+1), Iter2(ib+3), c)); |
51 | assert( std::lexicographical_compare(Iter1(ib+1), Iter1(ib+3), Iter2(ia), Iter2(ia+sa), c)); |
52 | } |
53 | |
54 | int main(int, char**) |
55 | { |
56 | test<cpp17_input_iterator<const int*>, cpp17_input_iterator<const int*> >(); |
57 | test<cpp17_input_iterator<const int*>, forward_iterator<const int*> >(); |
58 | test<cpp17_input_iterator<const int*>, bidirectional_iterator<const int*> >(); |
59 | test<cpp17_input_iterator<const int*>, random_access_iterator<const int*> >(); |
60 | test<cpp17_input_iterator<const int*>, const int*>(); |
61 | |
62 | test<forward_iterator<const int*>, cpp17_input_iterator<const int*> >(); |
63 | test<forward_iterator<const int*>, forward_iterator<const int*> >(); |
64 | test<forward_iterator<const int*>, bidirectional_iterator<const int*> >(); |
65 | test<forward_iterator<const int*>, random_access_iterator<const int*> >(); |
66 | test<forward_iterator<const int*>, const int*>(); |
67 | |
68 | test<bidirectional_iterator<const int*>, cpp17_input_iterator<const int*> >(); |
69 | test<bidirectional_iterator<const int*>, forward_iterator<const int*> >(); |
70 | test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >(); |
71 | test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >(); |
72 | test<bidirectional_iterator<const int*>, const int*>(); |
73 | |
74 | test<random_access_iterator<const int*>, cpp17_input_iterator<const int*> >(); |
75 | test<random_access_iterator<const int*>, forward_iterator<const int*> >(); |
76 | test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >(); |
77 | test<random_access_iterator<const int*>, random_access_iterator<const int*> >(); |
78 | test<random_access_iterator<const int*>, const int*>(); |
79 | |
80 | test<const int*, cpp17_input_iterator<const int*> >(); |
81 | test<const int*, forward_iterator<const int*> >(); |
82 | test<const int*, bidirectional_iterator<const int*> >(); |
83 | test<const int*, random_access_iterator<const int*> >(); |
84 | test<const int*, const int*>(); |
85 | |
86 | #if TEST_STD_VER > 17 |
87 | static_assert(test_constexpr()); |
88 | #endif |
89 | |
90 | return 0; |
91 | } |
92 | |