1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.autoquest.coverage;
|
---|
16 |
|
---|
17 | import java.util.ArrayList;
|
---|
18 | import java.util.Collection;
|
---|
19 | import java.util.LinkedHashSet;
|
---|
20 | import java.util.LinkedList;
|
---|
21 | import java.util.List;
|
---|
22 | import java.util.Map;
|
---|
23 | import java.util.Map.Entry;
|
---|
24 | import java.util.Random;
|
---|
25 | import java.util.Set;
|
---|
26 |
|
---|
27 | import de.ugoe.cs.autoquest.coverage.SequenceTools;
|
---|
28 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
29 | import de.ugoe.cs.autoquest.eventcore.StringEventType;
|
---|
30 | import de.ugoe.cs.autoquest.usageprofiles.FirstOrderMarkovModel;
|
---|
31 | import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess;
|
---|
32 | import de.ugoe.cs.autoquest.usageprofiles.MockTrieBasedModel;
|
---|
33 |
|
---|
34 | import org.junit.*;
|
---|
35 |
|
---|
36 | import static org.junit.Assert.*;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * The class <code>SequenceToolsTest</code> contains tests for the class <code>{@link SequenceTools}</code>.
|
---|
40 | *
|
---|
41 | * @author Steffen Herbold
|
---|
42 | * @version 1.0
|
---|
43 | */
|
---|
44 | public class SequenceToolsTest {
|
---|
45 |
|
---|
46 | Collection<List<Event>> sequences;
|
---|
47 | Set<List<Event>> subSequences;
|
---|
48 | MockTrieBasedModel mockProcess;
|
---|
49 |
|
---|
50 | @Test
|
---|
51 | public void testContainedSubSequences_1()
|
---|
52 | throws Exception {
|
---|
53 | int length = 2;
|
---|
54 |
|
---|
55 | Set<List<Event>> result = SequenceTools.containedSubSequences(sequences, length);
|
---|
56 |
|
---|
57 | assertNotNull(result);
|
---|
58 | assertTrue(result.containsAll(subSequences));
|
---|
59 | assertEquals(subSequences.size(), result.size());
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Test
|
---|
63 | public void testContainedSubSequences_2()
|
---|
64 | throws Exception {
|
---|
65 | int length = 2;
|
---|
66 |
|
---|
67 | Set<List<Event>> result = SequenceTools.containedSubSequences(null, length);
|
---|
68 | assertNotNull(result);
|
---|
69 | assertTrue(result.isEmpty());
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Test(expected=java.lang.IllegalArgumentException.class)
|
---|
73 | public void testContainedSubSequences_3()
|
---|
74 | throws Exception {
|
---|
75 | int length = 0;
|
---|
76 |
|
---|
77 | SequenceTools.containedSubSequences(sequences, length);
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Test(expected=java.lang.IllegalArgumentException.class)
|
---|
81 | public void testContainedSubSequences_4()
|
---|
82 | throws Exception {
|
---|
83 | int length = -1;
|
---|
84 |
|
---|
85 | SequenceTools.containedSubSequences(sequences, length);
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Test
|
---|
89 | public void testGenerateWeights_1()
|
---|
90 | throws Exception {
|
---|
91 | Map<List<Event>, Double> result = SequenceTools.generateWeights(mockProcess, subSequences);
|
---|
92 |
|
---|
93 | assertNotNull(result);
|
---|
94 | Set<Entry<List<Event>, Double>> entrySet = result.entrySet();
|
---|
95 | assertEquals(subSequences.size(),entrySet.size());
|
---|
96 | for( Entry<List<Event>, Double> entry : entrySet ) {
|
---|
97 | assertEquals(Double.valueOf(2.0d), entry.getValue());
|
---|
98 | assertTrue(subSequences.contains(entry.getKey()));
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | @Test
|
---|
103 | public void testGenerateWeights_2()
|
---|
104 | throws Exception {
|
---|
105 | Map<List<Event>, Double> result = SequenceTools.generateWeights(null, subSequences);
|
---|
106 |
|
---|
107 | Set<Entry<List<Event>, Double>> entrySet = result.entrySet();
|
---|
108 | assertEquals(subSequences.size(),entrySet.size());
|
---|
109 | for( Entry<List<Event>, Double> entry : entrySet ) {
|
---|
110 | assertEquals(Double.valueOf(0.0d), entry.getValue());
|
---|
111 | assertTrue(subSequences.contains(entry.getKey()));
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Test
|
---|
116 | public void testGenerateWeights_3()
|
---|
117 | throws Exception {
|
---|
118 | Map<List<Event>, Double> result = SequenceTools.generateWeights(mockProcess, null);
|
---|
119 |
|
---|
120 | assertNotNull(result);
|
---|
121 | assertTrue(result.isEmpty());
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Test
|
---|
125 | public void testNumSequences_1()
|
---|
126 | throws Exception {
|
---|
127 | int length = 2;
|
---|
128 | int expected = 49;
|
---|
129 |
|
---|
130 | long result = SequenceTools.numSequences(mockProcess, length);
|
---|
131 |
|
---|
132 | assertEquals(expected, result);
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Test
|
---|
136 | public void testNumSequences_2()
|
---|
137 | throws Exception {
|
---|
138 | int length = 2;
|
---|
139 | int expected = 49;
|
---|
140 |
|
---|
141 | long result = SequenceTools.numSequences(mockProcess, length);
|
---|
142 |
|
---|
143 | assertEquals(expected, result);
|
---|
144 | }
|
---|
145 |
|
---|
146 | @Test(expected = java.lang.IllegalArgumentException.class )
|
---|
147 | public void testNumSequences_3()
|
---|
148 | throws Exception {
|
---|
149 | int length = 0;
|
---|
150 |
|
---|
151 | SequenceTools.numSequences(mockProcess, length);
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Test(expected = java.lang.IllegalArgumentException.class )
|
---|
155 | public void testNumSequences_4()
|
---|
156 | throws Exception {
|
---|
157 | IStochasticProcess process = new FirstOrderMarkovModel(new Random());
|
---|
158 | int length = -1;
|
---|
159 |
|
---|
160 | SequenceTools.numSequences(process, length);
|
---|
161 | }
|
---|
162 |
|
---|
163 | @Before
|
---|
164 | public void setUp()
|
---|
165 | throws Exception {
|
---|
166 | sequences = new LinkedList<List<Event>>();
|
---|
167 | List<Event> sequence1 = new ArrayList<Event>();
|
---|
168 | sequence1.add(new Event(new StringEventType("a")));
|
---|
169 | sequence1.add(new Event(new StringEventType("b")));
|
---|
170 | sequence1.add(new Event(new StringEventType("r")));
|
---|
171 | sequence1.add(new Event(new StringEventType("a")));
|
---|
172 | List<Event> sequence2 = new ArrayList<Event>();
|
---|
173 | sequence2.add(new Event(new StringEventType("c")));
|
---|
174 | sequence2.add(new Event(new StringEventType("a")));
|
---|
175 | sequence2.add(new Event(new StringEventType("d")));
|
---|
176 | sequence2.add(new Event(new StringEventType("a")));
|
---|
177 | sequence2.add(new Event(new StringEventType("b")));
|
---|
178 | sequence2.add(new Event(new StringEventType("r")));
|
---|
179 | sequence2.add(new Event(new StringEventType("a")));
|
---|
180 | sequences.add(sequence1);
|
---|
181 | sequences.add(sequence2);
|
---|
182 |
|
---|
183 | subSequences = new LinkedHashSet<List<Event>>();
|
---|
184 | List<Event> tmpList = new ArrayList<Event>();
|
---|
185 | tmpList.add(new Event(new StringEventType("a")));
|
---|
186 | tmpList.add(new Event(new StringEventType("b")));
|
---|
187 | subSequences.add(tmpList);
|
---|
188 | tmpList = new ArrayList<Event>();
|
---|
189 | tmpList.add(new Event(new StringEventType("b")));
|
---|
190 | tmpList.add(new Event(new StringEventType("r")));
|
---|
191 | subSequences.add(tmpList);
|
---|
192 | tmpList = new ArrayList<Event>();
|
---|
193 | tmpList.add(new Event(new StringEventType("r")));
|
---|
194 | tmpList.add(new Event(new StringEventType("a")));
|
---|
195 | subSequences.add(tmpList);
|
---|
196 | tmpList = new ArrayList<Event>();
|
---|
197 | tmpList.add(new Event(new StringEventType("c")));
|
---|
198 | tmpList.add(new Event(new StringEventType("a")));
|
---|
199 | subSequences.add(tmpList);
|
---|
200 | tmpList = new ArrayList<Event>();
|
---|
201 | tmpList.add(new Event(new StringEventType("a")));
|
---|
202 | tmpList.add(new Event(new StringEventType("d")));
|
---|
203 | subSequences.add(tmpList);
|
---|
204 | tmpList = new ArrayList<Event>();
|
---|
205 | tmpList.add(new Event(new StringEventType("d")));
|
---|
206 | tmpList.add(new Event(new StringEventType("a")));
|
---|
207 | subSequences.add(tmpList);
|
---|
208 |
|
---|
209 | int markovOrder = 2;
|
---|
210 | mockProcess = new MockTrieBasedModel(markovOrder, new Random());
|
---|
211 | mockProcess.train(sequences);
|
---|
212 | }
|
---|
213 |
|
---|
214 | public static void main(String[] args) {
|
---|
215 | new org.junit.runner.JUnitCore().run(SequenceToolsTest.class);
|
---|
216 | }
|
---|
217 | } |
---|