test: tests for get

This commit is contained in:
2026-04-29 15:42:33 -06:00
parent b376e3018a
commit cb130f5d86

View File

@@ -50,9 +50,9 @@ impl Matrix {
if row >= self.rows || column >= self.columns {
return Err(MatrixError::IndexOutOfRange);
}
let mut index = 0;
index += row * self.columns;
index += column;
let index = row * self.columns + column;
return Ok(&self.data[index]);
}
@@ -595,4 +595,105 @@ mod tests {
assert_ne!(m1.data[0], m2.data[0]);
}
#[test]
fn test_get_valid_positions() {
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),
],
};
assert_eq!(*m.get(0, 0).unwrap(), Fraction::from(1));
assert_eq!(*m.get(0, 2).unwrap(), Fraction::from(3));
assert_eq!(*m.get(1, 0).unwrap(), Fraction::from(4));
assert_eq!(*m.get(1, 2).unwrap(), Fraction::from(6));
}
#[test]
fn test_get_middle_element() {
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),
],
};
assert_eq!(*m.get(1, 1).unwrap(), Fraction::from(5));
}
#[test]
fn test_get_out_of_bounds_row() {
let m = Matrix::new(2, 2, Fraction::from(0)).unwrap();
let result = m.get(2, 0);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), MatrixError::IndexOutOfRange);
}
#[test]
fn test_get_out_of_bounds_column() {
let m = Matrix::new(2, 2, Fraction::from(0)).unwrap();
let result = m.get(0, 2);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), MatrixError::IndexOutOfRange);
}
#[test]
fn test_get_last_element() {
let m = Matrix {
rows: 2,
columns: 2,
data: vec![
Fraction::from(10),
Fraction::from(20),
Fraction::from(30),
Fraction::from(40),
],
};
assert_eq!(*m.get(1, 1).unwrap(), Fraction::from(40));
}
#[test]
fn test_get_first_element() {
let m = Matrix::new(3, 3, Fraction::from(7)).unwrap();
assert_eq!(*m.get(0, 0).unwrap(), Fraction::from(7));
}
#[test]
fn test_get_consistency_with_manual_index() {
let m = Matrix {
rows: 3,
columns: 4,
data: (0..12).map(|x| Fraction::from(x as i64)).collect(),
};
for r in 0..3 {
for c in 0..4 {
let expected_index = r * 4 + c;
assert_eq!(*m.get(r, c).unwrap(), Fraction::from(expected_index as i64));
}
}
}
}