Skip to content

Commit

Permalink
#415 CR
Browse files Browse the repository at this point in the history
  • Loading branch information
HDouss committed Mar 19, 2020
1 parent d4c259d commit ed20b1f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/main/java/org/jpeek/graph/Disjoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ public List<Set<Node>> value() throws Exception {
final List<Set<Node>> result = new ArrayList<>(unvisited.size());
while (!unvisited.isEmpty()) {
final Node node = unvisited.iterator().next();
final Set<Node> adding = new HashSet<>();
adding.add(node);
final Set<Node> visiting = new HashSet<>();
visiting.add(node);
final Set<Node> current = new HashSet<>();
while (!adding.isEmpty()) {
final Node visit = adding.iterator().next();
while (!visiting.isEmpty()) {
final Node visit = visiting.iterator().next();
current.add(visit);
for (final Node connexion:visit.connections()) {
if (!current.contains(connexion)) {
adding.add(connexion);
visiting.add(connexion);
}
}
unvisited.remove(visit);
adding.remove(visit);
visiting.remove(visit);
}
result.add(current);
}
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/org/jpeek/graph/DisjointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,36 @@ public void calculatesDisjointSets() throws Exception {
).affirm();
}

@Test
@SuppressWarnings("unchecked")
public void calculatesDisjointSetsForUnconnected() throws Exception {
final Node one = new Node.Simple("1");
final Node two = new Node.Simple("2");
final Node three = new Node.Simple("3");
final Node four = new Node.Simple("4");
final Graph graph = new FakeGraph(
new ListOf<>(one, two, three, four)
);
new Assertion<>(
"Must build disjoint sets for unconnected graph",
new Disjoint(graph).value(),
new AllOf<Iterable<Set<Node>>>(
new ListOf<>(
new HasValuesMatching<>(
set -> set.contains(one) && set.size() == 1
),
new HasValuesMatching<>(
set -> set.contains(two) && set.size() == 1
),
new HasValuesMatching<>(
set -> set.contains(three) && set.size() == 1
),
new HasValuesMatching<>(
set -> set.contains(four) && set.size() == 1
)
)
)
).affirm();
}

}

0 comments on commit ed20b1f

Please sign in to comment.