Skip to content

Commit 3446578

Browse files
committed
rename 'initialize' to 'reset' and add related tests
1 parent 70437e4 commit 3446578

3 files changed

Lines changed: 87 additions & 54 deletions

File tree

src/metrohash.cpp

Lines changed: 38 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/metrohash.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ cdef class MetroHash64(object):
165165
del self._m
166166
self._m = NULL
167167

168-
def initialize(self, uint64 seed=0ULL):
169-
"""Initialize object with a new seed"""
168+
def reset(self, uint64 seed=0ULL):
169+
"""Reset state with a new seed"""
170170
self._m.Initialize(seed)
171171

172172
def update(self, data):
@@ -222,8 +222,8 @@ cdef class MetroHash128(object):
222222
del self._m
223223
self._m = NULL
224224

225-
def initialize(self, uint64 seed=0ULL):
226-
"""Initialize object with a new seed"""
225+
def reset(self, uint64 seed=0ULL):
226+
"""Reset state with a new seed"""
227227
self._m.Initialize(seed)
228228

229229
def update(self, data):

tests/test_metrohash.py

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ def random_splits(s, n, nsplits=2):
3535
yield s[begin:end]
3636

3737

38-
class TestStandalone(unittest.TestCase):
38+
class TestAtomic(unittest.TestCase):
3939

40-
"""test single-line methods"""
40+
"""test atomic (functional) interface"""
4141

4242
def test_string_unicode_64(self):
4343
"""Empty Python string has same hash value as empty Unicode string
@@ -98,18 +98,10 @@ def test_func_raises_type_error(self):
9898
with self.assertRaises(TypeError):
9999
func([])
100100

101-
def test_obj_raises_type_error(self):
102-
"""Check that hasher objects raise type error"""
103-
hasher_classes = [MetroHash64, MetroHash128]
104-
for hasher_class in hasher_classes:
105-
hasher = hasher_class()
106-
with self.assertRaises(TypeError):
107-
hasher.update([])
108101

102+
class TestIncremental(unittest.TestCase):
109103

110-
class TestCombiners(unittest.TestCase):
111-
112-
"""test combiners"""
104+
"""test incremental hashers"""
113105

114106
def test_compose_64(self):
115107
"""Test various ways to split a string
@@ -154,3 +146,44 @@ def test_compose_128(self):
154146
whole = hasher2.intdigest()
155147
msg = "\ndata: %s\nwhole: %s\nincremental: %s\n" % (pieces, whole, incremental)
156148
self.assertEqual(whole, incremental, msg)
149+
150+
def test_obj_raises_type_error(self):
151+
"""Check that hasher objects raise type error
152+
"""
153+
hasher_classes = [MetroHash64, MetroHash128]
154+
for hasher_class in hasher_classes:
155+
hasher = hasher_class()
156+
with self.assertRaises(TypeError):
157+
hasher.update([])
158+
159+
def test_reset_64(self):
160+
"""test that 64-bit hasher can be reset"""
161+
162+
seed1 = 42
163+
expected1 = metrohash64("ab", seed=seed1)
164+
hasher = MetroHash64(seed1)
165+
hasher.update("a")
166+
hasher.update("b")
167+
self.assertEqual(hasher.intdigest(), expected1)
168+
169+
seed2 = 0
170+
hasher.reset(seed=seed2)
171+
expected2 = metrohash64("c", seed=seed2)
172+
hasher.update("c")
173+
self.assertEqual(hasher.intdigest(), expected2)
174+
175+
def test_reset_128(self):
176+
"""test that 128-bit hasher can be reset"""
177+
178+
seed1 = 42
179+
expected1 = metrohash128("ab", seed=seed1)
180+
hasher = MetroHash128(seed1)
181+
hasher.update("a")
182+
hasher.update("b")
183+
self.assertEqual(hasher.intdigest(), expected1)
184+
185+
seed2 = 0
186+
hasher.reset(seed=seed2)
187+
expected2 = metrohash128("c", seed=seed2)
188+
hasher.update("c")
189+
self.assertEqual(hasher.intdigest(), expected2)

0 commit comments

Comments
 (0)