1 use core::num::Wrapping;
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);
10 ($result:expr, $op:tt $expr:expr) => {
11 assert_eq!($result, $op $expr);
12 assert_eq!($result, $op &$expr);
16 macro_rules! wrapping_assignment {
17 ($result:expr, $lhs:ident $op:tt $rhs:expr) => {
20 assert_eq!($result, lhs1);
24 assert_eq!($result, lhs2);
28 macro_rules! wrapping_test {
29 ($fn_name:ident, $type:ty, $min:expr, $max:expr) => {
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);
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);
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);
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);
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);
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);
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);
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));
108 (0xfedc_ba98_7654_3217_u64 as i64).wrapping_mul(16),
109 (0xedcb_a987_6543_2170_u64 as i64)
113 #[cfg(target_pointer_width = "32")]
115 assert_eq!((0xfedc_ba98_u32 as isize).wrapping_mul(16), (0xedcb_a980_u32 as isize));
117 #[cfg(target_pointer_width = "64")]
120 (0xfedc_ba98_7654_3217_u64 as isize).wrapping_mul(16),
121 (0xedcb_a987_6543_2170_u64 as isize)
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));
132 #[cfg(target_pointer_width = "32")]
134 assert_eq!((0xfedc_ba98 as usize).wrapping_mul(16), (0xedcb_a980 as usize));
136 #[cfg(target_pointer_width = "64")]
139 (0xfedc_ba98_7654_3217 as usize).wrapping_mul(16),
140 (0xedcb_a987_6543_2170 as usize)
145 macro_rules! check_mul_no_wrap {
146 ($e:expr, $f:expr) => {
147 assert_eq!(($e).wrapping_mul($f), ($e) * $f);
150 macro_rules! check_mul_wraps {
151 ($e:expr, $f:expr) => {
152 assert_eq!(($e).wrapping_mul($f), $e);
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);
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);
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);
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);
179 #[cfg(target_pointer_width = "32")]
181 check_mul_wraps!(0x8000_0000_u32 as isize, -1);
183 #[cfg(target_pointer_width = "64")]
185 check_mul_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
189 macro_rules! check_div_no_wrap {
190 ($e:expr, $f:expr) => {
191 assert_eq!(($e).wrapping_div($f), ($e) / $f);
194 macro_rules! check_div_wraps {
195 ($e:expr, $f:expr) => {
196 assert_eq!(($e).wrapping_div($f), $e);
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);
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);
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);
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);
223 #[cfg(target_pointer_width = "32")]
225 check_div_wraps!(0x8000_0000_u32 as isize, -1);
227 #[cfg(target_pointer_width = "64")]
229 check_div_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
233 macro_rules! check_rem_no_wrap {
234 ($e:expr, $f:expr) => {
235 assert_eq!(($e).wrapping_rem($f), ($e) % $f);
238 macro_rules! check_rem_wraps {
239 ($e:expr, $f:expr) => {
240 assert_eq!(($e).wrapping_rem($f), 0);
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);
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);
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);
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);
267 #[cfg(target_pointer_width = "32")]
269 check_rem_wraps!(0x8000_0000_u32 as isize, -1);
271 #[cfg(target_pointer_width = "64")]
273 check_rem_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
277 macro_rules! check_neg_no_wrap {
279 assert_eq!(($e).wrapping_neg(), -($e));
282 macro_rules! check_neg_wraps {
284 assert_eq!(($e).wrapping_neg(), ($e));
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);
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);
299 #[cfg(target_pointer_width = "32")]
301 check_neg_wraps!(0x8000_0000_u32 as isize);
303 #[cfg(target_pointer_width = "64")]
305 check_neg_wraps!(0x8000_0000_0000_0000_u64 as isize);
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.
315 assert!(i32::MIN.wrapping_div(-1) == i32::MIN);
316 assert!(i32::MIN.wrapping_rem(-1) == 0);