This repository was archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathByteArrayKeyAnalyzerTest.java
More file actions
executable file
·116 lines (88 loc) · 3.16 KB
/
ByteArrayKeyAnalyzerTest.java
File metadata and controls
executable file
·116 lines (88 loc) · 3.16 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
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
package org.ardverk.collection;
import java.math.BigInteger;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import junit.framework.TestCase;
import org.junit.Test;
public class ByteArrayKeyAnalyzerTest {
private static final int SIZE = 20000;
@Test
public void bitSet() {
byte[] key = toByteArray("10100110", 2);
ByteArrayKeyAnalyzer ka = ByteArrayKeyAnalyzer.create(key.length);
TestCase.assertTrue(ka.isBitSet(key, 0));
TestCase.assertFalse(ka.isBitSet(key, 1));
TestCase.assertTrue(ka.isBitSet(key, 2));
TestCase.assertFalse(ka.isBitSet(key, 3));
TestCase.assertFalse(ka.isBitSet(key, 4));
TestCase.assertTrue(ka.isBitSet(key, 5));
TestCase.assertTrue(ka.isBitSet(key, 6));
TestCase.assertFalse(ka.isBitSet(key, 7));
}
@Test
public void keys() {
PatriciaTrie<byte[], BigInteger> trie
= new PatriciaTrie<byte[], BigInteger>(
ByteArrayKeyAnalyzer.CONSTANT);
Map<byte[], BigInteger> map
= new TreeMap<byte[], BigInteger>(
ByteArrayKeyAnalyzer.CONSTANT);
for (int i = 0; i < SIZE; i++) {
BigInteger value = BigInteger.valueOf(i);
byte[] key = toByteArray(value);
BigInteger existing = trie.put(key, value);
TestCase.assertNull(existing);
map.put(key, value);
}
TestCase.assertEquals(map.size(), trie.size());
for (byte[] key : map.keySet()) {
BigInteger expected = new BigInteger(1, key);
BigInteger value = trie.get(key);
TestCase.assertEquals(expected, value);
}
}
private static byte[] toByteArray(String value, int radix) {
return toByteArray(Long.parseLong(value, radix));
}
private static byte[] toByteArray(long value) {
return toByteArray(BigInteger.valueOf(value));
}
private static byte[] toByteArray(BigInteger value) {
byte[] src = value.toByteArray();
if (src.length <= 1) {
return src;
}
if (src[0] != 0) {
return src;
}
byte[] dst = new byte[src.length-1];
System.arraycopy(src, 1, dst, 0, dst.length);
return dst;
}
@Test
public void variableLength() {
Trie<String, String> trie1
= new PatriciaTrie<String, String>(
StringKeyAnalyzer.CHAR);
trie1.put("Hello", "Hello");
trie1.put("World", "World");
trie1.put("Alfred", "Alfred");
trie1.put("Anna", "Anna");
trie1.put("Anna-Marie", "Anna-Marie");
Trie<byte[], String> trie2
= new PatriciaTrie<byte[], String>(
ByteArrayKeyAnalyzer.VARIABLE);
for (Entry<String, String> entry : trie1.entrySet()) {
byte[] key = entry.getKey().getBytes();
String value = entry.getValue();
trie2.put(key, value);
}
TestCase.assertEquals("Anna", trie1.selectValue("An"));
TestCase.assertEquals("Anna", trie2.selectValue("An".getBytes()));
TestCase.assertEquals("Anna-Marie", trie1.selectValue("Anna-"));
TestCase.assertEquals("Anna-Marie", trie2.selectValue("Anna-".getBytes()));
TestCase.assertEquals("World", trie1.selectValue("x"));
TestCase.assertEquals("World", trie2.selectValue("x".getBytes()));
}
}