diff --git a/tweed5-core/src/main/java/de/siphalor/tweed5/core/api/sort/AcyclicGraphSorter.java b/tweed5-core/src/main/java/de/siphalor/tweed5/core/api/sort/AcyclicGraphSorter.java index ac50871..32b62b7 100644 --- a/tweed5-core/src/main/java/de/siphalor/tweed5/core/api/sort/AcyclicGraphSorter.java +++ b/tweed5-core/src/main/java/de/siphalor/tweed5/core/api/sort/AcyclicGraphSorter.java @@ -72,6 +72,9 @@ public class AcyclicGraphSorter { break; } if (!visitedIter.hasNext()) { + if (lastVisited == -1) { + break; + } visitedIter.restart(); } } diff --git a/tweed5-core/src/test/java/de/siphalor/tweed5/core/impl/sort/AcyclicGraphSorterTest.java b/tweed5-core/src/test/java/de/siphalor/tweed5/core/impl/sort/AcyclicGraphSorterTest.java index ffe8ee5..2ed4986 100644 --- a/tweed5-core/src/test/java/de/siphalor/tweed5/core/impl/sort/AcyclicGraphSorterTest.java +++ b/tweed5-core/src/test/java/de/siphalor/tweed5/core/impl/sort/AcyclicGraphSorterTest.java @@ -63,4 +63,14 @@ class AcyclicGraphSorterTest { AcyclicGraphSorter.GraphCycleException exception = assertThrows(AcyclicGraphSorter.GraphCycleException.class, sorter::sort); assertEquals(Arrays.asList(2, 4, 5), exception.cycleIndeces()); } + + @Test + void minimalCycle() { + AcyclicGraphSorter sorter = new AcyclicGraphSorter(2); + sorter.addEdge(0, 1); + sorter.addEdge(1, 0); + + AcyclicGraphSorter.GraphCycleException exception = assertThrows(AcyclicGraphSorter.GraphCycleException.class, sorter::sort); + assertEquals(Arrays.asList(0, 1), exception.cycleIndeces()); + } } \ No newline at end of file