private static MutableValueGraph<Integer, String> buildUndirectedValueGraph() { MutableValueGraph<Integer, String> undirectedGraph = ValueGraphBuilder.undirected().allowsSelfLoops(true).build(); undirectedGraph.putEdgeValue(N1, N1, E11); undirectedGraph.putEdgeValue(N1, N2, E12); undirectedGraph.putEdgeValue(N2, N1, E21); // overwrites E12 return undirectedGraph; }
@Test public void putEdgeValue_undirected() { graph = ValueGraphBuilder.undirected().build(); assertThat(graph.putEdgeValue(1, 2, "valueA")).isNull(); assertThat(graph.putEdgeValue(2, 1, "valueB")).isEqualTo("valueA"); assertThat(graph.putEdgeValue(1, 2, "valueC")).isEqualTo("valueB"); assertThat(graph.putEdgeValue(2, 1, "valueD")).isEqualTo("valueC"); }
@Test public void equivalence_considersEdgeValue() { graph = ValueGraphBuilder.undirected().build(); graph.putEdgeValue(1, 2, "valueA"); MutableValueGraph<Integer, String> otherGraph = ValueGraphBuilder.undirected().build(); otherGraph.putEdgeValue(1, 2, "valueA"); assertThat(graph).isEqualTo(otherGraph); otherGraph.putEdgeValue(1, 2, "valueB"); assertThat(graph).isNotEqualTo(otherGraph); // values differ } }
@Test public void removeEdge_undirected() { graph = ValueGraphBuilder.undirected().build(); graph.putEdgeValue(1, 2, "valueA"); graph.putEdgeValue(2, 1, "valueB"); graph.putEdgeValue(2, 3, "valueC"); assertThat(graph.removeEdge(1, 2)).isEqualTo("valueB"); assertThat(graph.removeEdge(1, 2)).isNull(); assertThat(graph.removeEdge(2, 1)).isNull(); assertThat(graph.removeEdge(2, 3)).isEqualTo("valueC"); assertThat(graph.removeEdge(2, 3)).isNull(); }
@Test public void putEdgeValue_undirected_orderMismatch() { graph = ValueGraphBuilder.undirected().build(); assertThat(graph.putEdgeValue(EndpointPair.ordered(1, 2), "irrelevant")).isNull(); }
@Test public void transpose_undirectedValueGraph() { MutableValueGraph<Integer, String> undirectedGraph = ValueGraphBuilder.undirected().build(); undirectedGraph.putEdgeValue(N1, N2, E12); assertThat(transpose(undirectedGraph)).isSameAs(undirectedGraph); }
@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 hasEdgeConnecting_undirected_backwards() { graph = ValueGraphBuilder.undirected().build(); graph.putEdgeValue(1, 2, "A"); assertThat(graph.hasEdgeConnecting(EndpointPair.unordered(2, 1))).isTrue(); }
@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 hasEdgeConnecting_undirected_correct() { graph = ValueGraphBuilder.undirected().build(); graph.putEdgeValue(1, 2, "A"); assertThat(graph.hasEdgeConnecting(EndpointPair.unordered(1, 2))).isTrue(); }
@Test public void edgeValueOrDefault_undirected_correct() { graph = ValueGraphBuilder.undirected().build(); graph.putEdgeValue(1, 2, "A"); assertThat(graph.edgeValueOrDefault(EndpointPair.unordered(1, 2), "default")).isEqualTo("A"); }
@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 forTree_withUndirectedValueGraph_throws() throws Exception { MutableValueGraph<String, Integer> valueGraph = ValueGraphBuilder.undirected().build(); valueGraph.putEdgeValue("a", "b", 11); try { Traverser.forTree(valueGraph); fail("Expected exception"); } catch (IllegalArgumentException expected) { } }
@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(); }
@Test public void hasEdgeConnecting_undirected_mismatch() { graph = ValueGraphBuilder.undirected().build(); graph.putEdgeValue(1, 2, "A"); assertThat(graph.hasEdgeConnecting(EndpointPair.ordered(1, 2))).isTrue(); assertThat(graph.hasEdgeConnecting(EndpointPair.ordered(2, 1))).isTrue(); }
@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 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_undirected_backwards() { graph = ValueGraphBuilder.undirected().build(); graph.putEdgeValue(1, 2, "A"); assertThat(graph.edgeValue(EndpointPair.unordered(2, 1))).hasValue("A"); }
@Test public void edgeValue_undirected_mismatch() { graph = ValueGraphBuilder.undirected().build(); graph.putEdgeValue(1, 2, "A"); assertThat(graph.edgeValue(EndpointPair.ordered(1, 2))).hasValue("A"); assertThat(graph.edgeValue(EndpointPair.ordered(2, 1))).hasValue("A"); }