forked from yankun1992/fastbloom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_bloom.py
142 lines (106 loc) · 4.29 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
# -*- coding: utf-8 -*-
from fastbloom_rs import BloomFilter, FilterBuilder
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')
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_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