-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_hash.py
More file actions
77 lines (58 loc) · 1.78 KB
/
test_hash.py
File metadata and controls
77 lines (58 loc) · 1.78 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
import pytest
from hashtable import HashTable
@pytest.fixture()
def word_list():
with open('/usr/share/dict/words') as fh:
words = fh.read()
words = words.split('\n')
return words
def test_init_default():
foo = HashTable()
assert foo.table_size == 8192
assert len(foo.hashtable) == 8192
def test_init_set_size():
foo = HashTable(size=4096)
assert foo.table_size == 4096
assert len(foo.hashtable) == 4096
def test_len():
foo = HashTable(size=1024)
empty_len = len(foo)
stuff = [('a', 'a'), ('b', 'b'), ('c', 'c')]
more = [('d', 'd'), ('e', 'e')]
foo.hashtable[0].extend(stuff)
foo.hashtable[500].extend(more)
filled_len = len(foo)
assert empty_len == 0
assert filled_len == 5
def test_set():
foo = HashTable(size=1024)
foo.set('foo', 'foo')
foo.set('spoofs', 'spoofs')
foo.set('utopia', 'utopia')
assert foo.hashtable[91][0] == ('foo', 'foo')
assert foo.hashtable[91][1] == ('spoofs', 'spoofs')
assert foo.hashtable[885][0] == ('utopia', 'utopia')
def test_set_wrong_type():
foo = HashTable()
with pytest.raises(TypeError):
foo.set(898, [838])
def test_get():
foo = HashTable(size=1024)
foo.hashtable[91].append(('foo', 'foo'))
foo.hashtable[91].append(('spoofs', 'spoofs'))
foo.hashtable[885].append(('utopia', 'utopia'))
assert foo.get('foo') == 'foo'
assert foo.get('spoofs') == 'spoofs'
assert foo.get('utopia') == 'utopia'
def test_get_missing_key():
foo = HashTable()
with pytest.raises(KeyError):
foo.get('bar')
def test_set_and_get_word_list(word_list):
foo = HashTable()
words = word_list
for word in words:
foo.set(word, word)
for word in words:
value = foo.get(word)
assert word == value