3 // For types L and R, checks that a trait implementation exists for
4 // * binary ops: L op R, L op &R, &L op R and &L op &R
5 // * assign ops: &mut L op R, &mut L op &R
6 macro_rules! impl_defined {
7 ($op:ident, $method:ident($lhs:literal, $rhs:literal), $result:literal, $lt:ty, $rt:ty) => {
10 assert_eq!($result as $lt, $op::$method(lhs, rhs));
11 assert_eq!($result as $lt, $op::$method(lhs, &rhs));
12 assert_eq!($result as $lt, $op::$method(&lhs, rhs));
13 assert_eq!($result as $lt, $op::$method(&lhs, &rhs));
15 ($op:ident, $method:ident(&mut $lhs:literal, $rhs:literal), $result:literal, $lt:ty, $rt:ty) => {
16 let rhs = $rhs as $rt;
17 let mut lhs = $lhs as $lt;
18 $op::$method(&mut lhs, rhs);
19 assert_eq!($result as $lt, lhs);
21 let mut lhs = $lhs as $lt;
22 $op::$method(&mut lhs, &rhs);
23 assert_eq!($result as $lt, lhs);
27 // For all specified types T, checks that a trait implementation exists for
28 // * binary ops: T op T, T op &T, &T op T and &T op &T
29 // * assign ops: &mut T op T, &mut T op &T
30 // * unary ops: op T and op &T
31 macro_rules! impls_defined {
32 ($op:ident, $method:ident($lhs:literal, $rhs:literal), $result:literal, $($t:ty),+) => {$(
33 impl_defined!($op, $method($lhs, $rhs), $result, $t, $t);
35 ($op:ident, $method:ident(&mut $lhs:literal, $rhs:literal), $result:literal, $($t:ty),+) => {$(
36 impl_defined!($op, $method(&mut $lhs, $rhs), $result, $t, $t);
38 ($op:ident, $method:ident($operand:literal), $result:literal, $($t:ty),+) => {$(
39 let operand = $operand as $t;
40 assert_eq!($result as $t, $op::$method(operand));
41 assert_eq!($result as $t, $op::$method(&operand));
45 macro_rules! test_op {
46 ($fn_name:ident, $op:ident::$method:ident($lhs:literal), $result:literal, $($t:ty),+) => {
49 impls_defined!($op, $method($lhs), $result, $($t),+);
54 test_op!(test_neg_defined, Neg::neg(0), 0, i8, i16, i32, i64, f32, f64);
55 #[cfg(not(target_os = "emscripten"))]
56 test_op!(test_neg_defined_128, Neg::neg(0), 0, i128);
58 test_op!(test_not_defined_bool, Not::not(true), false, bool);
60 macro_rules! test_arith_op {
61 ($fn_name:ident, $op:ident::$method:ident($lhs:literal, $rhs:literal)) => {
81 #[cfg(not(target_os = "emscripten"))]
82 impls_defined!($op, $method($lhs, $rhs), 0, i128, u128);
85 ($fn_name:ident, $op:ident::$method:ident(&mut $lhs:literal, $rhs:literal)) => {
90 $method(&mut $lhs, $rhs),
105 #[cfg(not(target_os = "emscripten"))]
106 impls_defined!($op, $method(&mut $lhs, $rhs), 0, i128, u128);
111 test_arith_op!(test_add_defined, Add::add(0, 0));
112 test_arith_op!(test_add_assign_defined, AddAssign::add_assign(&mut 0, 0));
113 test_arith_op!(test_sub_defined, Sub::sub(0, 0));
114 test_arith_op!(test_sub_assign_defined, SubAssign::sub_assign(&mut 0, 0));
115 test_arith_op!(test_mul_defined, Mul::mul(0, 0));
116 test_arith_op!(test_mul_assign_defined, MulAssign::mul_assign(&mut 0, 0));
117 test_arith_op!(test_div_defined, Div::div(0, 1));
118 test_arith_op!(test_div_assign_defined, DivAssign::div_assign(&mut 0, 1));
119 test_arith_op!(test_rem_defined, Rem::rem(0, 1));
120 test_arith_op!(test_rem_assign_defined, RemAssign::rem_assign(&mut 0, 1));
122 macro_rules! test_bitop {
123 ($test_name:ident, $op:ident::$method:ident) => {
141 #[cfg(not(target_os = "emscripten"))]
142 impls_defined!($op, $method(0, 0), 0, i128, u128);
143 impls_defined!($op, $method(false, false), false, bool);
147 macro_rules! test_bitop_assign {
148 ($test_name:ident, $op:ident::$method:ident) => {
166 #[cfg(not(target_os = "emscripten"))]
167 impls_defined!($op, $method(&mut 0, 0), 0, i128, u128);
168 impls_defined!($op, $method(&mut false, false), false, bool);
173 test_bitop!(test_bitand_defined, BitAnd::bitand);
174 test_bitop_assign!(test_bitand_assign_defined, BitAndAssign::bitand_assign);
175 test_bitop!(test_bitor_defined, BitOr::bitor);
176 test_bitop_assign!(test_bitor_assign_defined, BitOrAssign::bitor_assign);
177 test_bitop!(test_bitxor_defined, BitXor::bitxor);
178 test_bitop_assign!(test_bitxor_assign_defined, BitXorAssign::bitxor_assign);
180 macro_rules! test_shift_inner {
181 ($op:ident::$method:ident, $lt:ty, $($rt:ty),+) => {
182 $(impl_defined!($op, $method(0,0), 0, $lt, $rt);)+
184 ($op:ident::$method:ident, $lt:ty) => {
185 test_shift_inner!($op::$method, $lt, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);
186 #[cfg(not(target_os = "emscripten"))]
187 test_shift_inner!($op::$method, $lt, i128, u128);
191 macro_rules! test_shift {
192 ($op:ident::$method:ident, $($lt:ty),+) => {
193 $(test_shift_inner!($op::$method, $lt);)+
195 ($test_name:ident, $op:ident::$method:ident) => {
198 test_shift!($op::$method, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);
199 #[cfg(not(target_os = "emscripten"))]
200 test_shift!($op::$method, i128, u128);
205 macro_rules! test_shift_assign_inner {
206 ($op:ident::$method:ident, $lt:ty, $($rt:ty),+) => {
207 $(impl_defined!($op, $method(&mut 0,0), 0, $lt, $rt);)+
209 ($op:ident::$method:ident, $lt:ty) => {
210 test_shift_assign_inner!($op::$method, $lt, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);
211 #[cfg(not(target_os = "emscripten"))]
212 test_shift_assign_inner!($op::$method, $lt, i128, u128);
216 macro_rules! test_shift_assign {
217 ($op:ident::$method:ident, $($lt:ty),+) => {
218 $(test_shift_assign_inner!($op::$method, $lt);)+
220 ($test_name:ident, $op:ident::$method:ident) => {
223 test_shift_assign!($op::$method, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);
224 #[cfg(not(target_os = "emscripten"))]
225 test_shift_assign!($op::$method, i128, u128);
229 test_shift!(test_shl_defined, Shl::shl);
230 test_shift_assign!(test_shl_assign_defined, ShlAssign::shl_assign);
231 test_shift!(test_shr_defined, Shr::shr);
232 test_shift_assign!(test_shr_assign_defined, ShrAssign::shr_assign);