|
| 1 | +package publicapi; |
| 2 | + |
| 3 | +import com.github.moaxcp.graphs.PropertyGraph; |
| 4 | +import com.github.moaxcp.graphs.PropertyGraph.Vertex; |
| 5 | +import com.github.moaxcp.graphs.testframework.TestGraphs; |
| 6 | +import java.util.List; |
| 7 | +import java.util.NoSuchElementException; |
| 8 | +import org.junit.jupiter.api.DisplayName; |
| 9 | +import org.junit.jupiter.params.ParameterizedTest; |
| 10 | +import org.junit.jupiter.params.provider.MethodSource; |
| 11 | + |
| 12 | +import static com.google.common.truth.Truth.assertThat; |
| 13 | +import static java.util.stream.Collectors.toList; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 15 | + |
| 16 | +public class ReversePostOrderDepthFirstIteratorTest { |
| 17 | + |
| 18 | + @TestGraphs |
| 19 | + void nullStart(PropertyGraph<String> graph) { |
| 20 | + var exception = assertThrows(NullPointerException.class, () -> graph.reversePostOrderIterator((String[]) null)); |
| 21 | + assertThat(exception).hasMessageThat().isEqualTo("start is marked non-null but is null"); |
| 22 | + } |
| 23 | + |
| 24 | + @TestGraphs |
| 25 | + void nullInOther(PropertyGraph<String> graph) { |
| 26 | + graph.vertex("A").vertex("B"); |
| 27 | + var exception = assertThrows(NullPointerException.class, () -> graph.reversePostOrderIterator("A", null, "B")); |
| 28 | + assertThat(exception).hasMessageThat().isEqualTo("\"id\" in \"start\" must not be null."); |
| 29 | + } |
| 30 | + |
| 31 | + @TestGraphs |
| 32 | + void startNotInGraph(PropertyGraph<String> graph) { |
| 33 | + var exception = assertThrows(IllegalArgumentException.class, () -> graph.reversePostOrderIterator("A")); |
| 34 | + assertThat(exception).hasMessageThat().isEqualTo("vertex \"A\" not found in graph."); |
| 35 | + } |
| 36 | + |
| 37 | + @TestGraphs |
| 38 | + void hasNext_EmptyGraph(PropertyGraph<String> graph) { |
| 39 | + var iterator = graph.reversePostOrderIterator(); |
| 40 | + assertThat(iterator.hasNext()).isFalse(); |
| 41 | + } |
| 42 | + |
| 43 | + @TestGraphs |
| 44 | + void next_EmptyGraph(PropertyGraph<String> graph) { |
| 45 | + var iterator = graph.reversePostOrderIterator(); |
| 46 | + var exception = assertThrows(NoSuchElementException.class, () -> iterator.next()); |
| 47 | + assertThat(exception).hasMessageThat().isEqualTo("Could not find next element."); |
| 48 | + } |
| 49 | + |
| 50 | + @TestGraphs |
| 51 | + void hasNext_beforeIteration(PropertyGraph<String> graph) { |
| 52 | + graph.vertex("A"); |
| 53 | + var iterator = graph.reversePostOrderIterator(); |
| 54 | + assertThat(iterator.hasNext()).isTrue(); |
| 55 | + } |
| 56 | + |
| 57 | + @TestGraphs |
| 58 | + void hasNext_MultipleBeforeIteration(PropertyGraph<String> graph) { |
| 59 | + graph.vertex("A"); |
| 60 | + var iterator = graph.reversePostOrderIterator(); |
| 61 | + assertThat(iterator.hasNext()).isTrue(); |
| 62 | + assertThat(iterator.hasNext()).isTrue(); |
| 63 | + assertThat(iterator.hasNext()).isTrue(); |
| 64 | + assertThat(iterator.next().getId()).isEqualTo("A"); |
| 65 | + } |
| 66 | + |
| 67 | + @TestGraphs |
| 68 | + void hasNext_MultipleBetweenComponents(PropertyGraph<String> graph) { |
| 69 | + graph.vertex("A"); |
| 70 | + graph.vertex("B"); |
| 71 | + var iterator = graph.reversePostOrderIterator(); |
| 72 | + assertThat(iterator.hasNext()).isTrue(); |
| 73 | + assertThat(iterator.next().getId()).isEqualTo("B"); |
| 74 | + assertThat(iterator.hasNext()).isTrue(); |
| 75 | + assertThat(iterator.hasNext()).isTrue(); |
| 76 | + assertThat(iterator.hasNext()).isTrue(); |
| 77 | + assertThat(iterator.next().getId()).isEqualTo("A"); |
| 78 | + } |
| 79 | + |
| 80 | + @TestGraphs |
| 81 | + void next_withoutCallingHasNext(PropertyGraph<String> graph) { |
| 82 | + graph.vertex("A"); |
| 83 | + var iterator = graph.reversePostOrderIterator(); |
| 84 | + assertThat(iterator.next().getId()).isEqualTo("A"); |
| 85 | + assertThat(iterator.hasNext()).isFalse(); |
| 86 | + } |
| 87 | + |
| 88 | + @MethodSource("com.github.moaxcp.graphs.testframework.MethodSources#graphsReversePostOrder") |
| 89 | + @DisplayName("reversePostOrderIterator matches expected order") |
| 90 | + @ParameterizedTest(name = "{index} - {0} {2}") |
| 91 | + void reversePostOrderIterator(String name, PropertyGraph<String> graph, String[] start, List<String> expectedOrder) { |
| 92 | + var iterator = graph.reversePostOrderIterator(start); |
| 93 | + for(String expected : expectedOrder) { |
| 94 | + String result = iterator.next().getId(); |
| 95 | + assertThat(result).isEqualTo(expected); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @MethodSource("com.github.moaxcp.graphs.testframework.MethodSources#graphsReversePostOrder") |
| 100 | + @DisplayName("reversePostOrderStream matches expected order") |
| 101 | + @ParameterizedTest(name = "{index} - {0} {2}") |
| 102 | + void reversePostOrderStream(String name, PropertyGraph<String> graph, String[] start, List<String> expectedOrder) { |
| 103 | + var result = graph.reversePostOrderStream(start) |
| 104 | + .map(Vertex::getId) |
| 105 | + .collect(toList()); |
| 106 | + |
| 107 | + assertThat(result).isEqualTo(expectedOrder); |
| 108 | + } |
| 109 | +} |
0 commit comments