Skip to content

Commit

Permalink
Merge pull request #477 from evenfurther/clippy-fixes-in-tests
Browse files Browse the repository at this point in the history
Clippy fixes in tests
  • Loading branch information
samueltardieu authored Nov 29, 2023
2 parents 9da0c54 + d6d14cd commit 1df26a1
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 24 deletions.
3 changes: 3 additions & 0 deletions benches/movingai.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Test with files from https://movingai.com/benchmarks/

#![allow(clippy::cast_precision_loss)]

use codspeed_criterion_compat::{criterion_group, criterion_main, Criterion};
use movingai::parser::{parse_map_file, parse_scen_file};
use movingai::{Coords2D, Map2D};
Expand All @@ -11,6 +13,7 @@ fn distance(a: &Coords2D, b: &Coords2D) -> R64 {
r64((a.0 as f64 - b.0 as f64).hypot(a.1 as f64 - b.1 as f64))
}

#[allow(clippy::missing_panics_doc)]
pub fn arena(c: &mut Criterion) {
c.bench_function("arena", |b| {
b.iter(|| {
Expand Down
2 changes: 2 additions & 0 deletions examples/sliding-puzzle.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::cast_possible_truncation)]

use itertools::Itertools;
use lazy_static::lazy_static;
use pathfinding::prelude::{astar, idastar};
Expand Down
10 changes: 8 additions & 2 deletions src/directed/edmonds_karp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@ where
let reverse = vertices.iter().collect::<FxIndexSet<_>>();
let mut capacities = EK::new(
vertices.len(),
reverse.get_index_of(source).unwrap(),
reverse.get_index_of(sink).unwrap(),
match reverse.get_index_of(source) {
Some(s) => s,
None => panic!("source not found in vertices"),
},
match reverse.get_index_of(sink) {
Some(s) => s,
None => panic!("sink not found in vertices"),
},
);
for ((from, to), capacity) in caps {
capacities.set_capacity(
Expand Down
10 changes: 8 additions & 2 deletions src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,10 @@ where
IC: IntoIterator<Item = C>,
{
fn from_iter<T: IntoIterator<Item = IC>>(iter: T) -> Self {
Matrix::from_rows(iter).unwrap()
match Matrix::from_rows(iter) {
Ok(matrix) => matrix,
Err(e) => panic!("{e}"),
}
}
}

Expand Down Expand Up @@ -735,7 +738,10 @@ macro_rules! matrix {
let mut m = pathfinding::matrix::Matrix::new_empty($a.len());
m.extend(&$a).unwrap();
$(
m.extend(&$b).expect("all rows must have the same width");
match m.extend(&$b) {
Ok(row) => row,
Err(_) => panic!("all rows must have the same width"),
}
)*
m
}};
Expand Down
6 changes: 4 additions & 2 deletions tests/codejam-2017-a.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Problem A from the Google Code Jam finals 2017.
// https://code.google.com/codejam/contest/dashboard?c=6314486#s=p0&a=0

#![allow(clippy::cast_sign_loss)]

use pathfinding::prelude::*;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::io::prelude::*;
Expand Down Expand Up @@ -35,10 +37,10 @@ fn read_ints(file: &mut dyn BufRead) -> Result<Vec<usize>, Error> {
}

fn test<EK: EdmondsKarp<i32>>(n: usize, file: &mut dyn BufRead) -> Result<String, Error> {
let ndices = read_ints(file)?[0];
let n_dices = read_ints(file)?[0];
let mut dices = Vec::new();
let mut values = HashMap::new();
for d in 0..ndices {
for d in 0..n_dices {
let mut dice = read_ints(file)?;
for v in dice.clone() {
values.entry(v).or_insert_with(HashSet::new).insert(d);
Expand Down
4 changes: 2 additions & 2 deletions tests/dijkstra-all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ fn partial_paths() {
// We cannot compare other paths since there is no guarantee that the
// paths variable is up-to-date as the algorithm stopped prematurely.
let cost = paths[&target].1;
let (path, dcost) =
let (path, dijkstra_cost) =
dijkstra(&start, neighbours(network.clone()), |&n| n == target).unwrap();
assert_eq!(
cost, dcost,
cost, dijkstra_cost,
"costs {start} -> {target} differ in {network:?}"
);
let other_path = build_path(&target, &paths);
Expand Down
7 changes: 4 additions & 3 deletions tests/edmondskarp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn wikipedia_example_sparse() {
wikipedia_example::<SparseCapacity<_>>();
}

#[allow(clippy::cast_possible_truncation)]
fn wikipedia_progressive_example<EK: EdmondsKarp<i32>>() {
let successors = successors_wikipedia();
let size = successors.len();
Expand Down Expand Up @@ -154,20 +155,20 @@ fn modified_sparse() {
}

#[test]
#[should_panic]
#[should_panic(expected = "source is greater or equal than size")]
fn empty() {
let mut ek = DenseCapacity::<i32>::new(0, 0, 0);
ek.augment();
}

#[test]
#[should_panic]
#[should_panic(expected = "source not found in vertices")]
fn unknown_source() {
edmonds_karp_dense(&[1, 2, 3], &0, &3, Vec::<((i32, i32), i32)>::new());
}

#[test]
#[should_panic]
#[should_panic(expected = "sink not found in vertices")]
fn unknown_sink() {
edmonds_karp_dense(&[1, 2, 3], &1, &4, Vec::<((i32, i32), i32)>::new());
}
Expand Down
1 change: 1 addition & 0 deletions tests/gps.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![cfg(test)]
#![allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]

use pathfinding::prelude::*;
use std::collections::HashMap;
Expand Down
2 changes: 1 addition & 1 deletion tests/kuhn_munkres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn empty() {
}

#[test]
#[should_panic]
#[should_panic(expected = "number of rows must not be larger than number of columns")]
fn unbalanced() {
kuhn_munkres(&Matrix::new(3, 2, 0));
}
31 changes: 19 additions & 12 deletions tests/matrix.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![cfg(test)]

use pathfinding::{matrix, matrix::Matrix};
use pathfinding::{
matrix,
matrix::{Matrix, MatrixFormatError},
};

#[test]
fn sm() {
Expand Down Expand Up @@ -55,7 +58,7 @@ fn from_vec_error() {
}

#[test]
#[should_panic]
#[should_panic(expected = "unable to create a matrix with empty rows")]
fn new_empty_row_panic() {
let _ = Matrix::new(1, 0, 42);
}
Expand Down Expand Up @@ -83,15 +86,19 @@ fn square_from_vec() {
}

#[test]
#[should_panic]
fn from_vec_panic() {
Matrix::from_vec(2, 3, vec![1, 2, 3]).unwrap();
assert!(matches!(
Matrix::from_vec(2, 3, vec![1, 2, 3]),
Err(MatrixFormatError::WrongLength)
));
}

#[test]
#[should_panic]
fn square_from_vec_panic() {
Matrix::square_from_vec(vec![1, 2, 3]).unwrap();
assert!(matches!(
Matrix::square_from_vec(vec![1, 2, 3]),
Err(MatrixFormatError::WrongLength)
));
}

#[test]
Expand Down Expand Up @@ -175,13 +182,13 @@ fn non_square_rotate() {
}

#[test]
#[should_panic]
#[should_panic(expected = "this operation would create a matrix with empty rows")]
fn no_rows_rotated_cw_panic() {
let _ = Matrix::<u32>::new_empty(10).rotated_cw(1);
}

#[test]
#[should_panic]
#[should_panic(expected = "this operation would create a matrix with empty rows")]
fn no_rows_rotated_ccw_panic() {
let _ = Matrix::<u32>::new_empty(10).rotated_ccw(1);
}
Expand Down Expand Up @@ -215,7 +222,7 @@ fn transpose() {
}

#[test]
#[should_panic]
#[should_panic(expected = "this operation would create a matrix with empty rows")]
fn no_rows_transposed_panic() {
let _ = Matrix::<u32>::new_empty(10).transposed();
}
Expand Down Expand Up @@ -334,13 +341,13 @@ fn matrix_macro() {
}

#[test]
#[should_panic]
#[should_panic(expected = "all rows must have the same width")]
fn matrix_macro_inconsistent_panic() {
matrix![[0, 1, 2], [3, 4]];
}

#[test]
#[should_panic]
#[should_panic(expected = "all rows must have the same width")]
fn matrix_macro_inconsistent_panic_2() {
matrix![0, 1, 2; 3, 4];
}
Expand Down Expand Up @@ -516,7 +523,7 @@ fn from_iter() {
}

#[test]
#[should_panic]
#[should_panic(expected = "provided data does not correspond to the expected length")]
fn from_iter_error() {
let _ = (1..3)
.map(|n| (1..n).map(move |x| x * n))
Expand Down

0 comments on commit 1df26a1

Please sign in to comment.