]> git.proxmox.com Git - rustc.git/blob - library/core/tests/num/wrapping.rs
New upstream version 1.51.0+dfsg1
[rustc.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 // Don't warn about overflowing ops on 32-bit platforms
79 #[cfg_attr(target_pointer_width = "32", allow(const_err))]
80 #[test]
81 fn wrapping_int_api() {
82 assert_eq!(i8::MAX.wrapping_add(1), i8::MIN);
83 assert_eq!(i16::MAX.wrapping_add(1), i16::MIN);
84 assert_eq!(i32::MAX.wrapping_add(1), i32::MIN);
85 assert_eq!(i64::MAX.wrapping_add(1), i64::MIN);
86 assert_eq!(isize::MAX.wrapping_add(1), isize::MIN);
87
88 assert_eq!(i8::MIN.wrapping_sub(1), i8::MAX);
89 assert_eq!(i16::MIN.wrapping_sub(1), i16::MAX);
90 assert_eq!(i32::MIN.wrapping_sub(1), i32::MAX);
91 assert_eq!(i64::MIN.wrapping_sub(1), i64::MAX);
92 assert_eq!(isize::MIN.wrapping_sub(1), isize::MAX);
93
94 assert_eq!(u8::MAX.wrapping_add(1), u8::MIN);
95 assert_eq!(u16::MAX.wrapping_add(1), u16::MIN);
96 assert_eq!(u32::MAX.wrapping_add(1), u32::MIN);
97 assert_eq!(u64::MAX.wrapping_add(1), u64::MIN);
98 assert_eq!(usize::MAX.wrapping_add(1), usize::MIN);
99
100 assert_eq!(u8::MIN.wrapping_sub(1), u8::MAX);
101 assert_eq!(u16::MIN.wrapping_sub(1), u16::MAX);
102 assert_eq!(u32::MIN.wrapping_sub(1), u32::MAX);
103 assert_eq!(u64::MIN.wrapping_sub(1), u64::MAX);
104 assert_eq!(usize::MIN.wrapping_sub(1), usize::MAX);
105
106 assert_eq!((0xfe_u8 as i8).wrapping_mul(16), (0xe0_u8 as i8));
107 assert_eq!((0xfedc_u16 as i16).wrapping_mul(16), (0xedc0_u16 as i16));
108 assert_eq!((0xfedc_ba98_u32 as i32).wrapping_mul(16), (0xedcb_a980_u32 as i32));
109 assert_eq!(
110 (0xfedc_ba98_7654_3217_u64 as i64).wrapping_mul(16),
111 (0xedcb_a987_6543_2170_u64 as i64)
112 );
113
114 match () {
115 #[cfg(target_pointer_width = "32")]
116 () => {
117 assert_eq!((0xfedc_ba98_u32 as isize).wrapping_mul(16), (0xedcb_a980_u32 as isize));
118 }
119 #[cfg(target_pointer_width = "64")]
120 () => {
121 assert_eq!(
122 (0xfedc_ba98_7654_3217_u64 as isize).wrapping_mul(16),
123 (0xedcb_a987_6543_2170_u64 as isize)
124 );
125 }
126 }
127
128 assert_eq!((0xfe as u8).wrapping_mul(16), (0xe0 as u8));
129 assert_eq!((0xfedc as u16).wrapping_mul(16), (0xedc0 as u16));
130 assert_eq!((0xfedc_ba98 as u32).wrapping_mul(16), (0xedcb_a980 as u32));
131 assert_eq!((0xfedc_ba98_7654_3217 as u64).wrapping_mul(16), (0xedcb_a987_6543_2170 as u64));
132
133 match () {
134 #[cfg(target_pointer_width = "32")]
135 () => {
136 assert_eq!((0xfedc_ba98 as usize).wrapping_mul(16), (0xedcb_a980 as usize));
137 }
138 #[cfg(target_pointer_width = "64")]
139 () => {
140 assert_eq!(
141 (0xfedc_ba98_7654_3217 as usize).wrapping_mul(16),
142 (0xedcb_a987_6543_2170 as usize)
143 );
144 }
145 }
146
147 macro_rules! check_mul_no_wrap {
148 ($e:expr, $f:expr) => {
149 assert_eq!(($e).wrapping_mul($f), ($e) * $f);
150 };
151 }
152 macro_rules! check_mul_wraps {
153 ($e:expr, $f:expr) => {
154 assert_eq!(($e).wrapping_mul($f), $e);
155 };
156 }
157
158 check_mul_no_wrap!(0xfe_u8 as i8, -1);
159 check_mul_no_wrap!(0xfedc_u16 as i16, -1);
160 check_mul_no_wrap!(0xfedc_ba98_u32 as i32, -1);
161 check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
162 check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
163
164 check_mul_no_wrap!(0xfe_u8 as i8, -2);
165 check_mul_no_wrap!(0xfedc_u16 as i16, -2);
166 check_mul_no_wrap!(0xfedc_ba98_u32 as i32, -2);
167 check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
168 check_mul_no_wrap!(0xfedc_ba98_fedc_ba98_u64 as u64 as isize, -2);
169
170 check_mul_no_wrap!(0xfe_u8 as i8, 2);
171 check_mul_no_wrap!(0xfedc_u16 as i16, 2);
172 check_mul_no_wrap!(0xfedc_ba98_u32 as i32, 2);
173 check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
174 check_mul_no_wrap!(0xfedc_ba98_fedc_ba98_u64 as u64 as isize, 2);
175
176 check_mul_wraps!(0x80_u8 as i8, -1);
177 check_mul_wraps!(0x8000_u16 as i16, -1);
178 check_mul_wraps!(0x8000_0000_u32 as i32, -1);
179 check_mul_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
180 match () {
181 #[cfg(target_pointer_width = "32")]
182 () => {
183 check_mul_wraps!(0x8000_0000_u32 as isize, -1);
184 }
185 #[cfg(target_pointer_width = "64")]
186 () => {
187 check_mul_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
188 }
189 }
190
191 macro_rules! check_div_no_wrap {
192 ($e:expr, $f:expr) => {
193 assert_eq!(($e).wrapping_div($f), ($e) / $f);
194 };
195 }
196 macro_rules! check_div_wraps {
197 ($e:expr, $f:expr) => {
198 assert_eq!(($e).wrapping_div($f), $e);
199 };
200 }
201
202 check_div_no_wrap!(0xfe_u8 as i8, -1);
203 check_div_no_wrap!(0xfedc_u16 as i16, -1);
204 check_div_no_wrap!(0xfedc_ba98_u32 as i32, -1);
205 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
206 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
207
208 check_div_no_wrap!(0xfe_u8 as i8, -2);
209 check_div_no_wrap!(0xfedc_u16 as i16, -2);
210 check_div_no_wrap!(0xfedc_ba98_u32 as i32, -2);
211 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
212 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -2);
213
214 check_div_no_wrap!(0xfe_u8 as i8, 2);
215 check_div_no_wrap!(0xfedc_u16 as i16, 2);
216 check_div_no_wrap!(0xfedc_ba98_u32 as i32, 2);
217 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
218 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, 2);
219
220 check_div_wraps!(-128 as i8, -1);
221 check_div_wraps!(0x8000_u16 as i16, -1);
222 check_div_wraps!(0x8000_0000_u32 as i32, -1);
223 check_div_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
224 match () {
225 #[cfg(target_pointer_width = "32")]
226 () => {
227 check_div_wraps!(0x8000_0000_u32 as isize, -1);
228 }
229 #[cfg(target_pointer_width = "64")]
230 () => {
231 check_div_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
232 }
233 }
234
235 macro_rules! check_rem_no_wrap {
236 ($e:expr, $f:expr) => {
237 assert_eq!(($e).wrapping_rem($f), ($e) % $f);
238 };
239 }
240 macro_rules! check_rem_wraps {
241 ($e:expr, $f:expr) => {
242 assert_eq!(($e).wrapping_rem($f), 0);
243 };
244 }
245
246 check_rem_no_wrap!(0xfe_u8 as i8, -1);
247 check_rem_no_wrap!(0xfedc_u16 as i16, -1);
248 check_rem_no_wrap!(0xfedc_ba98_u32 as i32, -1);
249 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
250 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
251
252 check_rem_no_wrap!(0xfe_u8 as i8, -2);
253 check_rem_no_wrap!(0xfedc_u16 as i16, -2);
254 check_rem_no_wrap!(0xfedc_ba98_u32 as i32, -2);
255 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
256 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -2);
257
258 check_rem_no_wrap!(0xfe_u8 as i8, 2);
259 check_rem_no_wrap!(0xfedc_u16 as i16, 2);
260 check_rem_no_wrap!(0xfedc_ba98_u32 as i32, 2);
261 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
262 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, 2);
263
264 check_rem_wraps!(0x80_u8 as i8, -1);
265 check_rem_wraps!(0x8000_u16 as i16, -1);
266 check_rem_wraps!(0x8000_0000_u32 as i32, -1);
267 check_rem_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
268 match () {
269 #[cfg(target_pointer_width = "32")]
270 () => {
271 check_rem_wraps!(0x8000_0000_u32 as isize, -1);
272 }
273 #[cfg(target_pointer_width = "64")]
274 () => {
275 check_rem_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
276 }
277 }
278
279 macro_rules! check_neg_no_wrap {
280 ($e:expr) => {
281 assert_eq!(($e).wrapping_neg(), -($e));
282 };
283 }
284 macro_rules! check_neg_wraps {
285 ($e:expr) => {
286 assert_eq!(($e).wrapping_neg(), ($e));
287 };
288 }
289
290 check_neg_no_wrap!(0xfe_u8 as i8);
291 check_neg_no_wrap!(0xfedc_u16 as i16);
292 check_neg_no_wrap!(0xfedc_ba98_u32 as i32);
293 check_neg_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64);
294 check_neg_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize);
295
296 check_neg_wraps!(0x80_u8 as i8);
297 check_neg_wraps!(0x8000_u16 as i16);
298 check_neg_wraps!(0x8000_0000_u32 as i32);
299 check_neg_wraps!(0x8000_0000_0000_0000_u64 as i64);
300 match () {
301 #[cfg(target_pointer_width = "32")]
302 () => {
303 check_neg_wraps!(0x8000_0000_u32 as isize);
304 }
305 #[cfg(target_pointer_width = "64")]
306 () => {
307 check_neg_wraps!(0x8000_0000_0000_0000_u64 as isize);
308 }
309 }
310 }