| 1 | use craterust-analyzer cargo vcs-graph 3.6.0 crate/::tsortrust-analyzer cargo vcs-graph 3.6.0 tsort/::TopoSorterrust-analyzer cargo vcs-graph 3.6.0 tsort/TopoSorter#; |
| 2 | use craterust-analyzer cargo vcs-graph 3.6.0 crate/::Errorrust-analyzer cargo vcs-graph 3.6.0 Error#; |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#; |
| 4 | |
| 5 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 6 | fn test_tsort_emptyrust-analyzer cargo vcs-graph 3.6.0 test/test_tsort_empty().() { |
| 7 | let graph = HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V>`]new().(); |
| 8 | assert_sort_and_iteraterust-analyzer cargo vcs-graph 3.6.0 test/assert_sort_and_iterate().(&graph, &[]); |
| 9 | } |
| 10 | |
| 11 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 12 | fn test_tsort_easyrust-analyzer cargo vcs-graph 3.6.0 test/test_tsort_easy().() { |
| 13 | let graph = [(0, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![])].iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 14 | assert_sort_and_iteraterust-analyzer cargo vcs-graph 3.6.0 test/assert_sort_and_iterate().(&graph, &[0]); |
| 15 | } |
| 16 | |
| 17 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 18 | fn test_tsort_cyclerust-analyzer cargo vcs-graph 3.6.0 test/test_tsort_cycle().() { |
| 19 | let graph = [(0, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1]), (1, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0])].iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 20 | assert_sort_and_iterate_cyclerust-analyzer cargo vcs-graph 3.6.0 test/assert_sort_and_iterate_cycle().(&graph); |
| 21 | } |
| 22 | |
| 23 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 24 | fn test_tsort_cycle_2rust-analyzer cargo vcs-graph 3.6.0 test/test_tsort_cycle_2().() { |
| 25 | let graph = [(0, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1]), (1, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![2]), (2, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0])] |
| 26 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 27 | .clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().() |
| 28 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 29 | assert_sort_and_iterate_cyclerust-analyzer cargo vcs-graph 3.6.0 test/assert_sort_and_iterate_cycle().(&graph); |
| 30 | } |
| 31 | |
| 32 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 33 | fn test_topo_sort_cycle_with_tailrust-analyzer cargo vcs-graph 3.6.0 test/test_topo_sort_cycle_with_tail().() { |
| 34 | let graph = [ |
| 35 | (0, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1]), |
| 36 | (1, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![2]), |
| 37 | (2, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![3, 4]), |
| 38 | (3, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0]), |
| 39 | (4, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]), |
| 40 | ] |
| 41 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 42 | .clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().() |
| 43 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 44 | assert_sort_and_iterate_cyclerust-analyzer cargo vcs-graph 3.6.0 test/assert_sort_and_iterate_cycle().(&graph); |
| 45 | } |
| 46 | |
| 47 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 48 | fn test_tsort_1rust-analyzer cargo vcs-graph 3.6.0 test/test_tsort_1().() { |
| 49 | let graph = [ |
| 50 | (0, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![3]), |
| 51 | (1, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![4]), |
| 52 | (2, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1, 4]), |
| 53 | (3, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]), |
| 54 | (4, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 3]), |
| 55 | ] |
| 56 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 57 | .clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().() |
| 58 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 59 | assert_sort_and_iterate_orderrust-analyzer cargo vcs-graph 3.6.0 test/assert_sort_and_iterate_order().(&graph); |
| 60 | } |
| 61 | |
| 62 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 63 | fn test_tsort_partialrust-analyzer cargo vcs-graph 3.6.0 test/test_tsort_partial().() { |
| 64 | let graph = [ |
| 65 | (0, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]), |
| 66 | (1, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0]), |
| 67 | (2, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0]), |
| 68 | (3, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0]), |
| 69 | (4, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1, 2, 3]), |
| 70 | (5, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1, 2]), |
| 71 | (6, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1, 2]), |
| 72 | (7, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![2, 3]), |
| 73 | (8, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 1, 4, 5, 6]), |
| 74 | ] |
| 75 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 76 | .clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().() |
| 77 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 78 | assert_sort_and_iterate_orderrust-analyzer cargo vcs-graph 3.6.0 test/assert_sort_and_iterate_order().(&graph); |
| 79 | } |
| 80 | |
| 81 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 82 | fn test_tsort_unincluded_parentrust-analyzer cargo vcs-graph 3.6.0 test/test_tsort_unincluded_parent().() { |
| 83 | let graph = [(0, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1]), (1, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![2])].iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 84 | assert_sort_and_iteraterust-analyzer cargo vcs-graph 3.6.0 test/assert_sort_and_iterate().(&graph, &[1, 0]); |
| 85 | } |
| 86 | |
| 87 | fn topo_sortrust-analyzer cargo vcs-graph 3.6.0 test/topo_sort().(graph: &HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<usize, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<usize>>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<usize>, Errorrust-analyzer cargo vcs-graph 3.6.0 Error#<usize>> { |
| 88 | TopoSorterrust-analyzer cargo vcs-graph 3.6.0 tsort/TopoSorter#::newrust-analyzer cargo vcs-graph 3.6.0 tsort/impl#[`TopoSorter<K>`]new().(graph.clonerust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][Clone]clone().().into_iterrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][IntoIterator]into_iter().()).sortedrust-analyzer cargo vcs-graph 3.6.0 tsort/impl#[`TopoSorter<K>`]sorted().() |
| 89 | } |
| 90 | |
| 91 | fn assert_sort_and_iterate_orderrust-analyzer cargo vcs-graph 3.6.0 test/assert_sort_and_iterate_order().(graph: &HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<usize, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<usize>>) { |
| 92 | let sort_result = topo_sortrust-analyzer cargo vcs-graph 3.6.0 test/topo_sort().(graph).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 93 | |
| 94 | for (node, parents) in graph { |
| 95 | for parent in parents { |
| 96 | if sort_result.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]position().(|&n| n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().node).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().() |
| 97 | <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().sort_result.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]position().(|&n| n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().parent).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().() |
| 98 | { |
| 99 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!( |
| 100 | "parent {} must come before child {}:\n{:?}", |
| 101 | parent, node, sort_result |
| 102 | ); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | fn assert_sort_and_iterate_cyclerust-analyzer cargo vcs-graph 3.6.0 test/assert_sort_and_iterate_cycle().(graph: &HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<usize, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<usize>>) { |
| 109 | let sort_result = topo_sortrust-analyzer cargo vcs-graph 3.6.0 test/topo_sort().(graph); |
| 110 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(sort_result.is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 111 | } |
| 112 | |
| 113 | fn assert_sort_and_iteraterust-analyzer cargo vcs-graph 3.6.0 test/assert_sort_and_iterate().(graph: &HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<usize, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<usize>>, expected: &[usize]) { |
| 114 | let sort_result = topo_sortrust-analyzer cargo vcs-graph 3.6.0 test/topo_sort().(graph).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 115 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sort_result, expected); |
| 116 | } |