| 1 | // Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++20 } } |
| 19 | // { dg-require-effective-target hosted } |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <functional> |
| 23 | #include <testsuite_hooks.h> |
| 24 | #include <testsuite_iterators.h> |
| 25 | |
| 26 | using __gnu_test::test_container; |
| 27 | using __gnu_test::test_range; |
| 28 | using __gnu_test::input_iterator_wrapper; |
| 29 | using __gnu_test::forward_iterator_wrapper; |
| 30 | |
| 31 | namespace ranges = std::ranges; |
| 32 | |
| 33 | void |
| 34 | test01() |
| 35 | { |
| 36 | int x[] = {1, 2, 3, 4, 5}; |
| 37 | char y[] = {1, 2, 3, 5}; |
| 38 | long z[] = {1, 2, 3, 4, 5, 6}; |
| 39 | |
| 40 | { |
| 41 | test_range<int, input_iterator_wrapper> rx(x); |
| 42 | test_range<char, input_iterator_wrapper> ry(y); |
| 43 | test_range<long, input_iterator_wrapper> rz(z); |
| 44 | |
| 45 | VERIFY( ranges::lexicographical_compare(rx, ry) ); |
| 46 | rx.bounds.first = x; |
| 47 | ry.bounds.first = y; |
| 48 | VERIFY( !ranges::lexicographical_compare(ry, rx) ); |
| 49 | } |
| 50 | |
| 51 | test_range<int, forward_iterator_wrapper> rx(x); |
| 52 | test_range<char, forward_iterator_wrapper> ry(y); |
| 53 | test_range<long, forward_iterator_wrapper> rz(z); |
| 54 | |
| 55 | VERIFY( ranges::lexicographical_compare(rx, rz) ); |
| 56 | VERIFY( !ranges::lexicographical_compare(rz, rx) ); |
| 57 | |
| 58 | VERIFY( !ranges::lexicographical_compare(rx, rx) ); |
| 59 | VERIFY( ranges::lexicographical_compare(rx, rx, {}, std::negate<>{}) ); |
| 60 | VERIFY( ranges::lexicographical_compare(rx, rx, std::greater{}, |
| 61 | {}, std::negate<>{}) ); |
| 62 | |
| 63 | VERIFY( !ranges::lexicographical_compare(rx, ry, {}, |
| 64 | std::negate<>{}, |
| 65 | std::negate<>{}) ); |
| 66 | VERIFY( ranges::lexicographical_compare(ry, rx, {}, |
| 67 | std::negate<>{}, |
| 68 | std::negate<>{}) ); |
| 69 | |
| 70 | VERIFY( ranges::lexicographical_compare(rx, rz, ranges::greater{}) ); |
| 71 | VERIFY( !ranges::lexicographical_compare(rz, rx, ranges::greater{}) ); |
| 72 | |
| 73 | VERIFY( ranges::lexicographical_compare(rx, ry, ranges::greater{}, |
| 74 | std::negate<>{}, |
| 75 | std::negate<>{}) ); |
| 76 | VERIFY( !ranges::lexicographical_compare(ry, rx, ranges::greater{}, |
| 77 | std::negate<>{}, |
| 78 | std::negate<>{}) ); |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | test02() |
| 83 | { |
| 84 | int x[] = {1, 2, 3, 4, 5}; |
| 85 | int y[] = {1, 2, 3, 5}; |
| 86 | int z[] = {1, 2, 3, 4, 5, 6}; |
| 87 | |
| 88 | VERIFY( ranges::lexicographical_compare(x, y) ); |
| 89 | VERIFY( !ranges::lexicographical_compare(y, x) ); |
| 90 | |
| 91 | VERIFY( ranges::lexicographical_compare(x, z) ); |
| 92 | VERIFY( !ranges::lexicographical_compare(z, x) ); |
| 93 | |
| 94 | VERIFY( !ranges::lexicographical_compare(x, x) ); |
| 95 | |
| 96 | VERIFY( !ranges::lexicographical_compare(x, y, {}, |
| 97 | std::negate<>{}, |
| 98 | std::negate<>{}) ); |
| 99 | VERIFY( ranges::lexicographical_compare(y, x, {}, |
| 100 | std::negate<>{}, |
| 101 | std::negate<>{}) ); |
| 102 | |
| 103 | VERIFY( ranges::lexicographical_compare(x, z, ranges::greater{}) ); |
| 104 | VERIFY( !ranges::lexicographical_compare(z, x, ranges::greater{}) ); |
| 105 | |
| 106 | VERIFY( ranges::lexicographical_compare(x, y, ranges::greater{}, |
| 107 | std::negate<>{}, |
| 108 | std::negate<>{}) ); |
| 109 | VERIFY( !ranges::lexicographical_compare(y, x, ranges::greater{}, |
| 110 | std::negate<>{}, |
| 111 | std::negate<>{}) ); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | test03() |
| 116 | { |
| 117 | int x[] = {1, 2, 3, 4, 5}; |
| 118 | int y[] = {1, 2, 5, 3}; |
| 119 | int z[] = {1, 2, 3, 5}; |
| 120 | |
| 121 | do |
| 122 | { |
| 123 | VERIFY( ranges::lexicographical_compare(x, y) ); |
| 124 | VERIFY( !ranges::lexicographical_compare(x, y, ranges::greater{}) ); |
| 125 | VERIFY( !ranges::lexicographical_compare(y, x) ); |
| 126 | VERIFY( ranges::lexicographical_compare(y, x, ranges::greater{}) ); |
| 127 | |
| 128 | test_container<int, forward_iterator_wrapper> cy(y); |
| 129 | VERIFY( ranges::lexicographical_compare(x, cy) ); |
| 130 | VERIFY( !ranges::lexicographical_compare(x, cy, ranges::greater{}) ); |
| 131 | VERIFY( !ranges::lexicographical_compare(cy, x) ); |
| 132 | VERIFY( ranges::lexicographical_compare(cy, x, ranges::greater{}) ); |
| 133 | |
| 134 | test_container<int, forward_iterator_wrapper> cz(z); |
| 135 | VERIFY( ranges::lexicographical_compare(cz.begin(), cz.end(), |
| 136 | cy.begin(), cy.end()) ); |
| 137 | VERIFY( !ranges::lexicographical_compare(cy.begin(), cy.end(), |
| 138 | cz.begin(), cz.end()) ); |
| 139 | |
| 140 | std::vector<int> vx(x, x+5), vy(y, y+4); |
| 141 | VERIFY( ranges::lexicographical_compare(vx, vy) ); |
| 142 | VERIFY( !ranges::lexicographical_compare(vx, vy, ranges::greater{}) ); |
| 143 | VERIFY( !ranges::lexicographical_compare(vy, vx) ); |
| 144 | VERIFY( ranges::lexicographical_compare(vy, vx, ranges::greater{}) ); |
| 145 | } while (ranges::next_permutation(y).found); |
| 146 | } |
| 147 | |
| 148 | constexpr bool |
| 149 | test04() |
| 150 | { |
| 151 | int x[] = {1}; |
| 152 | int y[] = {1}; |
| 153 | return (ranges::lexicographical_compare((int[]){1,2,3,5}, |
| 154 | (int[]){1,2,4}) |
| 155 | && !ranges::lexicographical_compare(x, x, y, y)); |
| 156 | } |
| 157 | |
| 158 | int |
| 159 | main() |
| 160 | { |
| 161 | test01(); |
| 162 | test02(); |
| 163 | test03(); |
| 164 | static_assert(test04()); |
| 165 | } |