From 03254dcad77f4ce5232a0ba75d55df714b79c51e Mon Sep 17 00:00:00 2001 From: laentropia Date: Wed, 29 Apr 2026 15:51:46 -0600 Subject: [PATCH] test: test for get_column --- src/lib.rs | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 9d6b791..8012e57 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -793,4 +793,131 @@ mod tests { assert_eq!(*val, *m.get(2, col).unwrap()); } } + + #[test] + fn test_get_column_valid() { + let m = Matrix { + rows: 3, + columns: 3, + data: vec![ + Fraction::from(1), + Fraction::from(2), + Fraction::from(3), + Fraction::from(4), + Fraction::from(5), + Fraction::from(6), + Fraction::from(7), + Fraction::from(8), + Fraction::from(9), + ], + }; + + let col: Vec = m.get_column(1).unwrap().cloned().collect(); + + assert_eq!( + col, + vec![Fraction::from(2), Fraction::from(5), Fraction::from(8)] + ); + } + + #[test] + fn test_get_first_column() { + let m = Matrix::new(3, 2, Fraction::from(10)).unwrap(); + + let col: Vec = m.get_column(0).unwrap().cloned().collect(); + + assert_eq!(col, vec![Fraction::from(10); 3]); + } + + #[test] + fn test_get_last_column() { + let m = Matrix { + rows: 2, + columns: 3, + data: vec![ + Fraction::from(1), + Fraction::from(2), + Fraction::from(3), + Fraction::from(4), + Fraction::from(5), + Fraction::from(6), + ], + }; + + let col: Vec = m.get_column(2).unwrap().cloned().collect(); + + assert_eq!(col, vec![Fraction::from(3), Fraction::from(6)]); + } + + #[test] + fn test_get_column_out_of_bounds() { + let m = Matrix::new(2, 2, Fraction::from(0)).unwrap(); + + let result = m.get_column(2); + + assert!(matches!(result, Err(MatrixError::ColumnOutOfRange))); + } + + #[test] + fn test_get_column_length() { + let m = Matrix::new(5, 4, Fraction::from(1)).unwrap(); + + let col: Vec = m.get_column(3).unwrap().cloned().collect(); + + assert_eq!(col.len(), 5); + } + + #[test] + fn test_get_column_matches_get() { + let m = Matrix { + rows: 4, + columns: 3, + data: (0..12).map(|x| Fraction::from(x)).collect(), + }; + + let col = m.get_column(2).unwrap(); + + for (row, val) in col.enumerate() { + assert_eq!(*val, *m.get(row, 2).unwrap()); + } + } + + #[test] + fn test_get_column_different_shapes() { + let m = Matrix { + rows: 3, + columns: 4, + data: vec![ + Fraction::from(1), + Fraction::from(2), + Fraction::from(3), + Fraction::from(4), + Fraction::from(5), + Fraction::from(6), + Fraction::from(7), + Fraction::from(8), + Fraction::from(9), + Fraction::from(10), + Fraction::from(11), + Fraction::from(12), + ], + }; + + let col: Vec = m.get_column(3).unwrap().cloned().collect(); + + assert_eq!( + col, + vec![Fraction::from(4), Fraction::from(8), Fraction::from(12)] + ); + } + + #[test] + fn test_get_column_iterator_repeatability() { + let m = Matrix::new(3, 3, Fraction::from(7)).unwrap(); + + let col1: Vec = m.get_column(1).unwrap().cloned().collect(); + let col2: Vec = m.get_column(1).unwrap().cloned().collect(); + + assert_eq!(col1, col2); + } }