numberlab/structure/matrix/
macros.rs

1/// Macro to create a `Matrix` from a nested array.
2///
3/// # Example
4///
5/// ```
6/// use numberlab::mat;
7/// use numberlab::structure::matrix::Matrix;
8///
9/// let matrix = mat![
10///     [1, 2, 3],
11///     [4, 5, 6],
12///     [7, 8, 9]
13/// ];
14///
15/// assert_eq!(matrix, mat![[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
16/// assert_eq!(format!("{}", matrix), "\n 1 2 3\n 4 5 6\n 7 8 9\n");
17/// ```
18#[macro_export]
19macro_rules! mat {
20    ($([$($elem:expr),* $(,)?]),* $(,)?) => {
21        {
22            let data = [$( [ $($elem),* ] ),*];
23            Matrix::from_array(data)
24        }
25    };
26}
27
28#[cfg(test)]
29mod mat_tests {
30    use crate::structure::matrix::Matrix;
31
32    #[test]
33    fn should_create_matrix() {
34        let matrix = mat![
35            [1, 2, 3],
36            [4, 5, 6],
37            [7, 8, 9]
38        ];
39
40        assert_eq!(matrix, mat![[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
41    }
42
43    #[test]
44    fn should_print_matrix() {
45        let matrix = mat![
46            [1, 2, 3],
47            [4, 5, 6],
48            [7, 8, 9]
49        ];
50
51        assert_eq!(format!("{}", matrix), "\n 1 2 3\n 4 5 6\n 7 8 9\n");
52    }
53}