mirror of
https://github.com/Magnus167/rustframe.git
synced 2025-08-20 04:00:01 +00:00
Add shape method to Matrix and corresponding unit test
This commit is contained in:
parent
54a266b630
commit
85154a3be0
@ -89,6 +89,10 @@ impl<T: Clone> Matrix<T> {
|
|||||||
self.cols
|
self.cols
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn shape(&self) -> (usize, usize) {
|
||||||
|
(self.rows, self.cols)
|
||||||
|
}
|
||||||
|
|
||||||
/// Get element reference (immutable). Panics on out-of-bounds.
|
/// Get element reference (immutable). Panics on out-of-bounds.
|
||||||
pub fn get(&self, r: usize, c: usize) -> &T {
|
pub fn get(&self, r: usize, c: usize) -> &T {
|
||||||
&self[(r, c)]
|
&self[(r, c)]
|
||||||
@ -1153,6 +1157,14 @@ mod tests {
|
|||||||
assert_eq!(ma.row(2), &[3, 6, 9]);
|
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]
|
#[test]
|
||||||
#[should_panic(expected = "row index 3 out of bounds for 3 rows")]
|
#[should_panic(expected = "row index 3 out of bounds for 3 rows")]
|
||||||
fn test_row_out_of_bounds() {
|
fn test_row_out_of_bounds() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user