]> git.lizzy.rs Git - rust.git/blob - library/core/tests/num/ops.rs
Rollup merge of #106622 - estebank:issue-68972, r=davidtwco
[rust.git] / library / core / tests / num / ops.rs
1 use core::ops::*;
2
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) => {
8         let lhs = $lhs as $lt;
9         let rhs = $rhs as $rt;
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));
14     };
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);
20
21         let mut lhs = $lhs as $lt;
22         $op::$method(&mut lhs, &rhs);
23         assert_eq!($result as $lt, lhs);
24     };
25 }
26
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);
34     )+};
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);
37     )+};
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));
42     )+};
43 }
44
45 macro_rules! test_op {
46     ($fn_name:ident, $op:ident::$method:ident($lhs:literal), $result:literal, $($t:ty),+) => {
47         #[test]
48         fn $fn_name() {
49             impls_defined!($op, $method($lhs), $result, $($t),+);
50         }
51     };
52 }
53
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);
57
58 test_op!(test_not_defined_bool, Not::not(true), false, bool);
59
60 macro_rules! test_arith_op {
61     ($fn_name:ident, $op:ident::$method:ident($lhs:literal, $rhs:literal)) => {
62         #[test]
63         fn $fn_name() {
64             impls_defined!(
65                 $op,
66                 $method($lhs, $rhs),
67                 0,
68                 i8,
69                 i16,
70                 i32,
71                 i64,
72                 isize,
73                 u8,
74                 u16,
75                 u32,
76                 u64,
77                 usize,
78                 f32,
79                 f64
80             );
81             #[cfg(not(target_os = "emscripten"))]
82             impls_defined!($op, $method($lhs, $rhs), 0, i128, u128);
83         }
84     };
85     ($fn_name:ident, $op:ident::$method:ident(&mut $lhs:literal, $rhs:literal)) => {
86         #[test]
87         fn $fn_name() {
88             impls_defined!(
89                 $op,
90                 $method(&mut $lhs, $rhs),
91                 0,
92                 i8,
93                 i16,
94                 i32,
95                 i64,
96                 isize,
97                 u8,
98                 u16,
99                 u32,
100                 u64,
101                 usize,
102                 f32,
103                 f64
104             );
105             #[cfg(not(target_os = "emscripten"))]
106             impls_defined!($op, $method(&mut $lhs, $rhs), 0, i128, u128);
107         }
108     };
109 }
110
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));
121
122 macro_rules! test_bitop {
123     ($test_name:ident, $op:ident::$method:ident) => {
124         #[test]
125         fn $test_name() {
126             impls_defined!(
127                 $op,
128                 $method(0, 0),
129                 0,
130                 i8,
131                 i16,
132                 i32,
133                 i64,
134                 isize,
135                 u8,
136                 u16,
137                 u32,
138                 u64,
139                 usize
140             );
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);
144         }
145     };
146 }
147 macro_rules! test_bitop_assign {
148     ($test_name:ident, $op:ident::$method:ident) => {
149         #[test]
150         fn $test_name() {
151             impls_defined!(
152                 $op,
153                 $method(&mut 0, 0),
154                 0,
155                 i8,
156                 i16,
157                 i32,
158                 i64,
159                 isize,
160                 u8,
161                 u16,
162                 u32,
163                 u64,
164                 usize
165             );
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);
169         }
170     };
171 }
172
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);
179
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);)+
183     };
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);
188     };
189 }
190
191 macro_rules! test_shift {
192     ($op:ident::$method:ident, $($lt:ty),+) => {
193         $(test_shift_inner!($op::$method, $lt);)+
194     };
195     ($test_name:ident, $op:ident::$method:ident) => {
196         #[test]
197         fn $test_name() {
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);
201         }
202     };
203 }
204
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);)+
208     };
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);
213     };
214 }
215
216 macro_rules! test_shift_assign {
217     ($op:ident::$method:ident, $($lt:ty),+) => {
218         $(test_shift_assign_inner!($op::$method, $lt);)+
219     };
220     ($test_name:ident, $op:ident::$method:ident) => {
221         #[test]
222         fn $test_name() {
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);
226         }
227     };
228 }
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);