]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/18_support/byte/ops.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 18_support / byte / ops.cc
1 // Copyright (C) 2017-2024 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 compile { target c++17 } }
19
20 #include <cstddef>
21
22 constexpr bool is_byte(std::byte) { return true; }
23 template<typename T> constexpr bool is_byte(T) { return false; }
24
25 template<typename T>
26 constexpr bool test_lshift_assign(unsigned char c, T t)
27 {
28 std::byte b{c};
29 b <<= t;
30 return b == std::byte(c << t);
31 }
32
33 static_assert( test_lshift_assign(0, 1) );
34 static_assert( test_lshift_assign(0, 1u) );
35 static_assert( test_lshift_assign(0, 1ll) );
36 static_assert( test_lshift_assign(4, 1) );
37 static_assert( test_lshift_assign(9, 2u) );
38 static_assert( test_lshift_assign(16, 3ll) );
39 static_assert( test_lshift_assign(127, 1) );
40 static_assert( test_lshift_assign(255, 2u) );
41 static_assert( test_lshift_assign(63, 3ll) );
42
43 template<typename T>
44 constexpr bool test_lshift(unsigned char c, T t)
45 {
46 const std::byte b{c};
47 const std::byte b2 = b << t;
48 return b2 == std::byte(c << t);
49 }
50
51 static_assert( test_lshift(0, 1) );
52 static_assert( test_lshift(0, 1u) );
53 static_assert( test_lshift(0, 1ll) );
54 static_assert( test_lshift(4, 1) );
55 static_assert( test_lshift(9, 2u) );
56 static_assert( test_lshift(16, 3ll) );
57 static_assert( test_lshift(127, 1) );
58 static_assert( test_lshift(255, 2u) );
59 static_assert( test_lshift(63, 3ll) );
60
61 template<typename T>
62 constexpr bool test_rshift_assign(unsigned char c, T t)
63 {
64 std::byte b{c};
65 b >>= t;
66 return b == std::byte(c >> t);
67 }
68
69 static_assert( test_rshift_assign(0, 1) );
70 static_assert( test_rshift_assign(0, 1u) );
71 static_assert( test_rshift_assign(0, 1ll) );
72 static_assert( test_rshift_assign(4, 1) );
73 static_assert( test_rshift_assign(9, 2u) );
74 static_assert( test_rshift_assign(16, 3ll) );
75 static_assert( test_rshift_assign(127, 1) );
76 static_assert( test_rshift_assign(255, 2u) );
77 static_assert( test_rshift_assign(63, 3ll) );
78
79 template<typename T>
80 constexpr bool test_rshift(unsigned char c, T t)
81 {
82 const std::byte b{c};
83 const std::byte b2 = b >> t;
84 return b2 == std::byte(c >> t);
85 }
86
87 static_assert( test_rshift(0, 1) );
88 static_assert( test_rshift(0, 1u) );
89 static_assert( test_rshift(0, 1ll) );
90 static_assert( test_rshift(4, 1) );
91 static_assert( test_rshift(9, 2u) );
92 static_assert( test_rshift(16, 3ll) );
93 static_assert( test_rshift(127, 1) );
94 static_assert( test_rshift(255, 2u) );
95 static_assert( test_rshift(63, 3ll) );
96
97 constexpr bool test_or_assign(unsigned char l, unsigned char r)
98 {
99 std::byte b{l};
100 b |= std::byte{r};
101 return b == std::byte(l | r);
102 }
103
104 static_assert( test_or_assign(0, 1) );
105 static_assert( test_or_assign(4, 1) );
106 static_assert( test_or_assign(9, 2) );
107 static_assert( test_or_assign(16, 3) );
108 static_assert( test_or_assign(63, 3) );
109 static_assert( test_or_assign(127, 1) );
110 static_assert( test_or_assign(255, 2) );
111
112 constexpr bool test_or(unsigned char l, unsigned char r)
113 {
114 const std::byte b1{l};
115 const std::byte b2{r};
116 return (b1 | b2) == std::byte(l | r);
117 }
118
119 static_assert( test_or(0, 1) );
120 static_assert( test_or(0, 1u) );
121 static_assert( test_or(0, 1ll) );
122 static_assert( test_or(4, 1) );
123 static_assert( test_or(9, 2u) );
124 static_assert( test_or(16, 3ll) );
125 static_assert( test_or(127, 1) );
126 static_assert( test_or(255, 2u) );
127 static_assert( test_or(63, 3ll) );
128
129 constexpr bool test_and_assign(unsigned char l, unsigned char r)
130 {
131 std::byte b{l};
132 b &= std::byte{r};
133 return b == std::byte(l & r);
134 }
135
136 static_assert( test_and_assign(0, 1) );
137 static_assert( test_and_assign(0, 1u) );
138 static_assert( test_and_assign(0, 1ll) );
139 static_assert( test_and_assign(4, 1) );
140 static_assert( test_and_assign(9, 2u) );
141 static_assert( test_and_assign(16, 3ll) );
142 static_assert( test_and_assign(127, 1) );
143 static_assert( test_and_assign(255, 2u) );
144 static_assert( test_and_assign(63, 3ll) );
145
146 constexpr bool test_and(unsigned char l, unsigned char r)
147 {
148 const std::byte b1{l};
149 const std::byte b2{r};
150 return (b1 & b2) == std::byte(l & r);
151 }
152
153 static_assert( test_and(0, 1) );
154 static_assert( test_and(0, 1u) );
155 static_assert( test_and(0, 1ll) );
156 static_assert( test_and(4, 1) );
157 static_assert( test_and(9, 2u) );
158 static_assert( test_and(16, 3ll) );
159 static_assert( test_and(127, 1) );
160 static_assert( test_and(255, 2u) );
161 static_assert( test_and(63, 3ll) );
162
163 constexpr bool test_xor_assign(unsigned char l, unsigned char r)
164 {
165 std::byte b{l};
166 b ^= std::byte{r};
167 return b == std::byte(l ^ r);
168 }
169
170 static_assert( test_xor_assign(0, 1) );
171 static_assert( test_xor_assign(0, 1u) );
172 static_assert( test_xor_assign(0, 1ll) );
173 static_assert( test_xor_assign(4, 1) );
174 static_assert( test_xor_assign(9, 2u) );
175 static_assert( test_xor_assign(16, 3ll) );
176 static_assert( test_xor_assign(127, 1) );
177 static_assert( test_xor_assign(255, 2u) );
178 static_assert( test_xor_assign(63, 3ll) );
179
180 constexpr bool test_xor(unsigned char l, unsigned char r)
181 {
182 const std::byte b1{l};
183 const std::byte b2{r};
184 return (b1 ^ b2) == std::byte(l ^ r);
185 }
186
187 static_assert( test_xor(0, 1) );
188 static_assert( test_xor(0, 1u) );
189 static_assert( test_xor(0, 1ll) );
190 static_assert( test_xor(4, 1) );
191 static_assert( test_xor(9, 2u) );
192 static_assert( test_xor(16, 3ll) );
193 static_assert( test_xor(127, 1) );
194 static_assert( test_xor(255, 2u) );
195 static_assert( test_xor(63, 3ll) );
196
197 constexpr bool test_complement(unsigned char c)
198 {
199 const std::byte b{c};
200 return ~b == std::byte(~c);
201 }
202
203 static_assert( test_complement(0) );
204 static_assert( test_complement(4) );
205 static_assert( test_complement(9) );
206 static_assert( test_complement(16) );
207 static_assert( test_complement(63) );
208 static_assert( test_complement(127) );
209 static_assert( test_complement(255) );
210
211 template<typename T>
212 constexpr bool test_to_integer(unsigned char c)
213 {
214 std::byte b{c};
215 return std::to_integer<T>(b) == T(c);
216 }
217
218 static_assert( test_to_integer<int>(0) );
219 static_assert( test_to_integer<int>(255) );
220 static_assert( test_to_integer<signed char>(0) );
221 static_assert( test_to_integer<signed char>(255) );
222 static_assert( test_to_integer<unsigned>(0) );
223 static_assert( test_to_integer<unsigned>(255) );
224 #ifdef _GLIBCXX_USE_CHAR8_T
225 static_assert( test_to_integer<char8_t>(0) );
226 static_assert( test_to_integer<char8_t>(255) );
227 #endif
228 static_assert( test_to_integer<char16_t>(0) );
229 static_assert( test_to_integer<char16_t>(255) );
230 static_assert( test_to_integer<char32_t>(0) );
231 static_assert( test_to_integer<char32_t>(255) );