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.usageprofiles;
|
---|
16 |
|
---|
17 | import java.util.ArrayList;
|
---|
18 | import java.util.Collection;
|
---|
19 | import java.util.HashSet;
|
---|
20 | import java.util.List;
|
---|
21 |
|
---|
22 | import junitx.framework.ListAssert;
|
---|
23 |
|
---|
24 | import org.junit.*;
|
---|
25 |
|
---|
26 | import de.ugoe.cs.autoquest.usageprofiles.Trie;
|
---|
27 | import de.ugoe.cs.autoquest.usageprofiles.TrieNode;
|
---|
28 | import de.ugoe.cs.autoquest.usageprofiles.Trie.Edge;
|
---|
29 | import de.ugoe.cs.autoquest.usageprofiles.Trie.TrieVertex;
|
---|
30 | import static org.junit.Assert.*;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * The class <code>TrieTest</code> contains tests for the class
|
---|
34 | * <code>{@link Trie}</code>.
|
---|
35 | *
|
---|
36 | * @author Steffen Herbold
|
---|
37 | * @version 1.0
|
---|
38 | */
|
---|
39 | public class TrieTest {
|
---|
40 |
|
---|
41 | List<String> sequence;
|
---|
42 | Collection<String> symbols;
|
---|
43 |
|
---|
44 | private static void assertCollectionContent(Collection<?> c1,
|
---|
45 | Collection<?> c2) {
|
---|
46 | assertEquals(c1.size(), c2.size());
|
---|
47 | for (Object obj : c1) {
|
---|
48 | assertTrue(c2.contains(obj));
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Test
|
---|
53 | public void testTrie_1() throws Exception {
|
---|
54 |
|
---|
55 | Trie<String> result = new Trie<String>();
|
---|
56 |
|
---|
57 | assertNotNull(result);
|
---|
58 | assertEquals(0, result.getNumLeafs());
|
---|
59 | assertEquals(0, result.getNumSymbols());
|
---|
60 | assertEquals(0, result.getNumLeafAncestors());
|
---|
61 | assertTrue(result.getKnownSymbols().isEmpty());
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Test
|
---|
65 | public void testTrie_2() throws Exception {
|
---|
66 | Trie<String> trie1 = new Trie<String>();
|
---|
67 | trie1.train(sequence, 3);
|
---|
68 |
|
---|
69 | Trie<String> result = new Trie<String>(trie1);
|
---|
70 |
|
---|
71 | assertEquals(trie1, result);
|
---|
72 | assertNotSame(trie1, result);
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
76 | public void testTrie_3() throws Exception {
|
---|
77 | new Trie<String>((SymbolComparator<String>) null);
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
81 | public void testTrie_4() throws Exception {
|
---|
82 | new Trie<String>((Trie<String>) null);
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Test
|
---|
86 | public void testAdd_1() throws Exception {
|
---|
87 | Trie<String> fixture = new Trie<String>();
|
---|
88 | List<String> seq = new ArrayList<String>();
|
---|
89 | seq.add("a");
|
---|
90 | seq.add("b");
|
---|
91 |
|
---|
92 | fixture.add(seq);
|
---|
93 |
|
---|
94 | assertEquals(1, fixture.getChild("a").getCount());
|
---|
95 | assertEquals(1, fixture.getChild("a").getChild("b").getCount());
|
---|
96 | assertNull(fixture.getChild("b"));
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Test
|
---|
100 | public void testAdd_2() throws Exception {
|
---|
101 | Trie<String> fixture = new Trie<String>();
|
---|
102 |
|
---|
103 | fixture.add(new ArrayList<String>());
|
---|
104 |
|
---|
105 | assertEquals(0, fixture.getNumSymbols());
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Test
|
---|
109 | public void testAdd_3() throws Exception {
|
---|
110 | Trie<String> fixture = new Trie<String>();
|
---|
111 |
|
---|
112 | fixture.add(null);
|
---|
113 |
|
---|
114 | assertEquals(0, fixture.getNumSymbols());
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Test
|
---|
118 | public void testFind_1() throws Exception {
|
---|
119 | Trie<String> fixture = new Trie<String>();
|
---|
120 | fixture.train(sequence, 3);
|
---|
121 | List<String> findSequence = new ArrayList<String>();
|
---|
122 | findSequence.add("a");
|
---|
123 | findSequence.add("b");
|
---|
124 | findSequence.add("r");
|
---|
125 | TrieNode<String> expected = fixture.getChild("a").getChild("b")
|
---|
126 | .getChild("r");
|
---|
127 |
|
---|
128 | TrieNode<String> result = fixture.find(findSequence);
|
---|
129 |
|
---|
130 | assertEquals(expected, result);
|
---|
131 | }
|
---|
132 |
|
---|
133 | @Test
|
---|
134 | public void testFind_2() throws Exception {
|
---|
135 | Trie<String> fixture = new Trie<String>();
|
---|
136 | fixture.train(sequence, 3);
|
---|
137 | List<String> findSequence = new ArrayList<String>();
|
---|
138 | findSequence.add("c");
|
---|
139 | findSequence.add("a");
|
---|
140 | TrieNode<String> expected = fixture.getChild("c").getChild("a");
|
---|
141 |
|
---|
142 | TrieNode<String> result = fixture.find(findSequence);
|
---|
143 |
|
---|
144 | assertEquals(expected, result);
|
---|
145 | }
|
---|
146 |
|
---|
147 | @Test
|
---|
148 | public void testFind_3() throws Exception {
|
---|
149 | Trie<String> fixture = new Trie<String>();
|
---|
150 | fixture.train(sequence, 3);
|
---|
151 | List<String> findSequence = new ArrayList<String>();
|
---|
152 |
|
---|
153 | TrieNode<String> result = fixture.find(findSequence);
|
---|
154 |
|
---|
155 | assertTrue(result.isRoot());
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Test
|
---|
159 | public void testFind_4() throws Exception {
|
---|
160 | Trie<String> fixture = new Trie<String>();
|
---|
161 | fixture.train(sequence, 3);
|
---|
162 |
|
---|
163 | TrieNode<String> result = fixture.find(null);
|
---|
164 |
|
---|
165 | assertTrue(result.isRoot());
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Test
|
---|
169 | public void testGetChildCreate_1() throws Exception {
|
---|
170 | Trie<String> fixture = new Trie<String>();
|
---|
171 | String symbol = "a";
|
---|
172 |
|
---|
173 | TrieNode<String> result = fixture.getChildCreate(symbol);
|
---|
174 |
|
---|
175 | assertEquals(symbol, result.getSymbol());
|
---|
176 | assertEquals(0, result.getCount());
|
---|
177 | assertTrue(result.isLeaf());
|
---|
178 | }
|
---|
179 |
|
---|
180 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
181 | public void testGetChildCreate_2() throws Exception {
|
---|
182 | Trie<String> fixture = new Trie<String>();
|
---|
183 | fixture.getChildCreate(null);
|
---|
184 | }
|
---|
185 |
|
---|
186 | @Test
|
---|
187 | public void testGetContextSuffix_1() throws Exception {
|
---|
188 | Trie<String> fixture = new Trie<String>();
|
---|
189 | fixture.train(sequence, 3);
|
---|
190 | List<String> context = new ArrayList<String>();
|
---|
191 | context.add("a");
|
---|
192 | context.add("a");
|
---|
193 | context.add("b");
|
---|
194 | List<String> expected = new ArrayList<String>();
|
---|
195 | expected.add("a");
|
---|
196 | expected.add("b");
|
---|
197 |
|
---|
198 | List<String> result = fixture.getContextSuffix(context);
|
---|
199 |
|
---|
200 | ListAssert.assertEquals(expected, result);
|
---|
201 | }
|
---|
202 |
|
---|
203 | @Test
|
---|
204 | public void testGetContextSuffix_2() throws Exception {
|
---|
205 | Trie<String> fixture = new Trie<String>();
|
---|
206 | fixture.train(sequence, 3);
|
---|
207 | List<String> context = new ArrayList<String>();
|
---|
208 | context.add("a");
|
---|
209 | context.add("a");
|
---|
210 | context.add("b");
|
---|
211 | context.add("r");
|
---|
212 | List<String> expected = new ArrayList<String>();
|
---|
213 | expected.add("b");
|
---|
214 | expected.add("r");
|
---|
215 |
|
---|
216 | List<String> result = fixture.getContextSuffix(context);
|
---|
217 |
|
---|
218 | ListAssert.assertEquals(expected, result);
|
---|
219 | }
|
---|
220 |
|
---|
221 | @Test
|
---|
222 | public void testGetContextSuffix_3() throws Exception {
|
---|
223 | Trie<String> fixture = new Trie<String>();
|
---|
224 | fixture.train(sequence, 3);
|
---|
225 | List<String> context = new ArrayList<String>();
|
---|
226 | context.add("a");
|
---|
227 | context.add("a");
|
---|
228 | context.add("b");
|
---|
229 | context.add("x");
|
---|
230 | List<String> expected = new ArrayList<String>();
|
---|
231 |
|
---|
232 | List<String> result = fixture.getContextSuffix(context);
|
---|
233 |
|
---|
234 | ListAssert.assertEquals(expected, result);
|
---|
235 | }
|
---|
236 |
|
---|
237 | @Test
|
---|
238 | public void testGetContextSuffix_4() throws Exception {
|
---|
239 | Trie<String> fixture = new Trie<String>();
|
---|
240 |
|
---|
241 | List<String> result = fixture.getContextSuffix(null);
|
---|
242 |
|
---|
243 | // add additional test code here
|
---|
244 | assertNotNull(result);
|
---|
245 | assertEquals(0, result.size());
|
---|
246 | }
|
---|
247 |
|
---|
248 | @Test
|
---|
249 | public void testGetSequencesWithMostOccurrences_1() throws Exception {
|
---|
250 | Trie<String> fixture = new Trie<String>();
|
---|
251 | fixture.train(sequence, 3);
|
---|
252 |
|
---|
253 | List<String> expected = new ArrayList<String>();
|
---|
254 | expected.add("a");
|
---|
255 |
|
---|
256 | Collection<List<String>> result = fixture.getSequencesWithMostOccurrences(1);
|
---|
257 |
|
---|
258 | assertEquals(1, result.size());
|
---|
259 | ListAssert.assertEquals(expected, result.iterator().next());
|
---|
260 | }
|
---|
261 |
|
---|
262 | @Test
|
---|
263 | public void testGetSequencesWithMostOccurrences_2() throws Exception {
|
---|
264 | Trie<String> fixture = new Trie<String>();
|
---|
265 | fixture.train(sequence, 3);
|
---|
266 |
|
---|
267 | Collection<List<String>> result = fixture.getSequencesWithMostOccurrences(2);
|
---|
268 |
|
---|
269 | assertEquals(5, result.size());
|
---|
270 |
|
---|
271 | List<String> expected = new ArrayList<String>();
|
---|
272 | expected.add("a");
|
---|
273 | expected.add("b");
|
---|
274 | ListAssert.assertContains((List<List<String>>) result, expected);
|
---|
275 |
|
---|
276 | expected.add("r");
|
---|
277 | ListAssert.assertContains((List<List<String>>) result, expected);
|
---|
278 |
|
---|
279 | expected.clear();
|
---|
280 | expected.add("b");
|
---|
281 | expected.add("r");
|
---|
282 | ListAssert.assertContains((List<List<String>>) result, expected);
|
---|
283 |
|
---|
284 | expected.add("a");
|
---|
285 | ListAssert.assertContains((List<List<String>>) result, expected);
|
---|
286 |
|
---|
287 | expected.clear();
|
---|
288 | expected.add("r");
|
---|
289 | expected.add("a");
|
---|
290 | ListAssert.assertContains((List<List<String>>) result, expected);
|
---|
291 | }
|
---|
292 |
|
---|
293 | @Test
|
---|
294 | public void testGetSequencesWithMostOccurrences_3() throws Exception {
|
---|
295 | Trie<String> fixture = new Trie<String>();
|
---|
296 | fixture.train(sequence, 3);
|
---|
297 |
|
---|
298 | Collection<List<String>> result = fixture.getSequencesWithMostOccurrences(3);
|
---|
299 |
|
---|
300 | assertEquals(2, result.size());
|
---|
301 |
|
---|
302 | List<String> expected = new ArrayList<String>();
|
---|
303 | expected.add("a");
|
---|
304 | expected.add("b");
|
---|
305 | expected.add("r");
|
---|
306 | ListAssert.assertContains((List<List<String>>) result, expected);
|
---|
307 |
|
---|
308 | expected.clear();
|
---|
309 | expected.add("b");
|
---|
310 | expected.add("r");
|
---|
311 | expected.add("a");
|
---|
312 | ListAssert.assertContains((List<List<String>>) result, expected);
|
---|
313 | }
|
---|
314 |
|
---|
315 | @Test
|
---|
316 | public void testGetSequencesWithMostOccurrences_4() throws Exception {
|
---|
317 | Trie<String> fixture = new Trie<String>();
|
---|
318 | fixture.train(sequence, 3);
|
---|
319 |
|
---|
320 | Collection<List<String>> result = fixture.getSequencesWithMostOccurrences(4);
|
---|
321 |
|
---|
322 | assertEquals(0, result.size());
|
---|
323 | }
|
---|
324 |
|
---|
325 | @Test
|
---|
326 | public void testGetContextSuffix_5() throws Exception {
|
---|
327 | Trie<String> fixture = new Trie<String>();
|
---|
328 | fixture.train(sequence, 3);
|
---|
329 | List<String> context = new ArrayList<String>();
|
---|
330 | context.add("a");
|
---|
331 | context.add("a");
|
---|
332 | context.add("b");
|
---|
333 | List<String> expected = new ArrayList<String>();
|
---|
334 | expected.add("a");
|
---|
335 | expected.add("b");
|
---|
336 |
|
---|
337 | List<String> result = fixture.getContextSuffix(context);
|
---|
338 |
|
---|
339 | ListAssert.assertEquals(expected, result);
|
---|
340 | }
|
---|
341 |
|
---|
342 | @Test
|
---|
343 | public void testGetCount_1() throws Exception {
|
---|
344 | Trie<String> fixture = new Trie<String>();
|
---|
345 | fixture.train(sequence, 3);
|
---|
346 | List<String> subSequence = new ArrayList<String>();
|
---|
347 | subSequence.add("a");
|
---|
348 |
|
---|
349 | int result = fixture.getCount(subSequence);
|
---|
350 |
|
---|
351 | assertEquals(5, result);
|
---|
352 | }
|
---|
353 |
|
---|
354 | @Test
|
---|
355 | public void testGetCount_2() throws Exception {
|
---|
356 | Trie<String> fixture = new Trie<String>();
|
---|
357 | fixture.train(sequence, 3);
|
---|
358 | List<String> subSequence = new ArrayList<String>();
|
---|
359 | subSequence.add("a");
|
---|
360 | subSequence.add("b");
|
---|
361 |
|
---|
362 | int result = fixture.getCount(subSequence);
|
---|
363 |
|
---|
364 | assertEquals(2, result);
|
---|
365 | }
|
---|
366 |
|
---|
367 | @Test
|
---|
368 | public void testGetCount_3() throws Exception {
|
---|
369 | Trie<String> fixture = new Trie<String>();
|
---|
370 | fixture.train(sequence, 3);
|
---|
371 | List<String> subSequence = new ArrayList<String>();
|
---|
372 | subSequence.add("x");
|
---|
373 |
|
---|
374 | int result = fixture.getCount(subSequence);
|
---|
375 |
|
---|
376 | assertEquals(0, result);
|
---|
377 | }
|
---|
378 |
|
---|
379 | @Test
|
---|
380 | public void testGetCount_4() throws Exception {
|
---|
381 | Trie<String> fixture = new Trie<String>();
|
---|
382 | fixture.train(sequence, 3);
|
---|
383 | List<String> subSequence = new ArrayList<String>();
|
---|
384 |
|
---|
385 | int result = fixture.getCount(subSequence, "a");
|
---|
386 |
|
---|
387 | assertEquals(5, result);
|
---|
388 | }
|
---|
389 |
|
---|
390 | @Test
|
---|
391 | public void testGetCount_5() throws Exception {
|
---|
392 | Trie<String> fixture = new Trie<String>();
|
---|
393 | fixture.train(sequence, 3);
|
---|
394 | List<String> subSequence = new ArrayList<String>();
|
---|
395 | subSequence.add("a");
|
---|
396 | subSequence.add("b");
|
---|
397 |
|
---|
398 | int result = fixture.getCount(subSequence, "r");
|
---|
399 |
|
---|
400 | assertEquals(2, result);
|
---|
401 | }
|
---|
402 |
|
---|
403 | @Test
|
---|
404 | public void testGetCount_6() throws Exception {
|
---|
405 | Trie<String> fixture = new Trie<String>();
|
---|
406 | fixture.train(sequence, 3);
|
---|
407 | List<String> subSequence = new ArrayList<String>();
|
---|
408 |
|
---|
409 | int result = fixture.getCount(subSequence, "x");
|
---|
410 |
|
---|
411 | assertEquals(0, result);
|
---|
412 | }
|
---|
413 |
|
---|
414 | @Test
|
---|
415 | public void testGetFollowingSymbols_1() throws Exception {
|
---|
416 | Trie<String> fixture = new Trie<String>();
|
---|
417 | fixture.train(sequence, 3);
|
---|
418 | List<String> subSequence = new ArrayList<String>();
|
---|
419 | subSequence.add("a");
|
---|
420 | Collection<String> expected = new ArrayList<String>();
|
---|
421 | expected.add("b");
|
---|
422 | expected.add("c");
|
---|
423 | expected.add("d");
|
---|
424 |
|
---|
425 | Collection<String> result = fixture.getFollowingSymbols(subSequence);
|
---|
426 |
|
---|
427 | assertCollectionContent(expected, result);
|
---|
428 | }
|
---|
429 |
|
---|
430 | @Test
|
---|
431 | public void testGetFollowingSymbols_2() throws Exception {
|
---|
432 | Trie<String> fixture = new Trie<String>();
|
---|
433 | fixture.train(sequence, 3);
|
---|
434 | List<String> subSequence = new ArrayList<String>();
|
---|
435 | subSequence.add("a");
|
---|
436 | subSequence.add("b");
|
---|
437 | subSequence.add("r");
|
---|
438 |
|
---|
439 | Collection<String> result = fixture.getFollowingSymbols(subSequence);
|
---|
440 |
|
---|
441 | assertEquals(0, result.size());
|
---|
442 | }
|
---|
443 |
|
---|
444 | @Test
|
---|
445 | public void testGetFollowingSymbols_3() throws Exception {
|
---|
446 | Trie<String> fixture = new Trie<String>();
|
---|
447 | fixture.train(sequence, 3);
|
---|
448 | List<String> subSequence = new ArrayList<String>();
|
---|
449 | subSequence.add("x");
|
---|
450 |
|
---|
451 | Collection<String> result = fixture.getFollowingSymbols(subSequence);
|
---|
452 |
|
---|
453 | assertEquals(0, result.size());
|
---|
454 | }
|
---|
455 |
|
---|
456 | @Test
|
---|
457 | public void testGetNumLeafAncestors_1() throws Exception {
|
---|
458 | Trie<String> fixture = new Trie<String>();
|
---|
459 | fixture.train(sequence, 3);
|
---|
460 |
|
---|
461 | int result = fixture.getNumLeafAncestors();
|
---|
462 |
|
---|
463 | assertEquals(7, result);
|
---|
464 | }
|
---|
465 |
|
---|
466 | @Test
|
---|
467 | public void testGetNumLeafs_1() throws Exception {
|
---|
468 | Trie<String> fixture = new Trie<String>();
|
---|
469 | fixture.train(sequence, 3);
|
---|
470 |
|
---|
471 | int result = fixture.getNumLeafs();
|
---|
472 |
|
---|
473 | assertEquals(7, result);
|
---|
474 | }
|
---|
475 |
|
---|
476 | @Test
|
---|
477 | public void testGetNumSymbols_1() throws Exception {
|
---|
478 | Trie<String> fixture = new Trie<String>();
|
---|
479 | fixture.train(sequence, 3);
|
---|
480 |
|
---|
481 | int result = fixture.getNumSymbols();
|
---|
482 |
|
---|
483 | assertEquals(5, result);
|
---|
484 | }
|
---|
485 |
|
---|
486 | @Test
|
---|
487 | public void testTrain_1() throws Exception {
|
---|
488 | Trie<String> fixture = new Trie<String>();
|
---|
489 | int maxOrder = 3;
|
---|
490 |
|
---|
491 | fixture.train(sequence, maxOrder);
|
---|
492 |
|
---|
493 | // check if symbols are correct
|
---|
494 | assertCollectionContent(symbols, fixture.getKnownSymbols());
|
---|
495 |
|
---|
496 | // check if counters are correct and only the correct nodes exist
|
---|
497 | TrieNode<String> root = fixture.find(new ArrayList<String>());
|
---|
498 | TrieNode<String> root_a = root.getChild("a");
|
---|
499 | TrieNode<String> root_a_a = root_a.getChild("a");
|
---|
500 | TrieNode<String> root_a_b = root_a.getChild("b");
|
---|
501 | TrieNode<String> root_a_b_a = root_a_b.getChild("a");
|
---|
502 | TrieNode<String> root_a_b_b = root_a_b.getChild("b");
|
---|
503 | TrieNode<String> root_a_b_c = root_a_b.getChild("c");
|
---|
504 | TrieNode<String> root_a_b_d = root_a_b.getChild("d");
|
---|
505 | TrieNode<String> root_a_b_r = root_a_b.getChild("r");
|
---|
506 | TrieNode<String> root_a_c = root_a.getChild("c");
|
---|
507 | TrieNode<String> root_a_c_a = root_a_c.getChild("a");
|
---|
508 | TrieNode<String> root_a_c_b = root_a_c.getChild("b");
|
---|
509 | TrieNode<String> root_a_c_c = root_a_c.getChild("c");
|
---|
510 | TrieNode<String> root_a_c_d = root_a_c.getChild("d");
|
---|
511 | TrieNode<String> root_a_c_r = root_a_c.getChild("r");
|
---|
512 | TrieNode<String> root_a_d = root_a.getChild("d");
|
---|
513 | TrieNode<String> root_a_d_a = root_a_d.getChild("a");
|
---|
514 | TrieNode<String> root_a_d_b = root_a_d.getChild("b");
|
---|
515 | TrieNode<String> root_a_d_c = root_a_d.getChild("c");
|
---|
516 | TrieNode<String> root_a_d_d = root_a_d.getChild("d");
|
---|
517 | TrieNode<String> root_a_d_r = root_a_d.getChild("r");
|
---|
518 | TrieNode<String> root_a_r = root_a.getChild("r");
|
---|
519 | TrieNode<String> root_b = root.getChild("b");
|
---|
520 | TrieNode<String> root_b_a = root_b.getChild("a");
|
---|
521 | TrieNode<String> root_b_b = root_b.getChild("b");
|
---|
522 | TrieNode<String> root_b_c = root_b.getChild("c");
|
---|
523 | TrieNode<String> root_b_d = root_b.getChild("d");
|
---|
524 | TrieNode<String> root_b_r = root_b.getChild("r");
|
---|
525 | TrieNode<String> root_b_r_a = root_b_r.getChild("a");
|
---|
526 | TrieNode<String> root_b_r_b = root_b_r.getChild("b");
|
---|
527 | TrieNode<String> root_b_r_c = root_b_r.getChild("c");
|
---|
528 | TrieNode<String> root_b_r_d = root_b_r.getChild("d");
|
---|
529 | TrieNode<String> root_b_r_r = root_b_r.getChild("r");
|
---|
530 | TrieNode<String> root_c = root.getChild("c");
|
---|
531 | TrieNode<String> root_c_a = root_c.getChild("a");
|
---|
532 | TrieNode<String> root_c_a_a = root_c_a.getChild("a");
|
---|
533 | TrieNode<String> root_c_a_b = root_c_a.getChild("b");
|
---|
534 | TrieNode<String> root_c_a_c = root_c_a.getChild("c");
|
---|
535 | TrieNode<String> root_c_a_d = root_c_a.getChild("d");
|
---|
536 | TrieNode<String> root_c_a_r = root_c_a.getChild("r");
|
---|
537 | TrieNode<String> root_c_b = root_c.getChild("b");
|
---|
538 | TrieNode<String> root_c_c = root_c.getChild("c");
|
---|
539 | TrieNode<String> root_c_d = root_c.getChild("d");
|
---|
540 | TrieNode<String> root_c_r = root_c.getChild("r");
|
---|
541 | TrieNode<String> root_d = root.getChild("d");
|
---|
542 | TrieNode<String> root_d_a = root_d.getChild("a");
|
---|
543 | TrieNode<String> root_d_a_a = root_d_a.getChild("a");
|
---|
544 | TrieNode<String> root_d_a_b = root_d_a.getChild("b");
|
---|
545 | TrieNode<String> root_d_a_c = root_d_a.getChild("c");
|
---|
546 | TrieNode<String> root_d_a_d = root_d_a.getChild("d");
|
---|
547 | TrieNode<String> root_d_a_r = root_d_a.getChild("r");
|
---|
548 | TrieNode<String> root_d_b = root_d.getChild("b");
|
---|
549 | TrieNode<String> root_d_c = root_d.getChild("c");
|
---|
550 | TrieNode<String> root_d_d = root_d.getChild("d");
|
---|
551 | TrieNode<String> root_d_r = root_d.getChild("r");
|
---|
552 | TrieNode<String> root_r = root.getChild("r");
|
---|
553 | TrieNode<String> root_r_a = root_r.getChild("a");
|
---|
554 | TrieNode<String> root_r_a_a = root_r_a.getChild("a");
|
---|
555 | TrieNode<String> root_r_a_b = root_r_a.getChild("b");
|
---|
556 | TrieNode<String> root_r_a_c = root_r_a.getChild("c");
|
---|
557 | TrieNode<String> root_r_a_d = root_r_a.getChild("d");
|
---|
558 | TrieNode<String> root_r_a_r = root_r_a.getChild("r");
|
---|
559 | TrieNode<String> root_r_b = root_r.getChild("b");
|
---|
560 | TrieNode<String> root_r_c = root_r.getChild("c");
|
---|
561 | TrieNode<String> root_r_d = root_r.getChild("d");
|
---|
562 | TrieNode<String> root_r_r = root_r.getChild("r");
|
---|
563 |
|
---|
564 | assertEquals(5, root_a.getCount());
|
---|
565 | assertNull(root_a_a);
|
---|
566 | assertEquals(2, root_a_b.getCount());
|
---|
567 | assertNull(root_a_b_a);
|
---|
568 | assertNull(root_a_b_b);
|
---|
569 | assertNull(root_a_b_c);
|
---|
570 | assertNull(root_a_b_d);
|
---|
571 | assertEquals(2, root_a_b_r.getCount());
|
---|
572 | assertEquals(1, root_a_c.getCount());
|
---|
573 | assertEquals(1, root_a_c_a.getCount());
|
---|
574 | assertNull(root_a_c_b);
|
---|
575 | assertNull(root_a_c_c);
|
---|
576 | assertNull(root_a_c_d);
|
---|
577 | assertNull(root_a_c_r);
|
---|
578 | assertEquals(1, root_a_d.getCount());
|
---|
579 | assertEquals(1, root_a_d_a.getCount());
|
---|
580 | assertNull(root_a_d_b);
|
---|
581 | assertNull(root_a_d_c);
|
---|
582 | assertNull(root_a_d_d);
|
---|
583 | assertNull(root_a_d_r);
|
---|
584 | assertNull(root_a_r);
|
---|
585 |
|
---|
586 | assertEquals(2, root_b.getCount());
|
---|
587 | assertNull(root_b_a);
|
---|
588 | assertNull(root_b_b);
|
---|
589 | assertNull(root_b_c);
|
---|
590 | assertNull(root_b_d);
|
---|
591 | assertEquals(2, root_b_r.getCount());
|
---|
592 | assertEquals(2, root_b_r_a.getCount());
|
---|
593 | assertNull(root_b_r_b);
|
---|
594 | assertNull(root_b_r_c);
|
---|
595 | assertNull(root_b_r_d);
|
---|
596 | assertNull(root_b_r_r);
|
---|
597 |
|
---|
598 | assertEquals(1, root_c.getCount());
|
---|
599 | assertEquals(1, root_c_a.getCount());
|
---|
600 | assertNull(root_c_a_a);
|
---|
601 | assertNull(root_c_a_b);
|
---|
602 | assertNull(root_c_a_c);
|
---|
603 | assertEquals(1, root_c_a_d.getCount());
|
---|
604 | assertNull(root_c_a_r);
|
---|
605 | assertNull(root_c_b);
|
---|
606 | assertNull(root_c_c);
|
---|
607 | assertNull(root_c_d);
|
---|
608 | assertNull(root_c_r);
|
---|
609 |
|
---|
610 | assertEquals(1, root_d.getCount());
|
---|
611 | assertEquals(1, root_d_a.getCount());
|
---|
612 | assertNull(root_d_a_a);
|
---|
613 | assertEquals(1, root_d_a_b.getCount());
|
---|
614 | assertNull(root_d_a_c);
|
---|
615 | assertNull(root_d_a_d);
|
---|
616 | assertNull(root_d_a_r);
|
---|
617 | assertNull(root_d_b);
|
---|
618 | assertNull(root_d_c);
|
---|
619 | assertNull(root_d_d);
|
---|
620 | assertNull(root_d_r);
|
---|
621 |
|
---|
622 | assertEquals(2, root_r.getCount());
|
---|
623 | assertEquals(2, root_r_a.getCount());
|
---|
624 | assertNull(root_r_a_a);
|
---|
625 | assertNull(root_r_a_b);
|
---|
626 | assertEquals(1, root_r_a_c.getCount());
|
---|
627 | assertNull(root_r_a_d);
|
---|
628 | assertNull(root_r_a_r);
|
---|
629 | assertNull(root_r_b);
|
---|
630 | assertNull(root_r_c);
|
---|
631 | assertNull(root_r_d);
|
---|
632 | assertNull(root_r_r);
|
---|
633 |
|
---|
634 | // check if leafs are really leafs
|
---|
635 | assertTrue(root_a_b_r.isLeaf());
|
---|
636 | assertTrue(root_a_c_a.isLeaf());
|
---|
637 | assertTrue(root_a_d_a.isLeaf());
|
---|
638 | assertTrue(root_b_r_a.isLeaf());
|
---|
639 | assertTrue(root_c_a_d.isLeaf());
|
---|
640 | assertTrue(root_d_a_b.isLeaf());
|
---|
641 | assertTrue(root_r_a_c.isLeaf());
|
---|
642 | }
|
---|
643 |
|
---|
644 | @Test
|
---|
645 | public void testTrain_2() throws Exception {
|
---|
646 | Trie<String> fixture = new Trie<String>();
|
---|
647 | int maxOrder = 0;
|
---|
648 |
|
---|
649 | fixture.train(sequence, maxOrder);
|
---|
650 |
|
---|
651 | assertTrue(fixture.getKnownSymbols().isEmpty());
|
---|
652 | }
|
---|
653 |
|
---|
654 | @Test
|
---|
655 | public void testTrain_3() throws Exception {
|
---|
656 | Trie<Object> fixture = new Trie<Object>();
|
---|
657 | List<Object> sequence = new ArrayList<Object>();
|
---|
658 | int maxOrder = 1;
|
---|
659 |
|
---|
660 | fixture.train(sequence, maxOrder);
|
---|
661 |
|
---|
662 | assertTrue(fixture.getKnownSymbols().isEmpty());
|
---|
663 | }
|
---|
664 |
|
---|
665 | @Test
|
---|
666 | public void testTrain_4() throws Exception {
|
---|
667 | Trie<String> fixture = new Trie<String>();
|
---|
668 | List<String> sequence = new ArrayList<String>();
|
---|
669 | sequence.add("a");
|
---|
670 | sequence.add("b");
|
---|
671 | int maxOrder = 3;
|
---|
672 |
|
---|
673 | fixture.train(sequence, maxOrder);
|
---|
674 |
|
---|
675 | assertCollectionContent(sequence, fixture.getKnownSymbols());
|
---|
676 | TrieNode<String> root = fixture.find(new ArrayList<String>());
|
---|
677 | TrieNode<String> root_a = root.getChild("a");
|
---|
678 | TrieNode<String> root_a_a = root_a.getChild("a");
|
---|
679 | TrieNode<String> root_a_b = root_a.getChild("b");
|
---|
680 | TrieNode<String> root_b = root.getChild("b");
|
---|
681 | TrieNode<String> root_b_a = root_b.getChild("a");
|
---|
682 | TrieNode<String> root_b_b = root_b.getChild("b");
|
---|
683 |
|
---|
684 | assertEquals(1, root_a.getCount());
|
---|
685 | assertNull(root_a_a);
|
---|
686 | assertEquals(1, root_a_b.getCount());
|
---|
687 | assertEquals(1, root_b.getCount());
|
---|
688 | assertNull(root_b_a);
|
---|
689 | assertNull(root_b_b);
|
---|
690 |
|
---|
691 | assertTrue(root_a_b.isLeaf());
|
---|
692 | assertTrue(root_b.isLeaf());
|
---|
693 | }
|
---|
694 |
|
---|
695 | @Test
|
---|
696 | public void testEdgeEdge_1() throws Exception {
|
---|
697 | Edge result = new Edge();
|
---|
698 |
|
---|
699 | assertNotNull(result);
|
---|
700 | }
|
---|
701 |
|
---|
702 | @Test
|
---|
703 | public void testTrieVertexTrieVertex_1() throws Exception {
|
---|
704 | String id = "idString";
|
---|
705 |
|
---|
706 | TrieVertex result = new TrieVertex(id);
|
---|
707 |
|
---|
708 | assertNotNull(result);
|
---|
709 | }
|
---|
710 |
|
---|
711 | @Test
|
---|
712 | public void testTrieVertexToString_1() throws Exception {
|
---|
713 | String id = "idString";
|
---|
714 | TrieVertex fixture = new TrieVertex(id);
|
---|
715 |
|
---|
716 | String result = fixture.toString();
|
---|
717 |
|
---|
718 | assertEquals(id, result);
|
---|
719 | }
|
---|
720 |
|
---|
721 | @Test
|
---|
722 | public void testEquals_1() throws Exception {
|
---|
723 | Trie<String> trieOther = new Trie<String>();
|
---|
724 | Trie<String> fixture = new Trie<String>();
|
---|
725 |
|
---|
726 | boolean result = fixture.equals(trieOther);
|
---|
727 |
|
---|
728 | assertEquals(true, result);
|
---|
729 | }
|
---|
730 |
|
---|
731 | @Test
|
---|
732 | public void testEquals_2() throws Exception {
|
---|
733 | Trie<String> trieOther = new Trie<String>();
|
---|
734 | trieOther.train(sequence, 2);
|
---|
735 | Trie<String> fixture = new Trie<String>();
|
---|
736 | fixture.train(sequence, 2);
|
---|
737 |
|
---|
738 | boolean result = fixture.equals(trieOther);
|
---|
739 |
|
---|
740 | assertEquals(true, result);
|
---|
741 | }
|
---|
742 |
|
---|
743 | @Test
|
---|
744 | public void testEquals_3() throws Exception {
|
---|
745 | Trie<String> trieOther = new Trie<String>();
|
---|
746 | trieOther.train(sequence, 2);
|
---|
747 | Trie<String> fixture = new Trie<String>();
|
---|
748 | fixture.train(sequence, 3);
|
---|
749 |
|
---|
750 | boolean result = fixture.equals(trieOther);
|
---|
751 |
|
---|
752 | assertEquals(false, result);
|
---|
753 | }
|
---|
754 |
|
---|
755 | @Test
|
---|
756 | public void testEquals_4() throws Exception {
|
---|
757 | Trie<String> trieOther = new Trie<String>();
|
---|
758 | Trie<String> fixture = new Trie<String>();
|
---|
759 | fixture.train(sequence, 2);
|
---|
760 |
|
---|
761 | boolean result = fixture.equals(trieOther);
|
---|
762 |
|
---|
763 | assertEquals(false, result);
|
---|
764 | }
|
---|
765 |
|
---|
766 | @Test
|
---|
767 | public void testEquals_5() throws Exception {
|
---|
768 | Trie<String> trieOther = new Trie<String>();
|
---|
769 | trieOther.train(sequence, 2);
|
---|
770 | Trie<String> fixture = new Trie<String>();
|
---|
771 |
|
---|
772 | boolean result = fixture.equals(trieOther);
|
---|
773 |
|
---|
774 | assertEquals(false, result);
|
---|
775 | }
|
---|
776 |
|
---|
777 | @Test
|
---|
778 | public void testEquals_6() throws Exception {
|
---|
779 | Trie<String> fixture = new Trie<String>();
|
---|
780 | fixture.train(sequence, 2);
|
---|
781 |
|
---|
782 | boolean result = fixture.equals(fixture);
|
---|
783 |
|
---|
784 | assertEquals(true, result);
|
---|
785 | }
|
---|
786 |
|
---|
787 | @Test
|
---|
788 | public void testEquals_7() throws Exception {
|
---|
789 | Trie<String> fixture = new Trie<String>();
|
---|
790 | fixture.train(sequence, 2);
|
---|
791 |
|
---|
792 | boolean result = fixture.equals(null);
|
---|
793 |
|
---|
794 | assertEquals(false, result);
|
---|
795 | }
|
---|
796 |
|
---|
797 | @Before
|
---|
798 | public void setUp() throws Exception {
|
---|
799 | sequence = new ArrayList<String>();
|
---|
800 | sequence.add("a");
|
---|
801 | sequence.add("b");
|
---|
802 | sequence.add("r");
|
---|
803 | sequence.add("a");
|
---|
804 | sequence.add("c");
|
---|
805 | sequence.add("a");
|
---|
806 | sequence.add("d");
|
---|
807 | sequence.add("a");
|
---|
808 | sequence.add("b");
|
---|
809 | sequence.add("r");
|
---|
810 | sequence.add("a");
|
---|
811 |
|
---|
812 | symbols = new HashSet<String>();
|
---|
813 | symbols.add("a");
|
---|
814 | symbols.add("b");
|
---|
815 | symbols.add("c");
|
---|
816 | symbols.add("d");
|
---|
817 | symbols.add("r");
|
---|
818 | }
|
---|
819 |
|
---|
820 | public static void main(String[] args) {
|
---|
821 | new org.junit.runner.JUnitCore().run(TrieTest.class);
|
---|
822 | }
|
---|
823 | } |
---|