forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBloomFilterTest.java
More file actions
68 lines (58 loc) · 1.87 KB
/
BloomFilterTest.java
File metadata and controls
68 lines (58 loc) · 1.87 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
package com.search;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
class BloomFilterTest {
@Test
void test() {
int count = 100000;
int low = 50, up = 100;
BloomFilter filter = BloomFilter.builder(10000).build();
String[] data = new String[count];
Set<String> dataSet = new HashSet<>();
for (int i = 0; i < count; i++) {
String str = randomString(low, up);
data[i] = str;
if (i % 2 == 0) {
dataSet.add(str);
filter.add(str);
}
}
int error = 0, total = 0;
for (int i = 0; i < count; i++) {
String str = data[i];
if (filter.contains(str)) {
total++;
if (!dataSet.contains(str)) {
error++;
}
} else {
Assertions.assertFalse(dataSet.contains(str));
}
}
System.out.println("error: " + error);
System.out.println("total: " + total);
System.out.println("error rate : " + (double) error / total);
}
private static String randomString(int minLength, int maxLength) {
ThreadLocalRandom r = ThreadLocalRandom.current();
int chLen = r.nextInt(minLength, maxLength),
poolSize = CHAR_POOL.length;
char[] chars = new char[chLen];
for (int i = 0; i < chLen; i++) {
chars[i] = CHAR_POOL[r.nextInt(poolSize)];
}
return new String(chars);
}
private static final char[] CHAR_POOL;
static {
CHAR_POOL = new char[52];
int i = 0;
for (char c = 'a'; c <= 'z'; c++) {
CHAR_POOL[i++] = c;
CHAR_POOL[i++] = (char) (c - 32);
}
}
}