-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
164 lines (142 loc) · 5.12 KB
/
test.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import unittest
import cuckoo
class TestCuckooHash(unittest.TestCase):
def setUp(self):
self.c = cuckoo.cuckoohash(10)
def testInsert(self):
# basic integer assignments
self.assertEqual(self.c.insert('a', 100), None)
self.c.delete('a')
# insert to a python object by direct value
self.c.insert('a', {'foo': 'bar'})
self.assertEqual(self.c.get('a'), {'foo': 'bar'})
self.c.delete('a')
# insert to a python object by reference
m = {'foo': 'bar'}
self.c.insert('a', m)
self.assertEqual(self.c.get('a'), {'foo': 'bar'})
self.assertEqual(self.c.get('a'), m)
self.c.delete('a')
# test reinserting the same value
self.c.insert('a', 'one')
self.assertEqual(self.c.get('a'), 'one')
x = self.c.insert('a', 'two')
self.assertEqual(x, None)
self.assertEqual(self.c.get('a'), 'two')
self.c.delete('a')
# test __ssetitem__
self.c['z'] = 'Foobar'
self.assertEqual(self.c.get('z'), 'Foobar')
def testDelete(self):
self.c = cuckoo.cuckoohash(10)
self.c.insert('a', 'Foobar')
self.assertEqual(self.c.has_key('a'), True)
self.c.delete('a')
self.assertEqual(self.c.has_key('a'), False)
# test __delitem__
self.c.insert('a', 'Foobar')
self.assertEqual(self.c.has_key('a'), True)
del self.c['a']
self.assertEqual(self.c.has_key('a'), False)
def testGet(self):
self.c.insert('a', 100)
self.assertEqual(self.c.get('a'), 100)
self.assertEqual(self.c['a'], 100)
def testLookup(self):
self.c.insert('a', 100)
self.assertEqual(self.c.lookup('a'), True)
self.c.delete('a')
self.assertEqual(self.c.lookup('a'), False)
def testHaskey(self):
self.c.insert('a', 100)
self.assertEqual(self.c.has_key('a'), True)
self.c.delete('a')
self.assertEqual(self.c.has_key('a'), False)
def testKeys(self):
self.c.insert('a', {'a': 100, 'b': 22})
self.c.insert('b', {'b': 'bar'})
self.c.insert('c', {'c': 10})
self.c.insert('d', {'d':20})
self.c.insert('e', {'e': 'g'})
k = list(self.c.keys())
k.sort()
keys = ['a', 'b', 'c', 'd', 'e']
self.assertEqual(k, keys)
def testValues(self):
self.c.insert('a', {'a': 100, 'b': 22})
self.c.insert('b', {'b': 'bar'})
self.c.insert('c', {'c': 10})
self.c.insert('d', {'d':20})
self.c.insert('e', {'e': 'g'})
v = list(self.c.values())
v.sort()
values = [{'b': 'bar'},
{'c': 10},
{'d': 20},
{'e': 'g'},
{'a': 100, 'b': 22}]
self.assertEqual(v, values)
def testSize(self):
del(self.c)
self.c = cuckoo.cuckoohash()
self.assertEqual(len(self.c), 0)
self.c.insert('a', {'a': 100, 'b': 22})
self.c.insert('b', {'b': 'bar'})
self.c.insert('c', {'c': 10})
self.c.insert('d', {'d':20})
self.c.insert('e', {'e': 'g'})
self.assertEqual(len(self.c), 5)
self.c.delete('a')
self.assertEqual(len(self.c), 4)
def testIteritems(self):
self.c = cuckoo.cuckoohash()
self.c.insert('a', {'a': 100, 'b': 22})
self.c.insert('b', {'b': 'bar'})
self.c.insert('c', {'c': 10})
self.c.insert('d', {'d':20})
self.c.insert('e', {'e': 'g'})
values = [{'b': {'b': 'bar'}},
{'c': {'c': 10}},
{'d': {'d': 20}},
{'e': {'e': 'g'}},
{'a': {'a': 100, 'b': 22}}]
for k, v in self.c.iteritems():
x = {k: v}
self.assertEqual(x in values, True)
values.remove(x)
self.assertEqual(len(values), 0)
def testIterkeys(self):
self.c = cuckoo.cuckoohash()
self.c.insert('a', {'a': 100, 'b': 22})
self.c.insert('b', {'b': 'bar'})
self.c.insert('c', {'c': 10})
self.c.insert('d', {'d':20})
self.c.insert('e', {'e': 'g'})
values = [{'b': {'b': 'bar'}},
{'c': {'c': 10}},
{'d': {'d': 20}},
{'e': {'e': 'g'}},
{'a': {'a': 100, 'b': 22}}]
keys = ['a', 'b', 'c', 'd', 'e']
for k in self.c.iterkeys():
self.assertEqual(k in keys, True)
keys.remove(k)
self.assertEqual(len(keys), 0)
def testItervalues(self):
self.c = cuckoo.cuckoohash()
self.c.insert('a', {'a': 100, 'b': 22})
self.c.insert('b', {'b': 'bar'})
self.c.insert('c', {'c': 10})
self.c.insert('d', {'d':20})
self.c.insert('e', {'e': 'g'})
values = [{'b': 'bar'},
{'c': 10},
{'d': 20},
{'e': 'g'},
{'a': 100, 'b': 22}]
for v in self.c.itervalues():
self.assertEqual(v in values, True)
values.remove(v)
self.assertEqual(len(values), 0)
if __name__ == '__main__':
unittest.main()