]>
Commit | Line | Data |
---|---|---|
47df7e19 | 1 | // { dg-do compile { target c++17 } } |
435e56fb | 2 | |
83ffe9cd | 3 | // Copyright (C) 2013-2023 Free Software Foundation, Inc. |
435e56fb VV |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
8 | // Free Software Foundation; either version 3, or (at your option) | |
9 | // any later version. | |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
c7cbb4da | 16 | // You should have received a copy of the GNU General Public License along |
435e56fb VV |
17 | // with this library; see the file COPYING3. If not see |
18 | // <http://www.gnu.org/licenses/>. | |
19 | ||
20 | #include <optional> | |
21 | #include <testsuite_hooks.h> | |
22 | ||
23 | namespace ns | |
24 | { | |
25 | struct value_type | |
26 | { | |
27 | int i; | |
28 | const char* s; | |
29 | }; | |
30 | ||
31 | constexpr bool | |
32 | strcmp(const char* lhs, const char* rhs) | |
33 | { | |
34 | return *lhs == *rhs && (!*lhs || strcmp(lhs + 1, rhs + 1)); | |
35 | } | |
36 | ||
37 | constexpr bool | |
38 | strrel(const char* lhs, const char* rhs) | |
39 | { | |
40 | return (*rhs && (!*lhs || (*lhs < *rhs))) | |
41 | || ((*lhs && *rhs && !(*rhs < *lhs)) && strrel(lhs + 1, rhs + 1)); | |
42 | } | |
43 | ||
44 | constexpr bool | |
45 | operator==(value_type const& lhs, value_type const& rhs) | |
46 | { return (lhs.i == rhs.i) && strcmp(lhs.s, rhs.s); } | |
47 | ||
48 | constexpr bool | |
49 | operator!=(value_type const& lhs, value_type const& rhs) | |
50 | { return !(lhs == rhs); } | |
51 | ||
52 | constexpr bool | |
53 | operator<(value_type const& lhs, value_type const& rhs) | |
54 | { return (lhs.i < rhs.i) || (!(rhs.i < lhs.i) && strrel(lhs.s, rhs.s)); } | |
55 | ||
86c0ec1d VV |
56 | constexpr bool |
57 | operator>(value_type const& lhs, value_type const& rhs) | |
58 | { return rhs < lhs; } | |
59 | ||
60 | constexpr bool | |
61 | operator<=(value_type const& lhs, value_type const& rhs) | |
62 | { return lhs < rhs || lhs == rhs; } | |
63 | ||
64 | constexpr bool | |
65 | operator>=(value_type const& lhs, value_type const& rhs) | |
66 | { return lhs > rhs || lhs == rhs; } | |
67 | ||
435e56fb VV |
68 | } // namespace ns |
69 | ||
70 | int main() | |
71 | { | |
72 | using ns::value_type; | |
73 | using O = std::optional<value_type>; | |
74 | ||
75 | constexpr value_type reference { 42, "forty-two" }; | |
76 | ||
77 | { | |
78 | constexpr O o; | |
79 | static_assert( o < reference, "" ); | |
80 | static_assert( !(reference < o), "" ); | |
81 | static_assert( !(o > reference), "" ); | |
82 | static_assert( reference > o, "" ); | |
83 | static_assert( o <= reference, "" ); | |
84 | static_assert( !(reference <= o), "" ); | |
85 | static_assert( !(o >= reference), "" ); | |
86 | static_assert( reference >= o, "" ); | |
87 | } | |
88 | ||
89 | { | |
90 | constexpr O o { value_type { 11, "eleventy" } }; | |
91 | static_assert( o < reference, "" ); | |
92 | static_assert( !(reference < o), "" ); | |
93 | static_assert( !(o > reference), "" ); | |
94 | static_assert( reference > o, "" ); | |
95 | static_assert( o <= reference, "" ); | |
96 | static_assert( !(reference <= o), "" ); | |
97 | static_assert( !(o >= reference), "" ); | |
98 | static_assert( reference >= o, "" ); | |
99 | } | |
100 | ||
101 | { | |
102 | constexpr O o { value_type { 42, "forty-two" } }; | |
103 | static_assert( !(o < reference), "" ); | |
104 | static_assert( !(reference < o), "" ); | |
105 | static_assert( !(o > reference), "" ); | |
106 | static_assert( !(reference > o), "" ); | |
107 | static_assert( o <= reference, "" ); | |
108 | static_assert( reference <= o, "" ); | |
109 | static_assert( o >= reference, "" ); | |
110 | static_assert( reference >= o, "" ); | |
111 | } | |
112 | } |