| 1 | // Copyright (C) 2019-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 | |
| 20 | #include <functional> |
| 21 | #include <testsuite_hooks.h> |
| 22 | |
| 23 | // C++20 [range.cmp] |
| 24 | |
| 25 | using F = std::ranges::less_equal; |
| 26 | static_assert( std::is_default_constructible_v<F> ); |
| 27 | static_assert( std::is_copy_constructible_v<F> ); |
| 28 | static_assert( std::is_move_constructible_v<F> ); |
| 29 | static_assert( std::is_copy_assignable_v<F> ); |
| 30 | static_assert( std::is_move_assignable_v<F> ); |
| 31 | |
| 32 | static_assert( ! std::is_invocable_v<F> ); |
| 33 | static_assert( ! std::is_invocable_v<F, int&> ); |
| 34 | static_assert( ! std::is_invocable_v<F, int, void> ); |
| 35 | static_assert( ! std::is_invocable_v<F, int, void*> ); |
| 36 | static_assert( std::is_nothrow_invocable_r_v<bool, F&, int&, int> ); |
| 37 | static_assert( std::is_nothrow_invocable_r_v<bool, F, const long&, char> ); |
| 38 | static_assert( std::is_nothrow_invocable_r_v<bool, const F&, short, int&> ); |
| 39 | static_assert( std::is_nothrow_invocable_r_v<bool, const F, const char, char> ); |
| 40 | |
| 41 | using T = F::is_transparent; // required typedef |
| 42 | |
| 43 | static_assert( std::ranges::less_equal{}(99, 99.0) ); |
| 44 | static_assert( ! std::ranges::less_equal{}(99.01, 99) ); |
| 45 | static_assert( std::ranges::less_equal{}(99, 140L) ); |
| 46 | |
| 47 | void |
| 48 | test01() |
| 49 | { |
| 50 | F f; |
| 51 | int a[2]{}; |
| 52 | VERIFY( f(&a, (void*)&a[0]) ); |
| 53 | VERIFY( ! f((void*)&a[1], &a) ); |
| 54 | VERIFY( ! f(&a + 1, (void*)(a + 1)) ); |
| 55 | VERIFY( f(&a, (void*)(a + 1)) ); |
| 56 | } |
| 57 | |
| 58 | struct X { }; |
| 59 | int operator==(X, X) { return 2; } |
| 60 | int operator!=(X, X) { return 0; } |
| 61 | int operator<(X, X) noexcept { return 0; } |
| 62 | int operator>(X, X) { return 0; } |
| 63 | int operator<=(X, X) { return 3; } |
| 64 | int operator>=(X, X) { return 4; } |
| 65 | |
| 66 | static_assert( std::is_nothrow_invocable_r_v<bool, F&, X, X> ); |
| 67 | |
| 68 | void |
| 69 | test02() |
| 70 | { |
| 71 | X x; |
| 72 | F f; |
| 73 | VERIFY( f(x, x) ); |
| 74 | } |
| 75 | |
| 76 | int |
| 77 | main() |
| 78 | { |
| 79 | test01(); |
| 80 | test02(); |
| 81 | } |