Debian Codegraph @

debian/missing-sources/nalgebra/tests/macros/matrix.rs

raw
1 use crate::macros::assert_eq_and_type;
2 use nalgebra::{
3 DMatrix, DVector, Matrix1x2, Matrix1x3, Matrix1x4, Matrix2, Matrix2x1, Matrix2x3, Matrix2x4,
4 Matrix3, Matrix3x1, Matrix3x2, Matrix3x4, Matrix4, Matrix4x1, Matrix4x2, Matrix4x3, Point,
5 Point1, Point2, Point3, Point4, Point5, Point6, SMatrix, SVector, Vector1, Vector2, Vector3,
6 Vector4, Vector5, Vector6,
7 };
8 use nalgebra_macros::{dmatrix, dvector, matrix, point, vector};
9
10 // Skip rustfmt because it just makes the test bloated without making it more readable
11 #[rustfmt::skip]
12 #[test]
13 fn matrix_small_dims_exhaustive() {
14 // 0x0
15 assert_eq_and_type!(matrix![], SMatrix::<i32, 0, 0>::zeros());
16
17 // 1xN
18 assert_eq_and_type!(matrix![1], SMatrix::<i32, 1, 1>::new(1));
19 assert_eq_and_type!(matrix![1, 2], Matrix1x2::new(1, 2));
20 assert_eq_and_type!(matrix![1, 2, 3], Matrix1x3::new(1, 2, 3));
21 assert_eq_and_type!(matrix![1, 2, 3, 4], Matrix1x4::new(1, 2, 3, 4));
22
23 // 2xN
24 assert_eq_and_type!(matrix![1; 2], Matrix2x1::new(1, 2));
25 assert_eq_and_type!(matrix![1, 2; 3, 4], Matrix2::new(1, 2, 3, 4));
26 assert_eq_and_type!(matrix![1, 2, 3; 4, 5, 6], Matrix2x3::new(1, 2, 3, 4, 5, 6));
27 assert_eq_and_type!(matrix![1, 2, 3, 4; 5, 6, 7, 8], Matrix2x4::new(1, 2, 3, 4, 5, 6, 7, 8));
28
29 // 3xN
30 assert_eq_and_type!(matrix![1; 2; 3], Matrix3x1::new(1, 2, 3));
31 assert_eq_and_type!(matrix![1, 2; 3, 4; 5, 6], Matrix3x2::new(1, 2, 3, 4, 5, 6));
32 assert_eq_and_type!(matrix![1, 2, 3; 4, 5, 6; 7, 8, 9], Matrix3::new(1, 2, 3, 4, 5, 6, 7, 8, 9));
33 assert_eq_and_type!(matrix![1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12],
34 Matrix3x4::new(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12));
35
36 // 4xN
37 assert_eq_and_type!(matrix![1; 2; 3; 4], Matrix4x1::new(1, 2, 3, 4));
38 assert_eq_and_type!(matrix![1, 2; 3, 4; 5, 6; 7, 8], Matrix4x2::new(1, 2, 3, 4, 5, 6, 7, 8));
39 assert_eq_and_type!(matrix![1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12],
40 Matrix4x3::new(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12));
41 assert_eq_and_type!(matrix![1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12; 13, 14, 15, 16],
42 Matrix4::new(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16));
43 }
44
45 #[test]
46 fn matrix_const_fn() {
47 // Ensure that matrix! can be used in const contexts
48 const _: SMatrix<i32, 0, 0> = matrix![];
49 const _: SMatrix<i32, 1, 2> = matrix![1, 2];
50 const _: SMatrix<i32, 2, 3> = matrix![1, 2, 3; 4, 5, 6];
51 }
52
53 // Skip rustfmt because it just makes the test bloated without making it more readable
54 #[rustfmt::skip]
55 #[test]
56 fn dmatrix_small_dims_exhaustive() {
57 // 0x0
58 assert_eq_and_type!(dmatrix![], DMatrix::<i32>::zeros(0, 0));
59
60 // 1xN
61 assert_eq_and_type!(dmatrix![1], DMatrix::from_row_slice(1, 1, &[1]));
62 assert_eq_and_type!(dmatrix![1, 2], DMatrix::from_row_slice(1, 2, &[1, 2]));
63 assert_eq_and_type!(dmatrix![1, 2, 3], DMatrix::from_row_slice(1, 3, &[1, 2, 3]));
64 assert_eq_and_type!(dmatrix![1, 2, 3, 4], DMatrix::from_row_slice(1, 4, &[1, 2, 3, 4]));
65
66 // 2xN
67 assert_eq_and_type!(dmatrix![1; 2], DMatrix::from_row_slice(2, 1, &[1, 2]));
68 assert_eq_and_type!(dmatrix![1, 2; 3, 4], DMatrix::from_row_slice(2, 2, &[1, 2, 3, 4]));
69 assert_eq_and_type!(dmatrix![1, 2, 3; 4, 5, 6], DMatrix::from_row_slice(2, 3, &[1, 2, 3, 4, 5, 6]));
70 assert_eq_and_type!(dmatrix![1, 2, 3, 4; 5, 6, 7, 8], DMatrix::from_row_slice(2, 4, &[1, 2, 3, 4, 5, 6, 7, 8]));
71
72 // 3xN
73 assert_eq_and_type!(dmatrix![1; 2; 3], DMatrix::from_row_slice(3, 1, &[1, 2, 3]));
74 assert_eq_and_type!(dmatrix![1, 2; 3, 4; 5, 6], DMatrix::from_row_slice(3, 2, &[1, 2, 3, 4, 5, 6]));
75 assert_eq_and_type!(dmatrix![1, 2, 3; 4, 5, 6; 7, 8, 9], DMatrix::from_row_slice(3, 3, &[1, 2, 3, 4, 5, 6, 7, 8, 9]));
76 assert_eq_and_type!(dmatrix![1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12],
77 DMatrix::from_row_slice(3, 4, &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]));
78
79 // 4xN
80 assert_eq_and_type!(dmatrix![1; 2; 3; 4], DMatrix::from_row_slice(4, 1, &[1, 2, 3, 4]));
81 assert_eq_and_type!(dmatrix![1, 2; 3, 4; 5, 6; 7, 8], DMatrix::from_row_slice(4, 2, &[1, 2, 3, 4, 5, 6, 7, 8]));
82 assert_eq_and_type!(dmatrix![1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12],
83 DMatrix::from_row_slice(4, 3, &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]));
84 assert_eq_and_type!(dmatrix![1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12; 13, 14, 15, 16],
85 DMatrix::from_row_slice(4, 4, &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]));
86 }
87
88 #[test]
89 fn matrix_trailing_semi() {
90 matrix![1, 2;];
91 dmatrix![1, 2;];
92 }
93
94 // Skip rustfmt because it just makes the test bloated without making it more readable
95 #[rustfmt::skip]
96 #[test]
97 fn vector_small_dims_exhaustive() {
98 assert_eq_and_type!(vector![], SVector::<i32, 0>::zeros());
99 assert_eq_and_type!(vector![1], Vector1::<i32>::new(1));
100 assert_eq_and_type!(vector![1, 2], Vector2::new(1, 2));
101 assert_eq_and_type!(vector![1, 2, 3], Vector3::new(1, 2, 3));
102 assert_eq_and_type!(vector![1, 2, 3, 4], Vector4::new(1, 2, 3, 4));
103 assert_eq_and_type!(vector![1, 2, 3, 4, 5], Vector5::new(1, 2, 3, 4, 5));
104 assert_eq_and_type!(vector![1, 2, 3, 4, 5, 6], Vector6::new(1, 2, 3, 4, 5, 6));
105 }
106
107 // Skip rustfmt because it just makes the test bloated without making it more readable
108 #[rustfmt::skip]
109 #[test]
110 fn point_small_dims_exhaustive() {
111 assert_eq_and_type!(point![], Point::<i32, 0>::origin());
112 assert_eq_and_type!(point![1], Point1::<i32>::new(1));
113 assert_eq_and_type!(point![1, 2], Point2::new(1, 2));
114 assert_eq_and_type!(point![1, 2, 3], Point3::new(1, 2, 3));
115 assert_eq_and_type!(point![1, 2, 3, 4], Point4::new(1, 2, 3, 4));
116 assert_eq_and_type!(point![1, 2, 3, 4, 5], Point5::new(1, 2, 3, 4, 5));
117 assert_eq_and_type!(point![1, 2, 3, 4, 5, 6], Point6::new(1, 2, 3, 4, 5, 6));
118 }
119
120 #[test]
121 fn vector_const_fn() {
122 // Ensure that vector! can be used in const contexts
123 const _: SVector<i32, 0> = vector![];
124 const _: Vector1<i32> = vector![1];
125 const _: Vector2<i32> = vector![1, 2];
126 const _: Vector6<i32> = vector![1, 2, 3, 4, 5, 6];
127 }
128
129 #[test]
130 fn point_const_fn() {
131 // Ensure that vector! can be used in const contexts
132 const _: Point<i32, 0> = point![];
133 const _: Point1<i32> = point![1];
134 const _: Point2<i32> = point![1, 2];
135 const _: Point6<i32> = point![1, 2, 3, 4, 5, 6];
136 }
137
138 // Skip rustfmt because it just makes the test bloated without making it more readable
139 #[rustfmt::skip]
140 #[test]
141 fn dvector_small_dims_exhaustive() {
142 assert_eq_and_type!(dvector![], DVector::<i32>::zeros(0));
143 assert_eq_and_type!(dvector![1], DVector::from_column_slice(&[1]));
144 assert_eq_and_type!(dvector![1, 2], DVector::from_column_slice(&[1, 2]));
145 assert_eq_and_type!(dvector![1, 2, 3], DVector::from_column_slice(&[1, 2, 3]));
146 assert_eq_and_type!(dvector![1, 2, 3, 4], DVector::from_column_slice(&[1, 2, 3, 4]));
147 assert_eq_and_type!(dvector![1, 2, 3, 4, 5], DVector::from_column_slice(&[1, 2, 3, 4, 5]));
148 assert_eq_and_type!(dvector![1, 2, 3, 4, 5, 6], DVector::from_column_slice(&[1, 2, 3, 4, 5, 6]));
149 }
150
151 #[test]
152 fn vector_trailing_comma() {
153 vector![1, 2,];
154 point![1, 2,];
155 dvector![1, 2,];
156 }
157
158 #[test]
159 fn matrix_trybuild_tests() {
160 let t = trybuild::TestCases::new();
161
162 // Verify error message when we give a matrix with mismatched dimensions
163 t.compile_fail("tests/macros/trybuild/matrix_mismatched_dimensions.rs");
164 }
165
166 #[test]
167 fn dmatrix_trybuild_tests() {
168 let t = trybuild::TestCases::new();
169
170 // Verify error message when we give a matrix with mismatched dimensions
171 t.compile_fail("tests/macros/trybuild/dmatrix_mismatched_dimensions.rs");
172 }
173
174 #[test]
175 fn matrix_builtin_types() {
176 // Check that matrix! compiles for all built-in types
177 const _: SMatrix<i8, 2, 2> = matrix![0, 1; 2, 3];
178 const _: SMatrix<i16, 2, 2> = matrix![0, 1; 2, 3];
179 const _: SMatrix<i32, 2, 2> = matrix![0, 1; 2, 3];
180 const _: SMatrix<i64, 2, 2> = matrix![0, 1; 2, 3];
181 const _: SMatrix<isize, 2, 2> = matrix![0, 1; 2, 3];
182 const _: SMatrix<u8, 2, 2> = matrix![0, 1; 2, 3];
183 const _: SMatrix<u16, 2, 2> = matrix![0, 1; 2, 3];
184 const _: SMatrix<u32, 2, 2> = matrix![0, 1; 2, 3];
185 const _: SMatrix<u64, 2, 2> = matrix![0, 1; 2, 3];
186 const _: SMatrix<usize, 2, 2> = matrix![0, 1; 2, 3];
187 const _: SMatrix<f32, 2, 2> = matrix![0.0, 1.0; 2.0, 3.0];
188 const _: SMatrix<f64, 2, 2> = matrix![0.0, 1.0; 2.0, 3.0];
189 }
190
191 #[test]
192 fn vector_builtin_types() {
193 // Check that vector! compiles for all built-in types
194 const _: SVector<i8, 4> = vector![0, 1, 2, 3];
195 const _: SVector<i16, 4> = vector![0, 1, 2, 3];
196 const _: SVector<i32, 4> = vector![0, 1, 2, 3];
197 const _: SVector<i64, 4> = vector![0, 1, 2, 3];
198 const _: SVector<isize, 4> = vector![0, 1, 2, 3];
199 const _: SVector<u8, 4> = vector![0, 1, 2, 3];
200 const _: SVector<u16, 4> = vector![0, 1, 2, 3];
201 const _: SVector<u32, 4> = vector![0, 1, 2, 3];
202 const _: SVector<u64, 4> = vector![0, 1, 2, 3];
203 const _: SVector<usize, 4> = vector![0, 1, 2, 3];
204 const _: SVector<f32, 4> = vector![0.0, 1.0, 2.0, 3.0];
205 const _: SVector<f64, 4> = vector![0.0, 1.0, 2.0, 3.0];
206 }
207
208 #[test]
209 fn dmatrix_builtin_types() {
210 // Check that dmatrix! compiles for all built-in types
211 let _: DMatrix<i8> = dmatrix![0, 1; 2, 3];
212 let _: DMatrix<i16> = dmatrix![0, 1; 2, 3];
213 let _: DMatrix<i32> = dmatrix![0, 1; 2, 3];
214 let _: DMatrix<i64> = dmatrix![0, 1; 2, 3];
215 let _: DMatrix<isize> = dmatrix![0, 1; 2, 3];
216 let _: DMatrix<u8> = dmatrix![0, 1; 2, 3];
217 let _: DMatrix<u16> = dmatrix![0, 1; 2, 3];
218 let _: DMatrix<u32> = dmatrix![0, 1; 2, 3];
219 let _: DMatrix<u64> = dmatrix![0, 1; 2, 3];
220 let _: DMatrix<usize> = dmatrix![0, 1; 2, 3];
221 let _: DMatrix<f32> = dmatrix![0.0, 1.0; 2.0, 3.0];
222 let _: DMatrix<f64> = dmatrix![0.0, 1.0; 2.0, 3.0];
223 }
224
225 #[test]
226 fn point_builtin_types() {
227 // Check that point! compiles for all built-in types
228 const _: Point<i8, 4> = point![0, 1, 2, 3];
229 const _: Point<i16, 4> = point![0, 1, 2, 3];
230 const _: Point<i32, 4> = point![0, 1, 2, 3];
231 const _: Point<i64, 4> = point![0, 1, 2, 3];
232 const _: Point<isize, 4> = point![0, 1, 2, 3];
233 const _: Point<u8, 4> = point![0, 1, 2, 3];
234 const _: Point<u16, 4> = point![0, 1, 2, 3];
235 const _: Point<u32, 4> = point![0, 1, 2, 3];
236 const _: Point<u64, 4> = point![0, 1, 2, 3];
237 const _: Point<usize, 4> = point![0, 1, 2, 3];
238 const _: Point<f32, 4> = point![0.0, 1.0, 2.0, 3.0];
239 const _: Point<f64, 4> = point![0.0, 1.0, 2.0, 3.0];
240 }
241
242 #[test]
243 fn dvector_builtin_types() {
244 // Check that dvector! compiles for all built-in types
245 let _: DVector<i8> = dvector![0, 1, 2, 3];
246 let _: DVector<i16> = dvector![0, 1, 2, 3];
247 let _: DVector<i32> = dvector![0, 1, 2, 3];
248 let _: DVector<i64> = dvector![0, 1, 2, 3];
249 let _: DVector<isize> = dvector![0, 1, 2, 3];
250 let _: DVector<u8> = dvector![0, 1, 2, 3];
251 let _: DVector<u16> = dvector![0, 1, 2, 3];
252 let _: DVector<u32> = dvector![0, 1, 2, 3];
253 let _: DVector<u64> = dvector![0, 1, 2, 3];
254 let _: DVector<usize> = dvector![0, 1, 2, 3];
255 let _: DVector<f32> = dvector![0.0, 1.0, 2.0, 3.0];
256 let _: DVector<f64> = dvector![0.0, 1.0, 2.0, 3.0];
257 }
258
259 /// Black box function that's just used for testing macros with function call expressions.
260 fn f<T>(x: T) -> T {
261 x
262 }
263
264 #[rustfmt::skip]
265 #[test]
266 fn matrix_arbitrary_expressions() {
267 // Test that matrix! supports arbitrary expressions for its elements
268 let a = matrix![1 + 2 , 2 * 3;
269 4 * f(5 + 6), 7 - 8 * 9];
270 let a_expected = Matrix2::new(1 + 2 , 2 * 3,
271 4 * f(5 + 6), 7 - 8 * 9);
272 assert_eq_and_type!(a, a_expected);
273 }
274
275 #[rustfmt::skip]
276 #[test]
277 fn dmatrix_arbitrary_expressions() {
278 // Test that dmatrix! supports arbitrary expressions for its elements
279 let a = dmatrix![1 + 2 , 2 * 3;
280 4 * f(5 + 6), 7 - 8 * 9];
281 let a_expected = DMatrix::from_row_slice(2, 2, &[1 + 2 , 2 * 3,
282 4 * f(5 + 6), 7 - 8 * 9]);
283 assert_eq_and_type!(a, a_expected);
284 }
285
286 #[rustfmt::skip]
287 #[test]
288 fn vector_arbitrary_expressions() {
289 // Test that vector! supports arbitrary expressions for its elements
290 let a = vector![1 + 2, 2 * 3, 4 * f(5 + 6), 7 - 8 * 9];
291 let a_expected = Vector4::new(1 + 2, 2 * 3, 4 * f(5 + 6), 7 - 8 * 9);
292 assert_eq_and_type!(a, a_expected);
293 }
294
295 #[rustfmt::skip]
296 #[test]
297 fn point_arbitrary_expressions() {
298 // Test that point! supports arbitrary expressions for its elements
299 let a = point![1 + 2, 2 * 3, 4 * f(5 + 6), 7 - 8 * 9];
300 let a_expected = Point4::new(1 + 2, 2 * 3, 4 * f(5 + 6), 7 - 8 * 9);
301 assert_eq_and_type!(a, a_expected);
302 }
303
304 #[rustfmt::skip]
305 #[test]
306 fn dvector_arbitrary_expressions() {
307 // Test that dvector! supports arbitrary expressions for its elements
308 let a = dvector![1 + 2, 2 * 3, 4 * f(5 + 6), 7 - 8 * 9];
309 let a_expected = DVector::from_column_slice(&[1 + 2, 2 * 3, 4 * f(5 + 6), 7 - 8 * 9]);
310 assert_eq_and_type!(a, a_expected);
311 }