Refactoring
- added more tests - added more interfaces for the visits - added satellite classes like Vertex - graph interface improved
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
package berack96.test.sim;
|
||||
|
||||
import berack96.sim.util.graph.Edge;
|
||||
import berack96.sim.util.graph.Graph;
|
||||
import berack96.sim.util.graph.MapGraph;
|
||||
import berack96.sim.util.graph.Vertex;
|
||||
import berack96.sim.util.graph.visit.BFS;
|
||||
import berack96.sim.util.graph.visit.DFS;
|
||||
import berack96.sim.util.graph.visit.VisitStrategy;
|
||||
import berack96.sim.util.graph.visit.VisitInfo;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -18,6 +21,7 @@ public class TestGraph {
|
||||
|
||||
private final Exception nullException = new NullPointerException(Graph.PARAM_NULL);
|
||||
private final Exception notException = new IllegalArgumentException(Graph.VERTEX_NOT_CONTAINED);
|
||||
private final Exception unsuppException = new UnsupportedOperationException(Vertex.REMOVED);
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
@@ -90,6 +94,10 @@ public class TestGraph {
|
||||
shouldThrow(nullException, () -> graph.addEdge(null, "2", 1));
|
||||
shouldThrow(nullException, () -> graph.addEdge(null, null, 1));
|
||||
shouldThrow(nullException, () -> graph.addEdge("1", null, 1));
|
||||
shouldThrow(new NullPointerException(), () -> graph.addEdge(null));
|
||||
shouldThrow(nullException, () -> graph.addEdge(new Edge<>("1", null, 1)));
|
||||
shouldThrow(nullException, () -> graph.addEdge(new Edge<>(null, null, 1)));
|
||||
shouldThrow(nullException, () -> graph.addEdge(new Edge<>(null, "2", 1)));
|
||||
shouldThrow(nullException, () -> graph.containsEdge(null, "2"));
|
||||
shouldThrow(nullException, () -> graph.containsEdge(null, null));
|
||||
shouldThrow(nullException, () -> graph.containsEdge("1", null));
|
||||
@@ -103,9 +111,6 @@ public class TestGraph {
|
||||
shouldThrow(notException, () -> graph.addEdge("0", "2", 1));
|
||||
shouldThrow(notException, () -> graph.addEdge("2", "8", 1));
|
||||
shouldThrow(notException, () -> graph.addEdge("9", "6", 1));
|
||||
shouldThrow(notException, () -> graph.containsEdge("01", "4"));
|
||||
shouldThrow(notException, () -> graph.containsEdge("3", "8132"));
|
||||
shouldThrow(notException, () -> graph.containsEdge("9423", "516"));
|
||||
shouldThrow(notException, () -> graph.removeEdge("012", "2"));
|
||||
shouldThrow(notException, () -> graph.removeEdge("2", "28"));
|
||||
shouldThrow(notException, () -> graph.removeEdge("4329", "62"));
|
||||
@@ -116,14 +121,18 @@ public class TestGraph {
|
||||
assertEquals(0, graph.numberOfEdges());
|
||||
|
||||
assertNull(graph.addEdge("1", "2", 1));
|
||||
assertNull(graph.addEdge("1", "3", 1));
|
||||
assertNull(graph.addEdge(new Edge<>("1", "3", 1)));
|
||||
assertNull(graph.addEdge("2", "5", 4));
|
||||
assertNull(graph.addEdge("3", "5", 2));
|
||||
assertNull(graph.addEdge("5", "3", 2));
|
||||
assertNull(graph.addEdge(new Edge<>("3", "5", 2)));
|
||||
assertNull(graph.addEdge(new Edge<>("5", "3", 2)));
|
||||
assertNull(graph.addEdge("5", "4", 3));
|
||||
|
||||
assertEquals(6, graph.numberOfEdges());
|
||||
|
||||
assertFalse(graph.containsEdge("01", "4"));
|
||||
assertFalse(graph.containsEdge("3", "8132"));
|
||||
assertFalse(graph.containsEdge("9423", "516"));
|
||||
|
||||
// All this calls should do nothing
|
||||
graph.removeEdge("1", "5");
|
||||
graph.removeEdge("1", "4");
|
||||
@@ -145,6 +154,9 @@ public class TestGraph {
|
||||
assertEquals(new Integer(1), graph.addEdge("1", "2", 102));
|
||||
assertEquals(new Integer(102), graph.addEdge("1", "2", 3));
|
||||
assertEquals(new Integer(3), graph.addEdge("1", "2", 1));
|
||||
assertEquals(new Integer(1), graph.addEdge(new Edge<>("1", "2", 102)));
|
||||
assertEquals(new Integer(102), graph.addEdge(new Edge<>("1", "2", 3)));
|
||||
assertEquals(new Integer(3), graph.addEdge(new Edge<>("1", "2", 1)));
|
||||
|
||||
assertEquals(6, graph.numberOfEdges());
|
||||
assertTrue(graph.containsEdge("1", "2"));
|
||||
@@ -196,19 +208,45 @@ public class TestGraph {
|
||||
assertFalse(graph.containsEdge("5", "4"));
|
||||
assertEquals(0, graph.numberOfEdges());
|
||||
|
||||
shouldThrow(notException, () -> graph.containsEdge("2", "323"));
|
||||
graph.addEdgeAndVertices("2", "323", 3);
|
||||
assertFalse(graph.containsEdge("2", "323"));
|
||||
assertNull(graph.addEdgeAndVertices("2", "323", 3));
|
||||
assertTrue(graph.containsEdge("2", "323"));
|
||||
shouldThrow(notException, () -> graph.containsEdge("2aa", "323"));
|
||||
graph.addEdgeAndVertices("2aa", "323", 3);
|
||||
assertFalse(graph.containsEdge("2aa", "323"));
|
||||
assertNull(graph.addEdgeAndVertices("2aa", "323", 35));
|
||||
assertTrue(graph.containsEdge("2aa", "323"));
|
||||
shouldThrow(notException, () -> graph.containsEdge("2bbb", "323bbb"));
|
||||
graph.addEdgeAndVertices("2bbb", "323bbb", 3);
|
||||
assertFalse(graph.containsEdge("2bbb", "323bbb"));
|
||||
assertNull(graph.addEdgeAndVertices("2bbb", "323bbb", 135));
|
||||
assertTrue(graph.containsEdge("2bbb", "323bbb"));
|
||||
|
||||
shouldThrow(nullException, () -> graph.addEdgeAndVertices(null, "1", 1));
|
||||
shouldThrow(nullException, () -> graph.addEdgeAndVertices(null, null, 1));
|
||||
shouldThrow(nullException, () -> graph.addEdgeAndVertices("2", null, 1));
|
||||
|
||||
assertEquals(3, graph.addEdgeAndVertices("2", "323", 50).intValue());
|
||||
assertEquals(35, graph.addEdgeAndVertices("2aa", "323", 5).intValue());
|
||||
assertEquals(50, graph.addEdgeAndVertices("2", "323", 500).intValue());
|
||||
|
||||
graph.removeAllEdge();
|
||||
|
||||
assertFalse(graph.containsEdge("2", "323"));
|
||||
assertNull(graph.addEdgeAndVertices(new Edge<>("2", "323", 3)));
|
||||
assertTrue(graph.containsEdge("2", "323"));
|
||||
assertFalse(graph.containsEdge("2aa", "323"));
|
||||
assertNull(graph.addEdgeAndVertices(new Edge<>("2aa", "323", 35)));
|
||||
assertTrue(graph.containsEdge("2aa", "323"));
|
||||
assertFalse(graph.containsEdge("2bbb", "323bbb"));
|
||||
assertNull(graph.addEdgeAndVertices(new Edge<>("2bbb", "323bbb", 135)));
|
||||
assertTrue(graph.containsEdge("2bbb", "323bbb"));
|
||||
|
||||
shouldThrow(nullException, () -> graph.addEdgeAndVertices(new Edge<>(null, "1", 1)));
|
||||
shouldThrow(nullException, () -> graph.addEdgeAndVertices(new Edge<>(null, null, 1)));
|
||||
shouldThrow(nullException, () -> graph.addEdgeAndVertices(new Edge<>("2", null, 1)));
|
||||
shouldThrow(new NullPointerException(), () -> graph.addEdgeAndVertices(null));
|
||||
|
||||
assertEquals(3, graph.addEdgeAndVertices(new Edge<>("2", "323", 50)).intValue());
|
||||
assertEquals(35, graph.addEdgeAndVertices(new Edge<>("2aa", "323", 5)).intValue());
|
||||
assertEquals(50, graph.addEdgeAndVertices(new Edge<>("2", "323", 500)).intValue());
|
||||
|
||||
graph.removeAllVertex();
|
||||
graph.addVertex("aaa");
|
||||
graph.addVertex("1");
|
||||
@@ -217,19 +255,19 @@ public class TestGraph {
|
||||
shouldContain(graph.vertices(), "1", "2", "aaa");
|
||||
shouldContain(graph.edges());
|
||||
|
||||
Set<Graph.Edge<String, Integer>> edges = new HashSet<>();
|
||||
edges.add(new Graph.Edge<>("aaa", "bbb", 3));
|
||||
edges.add(new Graph.Edge<>("bbb", "ccc", 4));
|
||||
edges.add(new Graph.Edge<>("ccc", "aaa", 5));
|
||||
edges.add(new Graph.Edge<>("1", "2", 2));
|
||||
Set<Edge<String, Integer>> edges = new HashSet<>();
|
||||
edges.add(new Edge<>("aaa", "bbb", 3));
|
||||
edges.add(new Edge<>("bbb", "ccc", 4));
|
||||
edges.add(new Edge<>("ccc", "aaa", 5));
|
||||
edges.add(new Edge<>("1", "2", 2));
|
||||
graph.addAllEdges(edges);
|
||||
|
||||
shouldContain(graph.vertices(), "1", "2", "aaa", "bbb", "ccc");
|
||||
shouldContain(graph.edges(),
|
||||
new Graph.Edge<>("aaa", "bbb", 3),
|
||||
new Graph.Edge<>("bbb", "ccc", 4),
|
||||
new Graph.Edge<>("ccc", "aaa", 5),
|
||||
new Graph.Edge<>("1", "2", 2));
|
||||
new Edge<>("aaa", "bbb", 3),
|
||||
new Edge<>("bbb", "ccc", 4),
|
||||
new Edge<>("ccc", "aaa", 5),
|
||||
new Edge<>("1", "2", 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -276,12 +314,19 @@ public class TestGraph {
|
||||
shouldContain(graph.getAncestors("5"), "2", "3");
|
||||
shouldContain(graph.getAncestors("6"), "2", "4");
|
||||
|
||||
shouldContain(graph.getChildrenAndWeight("1").entrySet(), new AbstractMap.SimpleEntry<>("2", 1), new AbstractMap.SimpleEntry<>("3", 1));
|
||||
shouldContain(graph.getChildrenAndWeight("2").entrySet(), new AbstractMap.SimpleEntry<>("5", 4), new AbstractMap.SimpleEntry<>("6", 5));
|
||||
shouldContain(graph.getChildrenAndWeight("3").entrySet(), new AbstractMap.SimpleEntry<>("5", 2));
|
||||
shouldContain(graph.getChildrenAndWeight("4").entrySet(), new AbstractMap.SimpleEntry<>("6", 6));
|
||||
shouldContain(graph.getChildrenAndWeight("5").entrySet(), new AbstractMap.SimpleEntry<>("3", 9), new AbstractMap.SimpleEntry<>("4", 5));
|
||||
shouldContain(graph.getChildrenAndWeight("6").entrySet());
|
||||
shouldContain(graph.getEdgesOut("1"), new Edge<>("1", "2", 1), new Edge<>("1", "3", 1));
|
||||
shouldContain(graph.getEdgesOut("2"), new Edge<>("2", "5", 4), new Edge<>("2", "6", 5));
|
||||
shouldContain(graph.getEdgesOut("3"), new Edge<>("3", "5", 2));
|
||||
shouldContain(graph.getEdgesOut("4"), new Edge<>("4", "6", 6));
|
||||
shouldContain(graph.getEdgesOut("5"), new Edge<>("5", "3", 9), new Edge<>("5", "4", 5));
|
||||
shouldContain(graph.getEdgesOut("6"));
|
||||
|
||||
shouldContain(graph.getEdgesIn("1"));
|
||||
shouldContain(graph.getEdgesIn("2"), new Edge<>("1", "2", 1));
|
||||
shouldContain(graph.getEdgesIn("3"), new Edge<>("1", "3", 1), new Edge<>("5", "3", 9));
|
||||
shouldContain(graph.getEdgesIn("4"), new Edge<>("5", "4", 5));
|
||||
shouldContain(graph.getEdgesIn("5"), new Edge<>("2", "5", 4), new Edge<>("3", "5", 2));
|
||||
shouldContain(graph.getEdgesIn("6"), new Edge<>("4", "6", 6), new Edge<>("2", "6", 5));
|
||||
|
||||
assertEquals(0, graph.degreeIn("1"));
|
||||
assertEquals(1, graph.degreeIn("2"));
|
||||
@@ -305,27 +350,27 @@ public class TestGraph {
|
||||
assertEquals(2, graph.degree("6"));
|
||||
|
||||
shouldContain(graph.edges(),
|
||||
new Graph.Edge<>("1", "2", 1),
|
||||
new Graph.Edge<>("1", "3", 1),
|
||||
new Graph.Edge<>("2", "5", 4),
|
||||
new Graph.Edge<>("2", "6", 5),
|
||||
new Graph.Edge<>("3", "5", 2),
|
||||
new Graph.Edge<>("4", "6", 6),
|
||||
new Graph.Edge<>("5", "3", 9),
|
||||
new Graph.Edge<>("5", "4", 5));
|
||||
new Edge<>("1", "2", 1),
|
||||
new Edge<>("1", "3", 1),
|
||||
new Edge<>("2", "5", 4),
|
||||
new Edge<>("2", "6", 5),
|
||||
new Edge<>("3", "5", 2),
|
||||
new Edge<>("4", "6", 6),
|
||||
new Edge<>("5", "3", 9),
|
||||
new Edge<>("5", "4", 5));
|
||||
|
||||
shouldThrow(nullException, () -> graph.edgesOf(null));
|
||||
shouldThrow(notException, () -> graph.edgesOf("rew"));
|
||||
shouldContain(graph.edgesOf("5"),
|
||||
new Graph.Edge<>("2", "5", 4),
|
||||
new Graph.Edge<>("3", "5", 2),
|
||||
new Graph.Edge<>("5", "3", 9),
|
||||
new Graph.Edge<>("5", "4", 5));
|
||||
new Edge<>("2", "5", 4),
|
||||
new Edge<>("3", "5", 2),
|
||||
new Edge<>("5", "3", 9),
|
||||
new Edge<>("5", "4", 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preBasicVisit() {
|
||||
VisitStrategy.VisitInfo<Integer> info = new VisitStrategy.VisitInfo<>(0);
|
||||
VisitInfo<Integer> info = new VisitInfo<>(0);
|
||||
assertTrue(info.isDiscovered(0));
|
||||
assertFalse(info.isVisited(0));
|
||||
assertEquals(0, info.getTimeDiscover(0));
|
||||
@@ -383,8 +428,7 @@ public class TestGraph {
|
||||
shouldThrow(notException, () -> graph.visit("1010", new DFS<>(), null));
|
||||
|
||||
DFS<String, Integer> dfs = new DFS<>();
|
||||
graph.visit("1", dfs, null);
|
||||
VisitStrategy.VisitInfo<String> visitDFS = dfs.getLastVisit();
|
||||
VisitInfo<String> visitDFS = graph.visit("1", dfs, null);
|
||||
assertEquals(0, visitDFS.getTimeDiscover("1"));
|
||||
assertEquals(1, visitDFS.getTimeDiscover("2"));
|
||||
assertEquals(2, visitDFS.getTimeDiscover("5"));
|
||||
@@ -400,9 +444,25 @@ public class TestGraph {
|
||||
assertFalse(visitDFS.isDiscovered("7"));
|
||||
assertFalse(visitDFS.isDiscovered("8"));
|
||||
|
||||
int[] discoverTime = {0, 1, 2, 3, 5, 6};
|
||||
String[] verticesDiscovered = {"1", "2", "5", "3", "4", "6"};
|
||||
AtomicInteger integer = new AtomicInteger(0);
|
||||
visitDFS.forEachDiscovered(vertexInfo -> {
|
||||
assertEquals(discoverTime[integer.get()], vertexInfo.timeDiscovered);
|
||||
assertEquals(verticesDiscovered[integer.get()], vertexInfo.vertex);
|
||||
integer.incrementAndGet();
|
||||
});
|
||||
integer.set(0);
|
||||
int[] visitTime = {4, 7, 8, 9, 10, 11};
|
||||
String[] verticesVisited = {"3", "6", "4", "5", "2", "1"};
|
||||
visitDFS.forEachVisited(vertexInfo -> {
|
||||
assertEquals(visitTime[integer.get()], vertexInfo.timeVisited);
|
||||
assertEquals(verticesVisited[integer.get()], vertexInfo.vertex);
|
||||
integer.incrementAndGet();
|
||||
});
|
||||
|
||||
BFS<String, Integer> bfs = new BFS<>();
|
||||
graph.visit("1", bfs, null);
|
||||
VisitStrategy.VisitInfo<String> visitBFS = bfs.getLastVisit();
|
||||
VisitInfo<String> visitBFS = graph.visit("1", bfs, null);
|
||||
assertEquals(0, visitBFS.getTimeDiscover("1"));
|
||||
assertEquals(1, visitBFS.getTimeVisit("1"));
|
||||
assertEquals(2, visitBFS.getTimeDiscover("2"));
|
||||
@@ -642,8 +702,7 @@ public class TestGraph {
|
||||
Graph<String, Integer> transposed = graph.transpose();
|
||||
|
||||
DFS<String, Integer> dfs = new DFS<>();
|
||||
transposed.visit("6", dfs, null);
|
||||
VisitStrategy.VisitInfo<String> visitDFS = dfs.getLastVisit();
|
||||
VisitInfo<String> visitDFS = transposed.visit("6", dfs, null);
|
||||
assertEquals(0, visitDFS.getTimeDiscover("6"));
|
||||
assertEquals(1, visitDFS.getTimeDiscover("4"));
|
||||
assertEquals(2, visitDFS.getTimeDiscover("5"));
|
||||
@@ -658,8 +717,7 @@ public class TestGraph {
|
||||
assertFalse(visitDFS.isDiscovered("7"));
|
||||
assertFalse(visitDFS.isDiscovered("8"));
|
||||
|
||||
transposed.visit("8", dfs, null);
|
||||
visitDFS = dfs.getLastVisit();
|
||||
visitDFS = transposed.visit("8", dfs, null);
|
||||
assertEquals(0, visitDFS.getTimeDiscover("8"));
|
||||
assertEquals(1, visitDFS.getTimeDiscover("7"));
|
||||
assertEquals(2, visitDFS.getTimeVisit("7"));
|
||||
@@ -733,23 +791,23 @@ public class TestGraph {
|
||||
graph.addEdge("6", "2", 2);
|
||||
graph.addEdge("7", "8", 8);
|
||||
|
||||
List<Graph.Edge<String, Integer>> distance = graph.distance("1", "6");
|
||||
int sum = distance.stream().mapToInt(Graph.Edge::getWeight).sum();
|
||||
List<Edge<String, Integer>> distance = graph.distance("1", "6");
|
||||
int sum = distance.stream().mapToInt(Edge::getWeight).sum();
|
||||
assertEquals(13, sum);
|
||||
shouldContainInOrder(distance,
|
||||
new Graph.Edge<>("1", "2", 1),
|
||||
new Graph.Edge<>("2", "5", 4),
|
||||
new Graph.Edge<>("5", "4", 3),
|
||||
new Graph.Edge<>("4", "6", 5));
|
||||
new Edge<>("1", "2", 1),
|
||||
new Edge<>("2", "5", 4),
|
||||
new Edge<>("5", "4", 3),
|
||||
new Edge<>("4", "6", 5));
|
||||
distance = graph.distance("1", "3");
|
||||
sum = distance.stream().mapToInt(Graph.Edge::getWeight).sum();
|
||||
sum = distance.stream().mapToInt(Edge::getWeight).sum();
|
||||
assertEquals(8, sum);
|
||||
shouldContainInOrder(distance,
|
||||
new Graph.Edge<>("1", "2", 1),
|
||||
new Graph.Edge<>("2", "5", 4),
|
||||
new Graph.Edge<>("5", "3", 3));
|
||||
new Edge<>("1", "2", 1),
|
||||
new Edge<>("2", "5", 4),
|
||||
new Edge<>("5", "3", 3));
|
||||
|
||||
shouldContainInOrder(graph.distance("7", "8"), new Graph.Edge<>("7", "8", 8));
|
||||
shouldContainInOrder(graph.distance("7", "8"), new Edge<>("7", "8", 8));
|
||||
|
||||
shouldThrow(nullException, () -> graph.distance(null, "1"));
|
||||
shouldThrow(nullException, () -> graph.distance(null, null));
|
||||
@@ -792,32 +850,146 @@ public class TestGraph {
|
||||
graph.addEdge("6", "2", 2);
|
||||
graph.addEdge("7", "8", 8);
|
||||
|
||||
Map<String, List<Graph.Edge<String, Integer>>> distance = graph.distance("1");
|
||||
Map<String, List<Edge<String, Integer>>> distance = graph.distance("1");
|
||||
assertNull(distance.get("1"));
|
||||
shouldContainInOrder(distance.get("2"),
|
||||
new Graph.Edge<>("1", "2", 1));
|
||||
new Edge<>("1", "2", 1));
|
||||
shouldContainInOrder(distance.get("3"),
|
||||
new Graph.Edge<>("1", "2", 1),
|
||||
new Graph.Edge<>("2", "5", 4),
|
||||
new Graph.Edge<>("5", "3", 3));
|
||||
new Edge<>("1", "2", 1),
|
||||
new Edge<>("2", "5", 4),
|
||||
new Edge<>("5", "3", 3));
|
||||
shouldContain(distance.get("4"),
|
||||
new Graph.Edge<>("1", "2", 1),
|
||||
new Graph.Edge<>("2", "5", 4),
|
||||
new Graph.Edge<>("5", "4", 3));
|
||||
new Edge<>("1", "2", 1),
|
||||
new Edge<>("2", "5", 4),
|
||||
new Edge<>("5", "4", 3));
|
||||
shouldContain(distance.get("5"),
|
||||
new Graph.Edge<>("1", "2", 1),
|
||||
new Graph.Edge<>("2", "5", 4));
|
||||
new Edge<>("1", "2", 1),
|
||||
new Edge<>("2", "5", 4));
|
||||
shouldContain(distance.get("6"),
|
||||
new Graph.Edge<>("1", "2", 1),
|
||||
new Graph.Edge<>("2", "5", 4),
|
||||
new Graph.Edge<>("5", "4", 3),
|
||||
new Graph.Edge<>("4", "6", 5));
|
||||
new Edge<>("1", "2", 1),
|
||||
new Edge<>("2", "5", 4),
|
||||
new Edge<>("5", "4", 3),
|
||||
new Edge<>("4", "6", 5));
|
||||
assertNull(distance.get("7"));
|
||||
shouldContain(distance.get("8"),
|
||||
new Graph.Edge<>("1", "2", 1),
|
||||
new Graph.Edge<>("2", "5", 4),
|
||||
new Graph.Edge<>("5", "4", 3),
|
||||
new Graph.Edge<>("4", "8", 2));
|
||||
new Edge<>("1", "2", 1),
|
||||
new Edge<>("2", "5", 4),
|
||||
new Edge<>("5", "4", 3),
|
||||
new Edge<>("4", "8", 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marker() {
|
||||
/*
|
||||
* This graph should be like this
|
||||
*
|
||||
* 1 -> 2 <- 6 7
|
||||
* ^ ^
|
||||
* | | | |
|
||||
* v v v
|
||||
* 3 <- 5 -> 4 8
|
||||
*/
|
||||
|
||||
graph.addVertexIfAbsent("1");
|
||||
graph.addVertexIfAbsent("2");
|
||||
graph.addVertexIfAbsent("3");
|
||||
graph.addVertexIfAbsent("4");
|
||||
graph.addVertexIfAbsent("5");
|
||||
graph.addVertexIfAbsent("6");
|
||||
graph.addVertexIfAbsent("7");
|
||||
graph.addVertexIfAbsent("8");
|
||||
|
||||
graph.addEdge("1", "2", 1);
|
||||
graph.addEdge("1", "3", 1);
|
||||
graph.addEdge("2", "5", 4);
|
||||
graph.addEdge("4", "6", 5);
|
||||
graph.addEdge("5", "3", 6);
|
||||
graph.addEdge("5", "4", 3);
|
||||
graph.addEdge("6", "2", 2);
|
||||
graph.addEdge("7", "8", 8);
|
||||
graph.addEdge("8", "7", 8);
|
||||
|
||||
shouldThrow(nullException, () -> graph.mark(null, null));
|
||||
shouldThrow(nullException, () -> graph.mark("1", null));
|
||||
shouldThrow(nullException, () -> graph.mark(null, "yellow"));
|
||||
shouldThrow(nullException, () -> graph.unMark(null));
|
||||
shouldThrow(nullException, () -> graph.getMarks(null));
|
||||
shouldThrow(nullException, () -> graph.unMark(null, null));
|
||||
shouldThrow(nullException, () -> graph.unMark("1", null));
|
||||
shouldThrow(nullException, () -> graph.unMark(null, "blue"));
|
||||
shouldThrow(nullException, () -> graph.unMarkAll(null));
|
||||
|
||||
shouldThrow(notException, () -> graph.mark("324", "yellow"));
|
||||
shouldThrow(notException, () -> graph.unMark("32423"));
|
||||
shouldThrow(notException, () -> graph.getMarks("hw7389"));
|
||||
|
||||
shouldContain(graph.getMarks("1"));
|
||||
graph.mark("1", "red");
|
||||
shouldContain(graph.getMarks("1"), "red");
|
||||
graph.mark("1", "yellow");
|
||||
graph.mark("1", "blue");
|
||||
shouldContain(graph.getMarks("1"), "red", "yellow", "blue");
|
||||
graph.mark("1", "red");
|
||||
shouldContain(graph.getMarks("1"), "red", "yellow", "blue");
|
||||
|
||||
shouldContain(graph.getMarks("2"));
|
||||
graph.mark("2", "red");
|
||||
shouldContain(graph.getMarks("8"));
|
||||
graph.mark("8", "blue");
|
||||
shouldContain(graph.getMarks("2"), "red");
|
||||
shouldContain(graph.getMarks("8"), "blue");
|
||||
|
||||
graph.unMark("2");
|
||||
shouldContain(graph.getMarks("2"));
|
||||
graph.unMark("1");
|
||||
shouldContain(graph.getMarks("1"));
|
||||
|
||||
graph.mark("2", "red");
|
||||
graph.mark("2", "blue");
|
||||
shouldContain(graph.getMarks("2"), "red", "blue");
|
||||
graph.mark("4", "green");
|
||||
shouldContain(graph.getMarks("4"), "green");
|
||||
graph.mark("5", "green");
|
||||
shouldContain(graph.getMarks("5"), "green");
|
||||
|
||||
graph.unMarkAll();
|
||||
shouldContain(graph.getMarks("1"));
|
||||
shouldContain(graph.getMarks("2"));
|
||||
shouldContain(graph.getMarks("3"));
|
||||
shouldContain(graph.getMarks("4"));
|
||||
shouldContain(graph.getMarks("5"));
|
||||
shouldContain(graph.getMarks("6"));
|
||||
shouldContain(graph.getMarks("7"));
|
||||
shouldContain(graph.getMarks("8"));
|
||||
|
||||
graph.mark("1", "mark");
|
||||
graph.mark("2", "mark");
|
||||
graph.mark("3", "mark2");
|
||||
graph.mark("1", "mark2");
|
||||
shouldContain(graph.getMarks("1"), "mark", "mark2");
|
||||
shouldContain(graph.getMarks("2"), "mark");
|
||||
shouldContain(graph.getMarks("3"), "mark2");
|
||||
|
||||
graph.unMark("1", "mark");
|
||||
shouldContain(graph.getMarks("1"), "mark2");
|
||||
shouldContain(graph.getMarks("2"), "mark");
|
||||
shouldContain(graph.getMarks("3"), "mark2");
|
||||
|
||||
graph.unMarkAll("mark2");
|
||||
shouldContain(graph.getMarks("1"));
|
||||
shouldContain(graph.getMarks("2"), "mark");
|
||||
shouldContain(graph.getMarks("3"));
|
||||
|
||||
graph.unMark("1", "mark");
|
||||
graph.unMark("2", "mark2");
|
||||
shouldContain(graph.getMarks("1"));
|
||||
shouldContain(graph.getMarks("2"), "mark");
|
||||
shouldContain(graph.getMarks("3"));
|
||||
|
||||
graph.unMark("2", "mark");
|
||||
shouldContain(graph.getMarks("1"));
|
||||
shouldContain(graph.getMarks("2"));
|
||||
shouldContain(graph.getMarks("3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -847,6 +1019,24 @@ public class TestGraph {
|
||||
graph.addEdge("5", "4", 5);
|
||||
graph.addEdge("6", "2", 2);
|
||||
|
||||
graph.mark("1", "blue");
|
||||
graph.mark("3", "blue");
|
||||
graph.mark("5", "blue");
|
||||
|
||||
graph.mark("2", "even");
|
||||
graph.mark("4", "even");
|
||||
graph.mark("6", "even");
|
||||
|
||||
graph.mark("2", "circle");
|
||||
graph.mark("4", "circle");
|
||||
graph.mark("5", "circle");
|
||||
graph.mark("6", "circle");
|
||||
|
||||
graph.mark("1", "z");
|
||||
graph.mark("2", "z");
|
||||
graph.mark("5", "z");
|
||||
graph.mark("4", "z");
|
||||
|
||||
Graph<String, Integer> sub = graph.subGraph("1", -541);
|
||||
shouldContain(sub.vertices(), "1");
|
||||
shouldContain(sub.edges());
|
||||
@@ -858,34 +1048,175 @@ public class TestGraph {
|
||||
sub = graph.subGraph("1", 1);
|
||||
shouldContain(sub.vertices(), "1", "2", "3");
|
||||
shouldContain(sub.edges(),
|
||||
new Graph.Edge<>("1", "2", 1),
|
||||
new Graph.Edge<>("1", "3", 1));
|
||||
new Edge<>("1", "2", 1),
|
||||
new Edge<>("1", "3", 1));
|
||||
|
||||
sub = graph.subGraph("1", 3);
|
||||
shouldContain(sub.vertices(), "1", "2", "3", "5", "4");
|
||||
shouldContain(sub.edges(),
|
||||
new Graph.Edge<>("1", "2", 1),
|
||||
new Graph.Edge<>("1", "3", 1),
|
||||
new Graph.Edge<>("2", "5", 4),
|
||||
new Graph.Edge<>("5", "3", 2),
|
||||
new Graph.Edge<>("5", "4", 5));
|
||||
new Edge<>("1", "2", 1),
|
||||
new Edge<>("1", "3", 1),
|
||||
new Edge<>("2", "5", 4),
|
||||
new Edge<>("5", "3", 2),
|
||||
new Edge<>("5", "4", 5));
|
||||
|
||||
sub = graph.subGraph("6", 2);
|
||||
shouldContain(sub.vertices(), "6", "2", "5");
|
||||
shouldContain(sub.edges(),
|
||||
new Graph.Edge<>("2", "5", 4),
|
||||
new Graph.Edge<>("6", "2", 2));
|
||||
new Edge<>("2", "5", 4),
|
||||
new Edge<>("6", "2", 2));
|
||||
|
||||
sub = graph.subGraph("1", 77689);
|
||||
shouldContain(sub.vertices(), "1", "2", "3", "5", "4", "6");
|
||||
shouldContain(sub.edges(),
|
||||
new Graph.Edge<>("1", "2", 1),
|
||||
new Graph.Edge<>("1", "3", 1),
|
||||
new Graph.Edge<>("2", "5", 4),
|
||||
new Graph.Edge<>("4", "6", 6),
|
||||
new Graph.Edge<>("5", "3", 2),
|
||||
new Graph.Edge<>("5", "4", 5),
|
||||
new Graph.Edge<>("6", "2", 2));
|
||||
new Edge<>("1", "2", 1),
|
||||
new Edge<>("1", "3", 1),
|
||||
new Edge<>("2", "5", 4),
|
||||
new Edge<>("4", "6", 6),
|
||||
new Edge<>("5", "3", 2),
|
||||
new Edge<>("5", "4", 5),
|
||||
new Edge<>("6", "2", 2));
|
||||
|
||||
/* MARKED */
|
||||
sub = graph.subGraph("z");
|
||||
shouldContain(sub.vertices(), "1", "2", "5", "4");
|
||||
shouldContain(sub.edges(),
|
||||
new Edge<>("1", "2", 1),
|
||||
new Edge<>("2", "5", 4),
|
||||
new Edge<>("5", "4", 5));
|
||||
|
||||
sub = graph.subGraph("circle");
|
||||
shouldContain(sub.vertices(), "2", "5", "4", "6");
|
||||
shouldContain(sub.edges(),
|
||||
new Edge<>("2", "5", 4),
|
||||
new Edge<>("4", "6", 6),
|
||||
new Edge<>("5", "4", 5),
|
||||
new Edge<>("6", "2", 2));
|
||||
|
||||
sub = graph.subGraph("blue");
|
||||
shouldContain(sub.vertices(), "1", "3", "5");
|
||||
shouldContain(sub.edges(),
|
||||
new Edge<>("1", "3", 1),
|
||||
new Edge<>("5", "3", 2));
|
||||
|
||||
sub = graph.subGraph("even");
|
||||
shouldContain(sub.vertices(), "2", "4", "6");
|
||||
shouldContain(sub.edges(),
|
||||
new Edge<>("4", "6", 6),
|
||||
new Edge<>("6", "2", 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void vertexClass() {
|
||||
Vertex<String> vertex = new Vertex<>(graph, "stronzo");
|
||||
|
||||
assertEquals("stronzo", vertex.getValue());
|
||||
assertEquals(0, graph.numberOfVertices());
|
||||
|
||||
shouldThrow(unsuppException, () -> vertex.addChild(null, null));
|
||||
shouldThrow(unsuppException, () -> vertex.mark(null));
|
||||
shouldThrow(unsuppException, () -> vertex.removeChild(null));
|
||||
shouldThrow(unsuppException, () -> vertex.visit(null, null));
|
||||
shouldThrow(unsuppException, vertex::unMark);
|
||||
shouldThrow(unsuppException, vertex::getAncestors);
|
||||
shouldThrow(unsuppException, vertex::getChildren);
|
||||
shouldThrow(unsuppException, vertex::getEdgesOut);
|
||||
shouldThrow(unsuppException, vertex::getEdgesIn);
|
||||
shouldThrow(unsuppException, vertex::getChildrenAsVertex);
|
||||
shouldThrow(unsuppException, vertex::getAncestorsAsVertex);
|
||||
shouldThrow(unsuppException, vertex::getMarks);
|
||||
|
||||
vertex.addIfAbsent();
|
||||
assertEquals(1, graph.numberOfVertices());
|
||||
vertex.addIfAbsent();
|
||||
assertEquals(1, graph.numberOfVertices());
|
||||
vertex.addIfAbsent();
|
||||
assertEquals(1, graph.numberOfVertices());
|
||||
|
||||
assertEquals(vertex, graph.getVertex("stronzo"));
|
||||
shouldThrow(nullException, () -> graph.getVertex(null));
|
||||
shouldThrow(notException, () -> graph.getVertex("stronzo1"));
|
||||
|
||||
shouldThrow(nullException, () -> vertex.addChild(null, 3));
|
||||
shouldThrow(nullException, () -> vertex.addChild(null, null));
|
||||
shouldThrow(nullException, () -> vertex.mark(null));
|
||||
shouldThrow(nullException, () -> vertex.removeChild(null));
|
||||
shouldThrow(new NullPointerException(), () -> vertex.visit(null, null));
|
||||
|
||||
shouldThrow(notException, () -> vertex.addChild("1", null));
|
||||
shouldThrow(notException, () -> vertex.addChild("ssdsad", 2));
|
||||
shouldThrow(notException, () -> vertex.removeChild("234"));
|
||||
|
||||
shouldContain(vertex.getMarks());
|
||||
shouldContain(vertex.getAncestors());
|
||||
shouldContain(vertex.getChildren());
|
||||
shouldContain(vertex.getChildrenAsVertex());
|
||||
shouldContain(vertex.getEdgesIn());
|
||||
shouldContain(vertex.getEdgesOut());
|
||||
|
||||
graph.addVertex("1");
|
||||
graph.addVertex("2");
|
||||
graph.addVertex("3");
|
||||
|
||||
graph.addEdge("1", "2", 2);
|
||||
graph.addEdge("3", "stronzo", 6);
|
||||
graph.addEdge("stronzo", "2", 1);
|
||||
graph.addEdge("stronzo", "1", 3);
|
||||
|
||||
shouldContain(vertex.getMarks());
|
||||
shouldContain(vertex.getAncestors(), "3");
|
||||
shouldContain(vertex.getChildren(), "1", "2");
|
||||
shouldContain(vertex.getChildrenAsVertex(), new Vertex<>(graph, "1"), new Vertex<>(graph, "2"));
|
||||
shouldContain(vertex.getAncestorsAsVertex(), new Vertex<>(graph, "3"));
|
||||
shouldContain(vertex.getEdgesIn(),
|
||||
new Edge<>("3", "stronzo", 6));
|
||||
shouldContain(graph.getEdgesIn(vertex.getValue()),
|
||||
new Edge<>("3", "stronzo", 6));
|
||||
shouldContain(vertex.getEdgesOut(),
|
||||
new Edge<>("stronzo", "1", 3),
|
||||
new Edge<>("stronzo", "2", 1));
|
||||
shouldContain(graph.getEdgesOut(vertex.getValue()),
|
||||
new Edge<>("stronzo", "1", 3),
|
||||
new Edge<>("stronzo", "2", 1));
|
||||
|
||||
vertex.mark("ciao");
|
||||
vertex.mark("ciao2");
|
||||
shouldContain(vertex.getMarks(), "ciao", "ciao2");
|
||||
shouldContain(graph.getMarks(vertex.getValue()), "ciao", "ciao2");
|
||||
vertex.unMark();
|
||||
shouldContain(vertex.getMarks());
|
||||
|
||||
vertex.removeChild("1");
|
||||
shouldContain(vertex.getChildren(), "2");
|
||||
vertex.addChild("3", 23);
|
||||
shouldContain(vertex.getChildren(), "2", "3");
|
||||
shouldContain(vertex.getAncestors(), "3");
|
||||
shouldContain(vertex.getEdgesOut(), new Edge<>("stronzo", "3", 23), new Edge<>("stronzo", "2", 1));
|
||||
shouldContain(graph.getEdgesOut(vertex.getValue()), new Edge<>("stronzo", "3", 23), new Edge<>("stronzo", "2", 1));
|
||||
shouldContain(vertex.getEdgesIn(), new Edge<>("3", "stronzo", 6));
|
||||
shouldContain(graph.getEdgesIn(vertex.getValue()), new Edge<>("3", "stronzo", 6));
|
||||
|
||||
assertTrue(vertex.isStillContained());
|
||||
vertex.remove();
|
||||
assertFalse(vertex.isStillContained());
|
||||
assertFalse(graph.contains(vertex.getValue()));
|
||||
assertEquals(3, graph.numberOfVertices());
|
||||
|
||||
shouldThrow(unsuppException, () -> vertex.addChild(null, null));
|
||||
shouldThrow(unsuppException, () -> vertex.mark(null));
|
||||
shouldThrow(unsuppException, () -> vertex.removeChild(null));
|
||||
shouldThrow(unsuppException, () -> vertex.visit(null, null));
|
||||
shouldThrow(unsuppException, vertex::unMark);
|
||||
shouldThrow(unsuppException, vertex::getAncestors);
|
||||
shouldThrow(unsuppException, vertex::getChildren);
|
||||
shouldThrow(unsuppException, vertex::getEdgesOut);
|
||||
shouldThrow(unsuppException, vertex::getEdgesIn);
|
||||
shouldThrow(unsuppException, vertex::getChildrenAsVertex);
|
||||
shouldThrow(unsuppException, vertex::getAncestorsAsVertex);
|
||||
shouldThrow(unsuppException, vertex::getMarks);
|
||||
|
||||
vertex.addIfAbsent();
|
||||
assertEquals(4, graph.numberOfVertices());
|
||||
}
|
||||
|
||||
// TODO test saveFile
|
||||
|
||||
Reference in New Issue
Block a user