Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matrix: add tests for zero indexes #1944

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions exercises/practice/matrix/.meta/additional-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@
"With Option in the API, it makes sense to test for None at least once."
]
},
{
"uuid": "83a3ade8-b44b-4e08-9092-89f5ff859050",
"description": "cannot extract row with index zero",
"property": "row",
"input": {
"string": "1 2 3\n4 5 6\n7 8 9",
"index": 0
},
"expected": null,
"comments": [
"Additional test to ensure the method can handle zero indexes without panicking.",
"Upstream does not want to add this test to avoid every exercise being about input validation.",
"A naive solution may handle the 1-based indexing using index - 1.",
"This panics in debug mode when index == 0 due to integer underflow."
]
},
{
"uuid": "304d71d4-cf26-41d4-9b74-cc04441fec8c",
"description": "cannot extract column with no corresponding column in matrix",
Expand All @@ -30,5 +46,21 @@
"Rust puts a lot of emphasis on error handling, hence the idiomatic Option return type.",
"With Option in the API, it makes sense to test for None at least once."
]
},
{
"uuid": "39366889-ccd9-4142-954d-28f76e6036e9",
"description": "cannot extract column with index zero",
"property": "column",
"input": {
"string": "1 2 3\n4 5 6\n7 8 9",
"index": 0
},
"expected": null,
"comments": [
"Additional test to ensure the method can handle zero indexes without panicking.",
"Upstream does not want to add this test to avoid every exercise being about input validation.",
"A naive solution may handle the 1-based indexing using index - 1.",
"This panics in debug mode when index == 0 due to integer underflow."
]
}
]
8 changes: 2 additions & 6 deletions exercises/practice/matrix/.meta/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ impl Matrix {
}

pub fn row(&self, row_no: usize) -> Option<Vec<u32>> {
self.grid.get(row_no - 1).map(|row| row.to_owned())
self.grid.get(row_no.checked_sub(1)?).cloned()
}

pub fn column(&self, col_no: usize) -> Option<Vec<u32>> {
if col_no > self.grid[0].len() {
return None;
};

Some(self.grid.iter().map(|row| row[col_no - 1]).collect())
self.grid.iter().map(|row| row.get(col_no.checked_sub(1)?).copied()).collect()
}
}
14 changes: 14 additions & 0 deletions exercises/practice/matrix/tests/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,23 @@ fn cannot_extract_row_with_no_corresponding_row_in_matrix() {
assert_eq!(matrix.row(4), None);
}

#[test]
#[ignore]
fn cannot_extract_row_with_index_zero() {
let matrix = Matrix::new("1 2 3\n4 5 6\n7 8 9");
assert_eq!(matrix.row(0), None);
}

#[test]
#[ignore]
fn cannot_extract_column_with_no_corresponding_column_in_matrix() {
let matrix = Matrix::new("1 2 3\n4 5 6\n7 8 9");
assert_eq!(matrix.column(4), None);
}

#[test]
#[ignore]
fn cannot_extract_column_with_index_zero() {
let matrix = Matrix::new("1 2 3\n4 5 6\n7 8 9");
assert_eq!(matrix.column(0), None);
}