@Test public void edgeValue_missing() { graph = ValueGraphBuilder.directed().build(); assertThat(graph.edgeValueOrDefault(1, 2, DEFAULT)).isEqualTo(DEFAULT); assertThat(graph.edgeValueOrDefault(2, 1, DEFAULT)).isEqualTo(DEFAULT); assertThat(graph.edgeValue(1, 2).orElse(DEFAULT)).isEqualTo(DEFAULT); assertThat(graph.edgeValue(2, 1).orElse(DEFAULT)).isEqualTo(DEFAULT); assertThat(graph.edgeValueOrDefault(1, 2, null)).isNull(); assertThat(graph.edgeValueOrDefault(2, 1, null)).isNull(); assertThat(graph.edgeValue(1, 2).orElse(null)).isNull(); assertThat(graph.edgeValue(2, 1).orElse(null)).isNull(); graph.putEdgeValue(1, 2, "valueA"); graph.putEdgeValue(2, 1, "valueB"); assertThat(graph.edgeValueOrDefault(1, 2, DEFAULT)).isEqualTo("valueA"); assertThat(graph.edgeValueOrDefault(2, 1, DEFAULT)).isEqualTo("valueB"); assertThat(graph.edgeValueOrDefault(1, 2, null)).isEqualTo("valueA"); assertThat(graph.edgeValueOrDefault(2, 1, null)).isEqualTo("valueB"); assertThat(graph.edgeValue(1, 2).get()).isEqualTo("valueA"); assertThat(graph.edgeValue(2, 1).get()).isEqualTo("valueB"); graph.removeEdge(1, 2); graph.putEdgeValue(2, 1, "valueC"); assertThat(graph.edgeValueOrDefault(1, 2, DEFAULT)).isEqualTo(DEFAULT); assertThat(graph.edgeValueOrDefault(2, 1, DEFAULT)).isEqualTo("valueC"); assertThat(graph.edgeValue(1, 2).orElse(DEFAULT)).isEqualTo(DEFAULT); assertThat(graph.edgeValueOrDefault(1, 2, null)).isNull(); assertThat(graph.edgeValueOrDefault(2, 1, null)).isEqualTo("valueC"); assertThat(graph.edgeValue(1, 2).orElse(null)).isNull(); assertThat(graph.edgeValue(2, 1).get()).isEqualTo("valueC"); }
@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_missing() { graph = ValueGraphBuilder.directed().build(); assertThat(graph.edgeValueOrDefault(1, 2, DEFAULT)).isEqualTo(DEFAULT); assertThat(graph.edgeValueOrDefault(2, 1, DEFAULT)).isEqualTo(DEFAULT); assertThat(graph.edgeValue(1, 2).orElse(DEFAULT)).isEqualTo(DEFAULT); assertThat(graph.edgeValue(2, 1).orElse(DEFAULT)).isEqualTo(DEFAULT); assertThat(graph.edgeValueOrDefault(1, 2, null)).isNull(); assertThat(graph.edgeValueOrDefault(2, 1, null)).isNull(); assertThat(graph.edgeValue(1, 2).orElse(null)).isNull(); assertThat(graph.edgeValue(2, 1).orElse(null)).isNull(); graph.putEdgeValue(1, 2, "valueA"); graph.putEdgeValue(2, 1, "valueB"); assertThat(graph.edgeValueOrDefault(1, 2, DEFAULT)).isEqualTo("valueA"); assertThat(graph.edgeValueOrDefault(2, 1, DEFAULT)).isEqualTo("valueB"); assertThat(graph.edgeValueOrDefault(1, 2, null)).isEqualTo("valueA"); assertThat(graph.edgeValueOrDefault(2, 1, null)).isEqualTo("valueB"); assertThat(graph.edgeValue(1, 2).get()).isEqualTo("valueA"); assertThat(graph.edgeValue(2, 1).get()).isEqualTo("valueB"); graph.removeEdge(1, 2); graph.putEdgeValue(2, 1, "valueC"); assertThat(graph.edgeValueOrDefault(1, 2, DEFAULT)).isEqualTo(DEFAULT); assertThat(graph.edgeValueOrDefault(2, 1, DEFAULT)).isEqualTo("valueC"); assertThat(graph.edgeValue(1, 2).orElse(DEFAULT)).isEqualTo(DEFAULT); assertThat(graph.edgeValueOrDefault(1, 2, null)).isNull(); assertThat(graph.edgeValueOrDefault(2, 1, null)).isEqualTo("valueC"); assertThat(graph.edgeValue(1, 2).orElse(null)).isNull(); assertThat(graph.edgeValue(2, 1).get()).isEqualTo("valueC"); }
@Test public void edgeValue_undirected_correct() { graph = ValueGraphBuilder.undirected().build(); graph.putEdgeValue(1, 2, "A"); assertThat(graph.edgeValue(EndpointPair.unordered(1, 2))).hasValue("A"); }
@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_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(); }
private void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); // write type oos.writeObject(getType()); // write vertices int n = vertexSet().size(); oos.writeInt(n); for (V v : vertexSet()) { oos.writeObject(v); } // write edges int m = edgeSet().size(); oos.writeInt(m); for (EndpointPair<V> e : edgeSet()) { V u = e.nodeU(); V v = e.nodeV(); oos.writeObject(u); oos.writeObject(v); oos.writeObject(valueGraph.edgeValue(u, v).get()); } }