Example #1
0
  @Test
  public void testSimple() {
    Trie trie = instance();

    assertTrue(trie.add("abc"));
    assertTrue(trie.contains("abc"));
    assertEquals(1, trie.size());
    assertEquals(1, trie.howManyStartsWithPrefix("abc"));
  }
Example #2
0
  @Test
  public void testSimpleSerialization() throws IOException {
    Trie trie = instance();

    assertTrue(trie.add("abc"));
    assertTrue(trie.add("cde"));
    assertEquals(2, trie.size());

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ((StreamSerializable) trie).serialize(outputStream);

    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    Trie newTrie = instance();
    ((StreamSerializable) newTrie).deserialize(inputStream);

    assertTrue(newTrie.contains("abc"));
    assertTrue(newTrie.contains("cde"));
    assertEquals(2, trie.size());
  }
Example #3
0
  @org.junit.Test
  public void testAdd() throws Exception {
    String str1 = "cat";
    String str2 = "dog";

    Trie trie = new TrieImpl();
    assertTrue(trie.add(str1));
    assertEquals(1, trie.size());
    assertTrue(trie.add(str2));
    assertFalse(trie.add(str1));
    assertFalse(trie.add(str2));

    assertTrue(trie.add("ca"));
    assertTrue(trie.add("c"));
    assertTrue(trie.add(""));
  }
Example #4
0
  @org.junit.Test
  public void testRemove() throws Exception {
    Trie trie = new TrieImpl();
    trie.add("String");
    trie.add("Word");
    assertEquals(2, trie.size());
    assertFalse(trie.remove("YetWord"));
    assertEquals(2, trie.size());
    assertTrue(trie.remove("Word"));
    assertEquals(1, trie.size());
    assertFalse(trie.contains("Word"));

    trie.add("Word");
    trie.add("word");

    assertTrue(trie.remove("word"));
    assertEquals(2, trie.size());
    assertFalse(trie.contains("word"));

    assertFalse(trie.contains(""));
    assertFalse(trie.remove(""));
    assertEquals(2, trie.size());

    trie.add("");
    assertEquals(3, trie.size());
    assertTrue(trie.remove(""));
    assertEquals(2, trie.size());
    assertFalse(trie.contains(""));

    trie.remove("Word");
    trie.remove("String");
    assertEquals(0, trie.size());

    trie.add("Very");
    trie.add("VeryVery");
    trie.add("VeryVeryLong");
    trie.add("VeryVeryLongWord");

    assertEquals(4, trie.size());
    trie.remove("Very");
    assertEquals(3, trie.size());
    assertFalse(trie.contains("Very"));
    assertTrue(trie.contains("VeryVery"));

    trie.remove("VeryVery");
    assertEquals(2, trie.size());
    assertFalse(trie.contains("VeryVery"));
    assertTrue(trie.contains("VeryVeryLong"));

    trie.add("VeryVeryLongWord1");
    assertEquals(3, trie.size());
    trie.remove("VeryVeryLongWord");
    assertEquals(2, trie.size());
    assertTrue(trie.contains("VeryVeryLongWord1"));
  }
Example #5
0
  @org.junit.Test
  public void testSize() throws Exception {
    Trie trie = new TrieImpl();
    assertEquals(0, trie.size());
    trie.add("String");
    assertEquals(1, trie.size());
    trie.add("String2");
    assertEquals(2, trie.size());
    trie.add("Word");
    assertEquals(3, trie.size());

    trie.remove("str");
    assertEquals(3, trie.size());

    trie.remove("String");
    assertEquals(2, trie.size());

    trie.remove("String2");
    assertEquals(1, trie.size());

    trie.remove("Word");
    assertEquals(0, trie.size());

    trie.remove("Word");
    assertEquals(0, trie.size());

    trie.add("");
    assertEquals(1, trie.size());

    trie.add("");
    assertEquals(1, trie.size());

    trie.remove("");
    assertEquals(0, trie.size());
  }