1 // Test inherent wrapping_* methods for {i,u}{size,8,16,32,64}.
3 use std::{i8, i16, i32, i64, isize};
4 use std::{u8, u16, u32, u64, usize};
7 assert_eq!( i8::MAX.wrapping_add(1), i8::MIN);
8 assert_eq!( i16::MAX.wrapping_add(1), i16::MIN);
9 assert_eq!( i32::MAX.wrapping_add(1), i32::MIN);
10 assert_eq!( i64::MAX.wrapping_add(1), i64::MIN);
11 assert_eq!(isize::MAX.wrapping_add(1), isize::MIN);
13 assert_eq!( i8::MIN.wrapping_sub(1), i8::MAX);
14 assert_eq!( i16::MIN.wrapping_sub(1), i16::MAX);
15 assert_eq!( i32::MIN.wrapping_sub(1), i32::MAX);
16 assert_eq!( i64::MIN.wrapping_sub(1), i64::MAX);
17 assert_eq!(isize::MIN.wrapping_sub(1), isize::MAX);
19 assert_eq!( u8::MAX.wrapping_add(1), u8::MIN);
20 assert_eq!( u16::MAX.wrapping_add(1), u16::MIN);
21 assert_eq!( u32::MAX.wrapping_add(1), u32::MIN);
22 assert_eq!( u64::MAX.wrapping_add(1), u64::MIN);
23 assert_eq!(usize::MAX.wrapping_add(1), usize::MIN);
25 assert_eq!( u8::MIN.wrapping_sub(1), u8::MAX);
26 assert_eq!( u16::MIN.wrapping_sub(1), u16::MAX);
27 assert_eq!( u32::MIN.wrapping_sub(1), u32::MAX);
28 assert_eq!( u64::MIN.wrapping_sub(1), u64::MAX);
29 assert_eq!(usize::MIN.wrapping_sub(1), usize::MAX);
31 assert_eq!((0xfe_u8 as i8).wrapping_mul(16),
33 assert_eq!((0xfedc_u16 as i16).wrapping_mul(16),
35 assert_eq!((0xfedc_ba98_u32 as i32).wrapping_mul(16),
36 (0xedcb_a980_u32 as i32));
37 assert_eq!((0xfedc_ba98_7654_3217_u64 as i64).wrapping_mul(16),
38 (0xedcb_a987_6543_2170_u64 as i64));
41 #[cfg(target_pointer_width = "32")]
43 assert_eq!((0xfedc_ba98_u32 as isize).wrapping_mul(16),
44 (0xedcb_a980_u32 as isize));
46 #[cfg(target_pointer_width = "64")]
48 assert_eq!((0xfedc_ba98_7654_3217_u64 as isize).wrapping_mul(16),
49 (0xedcb_a987_6543_2170_u64 as isize));
53 assert_eq!((0xfe as u8).wrapping_mul(16),
55 assert_eq!((0xfedc as u16).wrapping_mul(16),
57 assert_eq!((0xfedc_ba98 as u32).wrapping_mul(16),
58 (0xedcb_a980 as u32));
59 assert_eq!((0xfedc_ba98_7654_3217 as u64).wrapping_mul(16),
60 (0xedcb_a987_6543_2170 as u64));
63 #[cfg(target_pointer_width = "32")]
65 assert_eq!((0xfedc_ba98 as usize).wrapping_mul(16),
66 (0xedcb_a980 as usize));
68 #[cfg(target_pointer_width = "64")]
70 assert_eq!((0xfedc_ba98_7654_3217 as usize).wrapping_mul(16),
71 (0xedcb_a987_6543_2170 as usize));
75 macro_rules! check_mul_no_wrap {
76 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_mul($f), ($e) * $f); }
78 macro_rules! check_mul_wraps {
79 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_mul($f), $e); }
82 check_mul_no_wrap!(0xfe_u8 as i8, -1);
83 check_mul_no_wrap!(0xfedc_u16 as i16, -1);
84 check_mul_no_wrap!(0xfedc_ba98_u32 as i32, -1);
85 check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
86 check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
88 check_mul_no_wrap!(0xfe_u8 as i8, -2);
89 check_mul_no_wrap!(0xfedc_u16 as i16, -2);
90 check_mul_no_wrap!(0xfedc_ba98_u32 as i32, -2);
91 check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
92 check_mul_no_wrap!(0xfedc_ba98_fedc_ba98_u64 as u64 as isize, -2);
94 check_mul_no_wrap!(0xfe_u8 as i8, 2);
95 check_mul_no_wrap!(0xfedc_u16 as i16, 2);
96 check_mul_no_wrap!(0xfedc_ba98_u32 as i32, 2);
97 check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
98 check_mul_no_wrap!(0xfedc_ba98_fedc_ba98_u64 as u64 as isize, 2);
100 check_mul_wraps!(0x80_u8 as i8, -1);
101 check_mul_wraps!(0x8000_u16 as i16, -1);
102 check_mul_wraps!(0x8000_0000_u32 as i32, -1);
103 check_mul_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
105 #[cfg(target_pointer_width = "32")]
107 check_mul_wraps!(0x8000_0000_u32 as isize, -1);
109 #[cfg(target_pointer_width = "64")]
111 check_mul_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
115 macro_rules! check_div_no_wrap {
116 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_div($f), ($e) / $f); }
118 macro_rules! check_div_wraps {
119 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_div($f), $e); }
122 check_div_no_wrap!(0xfe_u8 as i8, -1);
123 check_div_no_wrap!(0xfedc_u16 as i16, -1);
124 check_div_no_wrap!(0xfedc_ba98_u32 as i32, -1);
125 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
126 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
128 check_div_no_wrap!(0xfe_u8 as i8, -2);
129 check_div_no_wrap!(0xfedc_u16 as i16, -2);
130 check_div_no_wrap!(0xfedc_ba98_u32 as i32, -2);
131 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
132 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -2);
134 check_div_no_wrap!(0xfe_u8 as i8, 2);
135 check_div_no_wrap!(0xfedc_u16 as i16, 2);
136 check_div_no_wrap!(0xfedc_ba98_u32 as i32, 2);
137 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
138 check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, 2);
140 check_div_wraps!(-128 as i8, -1);
141 check_div_wraps!(0x8000_u16 as i16, -1);
142 check_div_wraps!(0x8000_0000_u32 as i32, -1);
143 check_div_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
145 #[cfg(target_pointer_width = "32")]
147 check_div_wraps!(0x8000_0000_u32 as isize, -1);
149 #[cfg(target_pointer_width = "64")]
151 check_div_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
156 macro_rules! check_rem_no_wrap {
157 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_rem($f), ($e) % $f); }
159 macro_rules! check_rem_wraps {
160 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_rem($f), 0); }
163 check_rem_no_wrap!(0xfe_u8 as i8, -1);
164 check_rem_no_wrap!(0xfedc_u16 as i16, -1);
165 check_rem_no_wrap!(0xfedc_ba98_u32 as i32, -1);
166 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
167 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
169 check_rem_no_wrap!(0xfe_u8 as i8, -2);
170 check_rem_no_wrap!(0xfedc_u16 as i16, -2);
171 check_rem_no_wrap!(0xfedc_ba98_u32 as i32, -2);
172 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
173 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -2);
175 check_rem_no_wrap!(0xfe_u8 as i8, 2);
176 check_rem_no_wrap!(0xfedc_u16 as i16, 2);
177 check_rem_no_wrap!(0xfedc_ba98_u32 as i32, 2);
178 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
179 check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, 2);
181 check_rem_wraps!(0x80_u8 as i8, -1);
182 check_rem_wraps!(0x8000_u16 as i16, -1);
183 check_rem_wraps!(0x8000_0000_u32 as i32, -1);
184 check_rem_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
186 #[cfg(target_pointer_width = "32")]
188 check_rem_wraps!(0x8000_0000_u32 as isize, -1);
190 #[cfg(target_pointer_width = "64")]
192 check_rem_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
196 macro_rules! check_neg_no_wrap {
197 ($e:expr) => { assert_eq!(($e).wrapping_neg(), -($e)); }
199 macro_rules! check_neg_wraps {
200 ($e:expr) => { assert_eq!(($e).wrapping_neg(), ($e)); }
203 check_neg_no_wrap!(0xfe_u8 as i8);
204 check_neg_no_wrap!(0xfedc_u16 as i16);
205 check_neg_no_wrap!(0xfedc_ba98_u32 as i32);
206 check_neg_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64);
207 check_neg_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize);
209 check_neg_wraps!(0x80_u8 as i8);
210 check_neg_wraps!(0x8000_u16 as i16);
211 check_neg_wraps!(0x8000_0000_u32 as i32);
212 check_neg_wraps!(0x8000_0000_0000_0000_u64 as i64);
214 #[cfg(target_pointer_width = "32")]
216 check_neg_wraps!(0x8000_0000_u32 as isize);
218 #[cfg(target_pointer_width = "64")]
220 check_neg_wraps!(0x8000_0000_0000_0000_u64 as isize);