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))]
7 use std::{i8, i16, i32, i64, isize};
8 use std::{u8, u16, u32, u64, usize};
11 assert_eq!( i8::MAX.wrapping_add(1), i8::MIN);
12 assert_eq!( i16::MAX.wrapping_add(1), i16::MIN);
13 assert_eq!( i32::MAX.wrapping_add(1), i32::MIN);
14 assert_eq!( i64::MAX.wrapping_add(1), i64::MIN);
15 assert_eq!(isize::MAX.wrapping_add(1), isize::MIN);
17 assert_eq!( i8::MIN.wrapping_sub(1), i8::MAX);
18 assert_eq!( i16::MIN.wrapping_sub(1), i16::MAX);
19 assert_eq!( i32::MIN.wrapping_sub(1), i32::MAX);
20 assert_eq!( i64::MIN.wrapping_sub(1), i64::MAX);
21 assert_eq!(isize::MIN.wrapping_sub(1), isize::MAX);
23 assert_eq!( u8::MAX.wrapping_add(1), u8::MIN);
24 assert_eq!( u16::MAX.wrapping_add(1), u16::MIN);
25 assert_eq!( u32::MAX.wrapping_add(1), u32::MIN);
26 assert_eq!( u64::MAX.wrapping_add(1), u64::MIN);
27 assert_eq!(usize::MAX.wrapping_add(1), usize::MIN);
29 assert_eq!( u8::MIN.wrapping_sub(1), u8::MAX);
30 assert_eq!( u16::MIN.wrapping_sub(1), u16::MAX);
31 assert_eq!( u32::MIN.wrapping_sub(1), u32::MAX);
32 assert_eq!( u64::MIN.wrapping_sub(1), u64::MAX);
33 assert_eq!(usize::MIN.wrapping_sub(1), usize::MAX);
35 assert_eq!((0xfe_u8 as i8).wrapping_mul(16),
37 assert_eq!((0xfedc_u16 as i16).wrapping_mul(16),
39 assert_eq!((0xfedc_ba98_u32 as i32).wrapping_mul(16),
40 (0xedcb_a980_u32 as i32));
41 assert_eq!((0xfedc_ba98_7654_3217_u64 as i64).wrapping_mul(16),
42 (0xedcb_a987_6543_2170_u64 as i64));
45 #[cfg(target_pointer_width = "32")]
47 assert_eq!((0xfedc_ba98_u32 as isize).wrapping_mul(16),
48 (0xedcb_a980_u32 as isize));
50 #[cfg(target_pointer_width = "64")]
52 assert_eq!((0xfedc_ba98_7654_3217_u64 as isize).wrapping_mul(16),
53 (0xedcb_a987_6543_2170_u64 as isize));
57 assert_eq!((0xfe as u8).wrapping_mul(16),
59 assert_eq!((0xfedc as u16).wrapping_mul(16),
61 assert_eq!((0xfedc_ba98 as u32).wrapping_mul(16),
62 (0xedcb_a980 as u32));
63 assert_eq!((0xfedc_ba98_7654_3217 as u64).wrapping_mul(16),
64 (0xedcb_a987_6543_2170 as u64));
67 #[cfg(target_pointer_width = "32")]
69 assert_eq!((0xfedc_ba98 as usize).wrapping_mul(16),
70 (0xedcb_a980 as usize));
72 #[cfg(target_pointer_width = "64")]
74 assert_eq!((0xfedc_ba98_7654_3217 as usize).wrapping_mul(16),
75 (0xedcb_a987_6543_2170 as usize));
79 macro_rules! check_mul_no_wrap {
80 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_mul($f), ($e) * $f); }
82 macro_rules! check_mul_wraps {
83 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_mul($f), $e); }
86 check_mul_no_wrap!(0xfe_u8 as i8, -1);
87 check_mul_no_wrap!(0xfedc_u16 as i16, -1);
88 check_mul_no_wrap!(0xfedc_ba98_u32 as i32, -1);
89 check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
90 check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
92 check_mul_no_wrap!(0xfe_u8 as i8, -2);
93 check_mul_no_wrap!(0xfedc_u16 as i16, -2);
94 check_mul_no_wrap!(0xfedc_ba98_u32 as i32, -2);
95 check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
96 check_mul_no_wrap!(0xfedc_ba98_fedc_ba98_u64 as u64 as isize, -2);
98 check_mul_no_wrap!(0xfe_u8 as i8, 2);
99 check_mul_no_wrap!(0xfedc_u16 as i16, 2);
100 check_mul_no_wrap!(0xfedc_ba98_u32 as i32, 2);
101 check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
102 check_mul_no_wrap!(0xfedc_ba98_fedc_ba98_u64 as u64 as isize, 2);
104 check_mul_wraps!(0x80_u8 as i8, -1);
105 check_mul_wraps!(0x8000_u16 as i16, -1);
106 check_mul_wraps!(0x8000_0000_u32 as i32, -1);
107 check_mul_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
109 #[cfg(target_pointer_width = "32")]
111 check_mul_wraps!(0x8000_0000_u32 as isize, -1);
113 #[cfg(target_pointer_width = "64")]
115 check_mul_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
119 macro_rules! check_div_no_wrap {
120 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_div($f), ($e) / $f); }
122 macro_rules! check_div_wraps {
123 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_div($f), $e); }
126 check_div_no_wrap!(0xfe_u8 as i8, -1);
127 check_div_no_wrap!(0xfedc_u16 as i16, -1);
128 check_div_no_wrap!(0xfedc_ba98_u32 as i32, -1);
129 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
130 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
132 check_div_no_wrap!(0xfe_u8 as i8, -2);
133 check_div_no_wrap!(0xfedc_u16 as i16, -2);
134 check_div_no_wrap!(0xfedc_ba98_u32 as i32, -2);
135 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
136 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -2);
138 check_div_no_wrap!(0xfe_u8 as i8, 2);
139 check_div_no_wrap!(0xfedc_u16 as i16, 2);
140 check_div_no_wrap!(0xfedc_ba98_u32 as i32, 2);
141 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
142 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, 2);
144 check_div_wraps!(-128 as i8, -1);
145 check_div_wraps!(0x8000_u16 as i16, -1);
146 check_div_wraps!(0x8000_0000_u32 as i32, -1);
147 check_div_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
149 #[cfg(target_pointer_width = "32")]
151 check_div_wraps!(0x8000_0000_u32 as isize, -1);
153 #[cfg(target_pointer_width = "64")]
155 check_div_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
160 macro_rules! check_rem_no_wrap {
161 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_rem($f), ($e) % $f); }
163 macro_rules! check_rem_wraps {
164 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_rem($f), 0); }
167 check_rem_no_wrap!(0xfe_u8 as i8, -1);
168 check_rem_no_wrap!(0xfedc_u16 as i16, -1);
169 check_rem_no_wrap!(0xfedc_ba98_u32 as i32, -1);
170 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
171 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
173 check_rem_no_wrap!(0xfe_u8 as i8, -2);
174 check_rem_no_wrap!(0xfedc_u16 as i16, -2);
175 check_rem_no_wrap!(0xfedc_ba98_u32 as i32, -2);
176 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
177 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -2);
179 check_rem_no_wrap!(0xfe_u8 as i8, 2);
180 check_rem_no_wrap!(0xfedc_u16 as i16, 2);
181 check_rem_no_wrap!(0xfedc_ba98_u32 as i32, 2);
182 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
183 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, 2);
185 check_rem_wraps!(0x80_u8 as i8, -1);
186 check_rem_wraps!(0x8000_u16 as i16, -1);
187 check_rem_wraps!(0x8000_0000_u32 as i32, -1);
188 check_rem_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
190 #[cfg(target_pointer_width = "32")]
192 check_rem_wraps!(0x8000_0000_u32 as isize, -1);
194 #[cfg(target_pointer_width = "64")]
196 check_rem_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
200 macro_rules! check_neg_no_wrap {
201 ($e:expr) => { assert_eq!(($e).wrapping_neg(), -($e)); }
203 macro_rules! check_neg_wraps {
204 ($e:expr) => { assert_eq!(($e).wrapping_neg(), ($e)); }
207 check_neg_no_wrap!(0xfe_u8 as i8);
208 check_neg_no_wrap!(0xfedc_u16 as i16);
209 check_neg_no_wrap!(0xfedc_ba98_u32 as i32);
210 check_neg_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64);
211 check_neg_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize);
213 check_neg_wraps!(0x80_u8 as i8);
214 check_neg_wraps!(0x8000_u16 as i16);
215 check_neg_wraps!(0x8000_0000_u32 as i32);
216 check_neg_wraps!(0x8000_0000_0000_0000_u64 as i64);
218 #[cfg(target_pointer_width = "32")]
220 check_neg_wraps!(0x8000_0000_u32 as isize);
222 #[cfg(target_pointer_width = "64")]
224 check_neg_wraps!(0x8000_0000_0000_0000_u64 as isize);