66import de .variantsync .evolution .variability .EvolutionStep ;
77import de .variantsync .evolution .variability .sequenceextraction .CleaningEvolutionStepsStream ;
88import org .junit .Assert ;
9+ import org .junit .Before ;
910import org .junit .Test ;
1011
11- import java .util .List ;
12- import java .util .Stack ;
12+ import java .util .*;
1313
1414public 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