Skip to content

Commit

Permalink
Add more topological_sort tests (#37)
Browse files Browse the repository at this point in the history
Also make the tests nicer to read and write.
  • Loading branch information
9999years authored Oct 17, 2024
1 parent b0d86a4 commit 8de7be1
Showing 1 changed file with 59 additions and 41 deletions.
100 changes: 59 additions & 41 deletions src/topological_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,65 +102,83 @@ where
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;

#[track_caller]
fn test_topological_sort(input: &[&str], expect: &[&str]) {
let input = input.iter().map(Utf8Path::new).collect::<Vec<_>>();
let expect = expect.iter().map(Utf8Path::new).collect::<Vec<_>>();
assert_eq!(topological_sort(&input).unwrap(), expect);
}

#[test]
fn test_topological_sort_empty() {
assert_eq!(
topological_sort(&Vec::<&Utf8Path>::new()).unwrap(),
Vec::<Utf8PathBuf>::new()
);
test_topological_sort(&[], &[]);
}

#[test]
fn test_topological_sort_unrelated() {
assert_eq!(
topological_sort(&[
Utf8Path::new("/puppy"),
Utf8Path::new("/doggy"),
Utf8Path::new("/softie"),
Utf8Path::new("/cutie"),
])
.unwrap(),
vec![
// TODO: This probably depends on the hash function. >:(
Utf8PathBuf::from("/cutie"),
Utf8PathBuf::from("/softie"),
Utf8PathBuf::from("/doggy"),
Utf8PathBuf::from("/puppy"),
]
test_topological_sort(
&["/puppy", "/doggy", "/softie", "/cutie"],
&["/cutie", "/softie", "/doggy", "/puppy"],
);
}

#[test]
fn test_topological_sort_mixed() {
assert_eq!(
topological_sort(&[
Utf8Path::new("/puppy"),
Utf8Path::new("/puppy/doggy/cutie"),
Utf8Path::new("/puppy/softie"),
Utf8Path::new("/puppy/doggy"),
Utf8Path::new("/silly"),
Utf8Path::new("/silly/goofy"),
])
.unwrap(),
vec![
// TODO: This probably depends on the hash function. >:(
Utf8PathBuf::from("/silly/goofy"),
Utf8PathBuf::from("/silly"),
Utf8PathBuf::from("/puppy/softie"),
Utf8PathBuf::from("/puppy/doggy/cutie"),
Utf8PathBuf::from("/puppy/doggy"),
Utf8PathBuf::from("/puppy"),
]
test_topological_sort(
&[
"/puppy",
"/puppy/doggy/cutie",
"/puppy/softie",
"/puppy/doggy",
"/silly",
"/silly/goofy",
],
&[
"/silly/goofy",
"/silly",
"/puppy/softie",
"/puppy/doggy/cutie",
"/puppy/doggy",
"/puppy",
],
);
}

#[test]
fn test_topological_sort_duplicate() {
// This also warns the user.
assert_eq!(
topological_sort(&[Utf8Path::new("/puppy"), Utf8Path::new("/puppy")]).unwrap(),
vec![Utf8PathBuf::from("/puppy"), Utf8PathBuf::from("/puppy")]
test_topological_sort(&["/puppy", "/puppy"], &["/puppy", "/puppy"]);
}

#[test]
fn test_topological_sort_deterministic() {
test_topological_sort(
&[
"/puppy",
"/silly/puppy",
"/my-repo",
"/silly/my-repo",
"/puppy.git",
"/a",
"/b",
"/c",
"/d/c",
"/e/c",
],
&[
"/e/c",
"/d/c",
"/c",
"/b",
"/a",
"/puppy.git",
"/silly/my-repo",
"/my-repo",
"/silly/puppy",
"/puppy",
],
);
}
}

0 comments on commit 8de7be1

Please sign in to comment.