]> git.lizzy.rs Git - rust.git/blob - library/core/tests/num/wrapping.rs
Auto merge of #106340 - saethlin:propagate-operands, r=oli-obk
[rust.git] / library / core / tests / num / wrapping.rs
1 use core::num::Wrapping;
2
3 macro_rules! wrapping_operation {
4     ($result:expr, $lhs:ident $op:tt $rhs:expr) => {
5         assert_eq!($result, $lhs $op $rhs);
6         assert_eq!($result, &$lhs $op $rhs);
7         assert_eq!($result, $lhs $op &$rhs);
8         assert_eq!($result, &$lhs $op &$rhs);
9     };
10     ($result:expr, $op:tt $expr:expr) => {
11         assert_eq!($result, $op $expr);
12         assert_eq!($result, $op &$expr);
13     };
14 }
15
16 macro_rules! wrapping_assignment {
17     ($result:expr, $lhs:ident $op:tt $rhs:expr) => {
18         let mut lhs1 = $lhs;
19         lhs1 $op $rhs;
20         assert_eq!($result, lhs1);
21
22         let mut lhs2 = $lhs;
23         lhs2 $op &$rhs;
24         assert_eq!($result, lhs2);
25     };
26 }
27
28 macro_rules! wrapping_test {
29     ($fn_name:ident, $type:ty, $min:expr, $max:expr) => {
30         #[test]
31         fn $fn_name() {
32             let zero: Wrapping<$type> = Wrapping(0);
33             let one: Wrapping<$type> = Wrapping(1);
34             let min: Wrapping<$type> = Wrapping($min);
35             let max: Wrapping<$type> = Wrapping($max);
36
37             wrapping_operation!(min, max + one);
38             wrapping_assignment!(min, max += one);
39             wrapping_operation!(max, min - one);
40             wrapping_assignment!(max, min -= one);
41             wrapping_operation!(max, max * one);
42             wrapping_assignment!(max, max *= one);
43             wrapping_operation!(max, max / one);
44             wrapping_assignment!(max, max /= one);
45             wrapping_operation!(zero, max % one);
46             wrapping_assignment!(zero, max %= one);
47             wrapping_operation!(zero, zero & max);
48             wrapping_assignment!(zero, zero &= max);
49             wrapping_operation!(max, zero | max);
50             wrapping_assignment!(max, zero |= max);
51             wrapping_operation!(zero, max ^ max);
52             wrapping_assignment!(zero, max ^= max);
53             wrapping_operation!(zero, zero << 1usize);
54             wrapping_assignment!(zero, zero <<= 1usize);
55             wrapping_operation!(zero, zero >> 1usize);
56             wrapping_assignment!(zero, zero >>= 1usize);
57             wrapping_operation!(zero, -zero);
58             wrapping_operation!(max, !min);
59         }
60     };
61 }
62
63 wrapping_test!(test_wrapping_i8, i8, i8::MIN, i8::MAX);
64 wrapping_test!(test_wrapping_i16, i16, i16::MIN, i16::MAX);
65 wrapping_test!(test_wrapping_i32, i32, i32::MIN, i32::MAX);
66 wrapping_test!(test_wrapping_i64, i64, i64::MIN, i64::MAX);
67 #[cfg(not(target_os = "emscripten"))]
68 wrapping_test!(test_wrapping_i128, i128, i128::MIN, i128::MAX);
69 wrapping_test!(test_wrapping_isize, isize, isize::MIN, isize::MAX);
70 wrapping_test!(test_wrapping_u8, u8, u8::MIN, u8::MAX);
71 wrapping_test!(test_wrapping_u16, u16, u16::MIN, u16::MAX);
72 wrapping_test!(test_wrapping_u32, u32, u32::MIN, u32::MAX);
73 wrapping_test!(test_wrapping_u64, u64, u64::MIN, u64::MAX);
74 #[cfg(not(target_os = "emscripten"))]
75 wrapping_test!(test_wrapping_u128, u128, u128::MIN, u128::MAX);
76 wrapping_test!(test_wrapping_usize, usize, usize::MIN, usize::MAX);
77
78 #[test]
79 fn wrapping_int_api() {
80     assert_eq!(i8::MAX.wrapping_add(1), i8::MIN);
81     assert_eq!(i16::MAX.wrapping_add(1), i16::MIN);
82     assert_eq!(i32::MAX.wrapping_add(1), i32::MIN);
83     assert_eq!(i64::MAX.wrapping_add(1), i64::MIN);
84     assert_eq!(isize::MAX.wrapping_add(1), isize::MIN);
85
86     assert_eq!(i8::MIN.wrapping_sub(1), i8::MAX);
87     assert_eq!(i16::MIN.wrapping_sub(1), i16::MAX);
88     assert_eq!(i32::MIN.wrapping_sub(1), i32::MAX);
89     assert_eq!(i64::MIN.wrapping_sub(1), i64::MAX);
90     assert_eq!(isize::MIN.wrapping_sub(1), isize::MAX);
91
92     assert_eq!(u8::MAX.wrapping_add(1), u8::MIN);
93     assert_eq!(u16::MAX.wrapping_add(1), u16::MIN);
94     assert_eq!(u32::MAX.wrapping_add(1), u32::MIN);
95     assert_eq!(u64::MAX.wrapping_add(1), u64::MIN);
96     assert_eq!(usize::MAX.wrapping_add(1), usize::MIN);
97
98     assert_eq!(u8::MIN.wrapping_sub(1), u8::MAX);
99     assert_eq!(u16::MIN.wrapping_sub(1), u16::MAX);
100     assert_eq!(u32::MIN.wrapping_sub(1), u32::MAX);
101     assert_eq!(u64::MIN.wrapping_sub(1), u64::MAX);
102     assert_eq!(usize::MIN.wrapping_sub(1), usize::MAX);
103
104     assert_eq!((0xfe_u8 as i8).wrapping_mul(16), (0xe0_u8 as i8));
105     assert_eq!((0xfedc_u16 as i16).wrapping_mul(16), (0xedc0_u16 as i16));
106     assert_eq!((0xfedc_ba98_u32 as i32).wrapping_mul(16), (0xedcb_a980_u32 as i32));
107     assert_eq!(
108         (0xfedc_ba98_7654_3217_u64 as i64).wrapping_mul(16),
109         (0xedcb_a987_6543_2170_u64 as i64)
110     );
111
112     match () {
113         #[cfg(target_pointer_width = "32")]
114         () => {
115             assert_eq!((0xfedc_ba98_u32 as isize).wrapping_mul(16), (0xedcb_a980_u32 as isize));
116         }
117         #[cfg(target_pointer_width = "64")]
118         () => {
119             assert_eq!(
120                 (0xfedc_ba98_7654_3217_u64 as isize).wrapping_mul(16),
121                 (0xedcb_a987_6543_2170_u64 as isize)
122             );
123         }
124     }
125
126     assert_eq!((0xfe as u8).wrapping_mul(16), (0xe0 as u8));
127     assert_eq!((0xfedc as u16).wrapping_mul(16), (0xedc0 as u16));
128     assert_eq!((0xfedc_ba98 as u32).wrapping_mul(16), (0xedcb_a980 as u32));
129     assert_eq!((0xfedc_ba98_7654_3217 as u64).wrapping_mul(16), (0xedcb_a987_6543_2170 as u64));
130
131     match () {
132         #[cfg(target_pointer_width = "32")]
133         () => {
134             assert_eq!((0xfedc_ba98 as usize).wrapping_mul(16), (0xedcb_a980 as usize));
135         }
136         #[cfg(target_pointer_width = "64")]
137         () => {
138             assert_eq!(
139                 (0xfedc_ba98_7654_3217 as usize).wrapping_mul(16),
140                 (0xedcb_a987_6543_2170 as usize)
141             );
142         }
143     }
144
145     macro_rules! check_mul_no_wrap {
146         ($e:expr, $f:expr) => {
147             assert_eq!(($e).wrapping_mul($f), ($e) * $f);
148         };
149     }
150     macro_rules! check_mul_wraps {
151         ($e:expr, $f:expr) => {
152             assert_eq!(($e).wrapping_mul($f), $e);
153         };
154     }
155
156     check_mul_no_wrap!(0xfe_u8 as i8, -1);
157     check_mul_no_wrap!(0xfedc_u16 as i16, -1);
158     check_mul_no_wrap!(0xfedc_ba98_u32 as i32, -1);
159     check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
160     check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
161
162     check_mul_no_wrap!(0xfe_u8 as i8, -2);
163     check_mul_no_wrap!(0xfedc_u16 as i16, -2);
164     check_mul_no_wrap!(0xfedc_ba98_u32 as i32, -2);
165     check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
166     check_mul_no_wrap!(0xfedc_ba98_fedc_ba98_u64 as u64 as isize, -2);
167
168     check_mul_no_wrap!(0xfe_u8 as i8, 2);
169     check_mul_no_wrap!(0xfedc_u16 as i16, 2);
170     check_mul_no_wrap!(0xfedc_ba98_u32 as i32, 2);
171     check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
172     check_mul_no_wrap!(0xfedc_ba98_fedc_ba98_u64 as u64 as isize, 2);
173
174     check_mul_wraps!(0x80_u8 as i8, -1);
175     check_mul_wraps!(0x8000_u16 as i16, -1);
176     check_mul_wraps!(0x8000_0000_u32 as i32, -1);
177     check_mul_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
178     match () {
179         #[cfg(target_pointer_width = "32")]
180         () => {
181             check_mul_wraps!(0x8000_0000_u32 as isize, -1);
182         }
183         #[cfg(target_pointer_width = "64")]
184         () => {
185             check_mul_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
186         }
187     }
188
189     macro_rules! check_div_no_wrap {
190         ($e:expr, $f:expr) => {
191             assert_eq!(($e).wrapping_div($f), ($e) / $f);
192         };
193     }
194     macro_rules! check_div_wraps {
195         ($e:expr, $f:expr) => {
196             assert_eq!(($e).wrapping_div($f), $e);
197         };
198     }
199
200     check_div_no_wrap!(0xfe_u8 as i8, -1);
201     check_div_no_wrap!(0xfedc_u16 as i16, -1);
202     check_div_no_wrap!(0xfedc_ba98_u32 as i32, -1);
203     check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
204     check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
205
206     check_div_no_wrap!(0xfe_u8 as i8, -2);
207     check_div_no_wrap!(0xfedc_u16 as i16, -2);
208     check_div_no_wrap!(0xfedc_ba98_u32 as i32, -2);
209     check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
210     check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -2);
211
212     check_div_no_wrap!(0xfe_u8 as i8, 2);
213     check_div_no_wrap!(0xfedc_u16 as i16, 2);
214     check_div_no_wrap!(0xfedc_ba98_u32 as i32, 2);
215     check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
216     check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, 2);
217
218     check_div_wraps!(-128 as i8, -1);
219     check_div_wraps!(0x8000_u16 as i16, -1);
220     check_div_wraps!(0x8000_0000_u32 as i32, -1);
221     check_div_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
222     match () {
223         #[cfg(target_pointer_width = "32")]
224         () => {
225             check_div_wraps!(0x8000_0000_u32 as isize, -1);
226         }
227         #[cfg(target_pointer_width = "64")]
228         () => {
229             check_div_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
230         }
231     }
232
233     macro_rules! check_rem_no_wrap {
234         ($e:expr, $f:expr) => {
235             assert_eq!(($e).wrapping_rem($f), ($e) % $f);
236         };
237     }
238     macro_rules! check_rem_wraps {
239         ($e:expr, $f:expr) => {
240             assert_eq!(($e).wrapping_rem($f), 0);
241         };
242     }
243
244     check_rem_no_wrap!(0xfe_u8 as i8, -1);
245     check_rem_no_wrap!(0xfedc_u16 as i16, -1);
246     check_rem_no_wrap!(0xfedc_ba98_u32 as i32, -1);
247     check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
248     check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
249
250     check_rem_no_wrap!(0xfe_u8 as i8, -2);
251     check_rem_no_wrap!(0xfedc_u16 as i16, -2);
252     check_rem_no_wrap!(0xfedc_ba98_u32 as i32, -2);
253     check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
254     check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -2);
255
256     check_rem_no_wrap!(0xfe_u8 as i8, 2);
257     check_rem_no_wrap!(0xfedc_u16 as i16, 2);
258     check_rem_no_wrap!(0xfedc_ba98_u32 as i32, 2);
259     check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
260     check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, 2);
261
262     check_rem_wraps!(0x80_u8 as i8, -1);
263     check_rem_wraps!(0x8000_u16 as i16, -1);
264     check_rem_wraps!(0x8000_0000_u32 as i32, -1);
265     check_rem_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
266     match () {
267         #[cfg(target_pointer_width = "32")]
268         () => {
269             check_rem_wraps!(0x8000_0000_u32 as isize, -1);
270         }
271         #[cfg(target_pointer_width = "64")]
272         () => {
273             check_rem_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
274         }
275     }
276
277     macro_rules! check_neg_no_wrap {
278         ($e:expr) => {
279             assert_eq!(($e).wrapping_neg(), -($e));
280         };
281     }
282     macro_rules! check_neg_wraps {
283         ($e:expr) => {
284             assert_eq!(($e).wrapping_neg(), ($e));
285         };
286     }
287
288     check_neg_no_wrap!(0xfe_u8 as i8);
289     check_neg_no_wrap!(0xfedc_u16 as i16);
290     check_neg_no_wrap!(0xfedc_ba98_u32 as i32);
291     check_neg_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64);
292     check_neg_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize);
293
294     check_neg_wraps!(0x80_u8 as i8);
295     check_neg_wraps!(0x8000_u16 as i16);
296     check_neg_wraps!(0x8000_0000_u32 as i32);
297     check_neg_wraps!(0x8000_0000_0000_0000_u64 as i64);
298     match () {
299         #[cfg(target_pointer_width = "32")]
300         () => {
301             check_neg_wraps!(0x8000_0000_u32 as isize);
302         }
303         #[cfg(target_pointer_width = "64")]
304         () => {
305             check_neg_wraps!(0x8000_0000_0000_0000_u64 as isize);
306         }
307     }
308 }
309
310 #[test]
311 fn wrapping_const() {
312     // Specifically the wrapping behavior of division and remainder is subtle,
313     // see https://github.com/rust-lang/rust/pull/94512.
314     const _: () = {
315         assert!(i32::MIN.wrapping_div(-1) == i32::MIN);
316         assert!(i32::MIN.wrapping_rem(-1) == 0);
317     };
318 }