1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // compile-flags: -C debug-assertions
13 // Test std::num::Wrapping<T> for {uN, iN, usize, isize}
15 #![feature(num_bits_bytes, test)]
19 use std::num::Wrapping;
21 Add, Sub, Mul, Div, Rem, BitXor, BitOr, BitAnd,
22 AddAssign, SubAssign, MulAssign, DivAssign, RemAssign, BitXorAssign, BitOrAssign, BitAndAssign,
23 Shl, Shr, ShlAssign, ShrAssign
25 use std::{i8, i16, i32, i64, isize, u8, u16, u32, u64, usize};
36 macro_rules! op_test {
37 ($op:ident ($lhs:expr, $rhs:expr) == $ans:expr) => {
38 assert_eq!(black_box(Wrapping($lhs).$op(Wrapping($rhs))), Wrapping($ans));
39 // FIXME(30524): uncomment this test when it's implemented
40 // assert_eq!(black_box(Wrapping($lhs).$op($rhs)), Wrapping($ans));
44 op_test!(add(i8::MAX, 1) == i8::MIN);
45 op_test!(add(i16::MAX, 1) == i16::MIN);
46 op_test!(add(i32::MAX, 1) == i32::MIN);
47 op_test!(add(i64::MAX, 1) == i64::MIN);
48 op_test!(add(isize::MAX, 1) == isize::MIN);
50 op_test!(add(u8::MAX, 1) == 0);
51 op_test!(add(u16::MAX, 1) == 0);
52 op_test!(add(u32::MAX, 1) == 0);
53 op_test!(add(u64::MAX, 1) == 0);
54 op_test!(add(usize::MAX, 1) == 0);
57 op_test!(sub(i8::MIN, 1) == i8::MAX);
58 op_test!(sub(i16::MIN, 1) == i16::MAX);
59 op_test!(sub(i32::MIN, 1) == i32::MAX);
60 op_test!(sub(i64::MIN, 1) == i64::MAX);
61 op_test!(sub(isize::MIN, 1) == isize::MAX);
63 op_test!(sub(0u8, 1) == u8::MAX);
64 op_test!(sub(0u16, 1) == u16::MAX);
65 op_test!(sub(0u32, 1) == u32::MAX);
66 op_test!(sub(0u64, 1) == u64::MAX);
67 op_test!(sub(0usize, 1) == usize::MAX);
70 op_test!(mul(i8::MAX, 2) == -2);
71 op_test!(mul(i16::MAX, 2) == -2);
72 op_test!(mul(i32::MAX, 2) == -2);
73 op_test!(mul(i64::MAX, 2) == -2);
74 op_test!(mul(isize::MAX, 2) == -2);
76 op_test!(mul(u8::MAX, 2) == u8::MAX - 1);
77 op_test!(mul(u16::MAX, 2) == u16::MAX - 1);
78 op_test!(mul(u32::MAX, 2) == u32::MAX - 1);
79 op_test!(mul(u64::MAX, 2) == u64::MAX - 1);
80 op_test!(mul(usize::MAX, 2) == usize::MAX - 1);
83 op_test!(div(i8::MIN, -1) == i8::MIN);
84 op_test!(div(i16::MIN, -1) == i16::MIN);
85 op_test!(div(i32::MIN, -1) == i32::MIN);
86 op_test!(div(i64::MIN, -1) == i64::MIN);
87 op_test!(div(isize::MIN, -1) == isize::MIN);
90 op_test!(rem(i8::MIN, -1) == 0);
91 op_test!(rem(i16::MIN, -1) == 0);
92 op_test!(rem(i32::MIN, -1) == 0);
93 op_test!(rem(i64::MIN, -1) == 0);
94 op_test!(rem(isize::MIN, -1) == 0);
96 // these are not that interesting, just testing to make sure they are implemented correctly
97 op_test!(bitxor(0b101010i8, 0b100110) == 0b001100);
98 op_test!(bitxor(0b101010i16, 0b100110) == 0b001100);
99 op_test!(bitxor(0b101010i32, 0b100110) == 0b001100);
100 op_test!(bitxor(0b101010i64, 0b100110) == 0b001100);
101 op_test!(bitxor(0b101010isize, 0b100110) == 0b001100);
103 op_test!(bitxor(0b101010u8, 0b100110) == 0b001100);
104 op_test!(bitxor(0b101010u16, 0b100110) == 0b001100);
105 op_test!(bitxor(0b101010u32, 0b100110) == 0b001100);
106 op_test!(bitxor(0b101010u64, 0b100110) == 0b001100);
107 op_test!(bitxor(0b101010usize, 0b100110) == 0b001100);
110 op_test!(bitor(0b101010i8, 0b100110) == 0b101110);
111 op_test!(bitor(0b101010i16, 0b100110) == 0b101110);
112 op_test!(bitor(0b101010i32, 0b100110) == 0b101110);
113 op_test!(bitor(0b101010i64, 0b100110) == 0b101110);
114 op_test!(bitor(0b101010isize, 0b100110) == 0b101110);
116 op_test!(bitor(0b101010u8, 0b100110) == 0b101110);
117 op_test!(bitor(0b101010u16, 0b100110) == 0b101110);
118 op_test!(bitor(0b101010u32, 0b100110) == 0b101110);
119 op_test!(bitor(0b101010u64, 0b100110) == 0b101110);
120 op_test!(bitor(0b101010usize, 0b100110) == 0b101110);
123 op_test!(bitand(0b101010i8, 0b100110) == 0b100010);
124 op_test!(bitand(0b101010i16, 0b100110) == 0b100010);
125 op_test!(bitand(0b101010i32, 0b100110) == 0b100010);
126 op_test!(bitand(0b101010i64, 0b100110) == 0b100010);
127 op_test!(bitand(0b101010isize, 0b100110) == 0b100010);
129 op_test!(bitand(0b101010u8, 0b100110) == 0b100010);
130 op_test!(bitand(0b101010u16, 0b100110) == 0b100010);
131 op_test!(bitand(0b101010u32, 0b100110) == 0b100010);
132 op_test!(bitand(0b101010u64, 0b100110) == 0b100010);
133 op_test!(bitand(0b101010usize, 0b100110) == 0b100010);
136 fn test_op_assigns() {
137 macro_rules! op_assign_test {
138 ($op:ident ($initial:expr, $rhs:expr) == $ans:expr) => {
140 let mut tmp = Wrapping($initial);
141 tmp = black_box(tmp);
142 tmp.$op(Wrapping($rhs));
143 assert_eq!(black_box(tmp), Wrapping($ans));
145 // FIXME(30524): Uncomment this test
148 let mut tmp = Wrapping($initial);
149 tmp = black_box(tmp);
151 assert_eq!(black_box(tmp), Wrapping($ans));
156 op_assign_test!(add_assign(i8::MAX, 1) == i8::MIN);
157 op_assign_test!(add_assign(i16::MAX, 1) == i16::MIN);
158 op_assign_test!(add_assign(i32::MAX, 1) == i32::MIN);
159 op_assign_test!(add_assign(i64::MAX, 1) == i64::MIN);
160 op_assign_test!(add_assign(isize::MAX, 1) == isize::MIN);
162 op_assign_test!(add_assign(u8::MAX, 1) == u8::MIN);
163 op_assign_test!(add_assign(u16::MAX, 1) == u16::MIN);
164 op_assign_test!(add_assign(u32::MAX, 1) == u32::MIN);
165 op_assign_test!(add_assign(u64::MAX, 1) == u64::MIN);
166 op_assign_test!(add_assign(usize::MAX, 1) == usize::MIN);
169 op_assign_test!(sub_assign(i8::MIN, 1) == i8::MAX);
170 op_assign_test!(sub_assign(i16::MIN, 1) == i16::MAX);
171 op_assign_test!(sub_assign(i32::MIN, 1) == i32::MAX);
172 op_assign_test!(sub_assign(i64::MIN, 1) == i64::MAX);
173 op_assign_test!(sub_assign(isize::MIN, 1) == isize::MAX);
175 op_assign_test!(sub_assign(u8::MIN, 1) == u8::MAX);
176 op_assign_test!(sub_assign(u16::MIN, 1) == u16::MAX);
177 op_assign_test!(sub_assign(u32::MIN, 1) == u32::MAX);
178 op_assign_test!(sub_assign(u64::MIN, 1) == u64::MAX);
179 op_assign_test!(sub_assign(usize::MIN, 1) == usize::MAX);
182 op_assign_test!(mul_assign(i8::MAX, 2) == -2);
183 op_assign_test!(mul_assign(i16::MAX, 2) == -2);
184 op_assign_test!(mul_assign(i32::MAX, 2) == -2);
185 op_assign_test!(mul_assign(i64::MAX, 2) == -2);
186 op_assign_test!(mul_assign(isize::MAX, 2) == -2);
188 op_assign_test!(mul_assign(u8::MAX, 2) == u8::MAX - 1);
189 op_assign_test!(mul_assign(u16::MAX, 2) == u16::MAX - 1);
190 op_assign_test!(mul_assign(u32::MAX, 2) == u32::MAX - 1);
191 op_assign_test!(mul_assign(u64::MAX, 2) == u64::MAX - 1);
192 op_assign_test!(mul_assign(usize::MAX, 2) == usize::MAX - 1);
195 op_assign_test!(div_assign(i8::MIN, -1) == i8::MIN);
196 op_assign_test!(div_assign(i16::MIN, -1) == i16::MIN);
197 op_assign_test!(div_assign(i32::MIN, -1) == i32::MIN);
198 op_assign_test!(div_assign(i64::MIN, -1) == i64::MIN);
199 op_assign_test!(div_assign(isize::MIN, -1) == isize::MIN);
202 op_assign_test!(rem_assign(i8::MIN, -1) == 0);
203 op_assign_test!(rem_assign(i16::MIN, -1) == 0);
204 op_assign_test!(rem_assign(i32::MIN, -1) == 0);
205 op_assign_test!(rem_assign(i64::MIN, -1) == 0);
206 op_assign_test!(rem_assign(isize::MIN, -1) == 0);
209 // these are not that interesting, just testing to make sure they are implemented correctly
210 op_assign_test!(bitxor_assign(0b101010i8, 0b100110) == 0b001100);
211 op_assign_test!(bitxor_assign(0b101010i16, 0b100110) == 0b001100);
212 op_assign_test!(bitxor_assign(0b101010i32, 0b100110) == 0b001100);
213 op_assign_test!(bitxor_assign(0b101010i64, 0b100110) == 0b001100);
214 op_assign_test!(bitxor_assign(0b101010isize, 0b100110) == 0b001100);
216 op_assign_test!(bitxor_assign(0b101010u8, 0b100110) == 0b001100);
217 op_assign_test!(bitxor_assign(0b101010u16, 0b100110) == 0b001100);
218 op_assign_test!(bitxor_assign(0b101010u32, 0b100110) == 0b001100);
219 op_assign_test!(bitxor_assign(0b101010u64, 0b100110) == 0b001100);
220 op_assign_test!(bitxor_assign(0b101010usize, 0b100110) == 0b001100);
223 op_assign_test!(bitor_assign(0b101010i8, 0b100110) == 0b101110);
224 op_assign_test!(bitor_assign(0b101010i16, 0b100110) == 0b101110);
225 op_assign_test!(bitor_assign(0b101010i32, 0b100110) == 0b101110);
226 op_assign_test!(bitor_assign(0b101010i64, 0b100110) == 0b101110);
227 op_assign_test!(bitor_assign(0b101010isize, 0b100110) == 0b101110);
229 op_assign_test!(bitor_assign(0b101010u8, 0b100110) == 0b101110);
230 op_assign_test!(bitor_assign(0b101010u16, 0b100110) == 0b101110);
231 op_assign_test!(bitor_assign(0b101010u32, 0b100110) == 0b101110);
232 op_assign_test!(bitor_assign(0b101010u64, 0b100110) == 0b101110);
233 op_assign_test!(bitor_assign(0b101010usize, 0b100110) == 0b101110);
236 op_assign_test!(bitand_assign(0b101010i8, 0b100110) == 0b100010);
237 op_assign_test!(bitand_assign(0b101010i16, 0b100110) == 0b100010);
238 op_assign_test!(bitand_assign(0b101010i32, 0b100110) == 0b100010);
239 op_assign_test!(bitand_assign(0b101010i64, 0b100110) == 0b100010);
240 op_assign_test!(bitand_assign(0b101010isize, 0b100110) == 0b100010);
242 op_assign_test!(bitand_assign(0b101010u8, 0b100110) == 0b100010);
243 op_assign_test!(bitand_assign(0b101010u16, 0b100110) == 0b100010);
244 op_assign_test!(bitand_assign(0b101010u32, 0b100110) == 0b100010);
245 op_assign_test!(bitand_assign(0b101010u64, 0b100110) == 0b100010);
246 op_assign_test!(bitand_assign(0b101010usize, 0b100110) == 0b100010);
250 macro_rules! sh_test {
251 ($op:ident ($lhs:expr, $rhs:expr) == $ans:expr) => {
252 assert_eq!(black_box(Wrapping($lhs).$op($rhs)), Wrapping($ans));
255 // NOTE: This will break for i8 if we ever get i/u128
256 macro_rules! sh_test_all {
258 sh_test!(shl(i8::MAX, (i8::BITS + 1) as $t) == -2);
259 sh_test!(shl(i16::MAX, (i16::BITS + 1) as $t) == -2);
260 sh_test!(shl(i32::MAX, (i32::BITS + 1) as $t) == -2);
261 sh_test!(shl(i64::MAX, (i64::BITS + 1) as $t) == -2);
262 sh_test!(shl(isize::MAX, (isize::BITS + 1) as $t) == -2);
264 sh_test!(shl(u8::MAX, (u8::BITS + 1) as $t) == u8::MAX - 1);
265 sh_test!(shl(u16::MAX, (u16::BITS + 1) as $t) == u16::MAX - 1);
266 sh_test!(shl(u32::MAX, (u32::BITS + 1) as $t) == u32::MAX - 1);
267 sh_test!(shl(u64::MAX, (u64::BITS + 1) as $t) == u64::MAX - 1);
268 sh_test!(shl(usize::MAX, (usize::BITS + 1) as $t) == usize::MAX - 1);
271 sh_test!(shr(i8::MAX, (i8::BITS + 1) as $t) == i8::MAX / 2);
272 sh_test!(shr(i16::MAX, (i16::BITS + 1) as $t) == i16::MAX / 2);
273 sh_test!(shr(i32::MAX, (i32::BITS + 1) as $t) == i32::MAX / 2);
274 sh_test!(shr(i64::MAX, (i64::BITS + 1) as $t) == i64::MAX / 2);
275 sh_test!(shr(isize::MAX, (isize::BITS + 1) as $t) == isize::MAX / 2);
277 sh_test!(shr(u8::MAX, (u8::BITS + 1) as $t) == u8::MAX / 2);
278 sh_test!(shr(u16::MAX, (u16::BITS + 1) as $t) == u16::MAX / 2);
279 sh_test!(shr(u32::MAX, (u32::BITS + 1) as $t) == u32::MAX / 2);
280 sh_test!(shr(u64::MAX, (u64::BITS + 1) as $t) == u64::MAX / 2);
281 sh_test!(shr(usize::MAX, (usize::BITS + 1) as $t) == usize::MAX / 2);
284 macro_rules! sh_test_negative_all {
286 sh_test!(shr(i8::MAX, -((i8::BITS + 1) as $t)) == -2);
287 sh_test!(shr(i16::MAX, -((i16::BITS + 1) as $t)) == -2);
288 sh_test!(shr(i32::MAX, -((i32::BITS + 1) as $t)) == -2);
289 sh_test!(shr(i64::MAX, -((i64::BITS + 1) as $t)) == -2);
290 sh_test!(shr(isize::MAX, -((isize::BITS + 1) as $t)) == -2);
292 sh_test!(shr(u8::MAX, -((u8::BITS + 1) as $t)) == u8::MAX - 1);
293 sh_test!(shr(u16::MAX, -((u16::BITS + 1) as $t)) == u16::MAX - 1);
294 sh_test!(shr(u32::MAX, -((u32::BITS + 1) as $t)) == u32::MAX - 1);
295 sh_test!(shr(u64::MAX, -((u64::BITS + 1) as $t)) == u64::MAX - 1);
296 sh_test!(shr(usize::MAX, -((usize::BITS + 1) as $t)) == usize::MAX - 1);
299 sh_test!(shl(i8::MAX, -((i8::BITS + 1) as $t)) == i8::MAX / 2);
300 sh_test!(shl(i16::MAX, -((i16::BITS + 1) as $t)) == i16::MAX / 2);
301 sh_test!(shl(i32::MAX, -((i32::BITS + 1) as $t)) == i32::MAX / 2);
302 sh_test!(shl(i64::MAX, -((i64::BITS + 1) as $t)) == i64::MAX / 2);
303 sh_test!(shl(isize::MAX, -((isize::BITS + 1) as $t)) == isize::MAX / 2);
305 sh_test!(shl(u8::MAX, -((u8::BITS + 1) as $t)) == u8::MAX / 2);
306 sh_test!(shl(u16::MAX, -((u16::BITS + 1) as $t)) == u16::MAX / 2);
307 sh_test!(shl(u32::MAX, -((u32::BITS + 1) as $t)) == u32::MAX / 2);
308 sh_test!(shl(u64::MAX, -((u64::BITS + 1) as $t)) == u64::MAX / 2);
309 sh_test!(shl(usize::MAX, -((usize::BITS + 1) as $t)) == usize::MAX / 2);
312 // FIXME(#23545): Uncomment the remaining tests
321 //sh_test_all!(isize);
324 //sh_test_negative_all!(i8);
325 //sh_test_negative_all!(i16);
326 //sh_test_negative_all!(i32);
327 //sh_test_negative_all!(i64);
328 //sh_test_negative_all!(isize);
331 fn test_sh_op_assigns() {
332 macro_rules! sh_assign_test {
333 ($op:ident ($initial:expr, $rhs:expr) == $ans:expr) => {{
334 let mut tmp = Wrapping($initial);
335 tmp = black_box(tmp);
337 assert_eq!(black_box(tmp), Wrapping($ans));
340 macro_rules! sh_assign_test_all {
342 sh_assign_test!(shl_assign(i8::MAX, (i8::BITS + 1) as $t) == -2);
343 sh_assign_test!(shl_assign(i16::MAX, (i16::BITS + 1) as $t) == -2);
344 sh_assign_test!(shl_assign(i32::MAX, (i32::BITS + 1) as $t) == -2);
345 sh_assign_test!(shl_assign(i64::MAX, (i64::BITS + 1) as $t) == -2);
346 sh_assign_test!(shl_assign(isize::MAX, (isize::BITS + 1) as $t) == -2);
348 sh_assign_test!(shl_assign(u8::MAX, (u8::BITS + 1) as $t) == u8::MAX - 1);
349 sh_assign_test!(shl_assign(u16::MAX, (u16::BITS + 1) as $t) == u16::MAX - 1);
350 sh_assign_test!(shl_assign(u32::MAX, (u32::BITS + 1) as $t) == u32::MAX - 1);
351 sh_assign_test!(shl_assign(u64::MAX, (u64::BITS + 1) as $t) == u64::MAX - 1);
352 sh_assign_test!(shl_assign(usize::MAX, (usize::BITS + 1) as $t) == usize::MAX - 1);
355 sh_assign_test!(shr_assign(i8::MAX, (i8::BITS + 1) as $t) == i8::MAX / 2);
356 sh_assign_test!(shr_assign(i16::MAX, (i16::BITS + 1) as $t) == i16::MAX / 2);
357 sh_assign_test!(shr_assign(i32::MAX, (i32::BITS + 1) as $t) == i32::MAX / 2);
358 sh_assign_test!(shr_assign(i64::MAX, (i64::BITS + 1) as $t) == i64::MAX / 2);
359 sh_assign_test!(shr_assign(isize::MAX, (isize::BITS + 1) as $t) == isize::MAX / 2);
361 sh_assign_test!(shr_assign(u8::MAX, (u8::BITS + 1) as $t) == u8::MAX / 2);
362 sh_assign_test!(shr_assign(u16::MAX, (u16::BITS + 1) as $t) == u16::MAX / 2);
363 sh_assign_test!(shr_assign(u32::MAX, (u32::BITS + 1) as $t) == u32::MAX / 2);
364 sh_assign_test!(shr_assign(u64::MAX, (u64::BITS + 1) as $t) == u64::MAX / 2);
365 sh_assign_test!(shr_assign(usize::MAX, (usize::BITS + 1) as $t) == usize::MAX / 2);
368 macro_rules! sh_assign_test_negative_all {
370 sh_assign_test!(shr_assign(i8::MAX, -((i8::BITS + 1) as $t)) == -2);
371 sh_assign_test!(shr_assign(i16::MAX, -((i16::BITS + 1) as $t)) == -2);
372 sh_assign_test!(shr_assign(i32::MAX, -((i32::BITS + 1) as $t)) == -2);
373 sh_assign_test!(shr_assign(i64::MAX, -((i64::BITS + 1) as $t)) == -2);
374 sh_assign_test!(shr_assign(isize::MAX, -((isize::BITS + 1) as $t)) == -2);
376 sh_assign_test!(shr_assign(u8::MAX, -((u8::BITS + 1) as $t)) == u8::MAX - 1);
377 sh_assign_test!(shr_assign(u16::MAX, -((u16::BITS + 1) as $t)) == u16::MAX - 1);
378 sh_assign_test!(shr_assign(u32::MAX, -((u32::BITS + 1) as $t)) == u32::MAX - 1);
379 sh_assign_test!(shr_assign(u64::MAX, -((u64::BITS + 1) as $t)) == u64::MAX - 1);
380 sh_assign_test!(shr_assign(usize::MAX, -((usize::BITS + 1) as $t)) == usize::MAX - 1);
383 sh_assign_test!(shl_assign(i8::MAX, -((i8::BITS + 1) as $t)) == i8::MAX / 2);
384 sh_assign_test!(shl_assign(i16::MAX, -((i16::BITS + 1) as $t)) == i16::MAX / 2);
385 sh_assign_test!(shl_assign(i32::MAX, -((i32::BITS + 1) as $t)) == i32::MAX / 2);
386 sh_assign_test!(shl_assign(i64::MAX, -((i64::BITS + 1) as $t)) == i64::MAX / 2);
387 sh_assign_test!(shl_assign(isize::MAX, -((isize::BITS + 1) as $t)) == isize::MAX / 2);
389 sh_assign_test!(shl_assign(u8::MAX, -((u8::BITS + 1) as $t)) == u8::MAX / 2);
390 sh_assign_test!(shl_assign(u16::MAX, -((u16::BITS + 1) as $t)) == u16::MAX / 2);
391 sh_assign_test!(shl_assign(u32::MAX, -((u32::BITS + 1) as $t)) == u32::MAX / 2);
392 sh_assign_test!(shl_assign(u64::MAX, -((u64::BITS + 1) as $t)) == u64::MAX / 2);
393 sh_assign_test!(shl_assign(usize::MAX, -((usize::BITS + 1) as $t)) == usize::MAX / 2);
397 // FIXME(#23545): Uncomment the remaining tests
398 //sh_assign_test_all!(i8);
399 //sh_assign_test_all!(u8);
400 //sh_assign_test_all!(i16);
401 //sh_assign_test_all!(u16);
402 //sh_assign_test_all!(i32);
403 //sh_assign_test_all!(u32);
404 //sh_assign_test_all!(i64);
405 //sh_assign_test_all!(u64);
406 //sh_assign_test_all!(isize);
407 sh_assign_test_all!(usize);
409 //sh_assign_test_negative_all!(i8);
410 //sh_assign_test_negative_all!(i16);
411 //sh_assign_test_negative_all!(i32);
412 //sh_assign_test_negative_all!(i64);
413 //sh_assign_test_negative_all!(isize);