-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest_hashfns.py
More file actions
116 lines (100 loc) · 4.57 KB
/
test_hashfns.py
File metadata and controls
116 lines (100 loc) · 4.57 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
"""Hash function test module."""
#pylint: disable=no-self-use,protected-access
import io
import json
import os
import pytest
from splitio.engine import hashfns, splitters
from splitio.engine.hashfns.murmur3py import hash128_x64 as murmur3_128_py
from splitio.models import splits
class HashFunctionsTests(object):
"""Hash functions test cases."""
def test_get_hash_function(self):
"""Test that the correct hash function is returned."""
assert hashfns.get_hash_fn(splits.HashAlgorithm.LEGACY) == hashfns.legacy.legacy_hash
assert hashfns.get_hash_fn(splits.HashAlgorithm.MURMUR) == hashfns._murmur_hash
def test_legacy_hash_ascii_data(self):
"""Test legacy hash function against known results."""
splitter = splitters.Splitter()
file_name = os.path.join(os.path.dirname(__file__), 'files', 'sample-data.jsonl')
with open(file_name, 'r') as flo:
lines = flo.read().split('\n')
for line in lines:
if line is None or line == '':
continue
seed, key, hashed, bucket = json.loads(line)
assert hashfns.legacy.legacy_hash(key, seed) == hashed
assert splitter.get_bucket(key, seed, splits.HashAlgorithm.LEGACY) == bucket
def test_murmur_hash_ascii_data(self):
"""Test legacy hash function against known results."""
splitter = splitters.Splitter()
file_name = os.path.join(os.path.dirname(__file__), 'files', 'murmur3-sample-data-v2.csv')
with open(file_name, 'r') as flo:
lines = flo.read().split('\n')
for line in lines:
if line is None or line == '':
continue
seed, key, hashed, bucket = line.split(',')
seed = int(seed)
bucket = int(bucket)
hashed = int(hashed)
assert hashfns._murmur_hash(key, seed) == hashed
assert splitter.get_bucket(key, seed, splits.HashAlgorithm.MURMUR) == bucket
def test_murmur_more_ascii_data(self):
"""Test legacy hash function against known results."""
splitter = splitters.Splitter()
file_name = os.path.join(os.path.dirname(__file__), 'files', 'murmur3-custom-uuids.csv')
with open(file_name, 'r') as flo:
lines = flo.read().split('\n')
for line in lines:
if line is None or line == '':
continue
seed, key, hashed, bucket = line.split(',')
seed = int(seed)
bucket = int(bucket)
hashed = int(hashed)
assert hashfns._murmur_hash(key, seed) == hashed
assert splitter.get_bucket(key, seed, splits.HashAlgorithm.MURMUR) == bucket
def test_murmur_hash_non_ascii_data(self):
"""Test legacy hash function against known results."""
splitter = splitters.Splitter()
file_name = os.path.join(
os.path.dirname(__file__),
'files',
'murmur3-sample-data-non-alpha-numeric-v2.csv'
)
with io.open(file_name, 'r', encoding='utf-8') as flo:
lines = flo.read().split('\n')
for line in lines:
if line is None or line == '':
continue
seed, key, hashed, bucket = line.split(',')
seed = int(seed)
bucket = int(bucket)
hashed = int(hashed)
assert hashfns._murmur_hash(key, seed) == hashed
assert splitter.get_bucket(key, seed, splits.HashAlgorithm.MURMUR) == bucket
def test_murmur128(self):
"""Test legacy hash function against known results."""
file_name = os.path.join(os.path.dirname(__file__), 'files', 'murmur128_test_suite.csv')
with io.open(file_name, 'r', encoding='utf-8') as flo:
lines = flo.read().split('\n')
for line in lines:
if line is None or line == '':
continue
key, seed, hashed = line.split(',')
seed = int(seed)
hashed = int(hashed)
assert hashfns.murmur_128(key, seed) == hashed
def test_murmur128_pure_python(self):
"""Test legacy hash function against known results."""
file_name = os.path.join(os.path.dirname(__file__), 'files', 'murmur128_test_suite.csv')
with io.open(file_name, 'r', encoding='utf-8') as flo:
lines = flo.read().split('\n')
for line in lines:
if line is None or line == '':
continue
key, seed, hashed = line.split(',')
seed = int(seed)
hashed = int(hashed)
assert murmur3_128_py(key, seed)[0] == hashed