-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashmap_unittest.py
More file actions
108 lines (82 loc) · 3.69 KB
/
Copy pathhashmap_unittest.py
File metadata and controls
108 lines (82 loc) · 3.69 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
import unittest
from hashmap import Hashmap
class HashmapTest(unittest.TestCase):
def setUp(self):
self.hashmap = Hashmap()
self.sayings = {
'Lannister' : 'A Lannister always pays his debts',
'Baratheon' : 'Ours is the Fury',
'Greyjoy' : 'We Do Dot Sow',
'Tyrell' : 'Growing Strong',
'Stark' : 'Winter is Coming',
'Tully' : 'Unbowed, Unbent, Unbroken',
'Bolton' : 'Our Blades are Sharp',
'Karstark': 'The Sun of Winter',
}
def fill_sayings(self):
for house, saying in self.sayings.items():
self.hashmap[house] = saying
def test_setting_getting(self):
self.assertEqual(self.hashmap['Lannister'], Hashmap.absent)
self.assertEqual(self.hashmap['Baratheon'], Hashmap.absent)
self.hashmap['Lannister'] = self.sayings['Lannister']
self.assertEqual(self.hashmap['Lannister'], self.sayings['Lannister'])
self.assertEqual(self.hashmap['Baratheon'], Hashmap.absent)
self.hashmap['Baratheon'] = self.sayings['Baratheon']
self.assertEqual(self.hashmap['Lannister'], self.sayings['Lannister'])
self.assertEqual(self.hashmap['Baratheon'], self.sayings['Baratheon'])
def test_resizing_set(self):
sayings = list(self.sayings.items())
for house, saying in sayings[:5]:
self.hashmap[house] = saying
self.assertEqual(self.hashmap[house], saying)
self.assertEqual(len(self.hashmap._backing), 8)
for house, saying in sayings[5:]:
self.hashmap[house] = saying
self.assertEqual(self.hashmap[house], saying)
self.assertEqual(len(self.hashmap._backing), 16)
def test_resizing_del(self):
self.fill_sayings()
houses = list(self.sayings.keys())
for house in houses[:5]:
del self.hashmap[house]
self.assertEqual(len(self.hashmap._backing), 16)
for house in houses[5:]:
del self.hashmap[house]
self.assertEqual(len(self.hashmap._backing), 8)
def test_replacing(self):
self.hashmap['Lannister'] = self.sayings['Lannister']
self.assertEqual(self.hashmap['Lannister'], self.sayings['Lannister'])
self.hashmap['Lannister'] = 'Hear Me Roar!'
self.assertEqual(self.hashmap['Lannister'], 'Hear Me Roar!')
def test_deleting(self):
self.hashmap['Baratheon'] = self.sayings['Baratheon']
del self.hashmap['Baratheon']
self.assertEqual(self.hashmap['Baratheon'], Hashmap.absent)
self.hashmap['Baratheon'] = self.sayings['Lannister']
self.assertEqual(self.hashmap['Baratheon'], self.sayings['Lannister'])
def test_deleting_non_existant(self):
with self.assertRaises(KeyError):
del self.hashmap['Baratheon']
def test_containing(self):
self.assertTrue('Lannister' not in self.hashmap)
self.hashmap['Lannister'] = self.sayings['Lannister']
self.assertTrue('Lannister' in self.hashmap)
del self.hashmap['Lannister']
self.assertTrue('Lannister' not in self.hashmap)
def test_len(self):
count = 0
for house, saying in self.sayings.items():
self.hashmap[house] = saying
count += 1
self.assertEqual(len(self.hashmap), count)
for house in self.sayings.keys():
del self.hashmap[house]
count -= 1
self.assertEqual(len(self.hashmap), count)
def test_iteration(self):
self.fill_sayings()
for house, saying in self.hashmap:
self.assertEqual(self.sayings[house], saying)
if __name__ == '__main__':
unittest.main()