-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
344 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
graphs-manual/src/test/java/com/github/moaxcp/graphs/manual/ReversePostorderTraversal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.github.moaxcp.graphs.manual; | ||
|
||
import com.github.moaxcp.graphs.DirectedPropertyGraph; | ||
import com.github.moaxcp.graphs.PropertyGraph.Vertex; | ||
import com.github.moaxcp.graphs.graphviz.greenrobotgif.GreenrobotGifSubscriber; | ||
import com.github.moaxcp.graphs.greenrobot.DirectedEventPropertyGraph; | ||
import java.nio.file.Path; | ||
import java.util.Iterator; | ||
import org.greenrobot.eventbus.EventBus; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ReversePostorderTraversal { | ||
|
||
@Test | ||
void reversePostOrderTraversal() { | ||
var bus = new EventBus(); | ||
var graph = new DirectedEventPropertyGraph<String>(bus); | ||
|
||
graph.edge("A", "B") | ||
.edge("B", "C") | ||
.edge("B", "D") | ||
.edge("D", "E") | ||
.edge("D", "C") | ||
.edge("A", "D") | ||
.edge("D", "A") | ||
.edge("A", "E") | ||
.edge("F", "G") | ||
.edge("G", "D"); | ||
var gif = new GreenrobotGifSubscriber<>(graph, Path.of("src/docs/asciidoc/images/reversePostOrderTraversal.gif")); | ||
|
||
// tag::reversePostOrderIterator[] | ||
Iterator<Vertex<String>> iterator = graph.reversePostOrderIterator("A"); | ||
while(iterator.hasNext()) { | ||
Vertex<String> vertex = iterator.next(); | ||
vertex.property("color", "green"); | ||
} | ||
// end::reversePostOrderIterator[] | ||
gif.writeFile(); | ||
} | ||
|
||
@Test | ||
void postOrderStream() { | ||
var graph = new DirectedPropertyGraph<String>(); | ||
|
||
graph.edge("A", "B") | ||
.edge("B", "C") | ||
.edge("B", "D") | ||
.edge("D", "E") | ||
.edge("D", "C") | ||
.edge("A", "D") | ||
.edge("D", "A") | ||
.edge("A", "E") | ||
.edge("F", "G") | ||
.edge("G", "D"); | ||
// tag::reversePostOrderStream[] | ||
graph.reversePostOrderStream("A").forEach(v -> v.property("color", "green")); | ||
// end::reversePostOrderStream[] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
graphs-test/src/test/java/publicapi/ReversePostOrderDepthFirstIteratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package publicapi; | ||
|
||
import com.github.moaxcp.graphs.PropertyGraph; | ||
import com.github.moaxcp.graphs.PropertyGraph.Vertex; | ||
import com.github.moaxcp.graphs.testframework.TestGraphs; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static java.util.stream.Collectors.toList; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class ReversePostOrderDepthFirstIteratorTest { | ||
|
||
@TestGraphs | ||
void nullStart(PropertyGraph<String> graph) { | ||
var exception = assertThrows(NullPointerException.class, () -> graph.reversePostOrderIterator((String[]) null)); | ||
assertThat(exception).hasMessageThat().isEqualTo("start is marked non-null but is null"); | ||
} | ||
|
||
@TestGraphs | ||
void nullInOther(PropertyGraph<String> graph) { | ||
graph.vertex("A").vertex("B"); | ||
var exception = assertThrows(NullPointerException.class, () -> graph.reversePostOrderIterator("A", null, "B")); | ||
assertThat(exception).hasMessageThat().isEqualTo("\"id\" in \"start\" must not be null."); | ||
} | ||
|
||
@TestGraphs | ||
void startNotInGraph(PropertyGraph<String> graph) { | ||
var exception = assertThrows(IllegalArgumentException.class, () -> graph.reversePostOrderIterator("A")); | ||
assertThat(exception).hasMessageThat().isEqualTo("vertex \"A\" not found in graph."); | ||
} | ||
|
||
@TestGraphs | ||
void hasNext_EmptyGraph(PropertyGraph<String> graph) { | ||
var iterator = graph.reversePostOrderIterator(); | ||
assertThat(iterator.hasNext()).isFalse(); | ||
} | ||
|
||
@TestGraphs | ||
void next_EmptyGraph(PropertyGraph<String> graph) { | ||
var iterator = graph.reversePostOrderIterator(); | ||
var exception = assertThrows(NoSuchElementException.class, () -> iterator.next()); | ||
assertThat(exception).hasMessageThat().isEqualTo("Could not find next element."); | ||
} | ||
|
||
@TestGraphs | ||
void hasNext_beforeIteration(PropertyGraph<String> graph) { | ||
graph.vertex("A"); | ||
var iterator = graph.reversePostOrderIterator(); | ||
assertThat(iterator.hasNext()).isTrue(); | ||
} | ||
|
||
@TestGraphs | ||
void hasNext_MultipleBeforeIteration(PropertyGraph<String> graph) { | ||
graph.vertex("A"); | ||
var iterator = graph.reversePostOrderIterator(); | ||
assertThat(iterator.hasNext()).isTrue(); | ||
assertThat(iterator.hasNext()).isTrue(); | ||
assertThat(iterator.hasNext()).isTrue(); | ||
assertThat(iterator.next().getId()).isEqualTo("A"); | ||
} | ||
|
||
@TestGraphs | ||
void hasNext_MultipleBetweenComponents(PropertyGraph<String> graph) { | ||
graph.vertex("A"); | ||
graph.vertex("B"); | ||
var iterator = graph.reversePostOrderIterator(); | ||
assertThat(iterator.hasNext()).isTrue(); | ||
assertThat(iterator.next().getId()).isEqualTo("B"); | ||
assertThat(iterator.hasNext()).isTrue(); | ||
assertThat(iterator.hasNext()).isTrue(); | ||
assertThat(iterator.hasNext()).isTrue(); | ||
assertThat(iterator.next().getId()).isEqualTo("A"); | ||
} | ||
|
||
@TestGraphs | ||
void next_withoutCallingHasNext(PropertyGraph<String> graph) { | ||
graph.vertex("A"); | ||
var iterator = graph.reversePostOrderIterator(); | ||
assertThat(iterator.next().getId()).isEqualTo("A"); | ||
assertThat(iterator.hasNext()).isFalse(); | ||
} | ||
|
||
@MethodSource("com.github.moaxcp.graphs.testframework.MethodSources#graphsReversePostOrder") | ||
@DisplayName("reversePostOrderIterator matches expected order") | ||
@ParameterizedTest(name = "{index} - {0} {2}") | ||
void reversePostOrderIterator(String name, PropertyGraph<String> graph, String[] start, List<String> expectedOrder) { | ||
var iterator = graph.reversePostOrderIterator(start); | ||
for(String expected : expectedOrder) { | ||
String result = iterator.next().getId(); | ||
assertThat(result).isEqualTo(expected); | ||
} | ||
} | ||
|
||
@MethodSource("com.github.moaxcp.graphs.testframework.MethodSources#graphsReversePostOrder") | ||
@DisplayName("reversePostOrderStream matches expected order") | ||
@ParameterizedTest(name = "{index} - {0} {2}") | ||
void reversePostOrderStream(String name, PropertyGraph<String> graph, String[] start, List<String> expectedOrder) { | ||
var result = graph.reversePostOrderStream(start) | ||
.map(Vertex::getId) | ||
.collect(toList()); | ||
|
||
assertThat(result).isEqualTo(expectedOrder); | ||
} | ||
} |
Oops, something went wrong.