forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrieImpTest.java
76 lines (63 loc) · 2.7 KB
/
TrieImpTest.java
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
package com.thealgorithms.datastructures.trees;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class TrieImpTest {
private TrieImp trie;
@BeforeEach
public void setUp() {
trie = new TrieImp();
}
@Test
public void testInsertAndSearchBasic() {
String word = "hello";
trie.insert(word);
assertTrue(trie.search(word), "Search should return true for an inserted word.");
}
@Test
public void testSearchNonExistentWord() {
String word = "world";
assertFalse(trie.search(word), "Search should return false for a non-existent word.");
}
@Test
public void testInsertAndSearchMultipleWords() {
String word1 = "cat";
String word2 = "car";
trie.insert(word1);
trie.insert(word2);
assertTrue(trie.search(word1), "Search should return true for an inserted word.");
assertTrue(trie.search(word2), "Search should return true for another inserted word.");
assertFalse(trie.search("dog"), "Search should return false for a word not in the Trie.");
}
@Test
public void testDeleteExistingWord() {
String word = "remove";
trie.insert(word);
assertTrue(trie.delete(word), "Delete should return true for an existing word.");
assertFalse(trie.search(word), "Search should return false after deletion.");
}
@Test
public void testDeleteNonExistentWord() {
String word = "nonexistent";
assertFalse(trie.delete(word), "Delete should return false for a non-existent word.");
}
@Test
public void testInsertAndSearchPrefix() {
String prefix = "pre";
String word = "prefix";
trie.insert(prefix);
trie.insert(word);
assertTrue(trie.search(prefix), "Search should return true for an inserted prefix.");
assertTrue(trie.search(word), "Search should return true for a word with the prefix.");
assertFalse(trie.search("pref"), "Search should return false for a prefix that is not a full word.");
}
@Test
public void testIsValidWord() {
assertTrue(TrieImp.isValid("validword"), "Word should be valid (only lowercase letters).");
assertFalse(TrieImp.isValid("InvalidWord"), "Word should be invalid (contains uppercase letters).");
assertFalse(TrieImp.isValid("123abc"), "Word should be invalid (contains numbers).");
assertFalse(TrieImp.isValid("hello!"), "Word should be invalid (contains special characters).");
assertFalse(TrieImp.isValid(""), "Empty string should be invalid.");
}
}