Changeset 398 for trunk/EventBenchCoreTest
- Timestamp:
- 03/09/12 13:26:08 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchCoreTest/src/de/ugoe/cs/eventbench/models/TrieTest.java
r346 r398 15 15 16 16 /** 17 * The class <code>TrieTest</code> contains tests for the class <code>{@link Trie}</code>. 18 * 17 * The class <code>TrieTest</code> contains tests for the class 18 * <code>{@link Trie}</code>. 19 * 19 20 * @author Steffen Herbold 20 21 * @version 1.0 21 22 */ 22 23 public class TrieTest { 23 24 24 25 List<String> sequence; 25 26 Collection<String> symbols; 26 27 private static void assertCollectionContent(Collection<?> c1, Collection<?> c2) { 27 28 private static void assertCollectionContent(Collection<?> c1, 29 Collection<?> c2) { 28 30 assertEquals(c1.size(), c2.size()); 29 for ( Object obj : c1) {31 for (Object obj : c1) { 30 32 assertTrue(c2.contains(obj)); 31 33 } 32 34 } 33 34 @Test 35 public void testTrie_1() 36 throws Exception { 35 36 @Test 37 public void testTrie_1() throws Exception { 37 38 38 39 Trie<String> result = new Trie<String>(); … … 46 47 47 48 @Test 48 public void testAdd_1() 49 throws Exception { 49 public void testTrie_2() throws Exception { 50 Trie<String> trie1 = new Trie<String>(); 51 trie1.train(sequence, 3); 52 53 Trie<String> result = new Trie<String>(trie1); 54 55 assertEquals(trie1, result); 56 assertNotSame(trie1, result); 57 } 58 59 @Test(expected = java.security.InvalidParameterException.class) 60 public void testTrie_3() throws Exception { 61 new Trie<String>(null); 62 } 63 64 @Test 65 public void testAdd_1() throws Exception { 50 66 Trie<String> fixture = new Trie<String>(); 51 67 List<String> seq = new ArrayList<String>(); 52 68 seq.add("a"); 53 69 seq.add("b"); 54 70 55 71 fixture.add(seq); 56 72 57 73 assertEquals(1, fixture.getChild("a").getCount()); 58 74 assertEquals(1, fixture.getChild("a").getChild("b").getCount()); 59 75 assertNull(fixture.getChild("b")); 60 76 } 61 62 @Test 63 public void testAdd_2() 64 throws Exception { 65 Trie<String> fixture = new Trie<String>(); 66 77 78 @Test 79 public void testAdd_2() throws Exception { 80 Trie<String> fixture = new Trie<String>(); 81 67 82 fixture.add(new ArrayList<String>()); 68 83 69 84 assertEquals(0, fixture.getNumSymbols()); 70 85 } 71 72 @Test 73 public void testAdd_3() 74 throws Exception { 75 Trie<String> fixture = new Trie<String>(); 76 86 87 @Test 88 public void testAdd_3() throws Exception { 89 Trie<String> fixture = new Trie<String>(); 90 77 91 fixture.add(null); 78 92 79 93 assertEquals(0, fixture.getNumSymbols()); 80 94 } 81 95 82 96 @Test 83 public void testFind_1() 84 throws Exception { 97 public void testFind_1() throws Exception { 85 98 Trie<String> fixture = new Trie<String>(); 86 99 fixture.train(sequence, 3); … … 89 102 findSequence.add("b"); 90 103 findSequence.add("r"); 91 TrieNode<String> expected = fixture.getChild("a").getChild("b").getChild("r"); 92 104 TrieNode<String> expected = fixture.getChild("a").getChild("b") 105 .getChild("r"); 106 93 107 TrieNode<String> result = fixture.find(findSequence); 94 108 … … 97 111 98 112 @Test 99 public void testFind_2() 100 throws Exception { 113 public void testFind_2() throws Exception { 101 114 Trie<String> fixture = new Trie<String>(); 102 115 fixture.train(sequence, 3); … … 105 118 findSequence.add("a"); 106 119 TrieNode<String> expected = fixture.getChild("c").getChild("a"); 107 120 108 121 TrieNode<String> result = fixture.find(findSequence); 109 122 … … 112 125 113 126 @Test 114 public void testFind_3() 115 throws Exception { 127 public void testFind_3() throws Exception { 116 128 Trie<String> fixture = new Trie<String>(); 117 129 fixture.train(sequence, 3); 118 130 List<String> findSequence = new ArrayList<String>(); 119 131 120 132 TrieNode<String> result = fixture.find(findSequence); 121 133 … … 124 136 125 137 @Test 126 public void testFind_4() 127 throws Exception { 128 Trie<String> fixture = new Trie<String>(); 129 fixture.train(sequence, 3); 130 138 public void testFind_4() throws Exception { 139 Trie<String> fixture = new Trie<String>(); 140 fixture.train(sequence, 3); 141 131 142 TrieNode<String> result = fixture.find(null); 132 143 … … 135 146 136 147 @Test 137 public void testGetChildCreate_1() 138 throws Exception { 148 public void testGetChildCreate_1() throws Exception { 139 149 Trie<String> fixture = new Trie<String>(); 140 150 String symbol = "a"; … … 146 156 assertTrue(result.isLeaf()); 147 157 } 148 149 @Test(expected=java.security.InvalidParameterException.class) 150 public void testGetChildCreate_2() 151 throws Exception { 158 159 @Test(expected = java.security.InvalidParameterException.class) 160 public void testGetChildCreate_2() throws Exception { 152 161 Trie<String> fixture = new Trie<String>(); 153 162 fixture.getChildCreate(null); … … 155 164 156 165 @Test 157 public void testGetContextSuffix_1() 158 throws Exception { 166 public void testGetContextSuffix_1() throws Exception { 159 167 Trie<String> fixture = new Trie<String>(); 160 168 fixture.train(sequence, 3); … … 166 174 expected.add("a"); 167 175 expected.add("b"); 168 169 176 170 177 List<String> result = fixture.getContextSuffix(context); … … 174 181 175 182 @Test 176 public void testGetContextSuffix_2() 177 throws Exception { 183 public void testGetContextSuffix_2() throws Exception { 178 184 Trie<String> fixture = new Trie<String>(); 179 185 fixture.train(sequence, 3); … … 186 192 expected.add("b"); 187 193 expected.add("r"); 188 189 194 190 195 List<String> result = fixture.getContextSuffix(context); … … 194 199 195 200 @Test 196 public void testGetContextSuffix_3() 197 throws Exception { 201 public void testGetContextSuffix_3() throws Exception { 198 202 Trie<String> fixture = new Trie<String>(); 199 203 fixture.train(sequence, 3); … … 211 215 212 216 @Test 213 public void testGetContextSuffix_4() 214 throws Exception { 217 public void testGetContextSuffix_4() throws Exception { 215 218 Trie<String> fixture = new Trie<String>(); 216 219 … … 223 226 224 227 @Test 225 public void testGetContextSuffix_5() 226 throws Exception { 228 public void testGetContextSuffix_5() throws Exception { 227 229 Trie<String> fixture = new Trie<String>(); 228 230 fixture.train(sequence, 3); … … 234 236 expected.add("a"); 235 237 expected.add("b"); 236 237 238 238 239 List<String> result = fixture.getContextSuffix(context); … … 242 243 243 244 @Test 244 public void testGetCount_1() 245 throws Exception { 245 public void testGetCount_1() throws Exception { 246 246 Trie<String> fixture = new Trie<String>(); 247 247 fixture.train(sequence, 3); … … 255 255 256 256 @Test 257 public void testGetCount_2() 258 throws Exception { 257 public void testGetCount_2() throws Exception { 259 258 Trie<String> fixture = new Trie<String>(); 260 259 fixture.train(sequence, 3); … … 264 263 265 264 int result = fixture.getCount(subSequence); 266 265 267 266 assertEquals(2, result); 268 267 } 269 268 270 269 @Test 271 public void testGetCount_3() 272 throws Exception { 270 public void testGetCount_3() throws Exception { 273 271 Trie<String> fixture = new Trie<String>(); 274 272 fixture.train(sequence, 3); … … 280 278 assertEquals(0, result); 281 279 } 282 283 @Test 284 public void testGetCount_4() 285 throws Exception { 280 281 @Test 282 public void testGetCount_4() throws Exception { 286 283 Trie<String> fixture = new Trie<String>(); 287 284 fixture.train(sequence, 3); … … 294 291 295 292 @Test 296 public void testGetCount_5() 297 throws Exception { 293 public void testGetCount_5() throws Exception { 298 294 Trie<String> fixture = new Trie<String>(); 299 295 fixture.train(sequence, 3); … … 303 299 304 300 int result = fixture.getCount(subSequence, "r"); 305 301 306 302 assertEquals(2, result); 307 303 } 308 304 309 305 @Test 310 public void testGetCount_6() 311 throws Exception { 306 public void testGetCount_6() throws Exception { 312 307 Trie<String> fixture = new Trie<String>(); 313 308 fixture.train(sequence, 3); … … 320 315 321 316 @Test 322 public void testGetFollowingSymbols_1() 323 throws Exception { 317 public void testGetFollowingSymbols_1() throws Exception { 324 318 Trie<String> fixture = new Trie<String>(); 325 319 fixture.train(sequence, 3); … … 330 324 expected.add("c"); 331 325 expected.add("d"); 332 326 333 327 Collection<String> result = fixture.getFollowingSymbols(subSequence); 334 328 … … 337 331 338 332 @Test 339 public void testGetFollowingSymbols_2() 340 throws Exception { 333 public void testGetFollowingSymbols_2() throws Exception { 341 334 Trie<String> fixture = new Trie<String>(); 342 335 fixture.train(sequence, 3); … … 350 343 assertEquals(0, result.size()); 351 344 } 352 353 @Test 354 public void testGetFollowingSymbols_3() 355 throws Exception { 345 346 @Test 347 public void testGetFollowingSymbols_3() throws Exception { 356 348 Trie<String> fixture = new Trie<String>(); 357 349 fixture.train(sequence, 3); … … 363 355 assertEquals(0, result.size()); 364 356 } 365 366 @Test 367 public void testGetNumLeafAncestors_1() 368 throws Exception { 357 358 @Test 359 public void testGetNumLeafAncestors_1() throws Exception { 369 360 Trie<String> fixture = new Trie<String>(); 370 361 fixture.train(sequence, 3); … … 374 365 assertEquals(7, result); 375 366 } 376 377 @Test 378 public void testGetNumLeafs_1() 379 throws Exception { 367 368 @Test 369 public void testGetNumLeafs_1() throws Exception { 380 370 Trie<String> fixture = new Trie<String>(); 381 371 fixture.train(sequence, 3); … … 385 375 assertEquals(7, result); 386 376 } 387 388 @Test 389 public void testGetNumSymbols_1() 390 throws Exception { 377 378 @Test 379 public void testGetNumSymbols_1() throws Exception { 391 380 Trie<String> fixture = new Trie<String>(); 392 381 fixture.train(sequence, 3); … … 398 387 399 388 @Test 400 public void testTrain_1() 401 throws Exception { 389 public void testTrain_1() throws Exception { 402 390 Trie<String> fixture = new Trie<String>(); 403 391 int maxOrder = 3; 404 392 405 393 fixture.train(sequence, maxOrder); 406 394 407 395 // check if symbols are correct 408 396 assertCollectionContent(symbols, fixture.getKnownSymbols()); 409 397 410 398 // check if counters are correct and only the correct nodes exist 411 399 TrieNode<String> root = fixture.find(new ArrayList<String>()); … … 475 463 TrieNode<String> root_r_d = root_r.getChild("d"); 476 464 TrieNode<String> root_r_r = root_r.getChild("r"); 477 465 478 466 assertEquals(5, root_a.getCount()); 479 467 assertNull(root_a_a); … … 497 485 assertNull(root_a_d_r); 498 486 assertNull(root_a_r); 499 487 500 488 assertEquals(2, root_b.getCount()); 501 489 assertNull(root_b_a); … … 509 497 assertNull(root_b_r_d); 510 498 assertNull(root_b_r_r); 511 499 512 500 assertEquals(1, root_c.getCount()); 513 501 assertEquals(1, root_c_a.getCount()); … … 521 509 assertNull(root_c_d); 522 510 assertNull(root_c_r); 523 511 524 512 assertEquals(1, root_d.getCount()); 525 513 assertEquals(1, root_d_a.getCount()); … … 533 521 assertNull(root_d_d); 534 522 assertNull(root_d_r); 535 523 536 524 assertEquals(2, root_r.getCount()); 537 525 assertEquals(2, root_r_a.getCount()); … … 545 533 assertNull(root_r_d); 546 534 assertNull(root_r_r); 547 535 548 536 // check if leafs are really leafs 549 537 assertTrue(root_a_b_r.isLeaf()); … … 557 545 558 546 @Test 559 public void testTrain_2() 560 throws Exception { 547 public void testTrain_2() throws Exception { 561 548 Trie<String> fixture = new Trie<String>(); 562 549 int maxOrder = 0; … … 568 555 569 556 @Test 570 public void testTrain_3() 571 throws Exception { 557 public void testTrain_3() throws Exception { 572 558 Trie<Object> fixture = new Trie<Object>(); 573 559 List<Object> sequence = new ArrayList<Object>(); … … 575 561 576 562 fixture.train(sequence, maxOrder); 577 563 578 564 assertTrue(fixture.getKnownSymbols().isEmpty()); 579 565 } 580 566 581 567 @Test 582 public void testTrain_4() 583 throws Exception { 568 public void testTrain_4() throws Exception { 584 569 Trie<String> fixture = new Trie<String>(); 585 570 List<String> sequence = new ArrayList<String>(); … … 589 574 590 575 fixture.train(sequence, maxOrder); 591 576 592 577 assertCollectionContent(sequence, fixture.getKnownSymbols()); 593 578 TrieNode<String> root = fixture.find(new ArrayList<String>()); … … 598 583 TrieNode<String> root_b_a = root_b.getChild("a"); 599 584 TrieNode<String> root_b_b = root_b.getChild("b"); 600 585 601 586 assertEquals(1, root_a.getCount()); 602 587 assertNull(root_a_a); … … 605 590 assertNull(root_b_a); 606 591 assertNull(root_b_b); 607 592 608 593 assertTrue(root_a_b.isLeaf()); 609 594 assertTrue(root_b.isLeaf()); 610 595 } 611 596 612 597 @Test 613 598 public void testEdgeEdge_1() throws Exception { 614 599 Edge result = new Edge(); 615 600 616 601 assertNotNull(result); 617 602 } 618 603 619 604 @Test 620 605 public void testTrieVertexTrieVertex_1() throws Exception { 621 606 String id = "idString"; 622 607 623 608 TrieVertex result = new TrieVertex(id); 624 609 625 610 assertNotNull(result); 626 611 } 627 612 628 613 @Test 629 614 public void testTrieVertexToString_1() throws Exception { 630 615 String id = "idString"; 631 616 TrieVertex fixture = new TrieVertex(id); 632 617 633 618 String result = fixture.toString(); 634 619 635 620 assertEquals(id, result); 636 621 } 637 622 623 @Test 624 public void testEquals_1() throws Exception { 625 Trie<String> trieOther = new Trie<String>(); 626 Trie<String> fixture = new Trie<String>(); 627 628 boolean result = fixture.equals(trieOther); 629 630 assertEquals(true, result); 631 } 632 633 @Test 634 public void testEquals_2() throws Exception { 635 Trie<String> trieOther = new Trie<String>(); 636 trieOther.train(sequence, 2); 637 Trie<String> fixture = new Trie<String>(); 638 fixture.train(sequence, 2); 639 640 boolean result = fixture.equals(trieOther); 641 642 assertEquals(true, result); 643 } 644 645 @Test 646 public void testEquals_3() throws Exception { 647 Trie<String> trieOther = new Trie<String>(); 648 trieOther.train(sequence, 2); 649 Trie<String> fixture = new Trie<String>(); 650 fixture.train(sequence, 3); 651 652 boolean result = fixture.equals(trieOther); 653 654 assertEquals(false, result); 655 } 656 657 @Test 658 public void testEquals_4() throws Exception { 659 Trie<String> trieOther = new Trie<String>(); 660 Trie<String> fixture = new Trie<String>(); 661 fixture.train(sequence, 2); 662 663 boolean result = fixture.equals(trieOther); 664 665 assertEquals(false, result); 666 } 667 668 @Test 669 public void testEquals_5() throws Exception { 670 Trie<String> trieOther = new Trie<String>(); 671 trieOther.train(sequence, 2); 672 Trie<String> fixture = new Trie<String>(); 673 674 boolean result = fixture.equals(trieOther); 675 676 assertEquals(false, result); 677 } 678 679 @Test 680 public void testEquals_6() throws Exception { 681 Trie<String> fixture = new Trie<String>(); 682 fixture.train(sequence, 2); 683 684 boolean result = fixture.equals(fixture); 685 686 assertEquals(true, result); 687 } 688 689 @Test 690 public void testEquals_7() throws Exception { 691 Trie<String> fixture = new Trie<String>(); 692 fixture.train(sequence, 2); 693 694 boolean result = fixture.equals(null); 695 696 assertEquals(false, result); 697 } 698 638 699 @Before 639 public void setUp() 640 throws Exception { 700 public void setUp() throws Exception { 641 701 sequence = new ArrayList<String>(); 642 702 sequence.add("a"); … … 651 711 sequence.add("r"); 652 712 sequence.add("a"); 653 713 654 714 symbols = new HashSet<String>(); 655 715 symbols.add("a"); … … 660 720 } 661 721 662 @After663 public void tearDown()664 throws Exception {665 // Add additional tear down code here666 }667 668 722 public static void main(String[] args) { 669 723 new org.junit.runner.JUnitCore().run(TrieTest.class);
Note: See TracChangeset
for help on using the changeset viewer.