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

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
  • Property svn:mime-type set to text/plain
File size: 5.3 KB
Line 
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
15package de.ugoe.cs.autoquest.usageprofiles;
16
17import java.util.ArrayList;
18import java.util.Collection;
19import java.util.List;
20
21import de.ugoe.cs.autoquest.eventcore.Event;
22import de.ugoe.cs.autoquest.eventcore.StringEventType;
23import de.ugoe.cs.autoquest.usageprofiles.DeterministicFiniteAutomaton;
24
25import java.util.Random;
26import org.junit.*;
27
28import static org.junit.Assert.*;
29
30/**
31 * The class <code>DeterministicFiniteAutomatonTest</code> contains tests for
32 * the class <code>{@link DeterministicFiniteAutomaton}</code>.
33 *
34 * @author Steffen Herbold
35 * @version 1.0
36 */
37public class DeterministicFiniteAutomatonTest {
38
39        Collection<List<Event>> sequences;
40
41        @Test
42        public void testDeterministicFiniteAutomaton_1() throws Exception {
43                Random r = new Random();
44
45                DeterministicFiniteAutomaton result = new DeterministicFiniteAutomaton(
46                                r);
47
48                assertNotNull(result);
49                assertEquals(2, result.trieOrder);
50        }
51
52        @Test(expected = java.lang.IllegalArgumentException.class)
53        public void testDeterministicFiniteAutomaton_2() throws Exception {
54                new DeterministicFiniteAutomaton(null);
55        }
56
57        @Test
58        public void testGetProbability_1() throws Exception {
59                DeterministicFiniteAutomaton fixture = new DeterministicFiniteAutomaton(
60                                new Random());
61                fixture.train(sequences);
62
63                List<Event> context = new ArrayList<Event>();
64                context.add(new Event(new StringEventType("a")));
65                context.add(new Event(new StringEventType("b")));
66
67                Event symbol = new Event(new StringEventType("r"));
68
69                double result = fixture.getProbability(context, symbol);
70
71                assertEquals(1.0d, result, 0.0001);
72        }
73
74        @Test
75        public void testGetProbability_2() throws Exception {
76                DeterministicFiniteAutomaton fixture = new DeterministicFiniteAutomaton(
77                                new Random());
78                fixture.train(sequences);
79
80                List<Event> context = new ArrayList<Event>();
81                context.add(new Event(new StringEventType("a")));
82
83                Event symbol = new Event(new StringEventType("b"));
84
85                double result = fixture.getProbability(context, symbol);
86
87                assertEquals(1.0d / 4.0, result, 0.0001);
88        }
89
90        @Test
91        public void testGetProbability_3() throws Exception {
92                DeterministicFiniteAutomaton fixture = new DeterministicFiniteAutomaton(
93                                new Random());
94                fixture.train(sequences);
95
96                List<Event> context = new ArrayList<Event>();
97                context.add(new Event(new StringEventType("a")));
98
99                Event symbol = new Event(new StringEventType("c"));
100
101                double result = fixture.getProbability(context, symbol);
102
103                assertEquals(1.0d / 4.0, result, 0.0001);
104        }
105
106        @Test
107        public void testGetProbability_4() throws Exception {
108                DeterministicFiniteAutomaton fixture = new DeterministicFiniteAutomaton(
109                                new Random());
110                fixture.train(sequences);
111
112                List<Event> context = new ArrayList<Event>();
113                context.add(new Event(new StringEventType("a")));
114
115                Event symbol = new Event(new StringEventType("e"));
116
117                double result = fixture.getProbability(context, symbol);
118
119                assertEquals(0.0d, result, 0.0001);
120        }
121
122        @Test(expected = java.lang.IllegalArgumentException.class)
123        public void testGetProbability_5() throws Exception {
124                DeterministicFiniteAutomaton fixture = new DeterministicFiniteAutomaton(
125                                new Random());
126                fixture.train(sequences);
127
128                List<Event> context = new ArrayList<Event>();
129                context.add(new Event(new StringEventType("a")));
130
131                Event symbol = null;
132
133                fixture.getProbability(context, symbol);
134        }
135
136        @Test(expected = java.lang.IllegalArgumentException.class)
137        public void testGetProbability_6() throws Exception {
138                DeterministicFiniteAutomaton fixture = new DeterministicFiniteAutomaton(
139                                new Random());
140                fixture.train(sequences);
141
142                List<Event> context = null;
143
144                Event symbol = new Event(new StringEventType("a"));
145
146                fixture.getProbability(context, symbol);
147        }
148
149        @Before
150        public void setUp() throws Exception {
151                List<Event> sequence = new ArrayList<Event>();
152                sequence.add(new Event(new StringEventType("a")));
153                sequence.add(new Event(new StringEventType("b")));
154                sequence.add(new Event(new StringEventType("r")));
155                sequence.add(new Event(new StringEventType("a")));
156                sequence.add(new Event(new StringEventType("c")));
157                sequence.add(new Event(new StringEventType("a")));
158                sequence.add(new Event(new StringEventType("d")));
159                sequence.add(new Event(new StringEventType("a")));
160                sequence.add(new Event(new StringEventType("b")));
161                sequence.add(new Event(new StringEventType("r")));
162                sequence.add(new Event(new StringEventType("a")));
163
164                sequences = new ArrayList<List<Event>>();
165                sequences.add(sequence);
166        }
167
168        public static void main(String[] args) {
169                new org.junit.runner.JUnitCore()
170                                .run(DeterministicFiniteAutomatonTest.class);
171        }
172}
Note: See TracBrowser for help on using the repository browser.