source: trunk/autoquest-core-usageprofiles-test/src/test/java/de/ugoe/cs/autoquest/usageprofiles/PredictionByPartialMatchTest.java @ 922

Last change on this file since 922 was 922, checked in by sherbold, 12 years ago
  • renaming of packages from de.ugoe.cs.quest to de.ugoe.cs.autoquest
  • Property svn:mime-type set to text/plain
File size: 10.6 KB
Line 
1package de.ugoe.cs.autoquest.usageprofiles;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.List;
6
7import de.ugoe.cs.autoquest.eventcore.Event;
8import de.ugoe.cs.autoquest.eventcore.StringEventType;
9import de.ugoe.cs.autoquest.usageprofiles.PredictionByPartialMatch;
10
11import java.util.Random;
12import org.junit.*;
13
14import static org.junit.Assert.*;
15
16/**
17 * The class <code>PredictionByPartialMatchTest</code> contains tests for the
18 * class <code>{@link PredictionByPartialMatch}</code>.
19 *
20 * @author Steffen Herbold
21 * @version 1.0
22 */
23public class PredictionByPartialMatchTest {
24
25        Collection<List<Event>> sequences;
26
27        @Test
28        public void testPredictionByPartialMatch_1() throws Exception {
29                int markovOrder = 2;
30                Random r = new Random();
31
32                PredictionByPartialMatch result = new PredictionByPartialMatch(
33                                markovOrder, r);
34
35                assertNotNull(result);
36                assertEquals(markovOrder+1, result.trieOrder);
37                assertEquals(0, result.minOrder);
38                assertEquals(r, result.r);
39                assertEquals(0.1, result.probEscape, 0.0001);
40        }
41       
42        @Test(expected = java.lang.IllegalArgumentException.class)
43        public void testPredictionByPartialMatch_2() throws Exception {
44                int markovOrder = -1;
45                Random r = new Random();
46
47                new PredictionByPartialMatch(markovOrder, r);
48        }
49       
50        @Test(expected = java.lang.IllegalArgumentException.class)
51        public void testPredictionByPartialMatch_3() throws Exception {
52                int markovOrder = 2;
53                Random r = null;
54
55                new PredictionByPartialMatch(markovOrder, r);
56        }
57       
58        @Test
59        public void testPredictionByPartialMatch_4() throws Exception {
60                int markovOrder = 2;
61                Random r = new Random();
62                double probEscape = 0.2;
63
64                PredictionByPartialMatch result = new PredictionByPartialMatch(
65                                markovOrder, r, probEscape);
66
67                assertNotNull(result);
68                assertEquals(markovOrder+1, result.trieOrder);
69                assertEquals(0, result.minOrder);
70                assertEquals(r, result.r);
71                assertEquals(probEscape, result.probEscape, 0.0001);
72        }
73       
74        @Test(expected = java.lang.IllegalArgumentException.class)
75        public void testPredictionByPartialMatch_5() throws Exception {
76                int markovOrder = -1;
77                Random r = new Random();
78                double probEscape = 0.2;
79
80                new PredictionByPartialMatch(markovOrder, r, probEscape);
81        }
82       
83        @Test(expected = java.lang.IllegalArgumentException.class)
84        public void testPredictionByPartialMatch_6() throws Exception {
85                int markovOrder = 2;
86                Random r = null;
87                double probEscape = 0.2;
88
89                new PredictionByPartialMatch(markovOrder, r, probEscape);
90        }
91       
92        @Test(expected = java.lang.IllegalArgumentException.class)
93        public void testPredictionByPartialMatch_7() throws Exception {
94                int markovOrder = 2;
95                Random r = new Random();
96                double probEscape = 0.0;
97
98                new PredictionByPartialMatch(markovOrder, r, probEscape);
99        }
100       
101        @Test(expected = java.lang.IllegalArgumentException.class)
102        public void testPredictionByPartialMatch_8() throws Exception {
103                int markovOrder = 2;
104                Random r = new Random();
105                double probEscape = 1.0;
106
107                new PredictionByPartialMatch(markovOrder, r, probEscape);
108        }
109       
110        @Test
111        public void testPredictionByPartialMatch_9() throws Exception {
112                int markovOrder = 2;
113                Random r = new Random();
114                double probEscape = 0.2;
115                int minOrder = 1;
116
117                PredictionByPartialMatch result = new PredictionByPartialMatch(
118                                markovOrder, minOrder, r, probEscape);
119
120                assertNotNull(result);
121                assertEquals(markovOrder+1, result.trieOrder);
122                assertEquals(minOrder, result.minOrder);
123                assertEquals(r, result.r);
124                assertEquals(probEscape, result.probEscape, 0.0001);
125        }
126       
127        @Test(expected = java.lang.IllegalArgumentException.class)
128        public void testPredictionByPartialMatch_10() throws Exception {
129                int markovOrder = -1;
130                Random r = new Random();
131                double probEscape = 0.2;
132                int minOrder = 1;
133
134                new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
135        }
136       
137        @Test(expected = java.lang.IllegalArgumentException.class)
138        public void testPredictionByPartialMatch_11() throws Exception {
139                int markovOrder = 2;
140                Random r = null;
141                double probEscape = 0.2;
142                int minOrder = 1;
143
144                new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
145        }
146       
147        @Test(expected = java.lang.IllegalArgumentException.class)
148        public void testPredictionByPartialMatch_12() throws Exception {
149                int markovOrder = 2;
150                Random r = new Random();
151                double probEscape = 0.0;
152                int minOrder = 1;
153
154                new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
155        }
156       
157        @Test(expected = java.lang.IllegalArgumentException.class)
158        public void testPredictionByPartialMatch_13() throws Exception {
159                int markovOrder = 2;
160                Random r = new Random();
161                double probEscape = 1.0;
162                int minOrder = 1;
163
164                new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
165        }
166       
167        @Test(expected = java.lang.IllegalArgumentException.class)
168        public void testPredictionByPartialMatch_14() throws Exception {
169                int markovOrder = 2;
170                Random r = new Random();
171                double probEscape = 0.2;
172                int minOrder = 3;
173
174                new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
175        }
176       
177        @Test(expected = java.lang.IllegalArgumentException.class)
178        public void testPredictionByPartialMatch_15() throws Exception {
179                int markovOrder = 2;
180                Random r = new Random();
181                double probEscape = 0.2;
182                int minOrder = -1;
183
184                new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
185        }
186
187        @Test
188        public void testGetProbEscape_1() throws Exception {
189                int markovOrder = 2;
190                Random r = new Random();
191                double probEscape = 0.2;
192                int minOrder = 1;
193
194                PredictionByPartialMatch fixture = new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
195                fixture.probEscape = probEscape;
196               
197                double result = fixture.getProbEscape();
198
199                assertEquals(probEscape, result, 0.0001);
200        }
201
202        @Test
203        public void testGetProbability_1() throws Exception {
204                int markovOrder = 2;
205                Random r = new Random();
206                double probEscape = 0.2;
207                int minOrder = 1;
208
209                PredictionByPartialMatch fixture = new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
210                fixture.train(sequences);
211               
212                List<Event> context = new ArrayList<Event>();
213                context.add(Event.STARTEVENT);
214                context.add(new Event(new StringEventType("a")));
215
216                Event symbol = new Event(new StringEventType("b"));
217               
218                double result = fixture.getProbability(context, symbol);
219               
220                assertEquals(0.88d, result, 0.0001);
221        }
222       
223        @Test
224        public void testGetProbability_2() throws Exception {
225                int markovOrder = 2;
226                Random r = new Random();
227                double probEscape = 0.2;
228                int minOrder = 1;
229
230                PredictionByPartialMatch fixture = new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
231                fixture.train(sequences);
232               
233                List<Event> context = new ArrayList<Event>();
234                context.add(Event.STARTEVENT);
235                context.add(new Event(new StringEventType("a")));
236
237                Event symbol = new Event(new StringEventType("c"));
238               
239                double result = fixture.getProbability(context, symbol);
240               
241                assertEquals(0.04d, result, 0.0001);
242        }
243       
244        @Test
245        public void testGetProbability_3() throws Exception {
246                int markovOrder = 2;
247                Random r = new Random();
248                double probEscape = 0.2;
249                int minOrder = 2;
250
251                PredictionByPartialMatch fixture = new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
252                fixture.train(sequences);
253               
254                List<Event> context = new ArrayList<Event>();
255                context.add(Event.STARTEVENT);
256                context.add(new Event(new StringEventType("a")));
257
258                Event symbol = new Event(new StringEventType("b"));
259               
260                double result = fixture.getProbability(context, symbol);
261               
262                assertEquals(1.0d, result, 0.0001);
263        }
264       
265        @Test
266        public void testGetProbability_4() throws Exception {
267                int markovOrder = 2;
268                Random r = new Random();
269                double probEscape = 0.2;
270                int minOrder = 2;
271
272                PredictionByPartialMatch fixture = new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
273                fixture.train(sequences);
274               
275                List<Event> context = new ArrayList<Event>();
276                context.add(Event.STARTEVENT);
277                context.add(new Event(new StringEventType("a")));
278
279                Event symbol = new Event(new StringEventType("c"));
280               
281                double result = fixture.getProbability(context, symbol);
282               
283                assertEquals(0.0d, result, 0.0001);
284        }
285       
286        @Test
287        public void testGetProbability_5() throws Exception {
288                int markovOrder = 2;
289                Random r = new Random();
290                double probEscape = 0.2;
291                int minOrder = 0;
292
293                PredictionByPartialMatch fixture = new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
294                fixture.train(sequences);
295               
296                List<Event> context = new ArrayList<Event>();
297                context.add(Event.STARTEVENT);
298                context.add(new Event(new StringEventType("a")));
299
300                Event symbol = new Event(new StringEventType("b"));
301               
302                double result = fixture.getProbability(context, symbol);
303               
304                assertEquals(0.8701d, result, 0.0001);
305        }
306       
307        @Test
308        public void testGetProbability_6() throws Exception {
309                int markovOrder = 2;
310                Random r = new Random();
311                double probEscape = 0.2;
312                int minOrder = 0;
313
314                PredictionByPartialMatch fixture = new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
315                fixture.train(sequences);
316               
317                List<Event> context = new ArrayList<Event>();
318                context.add(Event.STARTEVENT);
319                context.add(new Event(new StringEventType("a")));
320
321                Event symbol = new Event(new StringEventType("c"));
322               
323                double result = fixture.getProbability(context, symbol);
324               
325                assertEquals(0.0350, result, 0.0001);
326        }
327
328        @Test
329        public void testSetProbEscape_1() throws Exception {
330                int markovOrder = 2;
331                Random r = new Random();
332                double probEscape = 0.2;
333                int minOrder = 1;
334                double newProbEscape = 0.3;
335
336                PredictionByPartialMatch fixture = new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
337                               
338                fixture.setProbEscape(newProbEscape);
339
340                assertEquals(newProbEscape, fixture.probEscape, 0.0001);
341        }
342
343        @Before
344        public void setUp() throws Exception {
345                List<Event> sequence = new ArrayList<Event>();
346                sequence.add(new Event(new StringEventType("a")));
347                sequence.add(new Event(new StringEventType("b")));
348                sequence.add(new Event(new StringEventType("r")));
349                sequence.add(new Event(new StringEventType("a")));
350                sequence.add(new Event(new StringEventType("c")));
351                sequence.add(new Event(new StringEventType("a")));
352                sequence.add(new Event(new StringEventType("d")));
353                sequence.add(new Event(new StringEventType("a")));
354                sequence.add(new Event(new StringEventType("b")));
355                sequence.add(new Event(new StringEventType("r")));
356                sequence.add(new Event(new StringEventType("a")));
357
358                sequences = new ArrayList<List<Event>>();
359                sequences.add(sequence);
360        }
361
362        public static void main(String[] args) {
363                new org.junit.runner.JUnitCore()
364                                .run(PredictionByPartialMatchTest.class);
365        }
366}
Note: See TracBrowser for help on using the repository browser.