forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatriciaTrieTest.java
More file actions
76 lines (61 loc) · 2.3 KB
/
PatriciaTrieTest.java
File metadata and controls
76 lines (61 loc) · 2.3 KB
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.tries;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class PatriciaTrieTest {
@Test
void insert_splitCausesNewBranch_incrementsSize() {
var t = new PatriciaTrie<Integer>();
t.put("romane", 1);
assertEquals(1, t.size());
// Split "romane" at "roman", create branch "us"
t.put("romanus", 2);
assertEquals(2, t.size(), "Size should increment when a split creates a new key branch");
assertTrue(t.contains("romane"));
assertTrue(t.contains("romanus"));
assertEquals(1, t.get("romane"));
assertEquals(2, t.get("romanus"));
}
@Test
void basicPutGetContainsAndRemove() {
var t = new PatriciaTrie<String>();
assertTrue(t.isEmpty());
t.put("", "root"); // empty key
t.put("a", "x");
t.put("ab", "y");
t.put("abc", "z");
assertEquals(4, t.size());
assertEquals("root", t.get(""));
assertEquals("x", t.get("a"));
assertEquals("y", t.get("ab"));
assertEquals("z", t.get("abc"));
assertTrue(t.contains("ab"));
assertFalse(t.contains("abcd"));
assertTrue(t.startsWith("ab"));
assertTrue(t.startsWith("abc"));
assertFalse(t.startsWith("zzz"));
assertTrue(t.remove("ab"));
assertFalse(t.contains("ab"));
assertEquals(3, t.size());
// removing non-existent
assertFalse(t.remove("ab"));
assertEquals(3, t.size());
}
@Test
void updatesDoNotIncreaseSize() {
var t = new PatriciaTrie<Integer>();
t.put("apple", 1);
t.put("apple", 2);
assertEquals(1, t.size());
assertEquals(2, t.get("apple"));
}
@Test
void nullContracts() {
var t = new PatriciaTrie<Integer>();
assertThrows(IllegalArgumentException.class, () -> t.put(null, 1));
assertThrows(IllegalArgumentException.class, () -> t.put("x", null));
assertThrows(IllegalArgumentException.class, () -> t.get(null));
assertThrows(IllegalArgumentException.class, () -> t.contains(null));
assertThrows(IllegalArgumentException.class, () -> t.remove(null));
assertThrows(IllegalArgumentException.class, () -> t.startsWith(null));
}
}