-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_bloom.py
203 lines (151 loc) · 5.8 KB
/
test_bloom.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# -*- coding: utf-8 -*-
from fastbloom_rs import BloomFilter, FilterBuilder
import os
def test_bloom_builder():
bloom = BloomFilter(100_000_000, 0.01)
bloom.add_bytes(b'hello')
bloom.add(87)
assert bloom.contains_bytes(b'hello')
assert bloom.contains('hello')
assert bloom.contains_int(87)
assert bloom.contains(87)
assert b'hello' in bloom
assert not bloom.contains_bytes(b'hello world')
assert not bloom.add_if_not_contains('hello2')
assert bloom.contains('hello2')
assert not bloom.add_if_not_contains(88)
assert bloom.contains(88)
bloom2 = BloomFilter.from_int_array(bloom.get_int_array(), bloom.hashes())
assert bloom2.contains_bytes(b'hello')
assert bloom2.contains('hello')
assert bloom2.contains_int(87)
assert bloom2.contains(87)
assert b'hello' in bloom2
assert not bloom2.contains_bytes(b'hello world')
bloom3 = BloomFilter.from_bytes(bloom.get_bytes(), bloom.hashes())
assert bloom3.contains_bytes(b'hello')
assert bloom3.contains('hello')
assert bloom3.contains_int(87)
assert bloom3.contains(87)
assert b'hello' in bloom3
assert not bloom3.contains_bytes(b'hello world')
builder = FilterBuilder(100_000_000, 0.01)
bloom4 = builder.build_bloom_filter()
bloom4.add_bytes(b'hello')
bloom4.add(87)
assert bloom4.contains_bytes(b'hello')
assert bloom4.contains('hello')
assert bloom4.contains_int(87)
assert bloom4.contains(87)
assert b'hello' in bloom4
assert not bloom4.contains_bytes(b'hello world')
def test_bloom_add():
bloom = BloomFilter(100_000_000, 0.01)
for data in range(0, 10_000_000):
bloom.add_int(data)
for data in range(0, 10_000_000):
assert data in bloom
assert not (1000_000_000 in bloom)
assert not ('hello' in bloom)
def test_bloom_estimate_set_cardinality():
bloom = BloomFilter(100_000_000, 0.01)
for data in range(0, 10_000_000):
bloom.add_int(data)
assert (bloom.estimate_set_cardinality() < 10_100_000) and (
bloom.estimate_set_cardinality() > 9_900_000)
def test_bloom_op():
bloom = BloomFilter(100_000_000, 0.001)
bloom.add_bytes(b'hello')
bloom.add(87)
assert b'hello' in bloom
assert 87 in bloom
bloom.clear()
assert not (b'hello' in bloom)
assert not (87 in bloom)
def test_bloom_union():
bloom = BloomFilter(100_000_000, 0.01)
bloom.add_bytes(b'hello')
assert bloom.contains_bytes(b'hello')
assert not bloom.contains(87)
bloom2 = BloomFilter(100_000_000, 0.01)
bloom2.add(87)
assert not bloom2.contains_bytes(b'hello')
assert bloom2.contains(87)
bloom.union(bloom2)
assert bloom.contains_bytes(b'hello')
assert bloom.contains(87)
def test_bloom_intersect():
bloom = BloomFilter(100_000_000, 0.01)
bloom.add_bytes(b'hello')
bloom.add(87)
assert bloom.contains_bytes(b'hello')
assert bloom.contains(87)
bloom2 = BloomFilter(100_000_000, 0.01)
bloom2.add(87)
assert not bloom2.contains_bytes(b'hello')
assert bloom2.contains(87)
bloom.intersect(bloom2)
assert not bloom.contains_bytes(b'hello')
assert bloom.contains(87)
def test_hash_indices():
bloom = BloomFilter(100_000_000, 0.01)
bloom.add_bytes(b'hello')
bloom.add_str("world")
bloom.add(87)
bloom2 = BloomFilter(100_000_000, 0.01)
bloom2.add_str("Yan Kun")
assert bloom.get_hash_indices(
b'hello') == bloom2.get_hash_indices(b'hello')
assert bloom.contains_hash_indices(bloom.get_hash_indices(b'hello'))
assert bloom.contains_hash_indices(bloom.get_hash_indices(87))
assert bloom.contains_hash_indices(bloom.get_hash_indices("world"))
assert not bloom.contains_hash_indices(bloom.get_hash_indices("Yan Kun"))
assert not bloom2.contains_hash_indices(bloom2.get_hash_indices(b'hello'))
assert not bloom2.contains_hash_indices(bloom2.get_hash_indices(87))
assert not bloom2.contains_hash_indices(bloom2.get_hash_indices("world"))
assert bloom2.contains_hash_indices(bloom2.get_hash_indices("Yan Kun"))
def test_batch_check():
bloom = BloomFilter(100_000_000, 0.01)
inserts = [1, 2, 3, 4, 5, 6, 7, 9, 18, 68, 90, 100]
checks = [1, 2, 3, 4, 5, 6, 7, 9, 18, 68, 90, 100, 190, 290, 390]
results = [True, True, True, True, True, True, True,
True, True, True, True, True, False, False, False]
bloom.add_int_batch(inserts)
contains = bloom.contains_int_batch(checks)
assert contains == results
bloom.add_str_batch(list(map(lambda x: str(x), inserts)))
assert bloom.contains_str_batch(
list(map(lambda x: str(x), checks))) == results
bloom.add_bytes_batch(list(map(lambda x: bytes(x), inserts)))
assert bloom.contains_bytes_batch(
list(map(lambda x: bytes(x), checks))) == results
def test_save_load_hashes_file():
bloom = BloomFilter(1_000_000_000, 0.0001)
bloom.add_bytes(b'hello')
bloom.add(87)
assert bloom.contains_bytes(b'hello')
assert bloom.contains_int(87)
assert not bloom.contains('world')
bloom.save_to_file_with_hashes('fst.bloom')
del bloom # gc
bloom = BloomFilter.from_file_with_hashes('fst.bloom')
assert bloom.contains_bytes(b'hello')
assert bloom.contains_int(87)
assert not bloom.contains('world')
os.remove('fst.bloom')
def test_save_load_file():
hashes = 0
bloom = BloomFilter(1_000_000_000, 0.01)
bloom.add_bytes(b'hello')
bloom.add(87)
hashes = bloom.hashes()
assert bloom.contains_bytes(b'hello')
assert bloom.contains_int(87)
assert not bloom.contains('world')
bloom.save_to_file('fst.bloom')
del bloom # gc
bloom = BloomFilter.from_file('fst.bloom', hashes)
assert bloom.contains_bytes(b'hello')
assert bloom.contains_int(87)
assert not bloom.contains('world')
os.remove('fst.bloom')