Debian Codegraph @

debian/missing-sources/nalgebra/tests/core/reshape.rs

raw
1 use na::{
2 Const, DMatrix, DMatrixView, DMatrixViewMut, Dyn, Matrix, MatrixView, MatrixViewMut, SMatrix,
3 SMatrixView, SMatrixViewMut, U3, U4, VecStorage,
4 };
5 use nalgebra_macros::matrix;
6 use simba::scalar::SupersetOf;
7
8 const MATRIX: SMatrix<i32, 4, 3> = matrix![
9 1, 2, 3;
10 4, 5, 6;
11 7, 8, 9;
12 10, 11, 12
13 ];
14
15 const RESHAPED_MATRIX: SMatrix<i32, 3, 4> = matrix![
16 1, 10, 8, 6;
17 4, 2, 11, 9;
18 7, 5, 3, 12
19 ];
20
21 // Helper alias for making it easier to specify dynamically allocated matrices with
22 // different dimension types (unlike DMatrix)
23 type GenericDMatrix<T, R, C> = Matrix<T, R, C, VecStorage<T, R, C>>;
24
25 #[test]
26 fn reshape_owned() {
27 macro_rules! test_reshape {
28 ($in_matrix:ty => $out_matrix:ty, $rows:expr, $cols:expr) => {{
29 // This is a pretty weird way to convert, but Matrix implements SubsetOf
30 let matrix: $in_matrix = MATRIX.to_subset().unwrap();
31 let reshaped: $out_matrix = matrix.reshape_generic($rows, $cols);
32 assert_eq!(reshaped, RESHAPED_MATRIX);
33 }};
34 }
35
36 test_reshape!(SMatrix<_, 4, 3> => SMatrix<_, 3, 4>, U3, U4);
37 test_reshape!(GenericDMatrix<_, U4, Dyn> => GenericDMatrix<_, Dyn, Dyn>, Dyn(3), Dyn(4));
38 test_reshape!(GenericDMatrix<_, U4, Dyn> => GenericDMatrix<_, U3, Dyn>, U3, Dyn(4));
39 test_reshape!(GenericDMatrix<_, U4, Dyn> => GenericDMatrix<_, Dyn, U4>, Dyn(3), U4);
40 test_reshape!(DMatrix<_> => DMatrix<_>, Dyn(3), Dyn(4));
41 }
42
43 #[test]
44 fn reshape_slice() {
45 macro_rules! test_reshape {
46 ($in_slice:ty => $out_slice:ty, $rows:expr, $cols:expr) => {
47 // We test both that types check out by being explicit about types
48 // and the actual contents of the matrix
49 {
50 // By constructing the slice from a mutable reference we can obtain *either*
51 // an immutable slice or a mutable slice, which simplifies the testing of both
52 // types of mutability
53 let mut source_matrix = MATRIX.clone();
54 let slice: $in_slice = Matrix::from(&mut source_matrix);
55 let reshaped: $out_slice = slice.reshape_generic($rows, $cols);
56 assert_eq!(reshaped, RESHAPED_MATRIX);
57 }
58 };
59 }
60
61 // Static "source slice"
62 test_reshape!(SMatrixView<_, 4, 3> => SMatrixView<_, 3, 4>, U3, U4);
63 test_reshape!(SMatrixView<_, 4, 3> => DMatrixView<_>, Dyn(3), Dyn(4));
64 test_reshape!(SMatrixView<_, 4, 3> => MatrixView<_, Const<3>, Dyn>, U3, Dyn(4));
65 test_reshape!(SMatrixView<_, 4, 3> => MatrixView<_, Dyn, Const<4>>, Dyn(3), U4);
66 test_reshape!(SMatrixViewMut<_, 4, 3> => SMatrixViewMut<_, 3, 4>, U3, U4);
67 test_reshape!(SMatrixViewMut<_, 4, 3> => DMatrixViewMut<_>, Dyn(3), Dyn(4));
68 test_reshape!(SMatrixViewMut<_, 4, 3> => MatrixViewMut<_, Const<3>, Dyn>, U3, Dyn(4));
69 test_reshape!(SMatrixViewMut<_, 4, 3> => MatrixViewMut<_, Dyn, Const<4>>, Dyn(3), U4);
70
71 // Dyn "source slice"
72 test_reshape!(DMatrixView<_> => SMatrixView<_, 3, 4>, U3, U4);
73 test_reshape!(DMatrixView<_> => DMatrixView<_>, Dyn(3), Dyn(4));
74 test_reshape!(DMatrixView<_> => MatrixView<_, Const<3>, Dyn>, U3, Dyn(4));
75 test_reshape!(DMatrixView<_> => MatrixView<_, Dyn, Const<4>>, Dyn(3), U4);
76 test_reshape!(DMatrixViewMut<_> => SMatrixViewMut<_, 3, 4>, U3, U4);
77 test_reshape!(DMatrixViewMut<_> => DMatrixViewMut<_>, Dyn(3), Dyn(4));
78 test_reshape!(DMatrixViewMut<_> => MatrixViewMut<_, Const<3>, Dyn>, U3, Dyn(4));
79 test_reshape!(DMatrixViewMut<_> => MatrixViewMut<_, Dyn, Const<4>>, Dyn(3), U4);
80 }