Skip to content

Commit b655eba

Browse files
add case-insensitive tests
1 parent 4a65017 commit b655eba

File tree

1 file changed

+69
-3
lines changed

1 file changed

+69
-3
lines changed

enigma/src/test/java/org/quiltmc/enigma/util/multi_trie/CompositeStringMultiTrieTest.java

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.google.common.collect.ImmutableList;
44
import com.google.common.collect.ImmutableMultimap;
55
import org.junit.jupiter.api.Test;
6+
import org.opentest4j.AssertionFailedError;
67
import org.quiltmc.enigma.util.multi_trie.MultiTrie.Node;
8+
import org.quiltmc.enigma.util.multi_trie.StringMultiTrie.MutableCharacterNode;
79

810
import java.util.Collection;
911
import java.util.List;
@@ -22,13 +24,36 @@ public class CompositeStringMultiTrieTest {
2224

2325
private static final String KEY_BY_KEY_SUBJECT = "key-by-key subject";
2426

27+
private static final String IGNORE_CASE_SUBJECT = "aBrAcAdAnIeL";
28+
29+
@SuppressWarnings("SameParameterValue")
30+
private static String caseInverted(String string) {
31+
final StringBuilder builder = new StringBuilder();
32+
for (int i = 0; i < string.length(); i++) {
33+
final char c = string.charAt(i);
34+
35+
final char inverted;
36+
if (Character.isLowerCase(c)) {
37+
inverted = Character.toUpperCase(c);
38+
} else if (Character.isUpperCase(c)) {
39+
inverted = Character.toLowerCase(c);
40+
} else {
41+
inverted = c;
42+
}
43+
44+
builder.append(inverted);
45+
}
46+
47+
return builder.toString();
48+
}
49+
2550
// test key-by-key put's orphan logic
2651
@Test
2752
void testPutKeyByKeyFromRoot() {
2853
final CompositeStringMultiTrie<Integer> trie = CompositeStringMultiTrie.createHashed();
2954

3055
for (int depth = 0; depth < KEY_BY_KEY_SUBJECT.length(); depth++) {
31-
MutableMapNode<Character, Integer, ?> node = trie.getRoot();
56+
MutableCharacterNode<Integer, ?> node = trie.getRoot();
3257
for (int iKey = 0; iKey <= depth; iKey++) {
3358
node = node.next(KEY_BY_KEY_SUBJECT.charAt(iKey));
3459
}
@@ -47,7 +72,7 @@ void testPutKeyByKeyFromStems() {
4772
final CompositeStringMultiTrie<Integer> trie = CompositeStringMultiTrie.createHashed();
4873

4974
for (int depth = KEY_BY_KEY_SUBJECT.length() - 1; depth >= 0; depth--) {
50-
MutableMapNode<Character, Integer, ?> node = trie.getRoot();
75+
MutableCharacterNode<Integer, ?> node = trie.getRoot();
5176
for (int iKey = 0; iKey <= depth; iKey++) {
5277
node = node.next(KEY_BY_KEY_SUBJECT.charAt(iKey));
5378
}
@@ -60,7 +85,7 @@ void testPutKeyByKeyFromStems() {
6085
}
6186
}
6287

63-
private static void assertOneLeaf(MutableMapNode<Character, Integer, ?> node) {
88+
private static void assertOneLeaf(MutableCharacterNode<?, ?> node) {
6489
assertEquals(
6590
1, node.streamLeaves().count(),
6691
() -> "Expected node to have only one leaf, but had the following: " + node.streamLeaves().toList()
@@ -210,6 +235,47 @@ private static <T> void assertUnorderedContentsForPrefix(
210235
);
211236
}
212237

238+
@Test
239+
void testNextIgnoreCase() {
240+
final CompositeStringMultiTrie<String> trie = CompositeStringMultiTrie.createHashed();
241+
242+
trie.put(IGNORE_CASE_SUBJECT, IGNORE_CASE_SUBJECT);
243+
244+
final String invertedSubject = caseInverted(IGNORE_CASE_SUBJECT);
245+
MutableCharacterNode<String, ?> node = trie.getRoot();
246+
for (int i = 0; i < invertedSubject.length(); i++) {
247+
node = node.nextIgnoreCase(invertedSubject.charAt(i));
248+
249+
assertOneValue(node);
250+
}
251+
252+
assertOneLeaf(node);
253+
}
254+
255+
private static void assertOneValue(MutableCharacterNode<String, ?> node) {
256+
assertEquals(
257+
1, node.getSize(),
258+
"Expected node to have only one value, but had the following: " + node.streamValues().toList()
259+
);
260+
}
261+
262+
@Test
263+
void testGetIgnoreCase() {
264+
final CompositeStringMultiTrie<String> trie = CompositeStringMultiTrie.createHashed();
265+
266+
trie.put(IGNORE_CASE_SUBJECT, IGNORE_CASE_SUBJECT);
267+
268+
final String invertedSubject = caseInverted(IGNORE_CASE_SUBJECT);
269+
270+
final MutableCharacterNode<String, ?> node = trie.getIgnoreCase(invertedSubject);
271+
272+
assertOneValue(node);
273+
274+
node.streamLeaves()
275+
.findAny()
276+
.orElseThrow(() -> new AssertionFailedError("Expected node to have a leaf, but had none!"));
277+
}
278+
213279
record Association(String key) {
214280
static final Association EMPTY = new Association("");
215281

0 commit comments

Comments
 (0)