Add shape method to Matrix and corresponding unit test

This commit is contained in:
Palash Tyagi 2025-07-06 18:58:38 +01:00
parent 54a266b630
commit 85154a3be0

View File

@ -89,6 +89,10 @@ impl<T: Clone> Matrix<T> {
self.cols
}
pub fn shape(&self) -> (usize, usize) {
(self.rows, self.cols)
}
/// Get element reference (immutable). Panics on out-of-bounds.
pub fn get(&self, r: usize, c: usize) -> &T {
&self[(r, c)]
@ -1153,6 +1157,14 @@ mod tests {
assert_eq!(ma.row(2), &[3, 6, 9]);
}
#[test]
fn test_shape(){
let ma = static_test_matrix_2x4();
assert_eq!(ma.shape(), (2, 4));
assert_eq!(ma.rows(), 2);
assert_eq!(ma.cols(), 4);
}
#[test]
#[should_panic(expected = "row index 3 out of bounds for 3 rows")]
fn test_row_out_of_bounds() {