Skip to content

Commit

Permalink
test(algorithm): add unittest to build_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
WokoLiu committed Oct 8, 2018
1 parent fd15bdc commit 0b901ad
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
10 changes: 8 additions & 2 deletions algorithm/build_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def need_md5(func):
"""如果需要对待处理值先md5一下再操作,就加上这个装饰器"""
@functools.wraps(func)
def wrapper(self, value):
if isinstance(value, int):
value = str(value)
if isinstance(value, str):
value = bytes(value, encoding='utf-8')
value = md5(value).hexdigest()
return func(self, value)
return wrapper
Expand Down Expand Up @@ -68,6 +72,8 @@ def hash_int(self, value):
return value

def hash_str(self, value):
if not value:
return 0
res = ''
for i in value:
res += str(ord(i))
Expand Down Expand Up @@ -101,8 +107,8 @@ def __init__(self, n):
super(SqrtMiddleHash, self).__init__(n=n)

def __middle(self, value):
length = len(value) / 2
return value[length-self.n/2:length+self.n/2+1]
length = len(value) // 2
return value[length-self.n//2:length+self.n//2+1]

def hash_int(self, value):
sqrt = str(value ** 2)
Expand Down
74 changes: 74 additions & 0 deletions tests/test_algorithm/test_build_hash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-
# @Time : 2018/10/8 23:20
# @Author : Woko
# @File : test_build_hash.py

import random
import string
from hashlib import md5
import pytest
from algorithm import build_hash


@pytest.fixture(scope='module')
def str_values():
res = set()
for _ in range(100):
res.add(''.join(random.sample(string.printable, random.randint(0, 100))))
return res


@pytest.fixture(scope='module')
def int_values():
return list(range(1000))


class RotateHashForTest(build_hash.Hash):
@build_hash.RotateHash()
def hash_int(self, value):
return value

@build_hash.RotateHash()
def hash_str(self, value):
return value


@pytest.fixture(
params=[build_hash.DirectHash(), build_hash.ModHash(997), build_hash.SqrtMiddleHash(4),
build_hash.FoldHash(5), build_hash.BitHash(31, 11), RotateHashForTest()],
ids=['DirectHash', 'ModHash', 'SqrtMiddleHash', 'FoldHash', 'BitHash', 'RotateHash'])
def hash_obj(request):
return request.param


class TestNeedMd5(object):
@pytest.fixture
def foo(self):
class Foo(object):
@build_hash.need_md5
def with_md5(self, value):
return value

return Foo()

def test_str(self, foo, str_values):
for i in str_values:
assert foo.with_md5(i) == md5(bytes(i, encoding='utf-8')).hexdigest()

def test_int(self, foo, int_values):
for i in int_values:
assert foo.with_md5(i) == md5(str(i).encode('utf-8')).hexdigest()


class TestHash(object):
def test_str(self, hash_obj, str_values):
str_res = set()
for i in str_values:
str_res.add(hash_obj.hash_str(i))
assert len(str_values) - len(str_res) < len(str_values) / 10

def test_int(self, hash_obj, int_values):
int_res = set()
for i in int_values:
int_res.add(hash_obj.hash_int(i))
assert len(int_values) - len(int_res) < len(int_values) / 10

0 comments on commit 0b901ad

Please sign in to comment.