| 1 | package de.ugoe.cs.eventbench.models;
|
|---|
| 2 |
|
|---|
| 3 | import java.util.ArrayList;
|
|---|
| 4 | import java.util.Collection;
|
|---|
| 5 | import java.util.HashSet;
|
|---|
| 6 | import java.util.List;
|
|---|
| 7 |
|
|---|
| 8 | import junitx.framework.ListAssert;
|
|---|
| 9 |
|
|---|
| 10 | import org.junit.*;
|
|---|
| 11 |
|
|---|
| 12 | import de.ugoe.cs.eventbench.models.Trie.Edge;
|
|---|
| 13 | import de.ugoe.cs.eventbench.models.Trie.TrieVertex;
|
|---|
| 14 | import static org.junit.Assert.*;
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * The class <code>TrieTest</code> contains tests for the class
|
|---|
| 18 | * <code>{@link Trie}</code>.
|
|---|
| 19 | *
|
|---|
| 20 | * @author Steffen Herbold
|
|---|
| 21 | * @version 1.0
|
|---|
| 22 | */
|
|---|
| 23 | public class TrieTest {
|
|---|
| 24 |
|
|---|
| 25 | List<String> sequence;
|
|---|
| 26 | Collection<String> symbols;
|
|---|
| 27 |
|
|---|
| 28 | private static void assertCollectionContent(Collection<?> c1,
|
|---|
| 29 | Collection<?> c2) {
|
|---|
| 30 | assertEquals(c1.size(), c2.size());
|
|---|
| 31 | for (Object obj : c1) {
|
|---|
| 32 | assertTrue(c2.contains(obj));
|
|---|
| 33 | }
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | @Test
|
|---|
| 37 | public void testTrie_1() throws Exception {
|
|---|
| 38 |
|
|---|
| 39 | Trie<String> result = new Trie<String>();
|
|---|
| 40 |
|
|---|
| 41 | assertNotNull(result);
|
|---|
| 42 | assertEquals(0, result.getNumLeafs());
|
|---|
| 43 | assertEquals(0, result.getNumSymbols());
|
|---|
| 44 | assertEquals(0, result.getNumLeafAncestors());
|
|---|
| 45 | assertTrue(result.getKnownSymbols().isEmpty());
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | @Test
|
|---|
| 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 {
|
|---|
| 66 | Trie<String> fixture = new Trie<String>();
|
|---|
| 67 | List<String> seq = new ArrayList<String>();
|
|---|
| 68 | seq.add("a");
|
|---|
| 69 | seq.add("b");
|
|---|
| 70 |
|
|---|
| 71 | fixture.add(seq);
|
|---|
| 72 |
|
|---|
| 73 | assertEquals(1, fixture.getChild("a").getCount());
|
|---|
| 74 | assertEquals(1, fixture.getChild("a").getChild("b").getCount());
|
|---|
| 75 | assertNull(fixture.getChild("b"));
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | @Test
|
|---|
| 79 | public void testAdd_2() throws Exception {
|
|---|
| 80 | Trie<String> fixture = new Trie<String>();
|
|---|
| 81 |
|
|---|
| 82 | fixture.add(new ArrayList<String>());
|
|---|
| 83 |
|
|---|
| 84 | assertEquals(0, fixture.getNumSymbols());
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | @Test
|
|---|
| 88 | public void testAdd_3() throws Exception {
|
|---|
| 89 | Trie<String> fixture = new Trie<String>();
|
|---|
| 90 |
|
|---|
| 91 | fixture.add(null);
|
|---|
| 92 |
|
|---|
| 93 | assertEquals(0, fixture.getNumSymbols());
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | @Test
|
|---|
| 97 | public void testFind_1() throws Exception {
|
|---|
| 98 | Trie<String> fixture = new Trie<String>();
|
|---|
| 99 | fixture.train(sequence, 3);
|
|---|
| 100 | List<String> findSequence = new ArrayList<String>();
|
|---|
| 101 | findSequence.add("a");
|
|---|
| 102 | findSequence.add("b");
|
|---|
| 103 | findSequence.add("r");
|
|---|
| 104 | TrieNode<String> expected = fixture.getChild("a").getChild("b")
|
|---|
| 105 | .getChild("r");
|
|---|
| 106 |
|
|---|
| 107 | TrieNode<String> result = fixture.find(findSequence);
|
|---|
| 108 |
|
|---|
| 109 | assertEquals(expected, result);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | @Test
|
|---|
| 113 | public void testFind_2() throws Exception {
|
|---|
| 114 | Trie<String> fixture = new Trie<String>();
|
|---|
| 115 | fixture.train(sequence, 3);
|
|---|
| 116 | List<String> findSequence = new ArrayList<String>();
|
|---|
| 117 | findSequence.add("c");
|
|---|
| 118 | findSequence.add("a");
|
|---|
| 119 | TrieNode<String> expected = fixture.getChild("c").getChild("a");
|
|---|
| 120 |
|
|---|
| 121 | TrieNode<String> result = fixture.find(findSequence);
|
|---|
| 122 |
|
|---|
| 123 | assertEquals(expected, result);
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | @Test
|
|---|
| 127 | public void testFind_3() throws Exception {
|
|---|
| 128 | Trie<String> fixture = new Trie<String>();
|
|---|
| 129 | fixture.train(sequence, 3);
|
|---|
| 130 | List<String> findSequence = new ArrayList<String>();
|
|---|
| 131 |
|
|---|
| 132 | TrieNode<String> result = fixture.find(findSequence);
|
|---|
| 133 |
|
|---|
| 134 | assertTrue(result.isRoot());
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | @Test
|
|---|
| 138 | public void testFind_4() throws Exception {
|
|---|
| 139 | Trie<String> fixture = new Trie<String>();
|
|---|
| 140 | fixture.train(sequence, 3);
|
|---|
| 141 |
|
|---|
| 142 | TrieNode<String> result = fixture.find(null);
|
|---|
| 143 |
|
|---|
| 144 | assertTrue(result.isRoot());
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | @Test
|
|---|
| 148 | public void testGetChildCreate_1() throws Exception {
|
|---|
| 149 | Trie<String> fixture = new Trie<String>();
|
|---|
| 150 | String symbol = "a";
|
|---|
| 151 |
|
|---|
| 152 | TrieNode<String> result = fixture.getChildCreate(symbol);
|
|---|
| 153 |
|
|---|
| 154 | assertEquals(symbol, result.getSymbol());
|
|---|
| 155 | assertEquals(0, result.getCount());
|
|---|
| 156 | assertTrue(result.isLeaf());
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | @Test(expected = java.security.InvalidParameterException.class)
|
|---|
| 160 | public void testGetChildCreate_2() throws Exception {
|
|---|
| 161 | Trie<String> fixture = new Trie<String>();
|
|---|
| 162 | fixture.getChildCreate(null);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | @Test
|
|---|
| 166 | public void testGetContextSuffix_1() throws Exception {
|
|---|
| 167 | Trie<String> fixture = new Trie<String>();
|
|---|
| 168 | fixture.train(sequence, 3);
|
|---|
| 169 | List<String> context = new ArrayList<String>();
|
|---|
| 170 | context.add("a");
|
|---|
| 171 | context.add("a");
|
|---|
| 172 | context.add("b");
|
|---|
| 173 | List<String> expected = new ArrayList<String>();
|
|---|
| 174 | expected.add("a");
|
|---|
| 175 | expected.add("b");
|
|---|
| 176 |
|
|---|
| 177 | List<String> result = fixture.getContextSuffix(context);
|
|---|
| 178 |
|
|---|
| 179 | ListAssert.assertEquals(expected, result);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | @Test
|
|---|
| 183 | public void testGetContextSuffix_2() throws Exception {
|
|---|
| 184 | Trie<String> fixture = new Trie<String>();
|
|---|
| 185 | fixture.train(sequence, 3);
|
|---|
| 186 | List<String> context = new ArrayList<String>();
|
|---|
| 187 | context.add("a");
|
|---|
| 188 | context.add("a");
|
|---|
| 189 | context.add("b");
|
|---|
| 190 | context.add("r");
|
|---|
| 191 | List<String> expected = new ArrayList<String>();
|
|---|
| 192 | expected.add("b");
|
|---|
| 193 | expected.add("r");
|
|---|
| 194 |
|
|---|
| 195 | List<String> result = fixture.getContextSuffix(context);
|
|---|
| 196 |
|
|---|
| 197 | ListAssert.assertEquals(expected, result);
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | @Test
|
|---|
| 201 | public void testGetContextSuffix_3() throws Exception {
|
|---|
| 202 | Trie<String> fixture = new Trie<String>();
|
|---|
| 203 | fixture.train(sequence, 3);
|
|---|
| 204 | List<String> context = new ArrayList<String>();
|
|---|
| 205 | context.add("a");
|
|---|
| 206 | context.add("a");
|
|---|
| 207 | context.add("b");
|
|---|
| 208 | context.add("x");
|
|---|
| 209 | List<String> expected = new ArrayList<String>();
|
|---|
| 210 |
|
|---|
| 211 | List<String> result = fixture.getContextSuffix(context);
|
|---|
| 212 |
|
|---|
| 213 | ListAssert.assertEquals(expected, result);
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | @Test
|
|---|
| 217 | public void testGetContextSuffix_4() throws Exception {
|
|---|
| 218 | Trie<String> fixture = new Trie<String>();
|
|---|
| 219 |
|
|---|
| 220 | List<String> result = fixture.getContextSuffix(null);
|
|---|
| 221 |
|
|---|
| 222 | // add additional test code here
|
|---|
| 223 | assertNotNull(result);
|
|---|
| 224 | assertEquals(0, result.size());
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | @Test
|
|---|
| 228 | public void testGetContextSuffix_5() throws Exception {
|
|---|
| 229 | Trie<String> fixture = new Trie<String>();
|
|---|
| 230 | fixture.train(sequence, 3);
|
|---|
| 231 | List<String> context = new ArrayList<String>();
|
|---|
| 232 | context.add("a");
|
|---|
| 233 | context.add("a");
|
|---|
| 234 | context.add("b");
|
|---|
| 235 | List<String> expected = new ArrayList<String>();
|
|---|
| 236 | expected.add("a");
|
|---|
| 237 | expected.add("b");
|
|---|
| 238 |
|
|---|
| 239 | List<String> result = fixture.getContextSuffix(context);
|
|---|
| 240 |
|
|---|
| 241 | ListAssert.assertEquals(expected, result);
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | @Test
|
|---|
| 245 | public void testGetCount_1() throws Exception {
|
|---|
| 246 | Trie<String> fixture = new Trie<String>();
|
|---|
| 247 | fixture.train(sequence, 3);
|
|---|
| 248 | List<String> subSequence = new ArrayList<String>();
|
|---|
| 249 | subSequence.add("a");
|
|---|
| 250 |
|
|---|
| 251 | int result = fixture.getCount(subSequence);
|
|---|
| 252 |
|
|---|
| 253 | assertEquals(5, result);
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | @Test
|
|---|
| 257 | public void testGetCount_2() throws Exception {
|
|---|
| 258 | Trie<String> fixture = new Trie<String>();
|
|---|
| 259 | fixture.train(sequence, 3);
|
|---|
| 260 | List<String> subSequence = new ArrayList<String>();
|
|---|
| 261 | subSequence.add("a");
|
|---|
| 262 | subSequence.add("b");
|
|---|
| 263 |
|
|---|
| 264 | int result = fixture.getCount(subSequence);
|
|---|
| 265 |
|
|---|
| 266 | assertEquals(2, result);
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | @Test
|
|---|
| 270 | public void testGetCount_3() throws Exception {
|
|---|
| 271 | Trie<String> fixture = new Trie<String>();
|
|---|
| 272 | fixture.train(sequence, 3);
|
|---|
| 273 | List<String> subSequence = new ArrayList<String>();
|
|---|
| 274 | subSequence.add("x");
|
|---|
| 275 |
|
|---|
| 276 | int result = fixture.getCount(subSequence);
|
|---|
| 277 |
|
|---|
| 278 | assertEquals(0, result);
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | @Test
|
|---|
| 282 | public void testGetCount_4() throws Exception {
|
|---|
| 283 | Trie<String> fixture = new Trie<String>();
|
|---|
| 284 | fixture.train(sequence, 3);
|
|---|
| 285 | List<String> subSequence = new ArrayList<String>();
|
|---|
| 286 |
|
|---|
| 287 | int result = fixture.getCount(subSequence, "a");
|
|---|
| 288 |
|
|---|
| 289 | assertEquals(5, result);
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | @Test
|
|---|
| 293 | public void testGetCount_5() throws Exception {
|
|---|
| 294 | Trie<String> fixture = new Trie<String>();
|
|---|
| 295 | fixture.train(sequence, 3);
|
|---|
| 296 | List<String> subSequence = new ArrayList<String>();
|
|---|
| 297 | subSequence.add("a");
|
|---|
| 298 | subSequence.add("b");
|
|---|
| 299 |
|
|---|
| 300 | int result = fixture.getCount(subSequence, "r");
|
|---|
| 301 |
|
|---|
| 302 | assertEquals(2, result);
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | @Test
|
|---|
| 306 | public void testGetCount_6() throws Exception {
|
|---|
| 307 | Trie<String> fixture = new Trie<String>();
|
|---|
| 308 | fixture.train(sequence, 3);
|
|---|
| 309 | List<String> subSequence = new ArrayList<String>();
|
|---|
| 310 |
|
|---|
| 311 | int result = fixture.getCount(subSequence, "x");
|
|---|
| 312 |
|
|---|
| 313 | assertEquals(0, result);
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | @Test
|
|---|
| 317 | public void testGetFollowingSymbols_1() throws Exception {
|
|---|
| 318 | Trie<String> fixture = new Trie<String>();
|
|---|
| 319 | fixture.train(sequence, 3);
|
|---|
| 320 | List<String> subSequence = new ArrayList<String>();
|
|---|
| 321 | subSequence.add("a");
|
|---|
| 322 | Collection<String> expected = new ArrayList<String>();
|
|---|
| 323 | expected.add("b");
|
|---|
| 324 | expected.add("c");
|
|---|
| 325 | expected.add("d");
|
|---|
| 326 |
|
|---|
| 327 | Collection<String> result = fixture.getFollowingSymbols(subSequence);
|
|---|
| 328 |
|
|---|
| 329 | assertCollectionContent(expected, result);
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | @Test
|
|---|
| 333 | public void testGetFollowingSymbols_2() throws Exception {
|
|---|
| 334 | Trie<String> fixture = new Trie<String>();
|
|---|
| 335 | fixture.train(sequence, 3);
|
|---|
| 336 | List<String> subSequence = new ArrayList<String>();
|
|---|
| 337 | subSequence.add("a");
|
|---|
| 338 | subSequence.add("b");
|
|---|
| 339 | subSequence.add("r");
|
|---|
| 340 |
|
|---|
| 341 | Collection<String> result = fixture.getFollowingSymbols(subSequence);
|
|---|
| 342 |
|
|---|
| 343 | assertEquals(0, result.size());
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | @Test
|
|---|
| 347 | public void testGetFollowingSymbols_3() throws Exception {
|
|---|
| 348 | Trie<String> fixture = new Trie<String>();
|
|---|
| 349 | fixture.train(sequence, 3);
|
|---|
| 350 | List<String> subSequence = new ArrayList<String>();
|
|---|
| 351 | subSequence.add("x");
|
|---|
| 352 |
|
|---|
| 353 | Collection<String> result = fixture.getFollowingSymbols(subSequence);
|
|---|
| 354 |
|
|---|
| 355 | assertEquals(0, result.size());
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | @Test
|
|---|
| 359 | public void testGetNumLeafAncestors_1() throws Exception {
|
|---|
| 360 | Trie<String> fixture = new Trie<String>();
|
|---|
| 361 | fixture.train(sequence, 3);
|
|---|
| 362 |
|
|---|
| 363 | int result = fixture.getNumLeafAncestors();
|
|---|
| 364 |
|
|---|
| 365 | assertEquals(7, result);
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | @Test
|
|---|
| 369 | public void testGetNumLeafs_1() throws Exception {
|
|---|
| 370 | Trie<String> fixture = new Trie<String>();
|
|---|
| 371 | fixture.train(sequence, 3);
|
|---|
| 372 |
|
|---|
| 373 | int result = fixture.getNumLeafs();
|
|---|
| 374 |
|
|---|
| 375 | assertEquals(7, result);
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | @Test
|
|---|
| 379 | public void testGetNumSymbols_1() throws Exception {
|
|---|
| 380 | Trie<String> fixture = new Trie<String>();
|
|---|
| 381 | fixture.train(sequence, 3);
|
|---|
| 382 |
|
|---|
| 383 | int result = fixture.getNumSymbols();
|
|---|
| 384 |
|
|---|
| 385 | assertEquals(5, result);
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | @Test
|
|---|
| 389 | public void testTrain_1() throws Exception {
|
|---|
| 390 | Trie<String> fixture = new Trie<String>();
|
|---|
| 391 | int maxOrder = 3;
|
|---|
| 392 |
|
|---|
| 393 | fixture.train(sequence, maxOrder);
|
|---|
| 394 |
|
|---|
| 395 | // check if symbols are correct
|
|---|
| 396 | assertCollectionContent(symbols, fixture.getKnownSymbols());
|
|---|
| 397 |
|
|---|
| 398 | // check if counters are correct and only the correct nodes exist
|
|---|
| 399 | TrieNode<String> root = fixture.find(new ArrayList<String>());
|
|---|
| 400 | TrieNode<String> root_a = root.getChild("a");
|
|---|
| 401 | TrieNode<String> root_a_a = root_a.getChild("a");
|
|---|
| 402 | TrieNode<String> root_a_b = root_a.getChild("b");
|
|---|
| 403 | TrieNode<String> root_a_b_a = root_a_b.getChild("a");
|
|---|
| 404 | TrieNode<String> root_a_b_b = root_a_b.getChild("b");
|
|---|
| 405 | TrieNode<String> root_a_b_c = root_a_b.getChild("c");
|
|---|
| 406 | TrieNode<String> root_a_b_d = root_a_b.getChild("d");
|
|---|
| 407 | TrieNode<String> root_a_b_r = root_a_b.getChild("r");
|
|---|
| 408 | TrieNode<String> root_a_c = root_a.getChild("c");
|
|---|
| 409 | TrieNode<String> root_a_c_a = root_a_c.getChild("a");
|
|---|
| 410 | TrieNode<String> root_a_c_b = root_a_c.getChild("b");
|
|---|
| 411 | TrieNode<String> root_a_c_c = root_a_c.getChild("c");
|
|---|
| 412 | TrieNode<String> root_a_c_d = root_a_c.getChild("d");
|
|---|
| 413 | TrieNode<String> root_a_c_r = root_a_c.getChild("r");
|
|---|
| 414 | TrieNode<String> root_a_d = root_a.getChild("d");
|
|---|
| 415 | TrieNode<String> root_a_d_a = root_a_d.getChild("a");
|
|---|
| 416 | TrieNode<String> root_a_d_b = root_a_d.getChild("b");
|
|---|
| 417 | TrieNode<String> root_a_d_c = root_a_d.getChild("c");
|
|---|
| 418 | TrieNode<String> root_a_d_d = root_a_d.getChild("d");
|
|---|
| 419 | TrieNode<String> root_a_d_r = root_a_d.getChild("r");
|
|---|
| 420 | TrieNode<String> root_a_r = root_a.getChild("r");
|
|---|
| 421 | TrieNode<String> root_b = root.getChild("b");
|
|---|
| 422 | TrieNode<String> root_b_a = root_b.getChild("a");
|
|---|
| 423 | TrieNode<String> root_b_b = root_b.getChild("b");
|
|---|
| 424 | TrieNode<String> root_b_c = root_b.getChild("c");
|
|---|
| 425 | TrieNode<String> root_b_d = root_b.getChild("d");
|
|---|
| 426 | TrieNode<String> root_b_r = root_b.getChild("r");
|
|---|
| 427 | TrieNode<String> root_b_r_a = root_b_r.getChild("a");
|
|---|
| 428 | TrieNode<String> root_b_r_b = root_b_r.getChild("b");
|
|---|
| 429 | TrieNode<String> root_b_r_c = root_b_r.getChild("c");
|
|---|
| 430 | TrieNode<String> root_b_r_d = root_b_r.getChild("d");
|
|---|
| 431 | TrieNode<String> root_b_r_r = root_b_r.getChild("r");
|
|---|
| 432 | TrieNode<String> root_c = root.getChild("c");
|
|---|
| 433 | TrieNode<String> root_c_a = root_c.getChild("a");
|
|---|
| 434 | TrieNode<String> root_c_a_a = root_c_a.getChild("a");
|
|---|
| 435 | TrieNode<String> root_c_a_b = root_c_a.getChild("b");
|
|---|
| 436 | TrieNode<String> root_c_a_c = root_c_a.getChild("c");
|
|---|
| 437 | TrieNode<String> root_c_a_d = root_c_a.getChild("d");
|
|---|
| 438 | TrieNode<String> root_c_a_r = root_c_a.getChild("r");
|
|---|
| 439 | TrieNode<String> root_c_b = root_c.getChild("b");
|
|---|
| 440 | TrieNode<String> root_c_c = root_c.getChild("c");
|
|---|
| 441 | TrieNode<String> root_c_d = root_c.getChild("d");
|
|---|
| 442 | TrieNode<String> root_c_r = root_c.getChild("r");
|
|---|
| 443 | TrieNode<String> root_d = root.getChild("d");
|
|---|
| 444 | TrieNode<String> root_d_a = root_d.getChild("a");
|
|---|
| 445 | TrieNode<String> root_d_a_a = root_d_a.getChild("a");
|
|---|
| 446 | TrieNode<String> root_d_a_b = root_d_a.getChild("b");
|
|---|
| 447 | TrieNode<String> root_d_a_c = root_d_a.getChild("c");
|
|---|
| 448 | TrieNode<String> root_d_a_d = root_d_a.getChild("d");
|
|---|
| 449 | TrieNode<String> root_d_a_r = root_d_a.getChild("r");
|
|---|
| 450 | TrieNode<String> root_d_b = root_d.getChild("b");
|
|---|
| 451 | TrieNode<String> root_d_c = root_d.getChild("c");
|
|---|
| 452 | TrieNode<String> root_d_d = root_d.getChild("d");
|
|---|
| 453 | TrieNode<String> root_d_r = root_d.getChild("r");
|
|---|
| 454 | TrieNode<String> root_r = root.getChild("r");
|
|---|
| 455 | TrieNode<String> root_r_a = root_r.getChild("a");
|
|---|
| 456 | TrieNode<String> root_r_a_a = root_r_a.getChild("a");
|
|---|
| 457 | TrieNode<String> root_r_a_b = root_r_a.getChild("b");
|
|---|
| 458 | TrieNode<String> root_r_a_c = root_r_a.getChild("c");
|
|---|
| 459 | TrieNode<String> root_r_a_d = root_r_a.getChild("d");
|
|---|
| 460 | TrieNode<String> root_r_a_r = root_r_a.getChild("r");
|
|---|
| 461 | TrieNode<String> root_r_b = root_r.getChild("b");
|
|---|
| 462 | TrieNode<String> root_r_c = root_r.getChild("c");
|
|---|
| 463 | TrieNode<String> root_r_d = root_r.getChild("d");
|
|---|
| 464 | TrieNode<String> root_r_r = root_r.getChild("r");
|
|---|
| 465 |
|
|---|
| 466 | assertEquals(5, root_a.getCount());
|
|---|
| 467 | assertNull(root_a_a);
|
|---|
| 468 | assertEquals(2, root_a_b.getCount());
|
|---|
| 469 | assertNull(root_a_b_a);
|
|---|
| 470 | assertNull(root_a_b_b);
|
|---|
| 471 | assertNull(root_a_b_c);
|
|---|
| 472 | assertNull(root_a_b_d);
|
|---|
| 473 | assertEquals(2, root_a_b_r.getCount());
|
|---|
| 474 | assertEquals(1, root_a_c.getCount());
|
|---|
| 475 | assertEquals(1, root_a_c_a.getCount());
|
|---|
| 476 | assertNull(root_a_c_b);
|
|---|
| 477 | assertNull(root_a_c_c);
|
|---|
| 478 | assertNull(root_a_c_d);
|
|---|
| 479 | assertNull(root_a_c_r);
|
|---|
| 480 | assertEquals(1, root_a_d.getCount());
|
|---|
| 481 | assertEquals(1, root_a_d_a.getCount());
|
|---|
| 482 | assertNull(root_a_d_b);
|
|---|
| 483 | assertNull(root_a_d_c);
|
|---|
| 484 | assertNull(root_a_d_d);
|
|---|
| 485 | assertNull(root_a_d_r);
|
|---|
| 486 | assertNull(root_a_r);
|
|---|
| 487 |
|
|---|
| 488 | assertEquals(2, root_b.getCount());
|
|---|
| 489 | assertNull(root_b_a);
|
|---|
| 490 | assertNull(root_b_b);
|
|---|
| 491 | assertNull(root_b_c);
|
|---|
| 492 | assertNull(root_b_d);
|
|---|
| 493 | assertEquals(2, root_b_r.getCount());
|
|---|
| 494 | assertEquals(2, root_b_r_a.getCount());
|
|---|
| 495 | assertNull(root_b_r_b);
|
|---|
| 496 | assertNull(root_b_r_c);
|
|---|
| 497 | assertNull(root_b_r_d);
|
|---|
| 498 | assertNull(root_b_r_r);
|
|---|
| 499 |
|
|---|
| 500 | assertEquals(1, root_c.getCount());
|
|---|
| 501 | assertEquals(1, root_c_a.getCount());
|
|---|
| 502 | assertNull(root_c_a_a);
|
|---|
| 503 | assertNull(root_c_a_b);
|
|---|
| 504 | assertNull(root_c_a_c);
|
|---|
| 505 | assertEquals(1, root_c_a_d.getCount());
|
|---|
| 506 | assertNull(root_c_a_r);
|
|---|
| 507 | assertNull(root_c_b);
|
|---|
| 508 | assertNull(root_c_c);
|
|---|
| 509 | assertNull(root_c_d);
|
|---|
| 510 | assertNull(root_c_r);
|
|---|
| 511 |
|
|---|
| 512 | assertEquals(1, root_d.getCount());
|
|---|
| 513 | assertEquals(1, root_d_a.getCount());
|
|---|
| 514 | assertNull(root_d_a_a);
|
|---|
| 515 | assertEquals(1, root_d_a_b.getCount());
|
|---|
| 516 | assertNull(root_d_a_c);
|
|---|
| 517 | assertNull(root_d_a_d);
|
|---|
| 518 | assertNull(root_d_a_r);
|
|---|
| 519 | assertNull(root_d_b);
|
|---|
| 520 | assertNull(root_d_c);
|
|---|
| 521 | assertNull(root_d_d);
|
|---|
| 522 | assertNull(root_d_r);
|
|---|
| 523 |
|
|---|
| 524 | assertEquals(2, root_r.getCount());
|
|---|
| 525 | assertEquals(2, root_r_a.getCount());
|
|---|
| 526 | assertNull(root_r_a_a);
|
|---|
| 527 | assertNull(root_r_a_b);
|
|---|
| 528 | assertEquals(1, root_r_a_c.getCount());
|
|---|
| 529 | assertNull(root_r_a_d);
|
|---|
| 530 | assertNull(root_r_a_r);
|
|---|
| 531 | assertNull(root_r_b);
|
|---|
| 532 | assertNull(root_r_c);
|
|---|
| 533 | assertNull(root_r_d);
|
|---|
| 534 | assertNull(root_r_r);
|
|---|
| 535 |
|
|---|
| 536 | // check if leafs are really leafs
|
|---|
| 537 | assertTrue(root_a_b_r.isLeaf());
|
|---|
| 538 | assertTrue(root_a_c_a.isLeaf());
|
|---|
| 539 | assertTrue(root_a_d_a.isLeaf());
|
|---|
| 540 | assertTrue(root_b_r_a.isLeaf());
|
|---|
| 541 | assertTrue(root_c_a_d.isLeaf());
|
|---|
| 542 | assertTrue(root_d_a_b.isLeaf());
|
|---|
| 543 | assertTrue(root_r_a_c.isLeaf());
|
|---|
| 544 | }
|
|---|
| 545 |
|
|---|
| 546 | @Test
|
|---|
| 547 | public void testTrain_2() throws Exception {
|
|---|
| 548 | Trie<String> fixture = new Trie<String>();
|
|---|
| 549 | int maxOrder = 0;
|
|---|
| 550 |
|
|---|
| 551 | fixture.train(sequence, maxOrder);
|
|---|
| 552 |
|
|---|
| 553 | assertTrue(fixture.getKnownSymbols().isEmpty());
|
|---|
| 554 | }
|
|---|
| 555 |
|
|---|
| 556 | @Test
|
|---|
| 557 | public void testTrain_3() throws Exception {
|
|---|
| 558 | Trie<Object> fixture = new Trie<Object>();
|
|---|
| 559 | List<Object> sequence = new ArrayList<Object>();
|
|---|
| 560 | int maxOrder = 1;
|
|---|
| 561 |
|
|---|
| 562 | fixture.train(sequence, maxOrder);
|
|---|
| 563 |
|
|---|
| 564 | assertTrue(fixture.getKnownSymbols().isEmpty());
|
|---|
| 565 | }
|
|---|
| 566 |
|
|---|
| 567 | @Test
|
|---|
| 568 | public void testTrain_4() throws Exception {
|
|---|
| 569 | Trie<String> fixture = new Trie<String>();
|
|---|
| 570 | List<String> sequence = new ArrayList<String>();
|
|---|
| 571 | sequence.add("a");
|
|---|
| 572 | sequence.add("b");
|
|---|
| 573 | int maxOrder = 3;
|
|---|
| 574 |
|
|---|
| 575 | fixture.train(sequence, maxOrder);
|
|---|
| 576 |
|
|---|
| 577 | assertCollectionContent(sequence, fixture.getKnownSymbols());
|
|---|
| 578 | TrieNode<String> root = fixture.find(new ArrayList<String>());
|
|---|
| 579 | TrieNode<String> root_a = root.getChild("a");
|
|---|
| 580 | TrieNode<String> root_a_a = root_a.getChild("a");
|
|---|
| 581 | TrieNode<String> root_a_b = root_a.getChild("b");
|
|---|
| 582 | TrieNode<String> root_b = root.getChild("b");
|
|---|
| 583 | TrieNode<String> root_b_a = root_b.getChild("a");
|
|---|
| 584 | TrieNode<String> root_b_b = root_b.getChild("b");
|
|---|
| 585 |
|
|---|
| 586 | assertEquals(1, root_a.getCount());
|
|---|
| 587 | assertNull(root_a_a);
|
|---|
| 588 | assertEquals(1, root_a_b.getCount());
|
|---|
| 589 | assertEquals(1, root_b.getCount());
|
|---|
| 590 | assertNull(root_b_a);
|
|---|
| 591 | assertNull(root_b_b);
|
|---|
| 592 |
|
|---|
| 593 | assertTrue(root_a_b.isLeaf());
|
|---|
| 594 | assertTrue(root_b.isLeaf());
|
|---|
| 595 | }
|
|---|
| 596 |
|
|---|
| 597 | @Test
|
|---|
| 598 | public void testEdgeEdge_1() throws Exception {
|
|---|
| 599 | Edge result = new Edge();
|
|---|
| 600 |
|
|---|
| 601 | assertNotNull(result);
|
|---|
| 602 | }
|
|---|
| 603 |
|
|---|
| 604 | @Test
|
|---|
| 605 | public void testTrieVertexTrieVertex_1() throws Exception {
|
|---|
| 606 | String id = "idString";
|
|---|
| 607 |
|
|---|
| 608 | TrieVertex result = new TrieVertex(id);
|
|---|
| 609 |
|
|---|
| 610 | assertNotNull(result);
|
|---|
| 611 | }
|
|---|
| 612 |
|
|---|
| 613 | @Test
|
|---|
| 614 | public void testTrieVertexToString_1() throws Exception {
|
|---|
| 615 | String id = "idString";
|
|---|
| 616 | TrieVertex fixture = new TrieVertex(id);
|
|---|
| 617 |
|
|---|
| 618 | String result = fixture.toString();
|
|---|
| 619 |
|
|---|
| 620 | assertEquals(id, result);
|
|---|
| 621 | }
|
|---|
| 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 |
|
|---|
| 699 | @Before
|
|---|
| 700 | public void setUp() throws Exception {
|
|---|
| 701 | sequence = new ArrayList<String>();
|
|---|
| 702 | sequence.add("a");
|
|---|
| 703 | sequence.add("b");
|
|---|
| 704 | sequence.add("r");
|
|---|
| 705 | sequence.add("a");
|
|---|
| 706 | sequence.add("c");
|
|---|
| 707 | sequence.add("a");
|
|---|
| 708 | sequence.add("d");
|
|---|
| 709 | sequence.add("a");
|
|---|
| 710 | sequence.add("b");
|
|---|
| 711 | sequence.add("r");
|
|---|
| 712 | sequence.add("a");
|
|---|
| 713 |
|
|---|
| 714 | symbols = new HashSet<String>();
|
|---|
| 715 | symbols.add("a");
|
|---|
| 716 | symbols.add("b");
|
|---|
| 717 | symbols.add("c");
|
|---|
| 718 | symbols.add("d");
|
|---|
| 719 | symbols.add("r");
|
|---|
| 720 | }
|
|---|
| 721 |
|
|---|
| 722 | public static void main(String[] args) {
|
|---|
| 723 | new org.junit.runner.JUnitCore().run(TrieTest.class);
|
|---|
| 724 | }
|
|---|
| 725 | } |
|---|