File size: 4,651 Bytes
04f192c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | /**
* SPDX-FileCopyrightText: Copyright contributors to the CopilotJ project.
*
* SPDX-License-Identifier: Apache-2.0
*/
package copilotj.util;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import copilotj.util.Trie.CharTrie;
import copilotj.util.Trie.SentenceTrie;
import copilotj.util.Trie.WordTrie;
/**
* Unit tests for the pure-logic {@link Trie} data structure. {@link Trie} has no
* ImageJ/AWT/runtime coupling, so it can be exercised directly.
*/
// Raw Trie.Map / Trie.SimpleMap are inner generic types; unchecked access is by design here.
@SuppressWarnings("rawtypes")
public class TrieTest {
@Test
public void charTrieInsertSearchAndPrefix() {
final CharTrie trie = new CharTrie();
trie.insert("apple");
assertTrue(trie.search("apple"), "exact sentence should be found");
assertFalse(trie.search("app"), "non-leaf prefix should not satisfy search");
assertTrue(trie.startsWith("app"), "prefix should be detected");
assertFalse(trie.startsWith("b"), "unknown prefix");
assertFalse(new CharTrie().search("x"), "empty trie lookup handled");
}
@Test
public void charTrieSharedPrefixDoesNotInterfere() {
final CharTrie trie = new CharTrie();
trie.insert("app");
trie.insert("apple");
assertTrue(trie.search("app"));
assertTrue(trie.search("apple"));
assertFalse(trie.search("ap"));
}
@Test
public void duplicateInsertIsIdempotent() {
final CharTrie trie = new CharTrie();
trie.insert("abc");
trie.insert("abc");
assertTrue(trie.search("abc"));
}
@Test
public void wordTrieSearchAndPrefix() {
final WordTrie trie = new WordTrie();
trie.insert(new String[] {"a", "b", "c"});
assertTrue(trie.search(new String[] {"a", "b", "c"}));
assertFalse(trie.search(new String[] {"a", "b"}));
assertTrue(trie.startsWith(new String[] {"a", "b"}));
assertFalse(trie.startsWith(new String[] {"x"}));
}
@Test
public void wordTrieToMapWithFlattenCollapsesSingleChildChains() {
// root -> a -> b -> {c (leaf), d (leaf)}; the single-child a->b chain is
// flattened into the combined key "ab".
final WordTrie trie = new WordTrie();
trie.insert(new String[] {"a", "b", "c"});
trie.insert(new String[] {"a", "b", "d"});
final Trie.Map flattened = trie.toMapWithFlatten();
assertNotNull(flattened.children);
assertTrue(flattened.children.containsKey("ab"), "flattened single-child chain should produce key 'ab'");
final Trie.Map ab = (Trie.Map) flattened.children.get("ab");
assertNotNull(ab.children);
assertTrue(ab.children.containsKey("c"));
assertTrue(ab.children.containsKey("d"));
assertTrue(((Trie.Map) ab.children.get("c")).isLeaf, "leaf node c should be marked");
}
@Test
public void wordTrieToSimpleMapWithFlattenCollapsesSingleChildChains() {
// nodeToSimpleMap flattens single-child chains via a while-loop (distinct from
// toMap's single-edge flatten). This is the path that previously held a debug
// println, so it is covered explicitly here.
final WordTrie trie = new WordTrie();
trie.insert(new String[] {"a", "b", "c"});
trie.insert(new String[] {"a", "b", "d"});
final Trie.SimpleMap flattened = trie.toSimpleMapWithFlatten();
assertTrue(flattened.containsKey("ab"), "flattened single-child chain should produce key 'ab'");
final Trie.SimpleMap ab = (Trie.SimpleMap) flattened.get("ab");
assertTrue(ab.containsKey("c"));
assertTrue(ab.containsKey("d"));
}
@Test
public void sentenceTrieSplitsOnDelimiters() {
// Delimiters stick to the preceding token: "a/b.c" -> ["a/", "b.", "c"].
final SentenceTrie trie = new SentenceTrie(new char[] {'/', '.'});
trie.insert("a/b.c");
assertTrue(trie.search("a/b.c"), "full sentence reconstructs");
assertFalse(trie.search("a/b"), "trailing-delimiter token is significant");
assertTrue(trie.startsWith("a/"), "prefix token including its delimiter");
assertTrue(trie.startsWith("a/b."), "prefix across two tokens");
assertFalse(trie.startsWith("a/b"));
}
@Test
public void toMapWithFlattenRejectsNullCombiner() {
assertThrows(IllegalArgumentException.class, () -> new WordTrie().toMapWithFlatten(null));
}
@Test
public void toSimpleMapWithFlattenRejectsNullCombiner() {
assertThrows(IllegalArgumentException.class, () -> new WordTrie().toSimpleMapWithFlatten(null));
}
}
|