test: test for get_column
This commit is contained in:
127
src/lib.rs
127
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<Fraction> = 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<Fraction> = 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<Fraction> = 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<Fraction> = 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<Fraction> = 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<Fraction> = m.get_column(1).unwrap().cloned().collect();
|
||||
let col2: Vec<Fraction> = m.get_column(1).unwrap().cloned().collect();
|
||||
|
||||
assert_eq!(col1, col2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user