[518] | 1 | package de.ugoe.cs.quest.coverage;
|
---|
| 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.Collection;
|
---|
| 5 | import java.util.LinkedHashSet;
|
---|
| 6 | import java.util.LinkedList;
|
---|
| 7 | import java.util.List;
|
---|
| 8 | import java.util.Map;
|
---|
| 9 | import java.util.Map.Entry;
|
---|
| 10 | import java.util.Random;
|
---|
| 11 | import java.util.Set;
|
---|
| 12 | import de.ugoe.cs.quest.coverage.SequenceTools;
|
---|
| 13 | import de.ugoe.cs.quest.eventcore.Event;
|
---|
| 14 | import de.ugoe.cs.quest.usageprofiles.FirstOrderMarkovModel;
|
---|
| 15 | import de.ugoe.cs.quest.usageprofiles.IStochasticProcess;
|
---|
| 16 | import de.ugoe.cs.quest.usageprofiles.MockTrieBasedModel;
|
---|
[548] | 17 | import de.ugoe.cs.quest.usageprofiles.mock.StringEventType;
|
---|
[518] | 18 |
|
---|
| 19 | import org.junit.*;
|
---|
| 20 |
|
---|
| 21 | import static org.junit.Assert.*;
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * The class <code>SequenceToolsTest</code> contains tests for the class <code>{@link SequenceTools}</code>.
|
---|
| 25 | *
|
---|
| 26 | * @author Steffen Herbold
|
---|
| 27 | * @version 1.0
|
---|
| 28 | */
|
---|
| 29 | public class SequenceToolsTest {
|
---|
| 30 |
|
---|
[548] | 31 | Collection<List<Event>> sequences;
|
---|
| 32 | Set<List<Event>> subSequences;
|
---|
[518] | 33 | MockTrieBasedModel mockProcess;
|
---|
| 34 |
|
---|
| 35 | @Test
|
---|
| 36 | public void testContainedSubSequences_1()
|
---|
| 37 | throws Exception {
|
---|
| 38 | int length = 2;
|
---|
| 39 |
|
---|
[548] | 40 | Set<List<Event>> result = SequenceTools.containedSubSequences(sequences, length);
|
---|
[518] | 41 |
|
---|
| 42 | assertNotNull(result);
|
---|
| 43 | assertTrue(result.containsAll(subSequences));
|
---|
| 44 | assertEquals(subSequences.size(), result.size());
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | @Test
|
---|
| 48 | public void testContainedSubSequences_2()
|
---|
| 49 | throws Exception {
|
---|
| 50 | int length = 2;
|
---|
| 51 |
|
---|
[548] | 52 | Set<List<Event>> result = SequenceTools.containedSubSequences(null, length);
|
---|
[518] | 53 | assertNotNull(result);
|
---|
| 54 | assertTrue(result.isEmpty());
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | @Test(expected=java.security.InvalidParameterException.class)
|
---|
| 58 | public void testContainedSubSequences_3()
|
---|
| 59 | throws Exception {
|
---|
| 60 | int length = 0;
|
---|
| 61 |
|
---|
| 62 | SequenceTools.containedSubSequences(sequences, length);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | @Test(expected=java.security.InvalidParameterException.class)
|
---|
| 66 | public void testContainedSubSequences_4()
|
---|
| 67 | throws Exception {
|
---|
| 68 | int length = -1;
|
---|
| 69 |
|
---|
| 70 | SequenceTools.containedSubSequences(sequences, length);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | @Test
|
---|
| 74 | public void testGenerateWeights_1()
|
---|
| 75 | throws Exception {
|
---|
[548] | 76 | Map<List<Event>, Double> result = SequenceTools.generateWeights(mockProcess, subSequences);
|
---|
[518] | 77 |
|
---|
| 78 | assertNotNull(result);
|
---|
[548] | 79 | Set<Entry<List<Event>, Double>> entrySet = result.entrySet();
|
---|
[518] | 80 | assertEquals(subSequences.size(),entrySet.size());
|
---|
[548] | 81 | for( Entry<List<Event>, Double> entry : entrySet ) {
|
---|
[518] | 82 | assertEquals(Double.valueOf(2.0d), entry.getValue());
|
---|
| 83 | assertTrue(subSequences.contains(entry.getKey()));
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | @Test
|
---|
| 88 | public void testGenerateWeights_2()
|
---|
| 89 | throws Exception {
|
---|
[548] | 90 | Map<List<Event>, Double> result = SequenceTools.generateWeights(null, subSequences);
|
---|
[518] | 91 |
|
---|
[548] | 92 | Set<Entry<List<Event>, Double>> entrySet = result.entrySet();
|
---|
[518] | 93 | assertEquals(subSequences.size(),entrySet.size());
|
---|
[548] | 94 | for( Entry<List<Event>, Double> entry : entrySet ) {
|
---|
[518] | 95 | assertEquals(Double.valueOf(0.0d), entry.getValue());
|
---|
| 96 | assertTrue(subSequences.contains(entry.getKey()));
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | @Test
|
---|
| 101 | public void testGenerateWeights_3()
|
---|
| 102 | throws Exception {
|
---|
[548] | 103 | Map<List<Event>, Double> result = SequenceTools.generateWeights(mockProcess, null);
|
---|
[518] | 104 |
|
---|
| 105 | assertNotNull(result);
|
---|
| 106 | assertTrue(result.isEmpty());
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | @Test
|
---|
| 110 | public void testNumSequences_1()
|
---|
| 111 | throws Exception {
|
---|
| 112 | int length = 2;
|
---|
| 113 | int expected = 49;
|
---|
| 114 |
|
---|
| 115 | long result = SequenceTools.numSequences(mockProcess, length);
|
---|
| 116 |
|
---|
| 117 | assertEquals(expected, result);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | @Test
|
---|
| 121 | public void testNumSequences_2()
|
---|
| 122 | throws Exception {
|
---|
| 123 | int length = 2;
|
---|
| 124 | int expected = 49;
|
---|
| 125 |
|
---|
| 126 | long result = SequenceTools.numSequences(mockProcess, length);
|
---|
| 127 |
|
---|
| 128 | assertEquals(expected, result);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | @Test(expected = java.security.InvalidParameterException.class )
|
---|
| 132 | public void testNumSequences_3()
|
---|
| 133 | throws Exception {
|
---|
| 134 | int length = 0;
|
---|
| 135 |
|
---|
| 136 | SequenceTools.numSequences(mockProcess, length);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | @Test(expected = java.security.InvalidParameterException.class )
|
---|
| 140 | public void testNumSequences_4()
|
---|
| 141 | throws Exception {
|
---|
| 142 | IStochasticProcess process = new FirstOrderMarkovModel(new Random());
|
---|
| 143 | int length = -1;
|
---|
| 144 |
|
---|
| 145 | SequenceTools.numSequences(process, length);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | @Before
|
---|
| 149 | public void setUp()
|
---|
| 150 | throws Exception {
|
---|
[548] | 151 | sequences = new LinkedList<List<Event>>();
|
---|
| 152 | List<Event> sequence1 = new ArrayList<Event>();
|
---|
| 153 | sequence1.add(new Event(new StringEventType("a")));
|
---|
| 154 | sequence1.add(new Event(new StringEventType("b")));
|
---|
| 155 | sequence1.add(new Event(new StringEventType("r")));
|
---|
| 156 | sequence1.add(new Event(new StringEventType("a")));
|
---|
| 157 | List<Event> sequence2 = new ArrayList<Event>();
|
---|
| 158 | sequence2.add(new Event(new StringEventType("c")));
|
---|
| 159 | sequence2.add(new Event(new StringEventType("a")));
|
---|
| 160 | sequence2.add(new Event(new StringEventType("d")));
|
---|
| 161 | sequence2.add(new Event(new StringEventType("a")));
|
---|
| 162 | sequence2.add(new Event(new StringEventType("b")));
|
---|
| 163 | sequence2.add(new Event(new StringEventType("r")));
|
---|
| 164 | sequence2.add(new Event(new StringEventType("a")));
|
---|
[518] | 165 | sequences.add(sequence1);
|
---|
| 166 | sequences.add(sequence2);
|
---|
| 167 |
|
---|
[548] | 168 | subSequences = new LinkedHashSet<List<Event>>();
|
---|
| 169 | List<Event> tmpList = new ArrayList<Event>();
|
---|
| 170 | tmpList.add(new Event(new StringEventType("a")));
|
---|
| 171 | tmpList.add(new Event(new StringEventType("b")));
|
---|
[518] | 172 | subSequences.add(tmpList);
|
---|
[548] | 173 | tmpList = new ArrayList<Event>();
|
---|
| 174 | tmpList.add(new Event(new StringEventType("b")));
|
---|
| 175 | tmpList.add(new Event(new StringEventType("r")));
|
---|
[518] | 176 | subSequences.add(tmpList);
|
---|
[548] | 177 | tmpList = new ArrayList<Event>();
|
---|
| 178 | tmpList.add(new Event(new StringEventType("r")));
|
---|
| 179 | tmpList.add(new Event(new StringEventType("a")));
|
---|
[518] | 180 | subSequences.add(tmpList);
|
---|
[548] | 181 | tmpList = new ArrayList<Event>();
|
---|
| 182 | tmpList.add(new Event(new StringEventType("c")));
|
---|
| 183 | tmpList.add(new Event(new StringEventType("a")));
|
---|
[518] | 184 | subSequences.add(tmpList);
|
---|
[548] | 185 | tmpList = new ArrayList<Event>();
|
---|
| 186 | tmpList.add(new Event(new StringEventType("a")));
|
---|
| 187 | tmpList.add(new Event(new StringEventType("d")));
|
---|
[518] | 188 | subSequences.add(tmpList);
|
---|
[548] | 189 | tmpList = new ArrayList<Event>();
|
---|
| 190 | tmpList.add(new Event(new StringEventType("d")));
|
---|
| 191 | tmpList.add(new Event(new StringEventType("a")));
|
---|
[518] | 192 | subSequences.add(tmpList);
|
---|
| 193 |
|
---|
| 194 | int markovOrder = 2;
|
---|
| 195 | mockProcess = new MockTrieBasedModel(markovOrder, new Random());
|
---|
| 196 | mockProcess.train(sequences);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | public static void main(String[] args) {
|
---|
| 200 | new org.junit.runner.JUnitCore().run(SequenceToolsTest.class);
|
---|
| 201 | }
|
---|
| 202 | } |
---|