@Override public MutableGraph<Integer> createGraph() { return GraphBuilder.directed().allowsSelfLoops(true).build(); }
@Override public MutableGraph<Integer> createGraph() { return GraphBuilder.directed().allowsSelfLoops(false).build(); }
@Test public void directedGraph() { testGraphMutation(GraphBuilder.directed()); }
private static <N> ImmutableGraph<N> graphWithNode(N node) { MutableGraph<N> graph = GraphBuilder.directed().build(); graph.addNode(node); return ImmutableGraph.copyOf(graph); }
private static ImmutableGraph<Character> createSingleRootGraph() { MutableGraph<Character> graph = GraphBuilder.directed().build(); graph.addNode('a'); return ImmutableGraph.copyOf(graph); }
@Test public void copyOfImmutableGraph_optimized() { Graph<String> graph1 = ImmutableGraph.copyOf(GraphBuilder.directed().<String>build()); Graph<String> graph2 = ImmutableGraph.copyOf(graph1); assertThat(graph2).isSameAs(graph1); }
private static MutableGraph<Integer> createGraph(EdgeType edgeType) { switch (edgeType) { case UNDIRECTED: return GraphBuilder.undirected().allowsSelfLoops(true).build(); case DIRECTED: return GraphBuilder.directed().allowsSelfLoops(true).build(); default: throw new IllegalStateException("Unexpected edge type: " + edgeType); } }
@Test public void immutableGraph() { MutableGraph<String> mutableGraph = GraphBuilder.directed().build(); mutableGraph.addNode("A"); ImmutableGraph<String> immutableGraph = ImmutableGraph.copyOf(mutableGraph); assertThat(immutableGraph).isNotInstanceOf(MutableValueGraph.class); assertThat(immutableGraph).isEqualTo(mutableGraph); mutableGraph.addNode("B"); assertThat(immutableGraph).isNotEqualTo(mutableGraph); }
@Test @SuppressWarnings("CheckReturnValue") public void forTree_acceptsDirectedGraph() throws Exception { MutableGraph<String> graph = GraphBuilder.directed().build(); graph.putEdge("a", "b"); Traverser.forTree(graph); // Does not throw }
private static MutableGraph<Integer> buildDirectedGraph() { MutableGraph<Integer> directedGraph = GraphBuilder.directed().allowsSelfLoops(true).build(); directedGraph.putEdge(N1, N1); directedGraph.putEdge(N1, N2); directedGraph.putEdge(N2, N1); return directedGraph; }
@Test public void nodeOrder_default() { MutableGraph<Integer> graph = GraphBuilder.directed().build(); addNodes(graph); assertThat(graph.nodeOrder()).isEqualTo(insertion()); assertThat(graph.nodes()).containsExactly(3, 1, 4).inOrder(); }
@Test public void nodeOrder_none() { MutableGraph<Integer> graph = GraphBuilder.directed().nodeOrder(unordered()).build(); assertThat(graph.nodeOrder()).isEqualTo(unordered()); }
@Test public void nodeOrder_natural() { MutableGraph<Integer> graph = GraphBuilder.directed().nodeOrder(ElementOrder.<Integer>natural()).build(); addNodes(graph); assertThat(graph.nodeOrder()).isEqualTo(ElementOrder.sorted(Ordering.<Integer>natural())); assertThat(graph.nodes()).containsExactly(1, 3, 4).inOrder(); }
@Test public void nodeOrder_sorted() { MutableGraph<Integer> graph = GraphBuilder.directed() .nodeOrder(ElementOrder.sorted(Ordering.<Integer>natural().reverse())) .build(); addNodes(graph); assertThat(graph.nodeOrder()) .isEqualTo(ElementOrder.sorted(Ordering.<Integer>natural().reverse())); assertThat(graph.nodes()).containsExactly(4, 3, 1).inOrder(); }
@Test public void endpointPair_directed_contains() { MutableGraph<Integer> directedGraph = GraphBuilder.directed().allowsSelfLoops(true).build(); directedGraph.putEdge(N1, N1); directedGraph.putEdge(N1, N2); Set<EndpointPair<Integer>> edges = directedGraph.edges(); assertThat(edges).hasSize(2); assertThat(edges).contains(EndpointPair.ordered(N1, N1)); assertThat(edges).contains(EndpointPair.ordered(N1, N2)); // unordered endpoints not OK for directed graph (undefined behavior) assertThat(edges).doesNotContain(EndpointPair.unordered(N1, N2)); assertThat(edges).doesNotContain(EndpointPair.ordered(N2, N1)); // wrong order assertThat(edges).doesNotContain(EndpointPair.ordered(N2, N2)); // edge not present assertThat(edges).doesNotContain(EndpointPair.ordered(N3, N4)); // nodes not in graph }
@Test public void inducedSubgraph_graph() { Set<Integer> nodeSubset = ImmutableSet.of(N1, N2, N4); MutableGraph<Integer> directedGraph = GraphBuilder.directed().allowsSelfLoops(true).build(); directedGraph.putEdge(N1, N2); directedGraph.putEdge(N2, N1); directedGraph.putEdge(N1, N3); // only incident to one node in nodeSubset directedGraph.putEdge(N4, N4); directedGraph.putEdge(5, 6); // not incident to any node in nodeSubset MutableGraph<Integer> expectedSubgraph = GraphBuilder.directed().allowsSelfLoops(true).build(); expectedSubgraph.putEdge(N1, N2); expectedSubgraph.putEdge(N2, N1); expectedSubgraph.putEdge(N4, N4); assertThat(inducedSubgraph(directedGraph, nodeSubset)).isEqualTo(expectedSubgraph); }
@Test public void nodeOrder_insertion() { MutableGraph<Integer> graph = GraphBuilder.directed().nodeOrder(insertion()).build(); addNodes(graph); assertThat(graph.nodeOrder()).isEqualTo(insertion()); assertThat(graph.nodes()).containsExactly(3, 1, 4).inOrder(); }
@Before public void init() { MutableGraph<Integer> mutableDirectedGraph = GraphBuilder.directed().allowsSelfLoops(true).build(); MutableGraph<Integer> mutableUndirectedGraph = GraphBuilder.undirected().allowsSelfLoops(true).build(); graphsToTest = ImmutableList.of(mutableDirectedGraph, mutableUndirectedGraph); directedGraph = mutableDirectedGraph; undirectedGraph = mutableUndirectedGraph; MutableNetwork<Integer, String> mutableDirectedNetwork = NetworkBuilder.directed().allowsParallelEdges(true).allowsSelfLoops(true).build(); MutableNetwork<Integer, String> mutableUndirectedNetwork = NetworkBuilder.undirected().allowsParallelEdges(true).allowsSelfLoops(true).build(); networksToTest = ImmutableList.of(mutableDirectedNetwork, mutableUndirectedNetwork); directedNetwork = mutableDirectedNetwork; undirectedNetwork = mutableUndirectedNetwork; }
@Test public void endpointPair_unmodifiableView() { MutableGraph<Integer> directedGraph = GraphBuilder.directed().build(); Set<EndpointPair<Integer>> edges = directedGraph.edges(); directedGraph.putEdge(N1, N2); containsExactlySanityCheck(edges, EndpointPair.ordered(N1, N2)); directedGraph.putEdge(N2, N1); containsExactlySanityCheck(edges, EndpointPair.ordered(N1, N2), EndpointPair.ordered(N2, N1)); directedGraph.removeEdge(N1, N2); directedGraph.removeEdge(N2, N1); containsExactlySanityCheck(edges); try { edges.add(EndpointPair.ordered(N1, N2)); fail("Set returned by edges() should be unmodifiable"); } catch (UnsupportedOperationException expected) { } }
@Test public void endpointPair_directedGraph() { MutableGraph<Integer> directedGraph = GraphBuilder.directed().allowsSelfLoops(true).build(); directedGraph.addNode(N0); directedGraph.putEdge(N1, N2); directedGraph.putEdge(N2, N1); directedGraph.putEdge(N1, N3); directedGraph.putEdge(N4, N4); containsExactlySanityCheck( directedGraph.edges(), EndpointPair.ordered(N1, N2), EndpointPair.ordered(N2, N1), EndpointPair.ordered(N1, N3), EndpointPair.ordered(N4, N4)); }