1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

tsort: print nodes and cycles as they are visited

Update `tsort` so that

* nodes are printed as they are visited,
* cycles are printed as they are discovered,
* finding a cycle doesn't terminate the traversal,
* multiple cycles can be found and displayed.

Fixes #7074
This commit is contained in:
Jeffrey Finkelstein 2025-01-07 19:42:45 -05:00
parent 1bb33e0446
commit af99952de6
2 changed files with 122 additions and 30 deletions

View file

@ -83,3 +83,31 @@ fn test_split_on_any_whitespace() {
.succeeds()
.stdout_only("a\nb\n");
}
#[test]
fn test_cycle() {
// The graph looks like: a --> b <==> c --> d
new_ucmd!()
.pipe_in("a b b c c d c b")
.fails()
.code_is(1)
.stdout_is("a\nc\nd\nb\n")
.stderr_is("tsort: -: input contains a loop:\ntsort: b\ntsort: c\n");
}
#[test]
fn test_two_cycles() {
// The graph looks like:
//
// a
// |
// V
// c <==> b <==> d
//
new_ucmd!()
.pipe_in("a b b c c b b d d b")
.fails()
.code_is(1)
.stdout_is("a\nc\nd\nb\n")
.stderr_is("tsort: -: input contains a loop:\ntsort: b\ntsort: c\ntsort: -: input contains a loop:\ntsort: b\ntsort: d\n");
}