@Test public void copyOfImmutableValueGraph_optimized() { ValueGraph<String, Integer> graph1 = ImmutableValueGraph.copyOf(ValueGraphBuilder.directed().<String, Integer>build()); ValueGraph<String, Integer> graph2 = ImmutableValueGraph.copyOf(graph1); assertThat(graph2).isSameAs(graph1); } }
@Test public void immutableValueGraph() { MutableValueGraph<String, Integer> mutableValueGraph = ValueGraphBuilder.directed().build(); mutableValueGraph.addNode("A"); ImmutableValueGraph<String, Integer> immutableValueGraph = ImmutableValueGraph.copyOf(mutableValueGraph); assertThat(immutableValueGraph.asGraph()).isInstanceOf(ImmutableGraph.class); assertThat(immutableValueGraph).isNotInstanceOf(MutableValueGraph.class); assertThat(immutableValueGraph).isEqualTo(mutableValueGraph); mutableValueGraph.addNode("B"); assertThat(immutableValueGraph).isNotEqualTo(mutableValueGraph); }
@Test @SuppressWarnings("CheckReturnValue") public void forTree_acceptsDirectedValueGraph() throws Exception { MutableValueGraph<String, Integer> valueGraph = ValueGraphBuilder.directed().build(); valueGraph.putEdgeValue("a", "b", 11); Traverser.forTree(valueGraph); // Does not throw }
@Test public void putEdgeValue_directed() { graph = ValueGraphBuilder.directed().build(); assertThat(graph.putEdgeValue(1, 2, "valueA")).isNull(); assertThat(graph.putEdgeValue(2, 1, "valueB")).isNull(); assertThat(graph.putEdgeValue(1, 2, "valueC")).isEqualTo("valueA"); assertThat(graph.putEdgeValue(2, 1, "valueD")).isEqualTo("valueB"); }
@Test public void hasEdgeConnecting_directed_correct() { graph = ValueGraphBuilder.directed().build(); graph.putEdgeValue(1, 2, "A"); assertThat(graph.hasEdgeConnecting(EndpointPair.ordered(1, 2))).isTrue(); }
@Test public void edgeValueOrDefault_directed_backwards() { graph = ValueGraphBuilder.directed().build(); graph.putEdgeValue(1, 2, "A"); assertThat(graph.edgeValueOrDefault(EndpointPair.ordered(2, 1), "default")) .isEqualTo("default"); }
@Test public void hasEdgeConnecting_directed_backwards() { graph = ValueGraphBuilder.directed().build(); graph.putEdgeValue(1, 2, "A"); assertThat(graph.hasEdgeConnecting(EndpointPair.ordered(2, 1))).isFalse(); }
@Test public void edgeValueOrDefault_directed_correct() { graph = ValueGraphBuilder.directed().build(); graph.putEdgeValue(1, 2, "A"); assertThat(graph.edgeValueOrDefault(EndpointPair.ordered(1, 2), "default")).isEqualTo("A"); }
@Test public void hasEdgeConnecting_directed_mismatch() { graph = ValueGraphBuilder.directed().build(); graph.putEdgeValue(1, 2, "A"); assertThat(graph.hasEdgeConnecting(EndpointPair.unordered(1, 2))).isFalse(); assertThat(graph.hasEdgeConnecting(EndpointPair.unordered(2, 1))).isFalse(); }
private static MutableValueGraph<Integer, String> buildDirectedValueGraph() { MutableValueGraph<Integer, String> directedGraph = ValueGraphBuilder.directed().allowsSelfLoops(true).build(); directedGraph.putEdgeValue(N1, N1, E11); directedGraph.putEdgeValue(N1, N2, E12); directedGraph.putEdgeValue(N2, N1, E21); return directedGraph; }
@Test public void putEdgeValue_directed_orderMismatch() { graph = ValueGraphBuilder.directed().build(); try { graph.putEdgeValue(EndpointPair.unordered(1, 2), "irrelevant"); fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); } catch (IllegalArgumentException e) { assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } }
@Test public void removeEdge_directed() { graph = ValueGraphBuilder.directed().build(); graph.putEdgeValue(1, 2, "valueA"); graph.putEdgeValue(2, 1, "valueB"); graph.putEdgeValue(2, 3, "valueC"); assertThat(graph.removeEdge(1, 2)).isEqualTo("valueA"); assertThat(graph.removeEdge(1, 2)).isNull(); assertThat(graph.removeEdge(2, 1)).isEqualTo("valueB"); assertThat(graph.removeEdge(2, 1)).isNull(); assertThat(graph.removeEdge(2, 3)).isEqualTo("valueC"); assertThat(graph.removeEdge(2, 3)).isNull(); }
@Test public void directedGraph() { graph = ValueGraphBuilder.directed().allowsSelfLoops(true).build(); graph.putEdgeValue(1, 2, "valueA"); graph.putEdgeValue(2, 1, "valueB"); graph.putEdgeValue(2, 3, "valueC"); graph.putEdgeValue(4, 4, "valueD"); assertThat(graph.edgeValueOrDefault(1, 2, null)).isEqualTo("valueA"); assertThat(graph.edgeValueOrDefault(2, 1, null)).isEqualTo("valueB"); assertThat(graph.edgeValueOrDefault(2, 3, null)).isEqualTo("valueC"); assertThat(graph.edgeValueOrDefault(4, 4, null)).isEqualTo("valueD"); assertThat(graph.edgeValueOrDefault(1, 2, DEFAULT)).isEqualTo("valueA"); assertThat(graph.edgeValueOrDefault(2, 1, DEFAULT)).isEqualTo("valueB"); assertThat(graph.edgeValueOrDefault(2, 3, DEFAULT)).isEqualTo("valueC"); assertThat(graph.edgeValueOrDefault(4, 4, DEFAULT)).isEqualTo("valueD"); String toString = graph.toString(); assertThat(toString).contains("valueA"); assertThat(toString).contains("valueB"); assertThat(toString).contains("valueC"); assertThat(toString).contains("valueD"); }
@Test public void inducedSubgraph_valueGraph() { Set<Integer> nodeSubset = ImmutableSet.of(N1, N2, N4); MutableValueGraph<Integer, String> directedGraph = ValueGraphBuilder.directed().allowsSelfLoops(true).build(); directedGraph.putEdgeValue(N1, N2, E12); directedGraph.putEdgeValue(N2, N1, E21); directedGraph.putEdgeValue(N1, N3, E13); // only incident to one node in nodeSubset directedGraph.putEdgeValue(N4, N4, E44); directedGraph.putEdgeValue(5, 6, "5-6"); // not incident to any node in nodeSubset MutableValueGraph<Integer, String> expectedSubgraph = ValueGraphBuilder.directed().allowsSelfLoops(true).build(); expectedSubgraph.putEdgeValue(N1, N2, E12); expectedSubgraph.putEdgeValue(N2, N1, E21); expectedSubgraph.putEdgeValue(N4, N4, E44); assertThat(inducedSubgraph(directedGraph, nodeSubset)).isEqualTo(expectedSubgraph); }
@Test public void edgeValueOrDefault_directed_mismatch() { graph = ValueGraphBuilder.directed().build(); graph.putEdgeValue(1, 2, "A"); try { String unused = graph.edgeValueOrDefault(EndpointPair.unordered(1, 2), "default"); unused = graph.edgeValueOrDefault(EndpointPair.unordered(2, 1), "default"); fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); } catch (IllegalArgumentException e) { assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } }
@Test public void edgeValue_directed_mismatch() { graph = ValueGraphBuilder.directed().build(); graph.putEdgeValue(1, 2, "A"); try { Optional<String> unused = graph.edgeValue(EndpointPair.unordered(1, 2)); unused = graph.edgeValue(EndpointPair.unordered(2, 1)); fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); } catch (IllegalArgumentException e) { assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } }
@Test public void edgeValue_directed_correct() { graph = ValueGraphBuilder.directed().build(); graph.putEdgeValue(1, 2, "A"); assertThat(graph.edgeValue(EndpointPair.ordered(1, 2))).hasValue("A"); }
@Test public void edgeValue_directed_backwards() { graph = ValueGraphBuilder.directed().build(); graph.putEdgeValue(1, 2, "A"); assertThat(graph.edgeValue(EndpointPair.ordered(2, 1))).isEmpty(); }
@Test public void removeEdge_directed_orderMismatch() { graph = ValueGraphBuilder.directed().build(); graph.putEdgeValue(1, 2, "1->2"); graph.putEdgeValue(2, 1, "2->1"); try { graph.removeEdge(EndpointPair.unordered(1, 2)); graph.removeEdge(EndpointPair.unordered(2, 1)); fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); } catch (IllegalArgumentException e) { assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } }
@Test public void transpose_directedValueGraph() { MutableValueGraph<Integer, String> directedGraph = ValueGraphBuilder.directed().allowsSelfLoops(true).build(); directedGraph.putEdgeValue(N1, N3, E13); directedGraph.putEdgeValue(N3, N1, E31); directedGraph.putEdgeValue(N1, N2, E12); directedGraph.putEdgeValue(N1, N1, E11); directedGraph.putEdgeValue(N3, N4, E34); MutableValueGraph<Integer, String> expectedTranspose = ValueGraphBuilder.directed().allowsSelfLoops(true).build(); expectedTranspose.putEdgeValue(N3, N1, E13); expectedTranspose.putEdgeValue(N1, N3, E31); expectedTranspose.putEdgeValue(N2, N1, E12); expectedTranspose.putEdgeValue(N1, N1, E11); expectedTranspose.putEdgeValue(N4, N3, E34); ValueGraph<Integer, String> transpose = transpose(directedGraph); assertThat(transpose).isEqualTo(expectedTranspose); assertThat(transpose(transpose)).isSameAs(directedGraph); AbstractGraphTest.validateGraph(transpose.asGraph()); assertThat(transpose.edgeValueOrDefault(N1, N2, null)).isNull(); for (Integer node : directedGraph.nodes()) { assertThat(directedGraph.inDegree(node)).isSameAs(transpose.outDegree(node)); assertThat(directedGraph.outDegree(node)).isSameAs(transpose.inDegree(node)); } directedGraph.putEdgeValue(N2, N1, E21); // View should be updated. assertThat(transpose.edgeValueOrDefault(N1, N2, null)).isEqualTo(E21); AbstractGraphTest.validateGraph(transpose.asGraph()); }