]>
git.proxmox.com Git - rustc.git/blob - library/core/tests/num/wrapping.rs
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
);
78 // Don't warn about overflowing ops on 32-bit platforms
79 #[cfg_attr(target_pointer_width = "32", allow(const_err))]
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
);
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
);
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
);
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
);
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));
110 (0xfedc_ba98_7654_3217_u64 as i64).wrapping_mul(16),
111 (0xedcb_a987_6543_2170_u64 as i64)
115 #[cfg(target_pointer_width = "32")]
117 assert_eq
!((0xfedc_ba98_u32 as isize).wrapping_mul(16), (0xedcb_a980_u32 as isize));
119 #[cfg(target_pointer_width = "64")]
122 (0xfedc_ba98_7654_3217_u64 as isize).wrapping_mul(16),
123 (0xedcb_a987_6543_2170_u64 as isize)
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));
134 #[cfg(target_pointer_width = "32")]
136 assert_eq
!((0xfedc_ba98 as usize).wrapping_mul(16), (0xedcb_a980 as usize));
138 #[cfg(target_pointer_width = "64")]
141 (0xfedc_ba98_7654_3217 as usize).wrapping_mul(16),
142 (0xedcb_a987_6543_2170 as usize)
147 macro_rules
! check_mul_no_wrap
{
148 ($e
:expr
, $f
:expr
) => {
149 assert_eq
!(($e
).wrapping_mul($f
), ($e
) * $f
);
152 macro_rules
! check_mul_wraps
{
153 ($e
:expr
, $f
:expr
) => {
154 assert_eq
!(($e
).wrapping_mul($f
), $e
);
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);
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);
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);
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);
181 #[cfg(target_pointer_width = "32")]
183 check_mul_wraps
!(0x8000_0000_u32 as isize, -1);
185 #[cfg(target_pointer_width = "64")]
187 check_mul_wraps
!(0x8000_0000_0000_0000_u64 as isize, -1);
191 macro_rules
! check_div_no_wrap
{
192 ($e
:expr
, $f
:expr
) => {
193 assert_eq
!(($e
).wrapping_div($f
), ($e
) / $f
);
196 macro_rules
! check_div_wraps
{
197 ($e
:expr
, $f
:expr
) => {
198 assert_eq
!(($e
).wrapping_div($f
), $e
);
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);
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);
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);
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);
225 #[cfg(target_pointer_width = "32")]
227 check_div_wraps
!(0x8000_0000_u32 as isize, -1);
229 #[cfg(target_pointer_width = "64")]
231 check_div_wraps
!(0x8000_0000_0000_0000_u64 as isize, -1);
235 macro_rules
! check_rem_no_wrap
{
236 ($e
:expr
, $f
:expr
) => {
237 assert_eq
!(($e
).wrapping_rem($f
), ($e
) % $f
);
240 macro_rules
! check_rem_wraps
{
241 ($e
:expr
, $f
:expr
) => {
242 assert_eq
!(($e
).wrapping_rem($f
), 0);
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);
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);
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);
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);
269 #[cfg(target_pointer_width = "32")]
271 check_rem_wraps
!(0x8000_0000_u32 as isize, -1);
273 #[cfg(target_pointer_width = "64")]
275 check_rem_wraps
!(0x8000_0000_0000_0000_u64 as isize, -1);
279 macro_rules
! check_neg_no_wrap
{
281 assert_eq
!(($e
).wrapping_neg(), -($e
));
284 macro_rules
! check_neg_wraps
{
286 assert_eq
!(($e
).wrapping_neg(), ($e
));
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);
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);
301 #[cfg(target_pointer_width = "32")]
303 check_neg_wraps
!(0x8000_0000_u32 as isize);
305 #[cfg(target_pointer_width = "64")]
307 check_neg_wraps
!(0x8000_0000_0000_0000_u64 as isize);