source: trunk/quest-core-coverage-test/src/test/java/de/ugoe/cs/quest/coverage/SequenceToolsTest.java @ 553

Last change on this file since 553 was 553, checked in by sherbold, 12 years ago
  • countless adaptations throughout nearly all components to remove errors introduced due to the refactoring of the event core
  • added StringEventType? as a simple-to-use event type instead of DummyEventType?
  • Property svn:mime-type set to text/plain
File size: 6.1 KB
Line 
1package de.ugoe.cs.quest.coverage;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.LinkedHashSet;
6import java.util.LinkedList;
7import java.util.List;
8import java.util.Map;
9import java.util.Map.Entry;
10import java.util.Random;
11import java.util.Set;
12import de.ugoe.cs.quest.coverage.SequenceTools;
13import de.ugoe.cs.quest.eventcore.Event;
14import de.ugoe.cs.quest.eventcore.StringEventType;
15import de.ugoe.cs.quest.usageprofiles.FirstOrderMarkovModel;
16import de.ugoe.cs.quest.usageprofiles.IStochasticProcess;
17import de.ugoe.cs.quest.usageprofiles.MockTrieBasedModel;
18
19import org.junit.*;
20
21import 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 */
29public class SequenceToolsTest {
30       
31        Collection<List<Event>> sequences;
32        Set<List<Event>> subSequences;
33        MockTrieBasedModel mockProcess;
34       
35        @Test
36        public void testContainedSubSequences_1()
37                throws Exception {
38                int length = 2;
39
40                Set<List<Event>> result = SequenceTools.containedSubSequences(sequences, length);
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
52                Set<List<Event>> result = SequenceTools.containedSubSequences(null, length);
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 {
76                Map<List<Event>, Double> result = SequenceTools.generateWeights(mockProcess, subSequences);
77
78                assertNotNull(result);
79                Set<Entry<List<Event>, Double>> entrySet = result.entrySet();
80                assertEquals(subSequences.size(),entrySet.size());
81                for( Entry<List<Event>, Double> entry : entrySet ) {
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 {
90                Map<List<Event>, Double> result = SequenceTools.generateWeights(null, subSequences);
91               
92                Set<Entry<List<Event>, Double>> entrySet = result.entrySet();
93                assertEquals(subSequences.size(),entrySet.size());
94                for( Entry<List<Event>, Double> entry : entrySet ) {
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 {
103                Map<List<Event>, Double> result = SequenceTools.generateWeights(mockProcess, null);
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 {
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")));
165                sequences.add(sequence1);
166                sequences.add(sequence2);
167               
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")));
172                subSequences.add(tmpList);
173                tmpList = new ArrayList<Event>();
174                tmpList.add(new Event(new StringEventType("b")));
175                tmpList.add(new Event(new StringEventType("r")));
176                subSequences.add(tmpList);
177                tmpList = new ArrayList<Event>();
178                tmpList.add(new Event(new StringEventType("r")));
179                tmpList.add(new Event(new StringEventType("a")));
180                subSequences.add(tmpList);
181                tmpList = new ArrayList<Event>();
182                tmpList.add(new Event(new StringEventType("c")));
183                tmpList.add(new Event(new StringEventType("a")));
184                subSequences.add(tmpList);
185                tmpList = new ArrayList<Event>();
186                tmpList.add(new Event(new StringEventType("a")));
187                tmpList.add(new Event(new StringEventType("d")));
188                subSequences.add(tmpList);
189                tmpList = new ArrayList<Event>();
190                tmpList.add(new Event(new StringEventType("d")));
191                tmpList.add(new Event(new StringEventType("a")));
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}
Note: See TracBrowser for help on using the repository browser.