Skip to content

Commit 394194e

Browse files
committed
made test really a test and not just a print
1 parent 7840df6 commit 394194e

2 files changed

Lines changed: 96 additions & 26 deletions

File tree

src/main/java/de/variantsync/evolution/variability/sequenceextraction/Domino.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public static <C extends Commit> Stack<Stack<C>> Sort(final Iterable<EvolutionSt
3333
*/
3434
final Stack<Stack<C>> chains = new Stack<>();
3535

36-
System.out.println("Step 1: Build Chains");
36+
// System.out.println("Step 1: Build Chains");
3737
buildChains : for (final EvolutionStep<C> domino : dominos) {
38-
System.out.println(" Found " + domino);
38+
// System.out.println(" Found " + domino);
3939
// push the domino to an existing stack if possible
4040
for (final Stack<C> row : chains) {
4141
if (row.peek().equals(domino.parent())) {
4242
row.push(domino.child());
43-
System.out.println(" Pushed it to chain " + row);
43+
// System.out.println(" Pushed it to chain " + row);
4444
continue buildChains;
4545
}
4646
}
@@ -50,39 +50,39 @@ public static <C extends Commit> Stack<Stack<C>> Sort(final Iterable<EvolutionSt
5050
newRow.add(domino.parent());
5151
newRow.add(domino.child());
5252
chains.push(newRow);
53-
System.out.println(" Created new chain " + newRow);
53+
// System.out.println(" Created new chain " + newRow);
5454
}
5555

5656

57-
System.out.println("Result of step 1: " + chains);
57+
// System.out.println("Result of step 1: " + chains);
5858

5959
/*
6060
* Second, merge all chains by stacking them ontop of each other.
6161
* Repeat this until we cannot merge any chains.
6262
*/
63-
System.out.println("Step 2: Merge Chains");
63+
// System.out.println("Step 2: Merge Chains");
6464
final Stack<Stack<C>> finalRows = new Stack<>();
6565
while (!chains.isEmpty()) {
6666
// Pick one of the chains ...
6767
Stack<C> current = chains.pop();
68-
System.out.println(" Inspecting " + current);
68+
// System.out.println(" Inspecting " + current);
6969
// ... and merge it with the other chains as long as possible.
7070
final Set<Stack<C>> mergedStacks = new HashSet<>();
7171
do {
7272
mergedStacks.clear();
7373
for (final Stack<C> other : chains) {
7474
if (current.firstElement().equals(other.lastElement())) {
75-
System.out.println(" pushing it to " + other);
75+
// System.out.println(" pushing it to " + other);
7676
other.pop();
7777
StackUtil.pushAToB(current, other);
7878
current = other;
79-
System.out.println(" got " + current);
79+
// System.out.println(" got " + current);
8080
mergedStacks.add(other);
8181
} else if (other.firstElement().equals(current.lastElement())) {
82-
System.out.println(" pushed " + other + " onto it");
82+
// System.out.println(" pushed " + other + " onto it");
8383
current.pop();
8484
StackUtil.pushAToB(other, current);
85-
System.out.println(" got " + current);
85+
// System.out.println(" got " + current);
8686
mergedStacks.add(other);
8787
}
8888
}

src/test/java/de/variantsync/evolution/EvolutionStepTest.java

Lines changed: 85 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,68 @@
66
import de.variantsync.evolution.variability.EvolutionStep;
77
import de.variantsync.evolution.variability.sequenceextraction.CleaningEvolutionStepsStream;
88
import org.junit.Assert;
9+
import org.junit.Before;
910
import org.junit.Test;
1011

11-
import java.util.List;
12-
import java.util.Stack;
12+
import java.util.*;
1313

1414
public class EvolutionStepTest {
1515
private static class TestCommit extends Commit implements CachedValue {
16+
private boolean forgot = false;
1617
public TestCommit(final String commitId) {
1718
super(commitId);
1819
}
1920
@Override
2021
public void forget() {
22+
forgot = true;
2123
System.out.println("forget(" + super.id() + ")");
2224
}
25+
26+
public boolean forgotten() {
27+
return forgot;
28+
}
2329
}
2430

25-
private static TestCommit commit(final String id) {
26-
return new TestCommit(id);
31+
private static TestCommit commit(final int id) {
32+
return new TestCommit("" + id);
2733
}
2834

29-
private static EvolutionStep<TestCommit> step(final String parent, final String child) {
30-
return new EvolutionStep<>(commit(parent), commit(child));
35+
private static EvolutionStep<TestCommit> step(final TestCommit parent, final TestCommit child) {
36+
return new EvolutionStep<>(parent, child);
37+
}
38+
39+
private static EvolutionStep<TestCommit> step(final Map<Integer, TestCommit> commits, final int parent, final int child) {
40+
return step(commits.get(parent), commits.get(child));
41+
}
42+
43+
private Map<Integer, TestCommit> successCommits;
44+
// valid pairs of commits
45+
private EvolutionStep<TestCommit>
46+
s12,
47+
s23,
48+
s34,
49+
s45,
50+
s1011,
51+
s1112,
52+
s1213;
53+
54+
@Before
55+
public void generateInput() {
56+
successCommits = new HashMap<>();
57+
for (int i = 1; i <= 5; ++i) {
58+
successCommits.put(i, commit(i));
59+
}
60+
for (int i = 10; i <= 13; ++i) {
61+
successCommits.put(i, commit(i));
62+
}
63+
64+
s12 = step(successCommits, 1, 2);
65+
s23 = step(successCommits, 2, 3);
66+
s34 = step(successCommits, 3, 4);
67+
s45 = step(successCommits, 4, 5);
68+
s1011 = step(successCommits, 10, 11);
69+
s1112 = step(successCommits, 11, 12);
70+
s1213 = step(successCommits, 12, 13);
3171
}
3272

3373
@Test
@@ -50,22 +90,52 @@ public void testMergeStacks() {
5090

5191
@Test
5292
public void evolSteps() {
93+
// random order of commit pairs
5394
final List<EvolutionStep<TestCommit>> input = List.of(
54-
step("1", "2"),
55-
step("4", "5"),
56-
step("2", "3"),
57-
step("3", "4"),
58-
59-
step("12", "13"),
60-
step("10", "11"),
61-
step("11", "12")
95+
s12,
96+
s45,
97+
s23,
98+
s34,
99+
100+
s1213,
101+
s1011,
102+
s1112
103+
);
104+
105+
// we expect two ordered sublists (in reverse order)
106+
final List<List<EvolutionStep<TestCommit>>> expectedOutputSequences = List.of(
107+
new ArrayList<>(List.of(s45, s34, s23, s12)),
108+
new ArrayList<>(List.of(s1213, s1112, s1011))
62109
);
63110

64111
final CleaningEvolutionStepsStream<TestCommit> steps = new CleaningEvolutionStepsStream<>(input);
65112
System.out.println("Sorted steps: " + steps);
66113

114+
List<EvolutionStep<TestCommit>> currentExpectedOutput = null;
67115
for (final EvolutionStep<TestCommit> step : steps) {
68-
System.out.println("Processing " + step);
116+
// System.out.println("Processing " + step);
117+
118+
if (currentExpectedOutput == null) {
119+
for (final List<EvolutionStep<TestCommit>> expectedChain : expectedOutputSequences) {
120+
if (!expectedChain.isEmpty() && step.equals(expectedChain.get(0))) {
121+
currentExpectedOutput = expectedChain;
122+
break;
123+
}
124+
}
125+
if (currentExpectedOutput == null) {
126+
Assert.fail("Found step " + step + " is not the start of any expected output sequence " + expectedOutputSequences + "!");
127+
}
128+
}
129+
130+
Assert.assertEquals(step, currentExpectedOutput.get(0));
131+
currentExpectedOutput.remove(0);
132+
if (currentExpectedOutput.isEmpty()) {
133+
currentExpectedOutput = null;
134+
}
135+
}
136+
137+
for (final TestCommit c : successCommits.values()) {
138+
Assert.assertTrue("Commit " + c + " is still cached!", c.forgotten());
69139
}
70140
}
71141
}

0 commit comments

Comments
 (0)