Debian Codegraph @

debian/missing-sources/glam/src/features/impl_arbitrary.rs

raw
1 use crate::{
2 BVec2, BVec3, BVec3A, BVec4, BVec4A, DQuat, DVec2, DVec3, DVec4, I16Vec2, I16Vec3, I16Vec4,
3 I64Vec2, I64Vec3, I64Vec4, I8Vec2, I8Vec3, I8Vec4, IVec2, IVec3, IVec4, Quat, U16Vec2, U16Vec3,
4 U16Vec4, U64Vec2, U64Vec3, U64Vec4, U8Vec2, U8Vec3, U8Vec4, USizeVec2, USizeVec3, USizeVec4,
5 UVec2, UVec3, UVec4, Vec2, Vec3, Vec3A, Vec4,
6 };
7
8 macro_rules! arbitrary_vector_impl {
9 ($($ty:ty),*) => {
10 $(
11 impl arbitrary::Arbitrary<'_> for $ty {
12 fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
13 u.arbitrary().map(Self::from_array)
14 }
15 }
16 )*
17 };
18 }
19
20 arbitrary_vector_impl!(
21 BVec2, BVec3, BVec3A, BVec4, BVec4A, DQuat, DVec2, DVec3, DVec4, I16Vec2, I16Vec3, I16Vec4,
22 I64Vec2, I64Vec3, I64Vec4, I8Vec2, I8Vec3, I8Vec4, IVec2, IVec3, IVec4, Quat, U16Vec2, U16Vec3,
23 U16Vec4, U64Vec2, U64Vec3, U64Vec4, U8Vec2, U8Vec3, U8Vec4, USizeVec2, USizeVec3, USizeVec4,
24 UVec2, UVec3, UVec4, Vec2, Vec3, Vec3A, Vec4
25 );
26
27 use crate::{Affine2, Affine3A, DAffine2, DAffine3, DMat2, DMat3, DMat4, Mat2, Mat3, Mat3A, Mat4};
28
29 macro_rules! arbitrary_matrix_impl {
30 ($($ty:ty),*) => {
31 $(
32 impl arbitrary::Arbitrary<'_> for $ty {
33 fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
34 let array = u.arbitrary()?;
35 Ok(Self::from_cols_array(&array))
36 }
37 }
38 )*
39 };
40 }
41
42 arbitrary_matrix_impl!(
43 Affine2, Affine3A, DAffine2, DAffine3, DMat2, DMat3, DMat4, Mat2, Mat3, Mat3A, Mat4
44 );
45
46 #[cfg(test)]
47 mod test {
48 use arbitrary::Unstructured;
49 use core::mem::size_of;
50
51 use crate::{
52 Affine2, Affine3A, BVec2, BVec3, BVec3A, BVec4, BVec4A, DAffine2, DAffine3, DMat2, DMat3,
53 DMat4, DQuat, DVec2, DVec3, DVec4, I16Vec2, I16Vec3, I16Vec4, I64Vec2, I64Vec3, I64Vec4,
54 I8Vec2, I8Vec3, I8Vec4, IVec2, IVec3, IVec4, Mat2, Mat3, Mat3A, Mat4, Quat, U16Vec2,
55 U16Vec3, U16Vec4, U64Vec2, U64Vec3, U64Vec4, U8Vec2, U8Vec3, U8Vec4, USizeVec2, USizeVec3,
56 USizeVec4, UVec2, UVec3, UVec4, Vec2, Vec3, Vec3A, Vec4,
57 };
58
59 #[test]
60 fn test_arbitrary_f32() {
61 // The float arbitrary impl converts byte array -> integer bits -> float bits.
62 // Writing little endian floats to the buffer allows tests without precision issues.
63 let mut bytes = [0u8; 16 * size_of::<f32>()];
64 for (i, chunk) in bytes.chunks_exact_mut(size_of::<f32>()).enumerate() {
65 chunk.copy_from_slice(&((i + 1) as f32).to_le_bytes());
66 }
67
68 assert_eq!(
69 Affine2::from_cols_array(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]),
70 Unstructured::new(&bytes).arbitrary().unwrap()
71 );
72 assert_eq!(
73 Affine3A::from_cols_array(&[
74 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0
75 ]),
76 Unstructured::new(&bytes).arbitrary().unwrap()
77 );
78
79 assert_eq!(
80 Mat2::from_cols_array(&[1.0, 2.0, 3.0, 4.0]),
81 Unstructured::new(&bytes).arbitrary().unwrap()
82 );
83 assert_eq!(
84 Mat3::from_cols_array(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]),
85 Unstructured::new(&bytes).arbitrary().unwrap()
86 );
87 assert_eq!(
88 Mat3A::from_cols_array(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]),
89 Unstructured::new(&bytes).arbitrary().unwrap()
90 );
91 assert_eq!(
92 Mat4::from_cols_array(&[
93 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0,
94 16.0
95 ]),
96 Unstructured::new(&bytes).arbitrary().unwrap()
97 );
98
99 assert_eq!(
100 Quat::from_array([1.0, 2.0, 3.0, 4.0]),
101 Unstructured::new(&bytes).arbitrary().unwrap()
102 );
103
104 assert_eq!(
105 Vec2::new(1.0, 2.0),
106 Unstructured::new(&bytes).arbitrary().unwrap()
107 );
108 assert_eq!(
109 Vec3::new(1.0, 2.0, 3.0),
110 Unstructured::new(&bytes).arbitrary().unwrap()
111 );
112 assert_eq!(
113 Vec3A::new(1.0, 2.0, 3.0),
114 Unstructured::new(&bytes).arbitrary().unwrap()
115 );
116 assert_eq!(
117 Vec4::new(1.0, 2.0, 3.0, 4.0),
118 Unstructured::new(&bytes).arbitrary().unwrap()
119 );
120 }
121
122 #[test]
123 fn test_arbitrary_f64() {
124 // The float arbitrary impl converts byte array -> integer bits -> float bits.
125 // Writing little endian floats to the buffer allows tests without precision issues.
126 let mut bytes = [0u8; 16 * size_of::<f64>()];
127 for (i, chunk) in bytes.chunks_exact_mut(size_of::<f64>()).enumerate() {
128 chunk.copy_from_slice(&((i + 1) as f64).to_le_bytes());
129 }
130
131 assert_eq!(
132 DAffine2::from_cols_array(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]),
133 Unstructured::new(&bytes).arbitrary().unwrap()
134 );
135 assert_eq!(
136 DAffine3::from_cols_array(&[
137 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0
138 ]),
139 Unstructured::new(&bytes).arbitrary().unwrap()
140 );
141
142 assert_eq!(
143 DMat2::from_cols_array(&[1.0, 2.0, 3.0, 4.0]),
144 Unstructured::new(&bytes).arbitrary().unwrap()
145 );
146 assert_eq!(
147 DMat3::from_cols_array(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]),
148 Unstructured::new(&bytes).arbitrary().unwrap()
149 );
150 assert_eq!(
151 DMat4::from_cols_array(&[
152 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0,
153 16.0
154 ]),
155 Unstructured::new(&bytes).arbitrary().unwrap()
156 );
157
158 assert_eq!(
159 DQuat::from_array([1.0, 2.0, 3.0, 4.0]),
160 Unstructured::new(&bytes).arbitrary().unwrap()
161 );
162
163 assert_eq!(
164 DVec2::new(1.0, 2.0),
165 Unstructured::new(&bytes).arbitrary().unwrap()
166 );
167 assert_eq!(
168 DVec3::new(1.0, 2.0, 3.0),
169 Unstructured::new(&bytes).arbitrary().unwrap()
170 );
171 assert_eq!(
172 DVec4::new(1.0, 2.0, 3.0, 4.0),
173 Unstructured::new(&bytes).arbitrary().unwrap()
174 );
175 }
176
177 #[test]
178 fn test_arbitrary_usize() {
179 // The integer arbitrary impl converts little endian bytes.
180 let mut bytes = [0u8; 4 * size_of::<usize>()];
181 for (i, chunk) in bytes.chunks_exact_mut(size_of::<usize>()).enumerate() {
182 chunk.copy_from_slice(&(i + 1).to_le_bytes());
183 }
184
185 assert_eq!(
186 USizeVec2::new(1, 2),
187 Unstructured::new(&bytes).arbitrary().unwrap()
188 );
189 assert_eq!(
190 USizeVec3::new(1, 2, 3),
191 Unstructured::new(&bytes).arbitrary().unwrap()
192 );
193 assert_eq!(
194 USizeVec4::new(1, 2, 3, 4),
195 Unstructured::new(&bytes).arbitrary().unwrap()
196 );
197 }
198
199 #[test]
200 fn test_arbitrary_u8() {
201 // The integer arbitrary impl converts little endian bytes.
202 let mut bytes = [0u8; 4 * size_of::<u8>()];
203 for (i, chunk) in bytes.chunks_exact_mut(size_of::<u8>()).enumerate() {
204 chunk.copy_from_slice(&(i as u8 + 1).to_le_bytes());
205 }
206
207 assert_eq!(
208 U8Vec2::new(1, 2),
209 Unstructured::new(&bytes).arbitrary().unwrap()
210 );
211 assert_eq!(
212 U8Vec3::new(1, 2, 3),
213 Unstructured::new(&bytes).arbitrary().unwrap()
214 );
215 assert_eq!(
216 U8Vec4::new(1, 2, 3, 4),
217 Unstructured::new(&bytes).arbitrary().unwrap()
218 );
219 }
220
221 #[test]
222 fn test_arbitrary_i8() {
223 // The integer arbitrary impl converts little endian bytes.
224 let mut bytes = [0u8; 4 * size_of::<i8>()];
225 for (i, chunk) in bytes.chunks_exact_mut(size_of::<i8>()).enumerate() {
226 chunk.copy_from_slice(&(i as i8 + 1).to_le_bytes());
227 }
228
229 assert_eq!(
230 I8Vec2::new(1, 2),
231 Unstructured::new(&bytes).arbitrary().unwrap()
232 );
233 assert_eq!(
234 I8Vec3::new(1, 2, 3),
235 Unstructured::new(&bytes).arbitrary().unwrap()
236 );
237 assert_eq!(
238 I8Vec4::new(1, 2, 3, 4),
239 Unstructured::new(&bytes).arbitrary().unwrap()
240 );
241 }
242
243 #[test]
244 fn test_arbitrary_u16() {
245 // The integer arbitrary impl converts little endian bytes.
246 let mut bytes = [0u8; 4 * size_of::<u16>()];
247 for (i, chunk) in bytes.chunks_exact_mut(size_of::<u16>()).enumerate() {
248 chunk.copy_from_slice(&(i as u16 + 1).to_le_bytes());
249 }
250
251 assert_eq!(
252 U16Vec2::new(1, 2),
253 Unstructured::new(&bytes).arbitrary().unwrap()
254 );
255 assert_eq!(
256 U16Vec3::new(1, 2, 3),
257 Unstructured::new(&bytes).arbitrary().unwrap()
258 );
259 assert_eq!(
260 U16Vec4::new(1, 2, 3, 4),
261 Unstructured::new(&bytes).arbitrary().unwrap()
262 );
263 }
264
265 #[test]
266 fn test_arbitrary_i16() {
267 // The integer arbitrary impl converts little endian bytes.
268 let mut bytes = [0u8; 4 * size_of::<i16>()];
269 for (i, chunk) in bytes.chunks_exact_mut(size_of::<i16>()).enumerate() {
270 chunk.copy_from_slice(&(i as i16 + 1).to_le_bytes());
271 }
272
273 assert_eq!(
274 I16Vec2::new(1, 2),
275 Unstructured::new(&bytes).arbitrary().unwrap()
276 );
277 assert_eq!(
278 I16Vec3::new(1, 2, 3),
279 Unstructured::new(&bytes).arbitrary().unwrap()
280 );
281 assert_eq!(
282 I16Vec4::new(1, 2, 3, 4),
283 Unstructured::new(&bytes).arbitrary().unwrap()
284 );
285 }
286
287 #[test]
288 fn test_arbitrary_u32() {
289 // The integer arbitrary impl converts little endian bytes.
290 let mut bytes = [0u8; 4 * size_of::<u32>()];
291 for (i, chunk) in bytes.chunks_exact_mut(size_of::<u32>()).enumerate() {
292 chunk.copy_from_slice(&(i as u32 + 1).to_le_bytes());
293 }
294
295 assert_eq!(
296 UVec2::new(1, 2),
297 Unstructured::new(&bytes).arbitrary().unwrap()
298 );
299 assert_eq!(
300 UVec3::new(1, 2, 3),
301 Unstructured::new(&bytes).arbitrary().unwrap()
302 );
303 assert_eq!(
304 UVec4::new(1, 2, 3, 4),
305 Unstructured::new(&bytes).arbitrary().unwrap()
306 );
307 }
308
309 #[test]
310 fn test_arbitrary_i32() {
311 // The integer arbitrary impl converts little endian bytes.
312 let mut bytes = [0u8; 4 * size_of::<i32>()];
313 for (i, chunk) in bytes.chunks_exact_mut(size_of::<i32>()).enumerate() {
314 chunk.copy_from_slice(&(i as i32 + 1).to_le_bytes());
315 }
316
317 assert_eq!(
318 IVec2::new(1, 2),
319 Unstructured::new(&bytes).arbitrary().unwrap()
320 );
321 assert_eq!(
322 IVec3::new(1, 2, 3),
323 Unstructured::new(&bytes).arbitrary().unwrap()
324 );
325 assert_eq!(
326 IVec4::new(1, 2, 3, 4),
327 Unstructured::new(&bytes).arbitrary().unwrap()
328 );
329 }
330
331 #[test]
332 fn test_arbitrary_u64() {
333 // The integer arbitrary impl converts little endian bytes.
334 let mut bytes = [0u8; 4 * size_of::<u64>()];
335 for (i, chunk) in bytes.chunks_exact_mut(size_of::<u64>()).enumerate() {
336 chunk.copy_from_slice(&(i as u64 + 1).to_le_bytes());
337 }
338
339 assert_eq!(
340 U64Vec2::new(1, 2),
341 Unstructured::new(&bytes).arbitrary().unwrap()
342 );
343 assert_eq!(
344 U64Vec3::new(1, 2, 3),
345 Unstructured::new(&bytes).arbitrary().unwrap()
346 );
347 assert_eq!(
348 U64Vec4::new(1, 2, 3, 4),
349 Unstructured::new(&bytes).arbitrary().unwrap()
350 );
351 }
352
353 #[test]
354 fn test_arbitrary_i64() {
355 // The integer arbitrary impl converts little endian bytes.
356 let mut bytes = [0u8; 4 * size_of::<i64>()];
357 for (i, chunk) in bytes.chunks_exact_mut(size_of::<i64>()).enumerate() {
358 chunk.copy_from_slice(&(i as i64 + 1).to_le_bytes());
359 }
360
361 assert_eq!(
362 I64Vec2::new(1, 2),
363 Unstructured::new(&bytes).arbitrary().unwrap()
364 );
365 assert_eq!(
366 I64Vec3::new(1, 2, 3),
367 Unstructured::new(&bytes).arbitrary().unwrap()
368 );
369 assert_eq!(
370 I64Vec4::new(1, 2, 3, 4),
371 Unstructured::new(&bytes).arbitrary().unwrap()
372 );
373 }
374
375 #[test]
376 fn test_arbitrary_bool() {
377 // The bool arbitrary impl checks if u8 == 1.
378 let bytes = [1u8, 0u8, 1u8, 0u8];
379 assert_eq!(
380 BVec2::new(true, false),
381 Unstructured::new(&bytes).arbitrary().unwrap()
382 );
383 assert_eq!(
384 BVec3::new(true, false, true),
385 Unstructured::new(&bytes).arbitrary().unwrap()
386 );
387 assert_eq!(
388 BVec3A::new(true, false, true),
389 Unstructured::new(&bytes).arbitrary().unwrap()
390 );
391 assert_eq!(
392 BVec4::new(true, false, true, false),
393 Unstructured::new(&bytes).arbitrary().unwrap()
394 );
395 assert_eq!(
396 BVec4A::new(true, false, true, false),
397 Unstructured::new(&bytes).arbitrary().unwrap()
398 );
399 }
400 }