Changeset 398


Ignore:
Timestamp:
03/09/12 13:26:08 (12 years ago)
Author:
sherbold
Message:
  • added test cases for de.ugoe.cs.eventbench.models.Trie.Trie(Trie) and de.ugoe.cs.eventbench.models.Trie.equals(Object)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/EventBenchCoreTest/src/de/ugoe/cs/eventbench/models/TrieTest.java

    r346 r398  
    1515 
    1616/** 
    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 *  
    1920 * @author Steffen Herbold 
    2021 * @version 1.0 
    2122 */ 
    2223public class TrieTest { 
    23          
     24 
    2425        List<String> sequence; 
    2526        Collection<String> symbols; 
    26          
    27         private static void assertCollectionContent(Collection<?> c1, Collection<?> c2) { 
     27 
     28        private static void assertCollectionContent(Collection<?> c1, 
     29                        Collection<?> c2) { 
    2830                assertEquals(c1.size(), c2.size()); 
    29                 for( Object obj : c1 ) { 
     31                for (Object obj : c1) { 
    3032                        assertTrue(c2.contains(obj)); 
    3133                } 
    3234        } 
    33          
    34         @Test 
    35         public void testTrie_1() 
    36                 throws Exception { 
     35 
     36        @Test 
     37        public void testTrie_1() throws Exception { 
    3738 
    3839                Trie<String> result = new Trie<String>(); 
     
    4647 
    4748        @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 { 
    5066                Trie<String> fixture = new Trie<String>(); 
    5167                List<String> seq = new ArrayList<String>(); 
    5268                seq.add("a"); 
    5369                seq.add("b"); 
    54                  
     70 
    5571                fixture.add(seq); 
    56                  
     72 
    5773                assertEquals(1, fixture.getChild("a").getCount()); 
    5874                assertEquals(1, fixture.getChild("a").getChild("b").getCount()); 
    5975                assertNull(fixture.getChild("b")); 
    6076        } 
    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 
    6782                fixture.add(new ArrayList<String>()); 
    68                  
     83 
    6984                assertEquals(0, fixture.getNumSymbols()); 
    7085        } 
    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 
    7791                fixture.add(null); 
    78                  
     92 
    7993                assertEquals(0, fixture.getNumSymbols()); 
    8094        } 
    8195 
    8296        @Test 
    83         public void testFind_1() 
    84                 throws Exception { 
     97        public void testFind_1() throws Exception { 
    8598                Trie<String> fixture = new Trie<String>(); 
    8699                fixture.train(sequence, 3); 
     
    89102                findSequence.add("b"); 
    90103                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 
    93107                TrieNode<String> result = fixture.find(findSequence); 
    94108 
     
    97111 
    98112        @Test 
    99         public void testFind_2() 
    100                 throws Exception { 
     113        public void testFind_2() throws Exception { 
    101114                Trie<String> fixture = new Trie<String>(); 
    102115                fixture.train(sequence, 3); 
     
    105118                findSequence.add("a"); 
    106119                TrieNode<String> expected = fixture.getChild("c").getChild("a"); 
    107                  
     120 
    108121                TrieNode<String> result = fixture.find(findSequence); 
    109122 
     
    112125 
    113126        @Test 
    114         public void testFind_3() 
    115                 throws Exception { 
     127        public void testFind_3() throws Exception { 
    116128                Trie<String> fixture = new Trie<String>(); 
    117129                fixture.train(sequence, 3); 
    118130                List<String> findSequence = new ArrayList<String>(); 
    119                  
     131 
    120132                TrieNode<String> result = fixture.find(findSequence); 
    121133 
     
    124136 
    125137        @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 
    131142                TrieNode<String> result = fixture.find(null); 
    132143 
     
    135146 
    136147        @Test 
    137         public void testGetChildCreate_1() 
    138                 throws Exception { 
     148        public void testGetChildCreate_1() throws Exception { 
    139149                Trie<String> fixture = new Trie<String>(); 
    140150                String symbol = "a"; 
     
    146156                assertTrue(result.isLeaf()); 
    147157        } 
    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 { 
    152161                Trie<String> fixture = new Trie<String>(); 
    153162                fixture.getChildCreate(null); 
     
    155164 
    156165        @Test 
    157         public void testGetContextSuffix_1() 
    158                 throws Exception { 
     166        public void testGetContextSuffix_1() throws Exception { 
    159167                Trie<String> fixture = new Trie<String>(); 
    160168                fixture.train(sequence, 3); 
     
    166174                expected.add("a"); 
    167175                expected.add("b"); 
    168                  
    169176 
    170177                List<String> result = fixture.getContextSuffix(context); 
     
    174181 
    175182        @Test 
    176         public void testGetContextSuffix_2() 
    177                 throws Exception { 
     183        public void testGetContextSuffix_2() throws Exception { 
    178184                Trie<String> fixture = new Trie<String>(); 
    179185                fixture.train(sequence, 3); 
     
    186192                expected.add("b"); 
    187193                expected.add("r"); 
    188                  
    189194 
    190195                List<String> result = fixture.getContextSuffix(context); 
     
    194199 
    195200        @Test 
    196         public void testGetContextSuffix_3() 
    197                 throws Exception { 
     201        public void testGetContextSuffix_3() throws Exception { 
    198202                Trie<String> fixture = new Trie<String>(); 
    199203                fixture.train(sequence, 3); 
     
    211215 
    212216        @Test 
    213         public void testGetContextSuffix_4() 
    214                 throws Exception { 
     217        public void testGetContextSuffix_4() throws Exception { 
    215218                Trie<String> fixture = new Trie<String>(); 
    216219 
     
    223226 
    224227        @Test 
    225         public void testGetContextSuffix_5() 
    226                 throws Exception { 
     228        public void testGetContextSuffix_5() throws Exception { 
    227229                Trie<String> fixture = new Trie<String>(); 
    228230                fixture.train(sequence, 3); 
     
    234236                expected.add("a"); 
    235237                expected.add("b"); 
    236                  
    237238 
    238239                List<String> result = fixture.getContextSuffix(context); 
     
    242243 
    243244        @Test 
    244         public void testGetCount_1() 
    245                 throws Exception { 
     245        public void testGetCount_1() throws Exception { 
    246246                Trie<String> fixture = new Trie<String>(); 
    247247                fixture.train(sequence, 3); 
     
    255255 
    256256        @Test 
    257         public void testGetCount_2() 
    258                 throws Exception { 
     257        public void testGetCount_2() throws Exception { 
    259258                Trie<String> fixture = new Trie<String>(); 
    260259                fixture.train(sequence, 3); 
     
    264263 
    265264                int result = fixture.getCount(subSequence); 
    266                  
     265 
    267266                assertEquals(2, result); 
    268267        } 
    269268 
    270269        @Test 
    271         public void testGetCount_3() 
    272                 throws Exception { 
     270        public void testGetCount_3() throws Exception { 
    273271                Trie<String> fixture = new Trie<String>(); 
    274272                fixture.train(sequence, 3); 
     
    280278                assertEquals(0, result); 
    281279        } 
    282          
    283         @Test 
    284         public void testGetCount_4() 
    285                 throws Exception { 
     280 
     281        @Test 
     282        public void testGetCount_4() throws Exception { 
    286283                Trie<String> fixture = new Trie<String>(); 
    287284                fixture.train(sequence, 3); 
     
    294291 
    295292        @Test 
    296         public void testGetCount_5() 
    297                 throws Exception { 
     293        public void testGetCount_5() throws Exception { 
    298294                Trie<String> fixture = new Trie<String>(); 
    299295                fixture.train(sequence, 3); 
     
    303299 
    304300                int result = fixture.getCount(subSequence, "r"); 
    305                  
     301 
    306302                assertEquals(2, result); 
    307303        } 
    308304 
    309305        @Test 
    310         public void testGetCount_6() 
    311                 throws Exception { 
     306        public void testGetCount_6() throws Exception { 
    312307                Trie<String> fixture = new Trie<String>(); 
    313308                fixture.train(sequence, 3); 
     
    320315 
    321316        @Test 
    322         public void testGetFollowingSymbols_1() 
    323                 throws Exception { 
     317        public void testGetFollowingSymbols_1() throws Exception { 
    324318                Trie<String> fixture = new Trie<String>(); 
    325319                fixture.train(sequence, 3); 
     
    330324                expected.add("c"); 
    331325                expected.add("d"); 
    332                  
     326 
    333327                Collection<String> result = fixture.getFollowingSymbols(subSequence); 
    334328 
     
    337331 
    338332        @Test 
    339         public void testGetFollowingSymbols_2() 
    340                 throws Exception { 
     333        public void testGetFollowingSymbols_2() throws Exception { 
    341334                Trie<String> fixture = new Trie<String>(); 
    342335                fixture.train(sequence, 3); 
     
    350343                assertEquals(0, result.size()); 
    351344        } 
    352          
    353         @Test 
    354         public void testGetFollowingSymbols_3() 
    355                 throws Exception { 
     345 
     346        @Test 
     347        public void testGetFollowingSymbols_3() throws Exception { 
    356348                Trie<String> fixture = new Trie<String>(); 
    357349                fixture.train(sequence, 3); 
     
    363355                assertEquals(0, result.size()); 
    364356        } 
    365          
    366         @Test 
    367         public void testGetNumLeafAncestors_1() 
    368                 throws Exception { 
     357 
     358        @Test 
     359        public void testGetNumLeafAncestors_1() throws Exception { 
    369360                Trie<String> fixture = new Trie<String>(); 
    370361                fixture.train(sequence, 3); 
     
    374365                assertEquals(7, result); 
    375366        } 
    376          
    377         @Test 
    378         public void testGetNumLeafs_1() 
    379                 throws Exception { 
     367 
     368        @Test 
     369        public void testGetNumLeafs_1() throws Exception { 
    380370                Trie<String> fixture = new Trie<String>(); 
    381371                fixture.train(sequence, 3); 
     
    385375                assertEquals(7, result); 
    386376        } 
    387          
    388         @Test 
    389         public void testGetNumSymbols_1() 
    390                 throws Exception { 
     377 
     378        @Test 
     379        public void testGetNumSymbols_1() throws Exception { 
    391380                Trie<String> fixture = new Trie<String>(); 
    392381                fixture.train(sequence, 3); 
     
    398387 
    399388        @Test 
    400         public void testTrain_1() 
    401                 throws Exception { 
     389        public void testTrain_1() throws Exception { 
    402390                Trie<String> fixture = new Trie<String>(); 
    403391                int maxOrder = 3; 
    404392 
    405393                fixture.train(sequence, maxOrder); 
    406                  
     394 
    407395                // check if symbols are correct 
    408396                assertCollectionContent(symbols, fixture.getKnownSymbols()); 
    409                  
     397 
    410398                // check if counters are correct and only the correct nodes exist 
    411399                TrieNode<String> root = fixture.find(new ArrayList<String>()); 
     
    475463                TrieNode<String> root_r_d = root_r.getChild("d"); 
    476464                TrieNode<String> root_r_r = root_r.getChild("r"); 
    477                  
     465 
    478466                assertEquals(5, root_a.getCount()); 
    479467                assertNull(root_a_a); 
     
    497485                assertNull(root_a_d_r); 
    498486                assertNull(root_a_r); 
    499                  
     487 
    500488                assertEquals(2, root_b.getCount()); 
    501489                assertNull(root_b_a); 
     
    509497                assertNull(root_b_r_d); 
    510498                assertNull(root_b_r_r); 
    511                  
     499 
    512500                assertEquals(1, root_c.getCount()); 
    513501                assertEquals(1, root_c_a.getCount()); 
     
    521509                assertNull(root_c_d); 
    522510                assertNull(root_c_r); 
    523                  
     511 
    524512                assertEquals(1, root_d.getCount()); 
    525513                assertEquals(1, root_d_a.getCount()); 
     
    533521                assertNull(root_d_d); 
    534522                assertNull(root_d_r); 
    535                  
     523 
    536524                assertEquals(2, root_r.getCount()); 
    537525                assertEquals(2, root_r_a.getCount()); 
     
    545533                assertNull(root_r_d); 
    546534                assertNull(root_r_r); 
    547                  
     535 
    548536                // check if leafs are really leafs 
    549537                assertTrue(root_a_b_r.isLeaf()); 
     
    557545 
    558546        @Test 
    559         public void testTrain_2() 
    560                 throws Exception { 
     547        public void testTrain_2() throws Exception { 
    561548                Trie<String> fixture = new Trie<String>(); 
    562549                int maxOrder = 0; 
     
    568555 
    569556        @Test 
    570         public void testTrain_3() 
    571                 throws Exception { 
     557        public void testTrain_3() throws Exception { 
    572558                Trie<Object> fixture = new Trie<Object>(); 
    573559                List<Object> sequence = new ArrayList<Object>(); 
     
    575561 
    576562                fixture.train(sequence, maxOrder); 
    577                  
     563 
    578564                assertTrue(fixture.getKnownSymbols().isEmpty()); 
    579565        } 
    580566 
    581567        @Test 
    582         public void testTrain_4() 
    583                 throws Exception { 
     568        public void testTrain_4() throws Exception { 
    584569                Trie<String> fixture = new Trie<String>(); 
    585570                List<String> sequence = new ArrayList<String>(); 
     
    589574 
    590575                fixture.train(sequence, maxOrder); 
    591                  
     576 
    592577                assertCollectionContent(sequence, fixture.getKnownSymbols()); 
    593578                TrieNode<String> root = fixture.find(new ArrayList<String>()); 
     
    598583                TrieNode<String> root_b_a = root_b.getChild("a"); 
    599584                TrieNode<String> root_b_b = root_b.getChild("b"); 
    600                  
     585 
    601586                assertEquals(1, root_a.getCount()); 
    602587                assertNull(root_a_a); 
     
    605590                assertNull(root_b_a); 
    606591                assertNull(root_b_b); 
    607                  
     592 
    608593                assertTrue(root_a_b.isLeaf()); 
    609594                assertTrue(root_b.isLeaf()); 
    610595        } 
    611          
     596 
    612597        @Test 
    613598        public void testEdgeEdge_1() throws Exception { 
    614599                Edge result = new Edge(); 
    615                  
     600 
    616601                assertNotNull(result); 
    617602        } 
    618          
     603 
    619604        @Test 
    620605        public void testTrieVertexTrieVertex_1() throws Exception { 
    621606                String id = "idString"; 
    622                  
     607 
    623608                TrieVertex result = new TrieVertex(id); 
    624                  
     609 
    625610                assertNotNull(result); 
    626611        } 
    627          
     612 
    628613        @Test 
    629614        public void testTrieVertexToString_1() throws Exception { 
    630615                String id = "idString"; 
    631616                TrieVertex fixture = new TrieVertex(id); 
    632                  
     617 
    633618                String result = fixture.toString(); 
    634                  
     619 
    635620                assertEquals(id, result); 
    636621        } 
    637622 
     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 
    638699        @Before 
    639         public void setUp() 
    640                 throws Exception { 
     700        public void setUp() throws Exception { 
    641701                sequence = new ArrayList<String>(); 
    642702                sequence.add("a"); 
     
    651711                sequence.add("r"); 
    652712                sequence.add("a"); 
    653                  
     713 
    654714                symbols = new HashSet<String>(); 
    655715                symbols.add("a"); 
     
    660720        } 
    661721 
    662         @After 
    663         public void tearDown() 
    664                 throws Exception { 
    665                 // Add additional tear down code here 
    666         } 
    667  
    668722        public static void main(String[] args) { 
    669723                new org.junit.runner.JUnitCore().run(TrieTest.class); 
Note: See TracChangeset for help on using the changeset viewer.