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