Skip to content

Commit 9af1775

Browse files
committed
Auto merge of #63692 - iluuu1994:issue-49660, r=sfackler
Test that Wrapping arithmetic ops are implemented for all int types Closes #49660
2 parents fdaf594 + 55800ce commit 9af1775

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

src/libcore/num/wrapping.rs

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ macro_rules! sh_impl_signed {
1818
}
1919
}
2020
}
21+
forward_ref_binop! { impl Shl, shl for Wrapping<$t>, $f,
22+
#[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }
2123

2224
#[stable(feature = "op_assign_traits", since = "1.8.0")]
2325
impl ShlAssign<$f> for Wrapping<$t> {
@@ -41,6 +43,8 @@ macro_rules! sh_impl_signed {
4143
}
4244
}
4345
}
46+
forward_ref_binop! { impl Shr, shr for Wrapping<$t>, $f,
47+
#[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }
4448

4549
#[stable(feature = "op_assign_traits", since = "1.8.0")]
4650
impl ShrAssign<$f> for Wrapping<$t> {
@@ -64,6 +68,8 @@ macro_rules! sh_impl_unsigned {
6468
Wrapping(self.0.wrapping_shl((other & self::shift_max::$t as $f) as u32))
6569
}
6670
}
71+
forward_ref_binop! { impl Shl, shl for Wrapping<$t>, $f,
72+
#[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }
6773

6874
#[stable(feature = "op_assign_traits", since = "1.8.0")]
6975
impl ShlAssign<$f> for Wrapping<$t> {
@@ -83,6 +89,8 @@ macro_rules! sh_impl_unsigned {
8389
Wrapping(self.0.wrapping_shr((other & self::shift_max::$t as $f) as u32))
8490
}
8591
}
92+
forward_ref_binop! { impl Shr, shr for Wrapping<$t>, $f,
93+
#[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }
8694

8795
#[stable(feature = "op_assign_traits", since = "1.8.0")]
8896
impl ShrAssign<$f> for Wrapping<$t> {
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// run-pass
2+
3+
use std::num::Wrapping;
4+
5+
macro_rules! wrapping_operation {
6+
($result:expr, $lhs:ident $op:tt $rhs:expr) => {
7+
assert_eq!($result, $lhs $op $rhs);
8+
assert_eq!($result, &$lhs $op $rhs);
9+
assert_eq!($result, $lhs $op &$rhs);
10+
assert_eq!($result, &$lhs $op &$rhs);
11+
};
12+
($result:expr, $op:tt $expr:expr) => {
13+
assert_eq!($result, $op $expr);
14+
assert_eq!($result, $op &$expr);
15+
};
16+
}
17+
18+
macro_rules! wrapping_assignment {
19+
($result:expr, $lhs:ident $op:tt $rhs:expr) => {
20+
let mut lhs1 = $lhs;
21+
lhs1 $op $rhs;
22+
assert_eq!($result, lhs1);
23+
24+
let mut lhs2 = $lhs;
25+
lhs2 $op &$rhs;
26+
assert_eq!($result, lhs2);
27+
};
28+
}
29+
30+
macro_rules! wrapping_test {
31+
($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);
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+
fn main() {
63+
wrapping_test!(i8, std::i8::MIN, std::i8::MAX);
64+
wrapping_test!(i16, std::i16::MIN, std::i16::MAX);
65+
wrapping_test!(i32, std::i32::MIN, std::i32::MAX);
66+
wrapping_test!(i64, std::i64::MIN, std::i64::MAX);
67+
#[cfg(not(target_os = "emscripten"))]
68+
wrapping_test!(i128, std::i128::MIN, std::i128::MAX);
69+
wrapping_test!(isize, std::isize::MIN, std::isize::MAX);
70+
wrapping_test!(u8, std::u8::MIN, std::u8::MAX);
71+
wrapping_test!(u16, std::u16::MIN, std::u16::MAX);
72+
wrapping_test!(u32, std::u32::MIN, std::u32::MAX);
73+
wrapping_test!(u64, std::u64::MIN, std::u64::MAX);
74+
#[cfg(not(target_os = "emscripten"))]
75+
wrapping_test!(u128, std::u128::MIN, std::u128::MAX);
76+
wrapping_test!(usize, std::usize::MIN, std::usize::MAX);
77+
}

0 commit comments

Comments
 (0)