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