|
| 1 | +package org.variantsync.vevos.simulation.variability.sequenceextraction; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | +import org.variantsync.functjonal.list.NonEmptyList; |
| 5 | +import org.variantsync.vevos.simulation.variability.SPLCommit; |
| 6 | + |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.HashSet; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +public class LongestNonOverlappingSequencesTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + public void exampleExtraction() { |
| 16 | + // For example, if the commits comprise three partially overlapping sequences ([A-B-C-D-E], [X-Y-Z], [A-B-F-G]), |
| 17 | + // the function will return the sequences ([A-B-C-D-E], [X-Y-Z], [F-G]). |
| 18 | + final SPLCommit a = new SPLCommit("A"); |
| 19 | + final SPLCommit b = new SPLCommit("B"); |
| 20 | + final SPLCommit c = new SPLCommit("C"); |
| 21 | + final SPLCommit d = new SPLCommit("D"); |
| 22 | + final SPLCommit e = new SPLCommit("E"); |
| 23 | + final SPLCommit f = new SPLCommit("F"); |
| 24 | + final SPLCommit g = new SPLCommit("G"); |
| 25 | + final SPLCommit x = new SPLCommit("X"); |
| 26 | + final SPLCommit y = new SPLCommit("Y"); |
| 27 | + final SPLCommit z = new SPLCommit("Z"); |
| 28 | + // A has no parent |
| 29 | + a.setParents(); |
| 30 | + // A-B |
| 31 | + b.setParents(a); |
| 32 | + // B-C |
| 33 | + c.setParents(b); |
| 34 | + // C-D |
| 35 | + d.setParents(c); |
| 36 | + // D-E |
| 37 | + e.setParents(d); |
| 38 | + // B-F |
| 39 | + f.setParents(b); |
| 40 | + // F-G |
| 41 | + g.setParents(f); |
| 42 | + // X has no parent |
| 43 | + x.setParents(); |
| 44 | + // X-Y |
| 45 | + y.setParents(x); |
| 46 | + // Y-Z |
| 47 | + z.setParents(y); |
| 48 | + |
| 49 | + List<SPLCommit> exampleCommits = Arrays.asList(a, b, c, d, e, f, g, x, y, z); |
| 50 | + NonEmptyList<SPLCommit> expectedOne = new NonEmptyList<>(Arrays.asList(a,b,c,d,e)); |
| 51 | + NonEmptyList<SPLCommit> expectedTwo = new NonEmptyList<>(Arrays.asList(x, y, z)); |
| 52 | + NonEmptyList<SPLCommit> expectedThree = new NonEmptyList<>(Arrays.asList(f, g)); |
| 53 | + |
| 54 | + LongestNonOverlappingSequences algorithm = new LongestNonOverlappingSequences(); |
| 55 | + List<NonEmptyList<SPLCommit>> result = algorithm.extract(exampleCommits); |
| 56 | + |
| 57 | + assert result.size() == 3; |
| 58 | + assert sequencesAreEqual(result.get(0), expectedOne); |
| 59 | + assert sequencesAreEqual(result.get(1), expectedTwo); |
| 60 | + assert sequencesAreEqual(result.get(2), expectedThree); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void stackOverflowPrevented() { |
| 65 | + int size = 10_000; |
| 66 | + Set<SPLCommit> commits = new HashSet<>(); |
| 67 | + SPLCommit previousCommit = new SPLCommit("0"); |
| 68 | + previousCommit.setParents(); |
| 69 | + commits.add(previousCommit); |
| 70 | + for (int i = 1; i < size; i++) { |
| 71 | + SPLCommit commit = new SPLCommit(String.valueOf(i)); |
| 72 | + commit.setParents(previousCommit); |
| 73 | + commits.add(commit); |
| 74 | + previousCommit = commit; |
| 75 | + } |
| 76 | + |
| 77 | + LongestNonOverlappingSequences algo = new LongestNonOverlappingSequences(); |
| 78 | + var result = algo.extract(commits); |
| 79 | + assert result.size() == 1; |
| 80 | + assert result.get(0).size() == size; |
| 81 | + } |
| 82 | + |
| 83 | + private boolean sequencesAreEqual(final NonEmptyList<SPLCommit> a, final NonEmptyList<SPLCommit> b) { |
| 84 | + if (a.size() != b.size()) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + for (int i = 0; i < a.size(); i++) { |
| 88 | + if (!a.get(i).id().equals(b.get(i).id())) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + } |
| 92 | + return true; |
| 93 | + } |
| 94 | +} |
0 commit comments