fix(rust): tests in likes kata
This commit is contained in:
parent
864453f1eb
commit
c32b453ed3
1 changed files with 17 additions and 68 deletions
|
@ -7,9 +7,6 @@ fn likes(names: &[&str]) -> String {
|
||||||
[a, b, rest @ ..] => format!("{}, {} and {} others like this", a, b, rest.len()),
|
[a, b, rest @ ..] => format!("{}, {} and {} others like this", a, b, rest.len()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::likes;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn fixed_tests() {
|
fn fixed_tests() {
|
||||||
|
@ -29,51 +26,3 @@ mod tests {
|
||||||
"a, b and 3 others like this"
|
"a, b and 3 others like this"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn random_tests() {
|
|
||||||
use rand::{seq::SliceRandom, thread_rng, Rng};
|
|
||||||
|
|
||||||
let mut rng = thread_rng();
|
|
||||||
|
|
||||||
let mut base = vec![
|
|
||||||
"Sylia Stingray",
|
|
||||||
"Priscilla S. Asagiri",
|
|
||||||
"Linna Yamazaki",
|
|
||||||
"Nene Romanova",
|
|
||||||
"Nigel",
|
|
||||||
"Macky Stingray",
|
|
||||||
"Largo",
|
|
||||||
"Brian J. Mason",
|
|
||||||
"Sylvie",
|
|
||||||
"Anri",
|
|
||||||
"Leon McNichol",
|
|
||||||
"Daley Wong",
|
|
||||||
"Galatea",
|
|
||||||
"Quincy Rosenkreutz",
|
|
||||||
];
|
|
||||||
|
|
||||||
for _ in 0..100 {
|
|
||||||
base.shuffle(&mut rng);
|
|
||||||
let len = rng.gen_range(0..base.len());
|
|
||||||
let names = base.iter().take(len).cloned().collect::<Vec<_>>();
|
|
||||||
|
|
||||||
let actual = likes(&names);
|
|
||||||
let expect = likes_solution(&names);
|
|
||||||
|
|
||||||
assert_eq!(actual, expect);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn likes_solution(names: &[&str]) -> String {
|
|
||||||
let l = names.len();
|
|
||||||
|
|
||||||
match l {
|
|
||||||
0 => "no one likes this".to_owned(),
|
|
||||||
1 => format!("{} likes this", names[0]),
|
|
||||||
2 => format!("{} and {} like this", names[0], names[1]),
|
|
||||||
3 => format!("{}, {} and {} like this", names[0], names[1], names[2]),
|
|
||||||
_ => format!("{}, {} and {} others like this", names[0], names[1], l - 2),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue