use lb::algebra::Algebra; use lb::algebra::AlgebraMut; use lb::mat; use lb::mat::Matrix; #[test] fn matrix_rotate_180() { let mat = mat::MatrixVec::from([ [0, 1, 2], // [3, 4, 5], // ]); let rot = mat.rotate_180(); assert_eq!( rot.as_slice(), [ 5, 4, // 3, 2, // 1, 0, // ] ); assert_eq!(rot.size(), (3, 2).into()); let rot = rot.rotate_180(); assert_eq!(rot.as_slice(), mat.as_slice()); assert_eq!(rot.size(), (3, 2).into()); } #[test] fn matrix_rotate_180_in_place() { let mut values = [ 0, 1, // 2, 3, // 4, 5, // ]; let mut mat = mat::MatrixRefMut::new(&mut values, (2, 3).into()).unwrap(); mat.rotate_180_in_place(); assert_eq!(mat.size(), (2, 3).into()); assert_eq!( values, [ 5, 4, // 3, 2, // 1, 0, // ] ); let mut mat = mat::MatrixRefMut::new(&mut values, (2, 3).into()).unwrap(); mat.rotate_180_in_place(); assert_eq!(mat.size(), (2, 3).into()); assert_eq!( values, [ 0, 1, // 2, 3, // 4, 5, // ] ); } #[test] fn matrix_transpose() { let values = [ 0, 1, 2, // 3, 4, 5, // 6, 7, 8, // 9, 10, 11, // ]; let mat = mat::MatrixRef::new(&values, (3, 4).into()).unwrap(); let transposed = mat.transpose(); assert_eq!(transposed.size(), (4, 3).into()); assert_eq!( transposed.as_slice(), [ 0, 3, 6, 9, // 1, 4, 7, 10, // 2, 5, 8, 11, // ] ); let transposed = transposed.transpose(); assert_eq!(transposed.size(), (3, 4).into()); assert_eq!(transposed.as_slice(), values); } #[test] fn matrix_rotate_90_right() { let values = [ 0, 1, // 2, 3, // 4, 5, // ]; let mat = mat::MatrixRef::new(&values, (2, 3).into()).unwrap(); let rot = mat.rotate_90_right(); assert_eq!(rot.size(), (3, 2).into()); assert_eq!( rot.as_slice(), [ 4, 2, 0, // 5, 3, 1, // ] ); let rot = rot.rotate_90_right(); assert_eq!(rot.size(), (2, 3).into()); assert_eq!( rot.as_slice(), [ 5, 4, // 3, 2, // 1, 0, // ] ); let rot = rot.rotate_90_right(); assert_eq!(rot.size(), (3, 2).into()); assert_eq!( rot.as_slice(), [ 1, 3, 5, // 0, 2, 4, // ] ); let rot = rot.rotate_90_right(); assert_eq!(rot.size(), (2, 3).into()); assert_eq!(rot.as_slice(), values); } #[test] fn matrix_rotate_90_left() { let values = [ 0, 1, // 2, 3, // 4, 5, // ]; let mat = mat::MatrixRef::new(&values, (2, 3).into()).unwrap(); let rot = mat.rotate_90_left(); assert_eq!(rot.size(), (3, 2).into()); assert_eq!( rot.as_slice(), [ 1, 3, 5, // 0, 2, 4, // ] ); let rot = rot.rotate_90_left(); assert_eq!(rot.size(), (2, 3).into()); assert_eq!( rot.as_slice(), [ 5, 4, // 3, 2, // 1, 0, // ] ); let rot = rot.rotate_90_left(); assert_eq!(rot.size(), (3, 2).into()); assert_eq!( rot.as_slice(), [ 4, 2, 0, // 5, 3, 1, // ] ); let rot = rot.rotate_90_left(); assert_eq!(rot.size(), (2, 3).into()); assert_eq!(rot.as_slice(), values); } #[test] fn matrix_reflect_horizontal() { { let mat = mat::MatrixVec::from([ [0, 1, 2], // [3, 4, 5], // ]); let refl = mat.reflect_horizontal(); assert_eq!( refl, mat::MatrixVec::from([ [2, 1, 0], // [5, 4, 3], // ]) ); let refl = refl.reflect_horizontal(); assert_eq!(refl, mat); } { let mat = mat::MatrixVec::from([ [0, 1, 2, 3], // [4, 5, 6, 7], // ]); let refl = mat.reflect_horizontal(); assert_eq!( refl, mat::MatrixVec::from([ [3, 2, 1, 0], // [7, 6, 5, 4], // ]) ); let refl = refl.reflect_horizontal(); assert_eq!(refl, mat); } } #[test] fn matrix_reflect_vertical() { { let mat = mat::MatrixVec::from([ [0, 1], // [2, 3], // [4, 5], // ]); let refl = mat.reflect_vertical(); assert_eq!( refl, mat::MatrixVec::from([ [4, 5], // [2, 3], // [0, 1], // ]) ); let refl = refl.reflect_vertical(); assert_eq!(refl, mat); } { let mat = mat::MatrixVec::from([ [0, 1], // [2, 3], // [4, 5], // [6, 7], // ]); let refl = mat.reflect_vertical(); assert_eq!( refl, mat::MatrixVec::from([ [6, 7], // [4, 5], // [2, 3], // [0, 1], // ]) ); let refl = refl.reflect_vertical(); assert_eq!(refl, mat); } }