mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Test that Wrapping arithmetic ops are implemented for all int types
This commit is contained in:
parent
d65e272a9f
commit
0c5220dc36
@ -18,6 +18,8 @@ macro_rules! sh_impl_signed {
|
||||
}
|
||||
}
|
||||
}
|
||||
forward_ref_binop! { impl Shl, shl for Wrapping<$t>, $f,
|
||||
#[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }
|
||||
|
||||
#[stable(feature = "op_assign_traits", since = "1.8.0")]
|
||||
impl ShlAssign<$f> for Wrapping<$t> {
|
||||
@ -41,6 +43,8 @@ macro_rules! sh_impl_signed {
|
||||
}
|
||||
}
|
||||
}
|
||||
forward_ref_binop! { impl Shr, shr for Wrapping<$t>, $f,
|
||||
#[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }
|
||||
|
||||
#[stable(feature = "op_assign_traits", since = "1.8.0")]
|
||||
impl ShrAssign<$f> for Wrapping<$t> {
|
||||
@ -64,6 +68,8 @@ macro_rules! sh_impl_unsigned {
|
||||
Wrapping(self.0.wrapping_shl((other & self::shift_max::$t as $f) as u32))
|
||||
}
|
||||
}
|
||||
forward_ref_binop! { impl Shl, shl for Wrapping<$t>, $f,
|
||||
#[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }
|
||||
|
||||
#[stable(feature = "op_assign_traits", since = "1.8.0")]
|
||||
impl ShlAssign<$f> for Wrapping<$t> {
|
||||
@ -83,6 +89,8 @@ macro_rules! sh_impl_unsigned {
|
||||
Wrapping(self.0.wrapping_shr((other & self::shift_max::$t as $f) as u32))
|
||||
}
|
||||
}
|
||||
forward_ref_binop! { impl Shr, shr for Wrapping<$t>, $f,
|
||||
#[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }
|
||||
|
||||
#[stable(feature = "op_assign_traits", since = "1.8.0")]
|
||||
impl ShrAssign<$f> for Wrapping<$t> {
|
||||
|
73
src/test/ui/wrapping-int-combinations.rs
Normal file
73
src/test/ui/wrapping-int-combinations.rs
Normal file
@ -0,0 +1,73 @@
|
||||
// run-pass
|
||||
|
||||
use std::num::Wrapping;
|
||||
|
||||
macro_rules! wrapping_operation {
|
||||
($result:expr, $lhs:ident $op:tt $rhs:expr) => {
|
||||
assert_eq!($result, $lhs $op $rhs);
|
||||
assert_eq!($result, &$lhs $op $rhs);
|
||||
assert_eq!($result, $lhs $op &$rhs);
|
||||
assert_eq!($result, &$lhs $op &$rhs);
|
||||
};
|
||||
($result:expr, $op:tt $expr:expr) => {
|
||||
assert_eq!($result, $op $expr);
|
||||
assert_eq!($result, $op &$expr);
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! wrapping_assignment {
|
||||
($result:expr, $lhs:ident $op:tt $rhs:expr) => {
|
||||
let mut lhs1 = $lhs;
|
||||
lhs1 $op $rhs;
|
||||
assert_eq!($result, lhs1);
|
||||
|
||||
let mut lhs2 = $lhs;
|
||||
lhs2 $op &$rhs;
|
||||
assert_eq!($result, lhs2);
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! wrapping_test {
|
||||
($type:ty, $min:expr, $max:expr) => {
|
||||
let zero: Wrapping<$type> = Wrapping(0);
|
||||
let one: Wrapping<$type> = Wrapping(1);
|
||||
let min: Wrapping<$type> = Wrapping($min);
|
||||
let max: Wrapping<$type> = Wrapping($max);
|
||||
|
||||
wrapping_operation!(min, max + one);
|
||||
wrapping_assignment!(min, max += one);
|
||||
wrapping_operation!(max, min - one);
|
||||
wrapping_assignment!(max, min -= one);
|
||||
wrapping_operation!(max, max * one);
|
||||
wrapping_assignment!(max, max *= one);
|
||||
wrapping_operation!(max, max / one);
|
||||
wrapping_assignment!(max, max /= one);
|
||||
wrapping_operation!(zero, max % one);
|
||||
wrapping_assignment!(zero, max %= one);
|
||||
wrapping_operation!(zero, zero & max);
|
||||
wrapping_assignment!(zero, zero &= max);
|
||||
wrapping_operation!(max, zero | max);
|
||||
wrapping_assignment!(max, zero |= max);
|
||||
wrapping_operation!(zero, max ^ max);
|
||||
wrapping_assignment!(zero, max ^= max);
|
||||
wrapping_operation!(zero, zero << 1usize);
|
||||
wrapping_assignment!(zero, zero <<= 1usize);
|
||||
wrapping_operation!(zero, zero >> 1usize);
|
||||
wrapping_assignment!(zero, zero >>= 1usize);
|
||||
wrapping_operation!(zero, -zero);
|
||||
wrapping_operation!(max, !min);
|
||||
};
|
||||
}
|
||||
|
||||
fn main() {
|
||||
wrapping_test!(i8, std::i8::MIN, std::i8::MAX);
|
||||
wrapping_test!(i16, std::i16::MIN, std::i16::MAX);
|
||||
wrapping_test!(i32, std::i32::MIN, std::i32::MAX);
|
||||
wrapping_test!(i64, std::i64::MIN, std::i64::MAX);
|
||||
wrapping_test!(i128, std::i128::MIN, std::i128::MAX);
|
||||
wrapping_test!(u8, std::u8::MIN, std::u8::MAX);
|
||||
wrapping_test!(u16, std::u16::MIN, std::u16::MAX);
|
||||
wrapping_test!(u32, std::u32::MIN, std::u32::MAX);
|
||||
wrapping_test!(u64, std::u64::MIN, std::u64::MAX);
|
||||
wrapping_test!(u128, std::u128::MIN, std::u128::MAX);
|
||||
}
|
Loading…
Reference in New Issue
Block a user