Allow generic bubble sorting + parameterised tests
parent
3ef2285c22
commit
42929247c4
@ -1,18 +1,22 @@
|
|||||||
use rand::seq::SliceRandom;
|
use std::fmt::Debug;
|
||||||
|
use rand::{
|
||||||
|
seq::SliceRandom,
|
||||||
|
thread_rng
|
||||||
|
};
|
||||||
use sorting;
|
use sorting;
|
||||||
use rand::thread_rng;
|
use test_case::test_case;
|
||||||
|
use sorting::bubble_sort::sort;
|
||||||
|
|
||||||
#[test]
|
#[test_case((0..20).rev().collect(), (0..20).collect() ; "integers")]
|
||||||
fn bubble_sort_ascending() {
|
#[test_case(vec!["c", "b", "a"], vec!["a", "b", "c"] ; "strings")]
|
||||||
|
#[test_case(vec![6.0, 10.0, 2.0, 4.0, 8.0], vec![2.0, 4.0, 6.0, 8.0, 10.0] ; "floats")]
|
||||||
|
fn can_bubble_sort<T: PartialOrd + Copy + Debug>(mut actual: Vec<T>, expected: Vec<T>) {
|
||||||
// Arrange
|
// Arrange
|
||||||
let mut vec: Vec<usize> = (0..20).rev().collect();
|
actual.shuffle(&mut thread_rng());
|
||||||
vec.shuffle(&mut thread_rng());
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
sorting::bubble_sort::sort(&mut vec);
|
sort(&mut actual);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
for i in 0..20 {
|
assert_eq!(actual, expected);
|
||||||
assert_eq!(vec[i], i);
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue