Debian Codegraph @

debian/missing-sources/derive_more/tests/mul.rs

raw
1 #![cfg_attr(not(feature = "std"), no_std)]
2 #![allow(dead_code)] // some code is tested for type checking only
3
4 #[cfg(not(feature = "std"))]
5 extern crate alloc;
6
7 mod structs {
8 mod scalar {
9 use derive_more::Mul;
10
11 #[test]
12 fn single_field_tuple() {
13 #[derive(Mul, Debug, PartialEq)]
14 struct MyInt(i32);
15
16 assert_eq!(MyInt(-1) * 5, MyInt(-5));
17 }
18
19 #[test]
20 fn multifield_field_tuple() {
21 #[derive(Mul, Debug, PartialEq)]
22 struct MyInts(i32, i32);
23
24 assert_eq!(MyInts(-1, 3) * 5, MyInts(-5, 15));
25 }
26
27 #[test]
28 fn single_field_struct() {
29 #[derive(Mul, Debug, PartialEq)]
30 struct Point1D {
31 x: i32,
32 }
33
34 assert_eq!(Point1D { x: -1 } * 5, Point1D { x: -5 });
35 }
36
37 #[test]
38 fn multi_field_struct() {
39 #[derive(Mul, Debug, PartialEq)]
40 struct Point2D {
41 x: i32,
42 y: i32,
43 }
44
45 assert_eq!(Point2D { x: -1, y: 3 } * 5, Point2D { x: -5, y: 15 });
46 }
47
48 mod generic {
49 use derive_more::Mul;
50
51 trait Some {
52 type Assoc;
53 }
54
55 impl<T> Some for T {
56 type Assoc = u32;
57 }
58
59 #[test]
60 fn multi_field_tuple() {
61 #[derive(Mul)]
62 struct Foo<A: Some, B>(A::Assoc, B);
63
64 let a = Foo::<(), _>(12, 21);
65 let res = a * 3;
66
67 assert_eq!((res.0, res.1), (36, 63));
68 }
69
70 #[test]
71 fn multi_field_struct() {
72 #[derive(Mul)]
73 struct Bar<A, B: Some> {
74 b: B::Assoc,
75 i: A,
76 }
77
78 let a = Bar::<_, ()> { b: 12, i: 21 };
79 let res = a * 5;
80
81 assert_eq!((res.b, res.i), (60, 105));
82 }
83 }
84
85 mod ignore {
86 use core::marker::PhantomData;
87
88 use derive_more::Mul;
89
90 #[test]
91 fn tuple() {
92 #[derive(Mul)]
93 struct TupleWithZst<T = ()>(i32, #[mul(ignore)] PhantomData<T>);
94
95 let a: TupleWithZst = TupleWithZst(12, PhantomData);
96
97 assert_eq!((a * 3).0, 36);
98 }
99
100 #[test]
101 fn struct_() {
102 #[derive(Mul)]
103 struct StructWithZst<T = ()> {
104 x: i32,
105 #[mul(skip)]
106 _marker: PhantomData<T>,
107 }
108
109 let a: StructWithZst<()> = StructWithZst {
110 x: 12,
111 _marker: PhantomData,
112 };
113
114 assert_eq!((a * -3).x, -36);
115 }
116 }
117 }
118
119 mod structural {
120 use derive_more::Mul;
121
122 #[test]
123 fn single_field_tuple() {
124 #[derive(Mul, Debug, PartialEq)]
125 #[mul(forward)]
126 struct MyInt(i32);
127
128 assert_eq!(MyInt(-1) * MyInt(5), MyInt(-5));
129 }
130
131 #[test]
132 fn multifield_field_tuple() {
133 #[derive(Mul, Debug, PartialEq)]
134 #[mul(forward)]
135 struct MyInts(i32, i32);
136
137 assert_eq!(MyInts(-1, 3) * MyInts(3, 5), MyInts(-3, 15));
138 }
139
140 #[test]
141 fn single_field_struct() {
142 #[derive(Mul, Debug, PartialEq)]
143 #[mul(forward)]
144 struct Point1D {
145 x: i32,
146 }
147
148 assert_eq!(Point1D { x: -1 } * Point1D { x: 5 }, Point1D { x: -5 });
149 }
150
151 #[test]
152 fn multi_field_struct() {
153 #[derive(Mul, Debug, PartialEq)]
154 #[mul(forward)]
155 struct Point2D {
156 x: i32,
157 y: i32,
158 }
159
160 assert_eq!(
161 Point2D { x: -1, y: 3 } * Point2D { x: 3, y: 5 },
162 Point2D { x: -3, y: 15 },
163 );
164 }
165
166 mod generic {
167 use derive_more::Mul;
168
169 trait Some {
170 type Assoc;
171 }
172
173 impl<T> Some for T {
174 type Assoc = u32;
175 }
176
177 #[test]
178 fn multi_field_tuple() {
179 #[derive(Mul)]
180 #[mul(forward)]
181 struct Foo<A: Some, B>(A::Assoc, B);
182
183 let a = Foo::<(), _>(12, 21);
184 let b = Foo::<(), _>(1, 2);
185 let res = a * b;
186
187 assert_eq!((res.0, res.1), (12, 42));
188 }
189
190 #[test]
191 fn multi_field_struct() {
192 #[derive(Mul)]
193 #[mul(forward)]
194 struct Bar<A, B: Some> {
195 b: B::Assoc,
196 i: A,
197 }
198
199 let a = Bar::<_, ()> { b: 12, i: 21 };
200 let b = Bar::<_, ()> { b: 1, i: 2 };
201 let res = a * b;
202
203 assert_eq!((res.b, res.i), (12, 42));
204 }
205 }
206
207 mod ignore {
208 use core::marker::PhantomData;
209
210 use derive_more::Mul;
211
212 #[test]
213 fn tuple() {
214 #[derive(Mul)]
215 #[mul(forward)]
216 struct TupleWithZst<T = ()>(i32, #[mul(ignore)] PhantomData<T>);
217
218 let a: TupleWithZst = TupleWithZst(12, PhantomData);
219 let b: TupleWithZst = TupleWithZst(2, PhantomData);
220
221 assert_eq!((a * b).0, 24);
222 }
223
224 #[test]
225 fn struct_() {
226 #[derive(Mul)]
227 #[mul(forward)]
228 struct StructWithZst<T = ()> {
229 x: i32,
230 #[mul(skip)]
231 _marker: PhantomData<T>,
232 }
233
234 let a: StructWithZst<()> = StructWithZst {
235 x: 12,
236 _marker: PhantomData,
237 };
238 let b: StructWithZst<()> = StructWithZst {
239 x: 2,
240 _marker: PhantomData,
241 };
242
243 assert_eq!((a * b).x, 24);
244 }
245 }
246 }
247 }
248
249 mod enums {
250 mod structural {
251 #[cfg(not(feature = "std"))]
252 use alloc::string::ToString as _;
253 use derive_more::Mul;
254
255 #[test]
256 fn empty() {
257 #[derive(Mul)]
258 #[mul(forward)]
259 enum Empty {}
260 }
261
262 #[test]
263 fn multi() {
264 #[derive(Mul, Debug, PartialEq)]
265 #[mul(forward)]
266 enum MixedInts {
267 SmallInt(i32),
268 BigInt(i64),
269 TwoSmallInts(i32, i32),
270 NamedSmallInts { x: i32, y: i32 },
271 UnsignedOne(u32),
272 UnsignedTwo(u32),
273 Unit,
274 }
275
276 assert_eq!(
277 (MixedInts::SmallInt(-1) * MixedInts::SmallInt(2)).unwrap(),
278 MixedInts::SmallInt(-2),
279 );
280 assert_eq!(
281 (MixedInts::BigInt(-1) * MixedInts::BigInt(2)).unwrap(),
282 MixedInts::BigInt(-2),
283 );
284 assert_eq!(
285 (MixedInts::TwoSmallInts(-1, 3) * MixedInts::TwoSmallInts(2, -5))
286 .unwrap(),
287 MixedInts::TwoSmallInts(-2, -15),
288 );
289 assert_eq!(
290 (MixedInts::NamedSmallInts { x: -1, y: 3 }
291 * MixedInts::NamedSmallInts { x: 2, y: -5 })
292 .unwrap(),
293 MixedInts::NamedSmallInts { x: -2, y: -15 },
294 );
295 assert_eq!(
296 (MixedInts::UnsignedOne(1) * MixedInts::UnsignedOne(2)).unwrap(),
297 MixedInts::UnsignedOne(2),
298 );
299 assert_eq!(
300 (MixedInts::UnsignedTwo(1) * MixedInts::UnsignedTwo(2)).unwrap(),
301 MixedInts::UnsignedTwo(2),
302 );
303
304 assert_eq!(
305 (MixedInts::Unit * MixedInts::Unit).unwrap_err().to_string(),
306 "Cannot mul() unit variants",
307 );
308 assert_eq!(
309 (MixedInts::SmallInt(-1) * MixedInts::BigInt(2))
310 .unwrap_err()
311 .to_string(),
312 "Trying to mul() mismatched enum variants",
313 );
314 }
315
316 mod ignore {
317 #[cfg(not(feature = "std"))]
318 use alloc::string::ToString as _;
319 use derive_more::Mul;
320
321 #[test]
322 fn multi() {
323 #[derive(Mul, Debug, PartialEq)]
324 #[mul(forward)]
325 enum MixedInts {
326 TwoSmallInts(i32, #[mul(skip)] i32),
327 NamedSmallInts {
328 #[mul(ignore)]
329 x: i32,
330 y: i32,
331 },
332 Unit,
333 }
334
335 assert_eq!(
336 (MixedInts::TwoSmallInts(-1, 3) * MixedInts::TwoSmallInts(2, -5))
337 .unwrap(),
338 MixedInts::TwoSmallInts(-2, 3),
339 );
340 assert_eq!(
341 (MixedInts::NamedSmallInts { x: -1, y: 3 }
342 * MixedInts::NamedSmallInts { x: 2, y: -5 })
343 .unwrap(),
344 MixedInts::NamedSmallInts { x: -1, y: -15 },
345 );
346
347 assert_eq!(
348 (MixedInts::Unit * MixedInts::Unit).unwrap_err().to_string(),
349 "Cannot mul() unit variants",
350 );
351 assert_eq!(
352 (MixedInts::TwoSmallInts(-1, 3)
353 * MixedInts::NamedSmallInts { x: -1, y: 3 })
354 .unwrap_err()
355 .to_string(),
356 "Trying to mul() mismatched enum variants",
357 );
358 }
359 }
360 }
361 }