1 // Copyright (C) 2017-2018 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-options "-std=gnu++17" }
19 // { dg-do compile { target c++17 } }
20 
21 #include <cstddef>
22 
is_byte(std::byte)23 constexpr bool is_byte(std::byte) { return true; }
is_byte(T)24 template<typename T> constexpr bool is_byte(T) { return false; }
25 
26 template<typename T>
test_lshift_assign(unsigned char c,T t)27 constexpr 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 
34 static_assert( test_lshift_assign(0, 1) );
35 static_assert( test_lshift_assign(0, 1u) );
36 static_assert( test_lshift_assign(0, 1ll) );
37 static_assert( test_lshift_assign(4, 1) );
38 static_assert( test_lshift_assign(9, 2u) );
39 static_assert( test_lshift_assign(16, 3ll) );
40 static_assert( test_lshift_assign(127, 1) );
41 static_assert( test_lshift_assign(255, 2u) );
42 static_assert( test_lshift_assign(63, 3ll) );
43 
44 template<typename T>
test_lshift(unsigned char c,T t)45 constexpr 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 
52 static_assert( test_lshift(0, 1) );
53 static_assert( test_lshift(0, 1u) );
54 static_assert( test_lshift(0, 1ll) );
55 static_assert( test_lshift(4, 1) );
56 static_assert( test_lshift(9, 2u) );
57 static_assert( test_lshift(16, 3ll) );
58 static_assert( test_lshift(127, 1) );
59 static_assert( test_lshift(255, 2u) );
60 static_assert( test_lshift(63, 3ll) );
61 
62 template<typename T>
test_rshift_assign(unsigned char c,T t)63 constexpr 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 
70 static_assert( test_rshift_assign(0, 1) );
71 static_assert( test_rshift_assign(0, 1u) );
72 static_assert( test_rshift_assign(0, 1ll) );
73 static_assert( test_rshift_assign(4, 1) );
74 static_assert( test_rshift_assign(9, 2u) );
75 static_assert( test_rshift_assign(16, 3ll) );
76 static_assert( test_rshift_assign(127, 1) );
77 static_assert( test_rshift_assign(255, 2u) );
78 static_assert( test_rshift_assign(63, 3ll) );
79 
80 template<typename T>
test_rshift(unsigned char c,T t)81 constexpr 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 
88 static_assert( test_rshift(0, 1) );
89 static_assert( test_rshift(0, 1u) );
90 static_assert( test_rshift(0, 1ll) );
91 static_assert( test_rshift(4, 1) );
92 static_assert( test_rshift(9, 2u) );
93 static_assert( test_rshift(16, 3ll) );
94 static_assert( test_rshift(127, 1) );
95 static_assert( test_rshift(255, 2u) );
96 static_assert( test_rshift(63, 3ll) );
97 
test_or_assign(unsigned char l,unsigned char r)98 constexpr 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 
105 static_assert( test_or_assign(0, 1) );
106 static_assert( test_or_assign(4, 1) );
107 static_assert( test_or_assign(9, 2) );
108 static_assert( test_or_assign(16, 3) );
109 static_assert( test_or_assign(63, 3) );
110 static_assert( test_or_assign(127, 1) );
111 static_assert( test_or_assign(255, 2) );
112 
test_or(unsigned char l,unsigned char r)113 constexpr 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 
120 static_assert( test_or(0, 1) );
121 static_assert( test_or(0, 1u) );
122 static_assert( test_or(0, 1ll) );
123 static_assert( test_or(4, 1) );
124 static_assert( test_or(9, 2u) );
125 static_assert( test_or(16, 3ll) );
126 static_assert( test_or(127, 1) );
127 static_assert( test_or(255, 2u) );
128 static_assert( test_or(63, 3ll) );
129 
test_and_assign(unsigned char l,unsigned char r)130 constexpr 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 
137 static_assert( test_and_assign(0, 1) );
138 static_assert( test_and_assign(0, 1u) );
139 static_assert( test_and_assign(0, 1ll) );
140 static_assert( test_and_assign(4, 1) );
141 static_assert( test_and_assign(9, 2u) );
142 static_assert( test_and_assign(16, 3ll) );
143 static_assert( test_and_assign(127, 1) );
144 static_assert( test_and_assign(255, 2u) );
145 static_assert( test_and_assign(63, 3ll) );
146 
test_and(unsigned char l,unsigned char r)147 constexpr 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 
154 static_assert( test_and(0, 1) );
155 static_assert( test_and(0, 1u) );
156 static_assert( test_and(0, 1ll) );
157 static_assert( test_and(4, 1) );
158 static_assert( test_and(9, 2u) );
159 static_assert( test_and(16, 3ll) );
160 static_assert( test_and(127, 1) );
161 static_assert( test_and(255, 2u) );
162 static_assert( test_and(63, 3ll) );
163 
test_xor_assign(unsigned char l,unsigned char r)164 constexpr 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 
171 static_assert( test_xor_assign(0, 1) );
172 static_assert( test_xor_assign(0, 1u) );
173 static_assert( test_xor_assign(0, 1ll) );
174 static_assert( test_xor_assign(4, 1) );
175 static_assert( test_xor_assign(9, 2u) );
176 static_assert( test_xor_assign(16, 3ll) );
177 static_assert( test_xor_assign(127, 1) );
178 static_assert( test_xor_assign(255, 2u) );
179 static_assert( test_xor_assign(63, 3ll) );
180 
test_xor(unsigned char l,unsigned char r)181 constexpr 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 
188 static_assert( test_xor(0, 1) );
189 static_assert( test_xor(0, 1u) );
190 static_assert( test_xor(0, 1ll) );
191 static_assert( test_xor(4, 1) );
192 static_assert( test_xor(9, 2u) );
193 static_assert( test_xor(16, 3ll) );
194 static_assert( test_xor(127, 1) );
195 static_assert( test_xor(255, 2u) );
196 static_assert( test_xor(63, 3ll) );
197 
test_complement(unsigned char c)198 constexpr bool test_complement(unsigned char c)
199 {
200   const std::byte b{c};
201   return ~b == std::byte(~c);
202 }
203 
204 static_assert( test_complement(0) );
205 static_assert( test_complement(4) );
206 static_assert( test_complement(9) );
207 static_assert( test_complement(16) );
208 static_assert( test_complement(63) );
209 static_assert( test_complement(127) );
210 static_assert( test_complement(255) );
211 
212 template<typename T>
test_to_integer(unsigned char c)213 constexpr bool test_to_integer(unsigned char c)
214 {
215   std::byte b{c};
216   return std::to_integer<T>(b) == T(c);
217 }
218 
219 static_assert( test_to_integer<int>(0) );
220 static_assert( test_to_integer<int>(255) );
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 
225