2 // Test inherent wrapping_* methods for {i,u}{size,8,16,32,64}.
4 // Don't warn about overflowing ops on 32-bit platforms
5 #![cfg_attr(target_pointer_width = "32", allow(const_err))]
8 assert_eq!( i8::MAX.wrapping_add(1), i8::MIN);
9 assert_eq!( i16::MAX.wrapping_add(1), i16::MIN);
10 assert_eq!( i32::MAX.wrapping_add(1), i32::MIN);
11 assert_eq!( i64::MAX.wrapping_add(1), i64::MIN);
12 assert_eq!(isize::MAX.wrapping_add(1), isize::MIN);
14 assert_eq!( i8::MIN.wrapping_sub(1), i8::MAX);
15 assert_eq!( i16::MIN.wrapping_sub(1), i16::MAX);
16 assert_eq!( i32::MIN.wrapping_sub(1), i32::MAX);
17 assert_eq!( i64::MIN.wrapping_sub(1), i64::MAX);
18 assert_eq!(isize::MIN.wrapping_sub(1), isize::MAX);
20 assert_eq!( u8::MAX.wrapping_add(1), u8::MIN);
21 assert_eq!( u16::MAX.wrapping_add(1), u16::MIN);
22 assert_eq!( u32::MAX.wrapping_add(1), u32::MIN);
23 assert_eq!( u64::MAX.wrapping_add(1), u64::MIN);
24 assert_eq!(usize::MAX.wrapping_add(1), usize::MIN);
26 assert_eq!( u8::MIN.wrapping_sub(1), u8::MAX);
27 assert_eq!( u16::MIN.wrapping_sub(1), u16::MAX);
28 assert_eq!( u32::MIN.wrapping_sub(1), u32::MAX);
29 assert_eq!( u64::MIN.wrapping_sub(1), u64::MAX);
30 assert_eq!(usize::MIN.wrapping_sub(1), usize::MAX);
32 assert_eq!((0xfe_u8 as i8).wrapping_mul(16),
34 assert_eq!((0xfedc_u16 as i16).wrapping_mul(16),
36 assert_eq!((0xfedc_ba98_u32 as i32).wrapping_mul(16),
37 (0xedcb_a980_u32 as i32));
38 assert_eq!((0xfedc_ba98_7654_3217_u64 as i64).wrapping_mul(16),
39 (0xedcb_a987_6543_2170_u64 as i64));
42 #[cfg(target_pointer_width = "32")]
44 assert_eq!((0xfedc_ba98_u32 as isize).wrapping_mul(16),
45 (0xedcb_a980_u32 as isize));
47 #[cfg(target_pointer_width = "64")]
49 assert_eq!((0xfedc_ba98_7654_3217_u64 as isize).wrapping_mul(16),
50 (0xedcb_a987_6543_2170_u64 as isize));
54 assert_eq!((0xfe as u8).wrapping_mul(16),
56 assert_eq!((0xfedc as u16).wrapping_mul(16),
58 assert_eq!((0xfedc_ba98 as u32).wrapping_mul(16),
59 (0xedcb_a980 as u32));
60 assert_eq!((0xfedc_ba98_7654_3217 as u64).wrapping_mul(16),
61 (0xedcb_a987_6543_2170 as u64));
64 #[cfg(target_pointer_width = "32")]
66 assert_eq!((0xfedc_ba98 as usize).wrapping_mul(16),
67 (0xedcb_a980 as usize));
69 #[cfg(target_pointer_width = "64")]
71 assert_eq!((0xfedc_ba98_7654_3217 as usize).wrapping_mul(16),
72 (0xedcb_a987_6543_2170 as usize));
76 macro_rules! check_mul_no_wrap {
77 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_mul($f), ($e) * $f); }
79 macro_rules! check_mul_wraps {
80 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_mul($f), $e); }
83 check_mul_no_wrap!(0xfe_u8 as i8, -1);
84 check_mul_no_wrap!(0xfedc_u16 as i16, -1);
85 check_mul_no_wrap!(0xfedc_ba98_u32 as i32, -1);
86 check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
87 check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
89 check_mul_no_wrap!(0xfe_u8 as i8, -2);
90 check_mul_no_wrap!(0xfedc_u16 as i16, -2);
91 check_mul_no_wrap!(0xfedc_ba98_u32 as i32, -2);
92 check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
93 check_mul_no_wrap!(0xfedc_ba98_fedc_ba98_u64 as u64 as isize, -2);
95 check_mul_no_wrap!(0xfe_u8 as i8, 2);
96 check_mul_no_wrap!(0xfedc_u16 as i16, 2);
97 check_mul_no_wrap!(0xfedc_ba98_u32 as i32, 2);
98 check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
99 check_mul_no_wrap!(0xfedc_ba98_fedc_ba98_u64 as u64 as isize, 2);
101 check_mul_wraps!(0x80_u8 as i8, -1);
102 check_mul_wraps!(0x8000_u16 as i16, -1);
103 check_mul_wraps!(0x8000_0000_u32 as i32, -1);
104 check_mul_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
106 #[cfg(target_pointer_width = "32")]
108 check_mul_wraps!(0x8000_0000_u32 as isize, -1);
110 #[cfg(target_pointer_width = "64")]
112 check_mul_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
116 macro_rules! check_div_no_wrap {
117 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_div($f), ($e) / $f); }
119 macro_rules! check_div_wraps {
120 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_div($f), $e); }
123 check_div_no_wrap!(0xfe_u8 as i8, -1);
124 check_div_no_wrap!(0xfedc_u16 as i16, -1);
125 check_div_no_wrap!(0xfedc_ba98_u32 as i32, -1);
126 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
127 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
129 check_div_no_wrap!(0xfe_u8 as i8, -2);
130 check_div_no_wrap!(0xfedc_u16 as i16, -2);
131 check_div_no_wrap!(0xfedc_ba98_u32 as i32, -2);
132 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
133 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -2);
135 check_div_no_wrap!(0xfe_u8 as i8, 2);
136 check_div_no_wrap!(0xfedc_u16 as i16, 2);
137 check_div_no_wrap!(0xfedc_ba98_u32 as i32, 2);
138 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
139 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, 2);
141 check_div_wraps!(-128 as i8, -1);
142 check_div_wraps!(0x8000_u16 as i16, -1);
143 check_div_wraps!(0x8000_0000_u32 as i32, -1);
144 check_div_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
146 #[cfg(target_pointer_width = "32")]
148 check_div_wraps!(0x8000_0000_u32 as isize, -1);
150 #[cfg(target_pointer_width = "64")]
152 check_div_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
157 macro_rules! check_rem_no_wrap {
158 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_rem($f), ($e) % $f); }
160 macro_rules! check_rem_wraps {
161 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_rem($f), 0); }
164 check_rem_no_wrap!(0xfe_u8 as i8, -1);
165 check_rem_no_wrap!(0xfedc_u16 as i16, -1);
166 check_rem_no_wrap!(0xfedc_ba98_u32 as i32, -1);
167 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
168 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
170 check_rem_no_wrap!(0xfe_u8 as i8, -2);
171 check_rem_no_wrap!(0xfedc_u16 as i16, -2);
172 check_rem_no_wrap!(0xfedc_ba98_u32 as i32, -2);
173 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
174 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -2);
176 check_rem_no_wrap!(0xfe_u8 as i8, 2);
177 check_rem_no_wrap!(0xfedc_u16 as i16, 2);
178 check_rem_no_wrap!(0xfedc_ba98_u32 as i32, 2);
179 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
180 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, 2);
182 check_rem_wraps!(0x80_u8 as i8, -1);
183 check_rem_wraps!(0x8000_u16 as i16, -1);
184 check_rem_wraps!(0x8000_0000_u32 as i32, -1);
185 check_rem_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
187 #[cfg(target_pointer_width = "32")]
189 check_rem_wraps!(0x8000_0000_u32 as isize, -1);
191 #[cfg(target_pointer_width = "64")]
193 check_rem_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
197 macro_rules! check_neg_no_wrap {
198 ($e:expr) => { assert_eq!(($e).wrapping_neg(), -($e)); }
200 macro_rules! check_neg_wraps {
201 ($e:expr) => { assert_eq!(($e).wrapping_neg(), ($e)); }
204 check_neg_no_wrap!(0xfe_u8 as i8);
205 check_neg_no_wrap!(0xfedc_u16 as i16);
206 check_neg_no_wrap!(0xfedc_ba98_u32 as i32);
207 check_neg_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64);
208 check_neg_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize);
210 check_neg_wraps!(0x80_u8 as i8);
211 check_neg_wraps!(0x8000_u16 as i16);
212 check_neg_wraps!(0x8000_0000_u32 as i32);
213 check_neg_wraps!(0x8000_0000_0000_0000_u64 as i64);
215 #[cfg(target_pointer_width = "32")]
217 check_neg_wraps!(0x8000_0000_u32 as isize);
219 #[cfg(target_pointer_width = "64")]
221 check_neg_wraps!(0x8000_0000_0000_0000_u64 as isize);