@Test public void nodes_noNodes() { assertThat(graph.nodes()).isEmpty(); }
@Test public void nodes_oneNode() { addNode(N1); assertThat(graph.nodes()).containsExactly(N1); }
@Test public void addNode_newNode() { assertThat(addNode(N1)).isTrue(); assertThat(graph.nodes()).contains(N1); }
@Test public void removeNode_antiparallelEdges() { putEdge(N1, N2); putEdge(N2, N1); assertThat(graph.removeNode(N1)).isTrue(); assertThat(graph.nodes()).containsExactly(N2); assertThat(graph.edges()).isEmpty(); assertThat(graph.removeNode(N2)).isTrue(); assertThat(graph.nodes()).isEmpty(); assertThat(graph.edges()).isEmpty(); }
@Test public void removeEdge_existingSelfLoopEdge() { putEdge(N1, N1); assertThat(graph.removeEdge(N1, N1)).isTrue(); assertThat(graph.nodes()).containsExactly(N1); assertThat(graph.adjacentNodes(N1)).isEmpty(); } }
@Test public void removeEdge_existingSelfLoopEdge() { putEdge(N1, N1); assertThat(graph.removeEdge(N1, N1)).isTrue(); assertThat(graph.nodes()).containsExactly(N1); assertThat(graph.successors(N1)).isEmpty(); } }
@Test public void removeNode_existingNodeWithSelfLoopEdge() { addNode(N1); putEdge(N1, N1); assertThat(graph.removeNode(N1)).isTrue(); assertThat(graph.nodes()).isEmpty(); }
@Test public void removeNode_existingNodeWithSelfLoopEdge() { addNode(N1); putEdge(N1, N1); assertThat(graph.removeNode(N1)).isTrue(); assertThat(graph.nodes()).isEmpty(); }
@Test public void removeNode_existingNode() { putEdge(N1, N2); putEdge(N4, N1); assertThat(graph.removeNode(N1)).isTrue(); assertThat(graph.removeNode(N1)).isFalse(); assertThat(graph.nodes()).containsExactly(N2, N4); assertThat(graph.adjacentNodes(N2)).isEmpty(); assertThat(graph.adjacentNodes(N4)).isEmpty(); }
@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_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 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(); }
@After public void validateUndirectedEdges() { for (Integer node : graph.nodes()) { new EqualsTester() .addEqualityGroup( graph.predecessors(node), graph.successors(node), graph.adjacentNodes(node)) .testEquals(); } }
/** * This test checks an implementation dependent feature. It tests that the method {@code addEdge} * will silently add the missing nodes to the graph, then add the edge connecting them. We are not * using the proxy methods here as we want to test {@code addEdge} when the end-points are not * elements of the graph. */ @Test public void addEdge_nodesNotInGraph() { graph.addNode(N1); assertTrue(graph.putEdge(N1, N5)); assertTrue(graph.putEdge(N4, N1)); assertTrue(graph.putEdge(N2, N3)); assertThat(graph.nodes()).containsExactly(N1, N5, N4, N2, N3).inOrder(); assertThat(graph.successors(N1)).containsExactly(N5); assertThat(graph.successors(N2)).containsExactly(N3); assertThat(graph.successors(N3)).isEmpty(); assertThat(graph.successors(N4)).containsExactly(N1); assertThat(graph.successors(N5)).isEmpty(); } }
@Test public void customComparable() { MutableGraph<ComparableSubClass> graph = GraphBuilder.undirected().nodeOrder(ElementOrder.<ComparableSubClass>natural()).build(); ComparableSubClass node2 = new ComparableSubClass(2); ComparableSubClass node4 = new ComparableSubClass(4); ComparableSubClass node6 = new ComparableSubClass(6); ComparableSubClass node8 = new ComparableSubClass(8); graph.addNode(node4); graph.addNode(node2); graph.addNode(node6); graph.addNode(node8); assertThat(graph.nodeOrder().comparator()).isEqualTo(Ordering.natural()); assertThat(graph.nodes()).containsExactly(node2, node4, node6, node8).inOrder(); }