4763.2.4
by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry. |
1 |
# Copyright (C) 2009, 2010 Canonical Ltd
|
4679.3.58
by John Arbash Meinel
Adding a StaticTupleInterner class. |
2 |
#
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
16 |
||
17 |
"""Tests for the StaticTupleInterned type."""
|
|
18 |
||
4679.3.60
by John Arbash Meinel
Start working on more of the C api for StaticTupleInterner. |
19 |
import sys |
20 |
||
4679.3.58
by John Arbash Meinel
Adding a StaticTupleInterner class. |
21 |
from bzrlib import ( |
22 |
errors, |
|
23 |
osutils, |
|
24 |
tests, |
|
25 |
)
|
|
26 |
||
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
27 |
try: |
28 |
from bzrlib import _simple_set_pyx |
|
29 |
except ImportError: |
|
30 |
_simple_set_pyx = None |
|
4679.3.58
by John Arbash Meinel
Adding a StaticTupleInterner class. |
31 |
|
32 |
||
4679.3.89
by John Arbash Meinel
Switch to using a _Hashable class, rather than using tuples. |
33 |
class _Hashable(object): |
34 |
"""A simple object which has a fixed hash value.
|
|
35 |
||
36 |
We could have used an 'int', but it turns out that Int objects don't
|
|
37 |
implement tp_richcompare...
|
|
38 |
"""
|
|
39 |
||
40 |
def __init__(self, the_hash): |
|
41 |
self.hash = the_hash |
|
42 |
||
43 |
def __hash__(self): |
|
44 |
return self.hash |
|
45 |
||
46 |
def __eq__(self, other): |
|
47 |
if not isinstance(other, _Hashable): |
|
48 |
return NotImplemented |
|
49 |
return other.hash == self.hash |
|
50 |
||
4679.3.90
by John Arbash Meinel
A bit more error checking in _is_equal. |
51 |
|
52 |
class _BadSecondHash(_Hashable): |
|
53 |
||
54 |
def __init__(self, the_hash): |
|
55 |
_Hashable.__init__(self, the_hash) |
|
56 |
self._first = True |
|
57 |
||
58 |
def __hash__(self): |
|
59 |
if self._first: |
|
60 |
self._first = False |
|
61 |
return self.hash |
|
62 |
else: |
|
63 |
raise ValueError('I can only be hashed once.') |
|
64 |
||
65 |
||
66 |
class _BadCompare(_Hashable): |
|
67 |
||
68 |
def __eq__(self, other): |
|
69 |
raise RuntimeError('I refuse to play nice') |
|
70 |
||
71 |
||
4744.1.1
by John Arbash Meinel
Add a test case for the bug w/ NotImplemented. |
72 |
class _NoImplementCompare(_Hashable): |
73 |
||
74 |
def __eq__(self, other): |
|
75 |
return NotImplemented |
|
76 |
||
77 |
||
4679.3.58
by John Arbash Meinel
Adding a StaticTupleInterner class. |
78 |
# Even though this is an extension, we don't permute the tests for a python
|
4679.3.76
by John Arbash Meinel
Rename StaticTupleInterner => SimpleSet. |
79 |
# version. As the plain python version is just a dict or set
|
4913.2.20
by John Arbash Meinel
Change all of the compiled_foo to compiled_foo_feature |
80 |
compiled_simpleset_feature = tests.ModuleAvailableFeature( |
81 |
'bzrlib._simple_set_pyx') |
|
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
82 |
|
83 |
||
84 |
class TestSimpleSet(tests.TestCase): |
|
85 |
||
4913.2.20
by John Arbash Meinel
Change all of the compiled_foo to compiled_foo_feature |
86 |
_test_needs_features = [compiled_simpleset_feature] |
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
87 |
module = _simple_set_pyx |
4679.3.58
by John Arbash Meinel
Adding a StaticTupleInterner class. |
88 |
|
89 |
def assertIn(self, obj, container): |
|
90 |
self.assertTrue(obj in container, |
|
91 |
'%s not found in %s' % (obj, container)) |
|
92 |
||
93 |
def assertNotIn(self, obj, container): |
|
94 |
self.assertTrue(obj not in container, |
|
95 |
'We found %s in %s' % (obj, container)) |
|
96 |
||
4679.3.60
by John Arbash Meinel
Start working on more of the C api for StaticTupleInterner. |
97 |
def assertFillState(self, used, fill, mask, obj): |
98 |
self.assertEqual((used, fill, mask), (obj.used, obj.fill, obj.mask)) |
|
99 |
||
4679.3.91
by John Arbash Meinel
Change the _lookup function to use Quadratic Probing. |
100 |
def assertLookup(self, offset, value, obj, key): |
101 |
self.assertEqual((offset, value), obj._test_lookup(key)) |
|
102 |
||
4679.3.60
by John Arbash Meinel
Start working on more of the C api for StaticTupleInterner. |
103 |
def assertRefcount(self, count, obj): |
104 |
"""Assert that the refcount for obj is what we expect.
|
|
105 |
||
106 |
Note that this automatically adjusts for the fact that calling
|
|
107 |
assertRefcount actually creates a new pointer, as does calling
|
|
108 |
sys.getrefcount. So pass the expected value *before* the call.
|
|
109 |
"""
|
|
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
110 |
# I'm not sure why the offset is 3, but I've check that in the caller,
|
111 |
# an offset of 1 works, which is expected. Not sure why assertRefcount
|
|
112 |
# is incrementing/decrementing 2 times
|
|
113 |
self.assertEqual(count, sys.getrefcount(obj)-3) |
|
4679.3.60
by John Arbash Meinel
Start working on more of the C api for StaticTupleInterner. |
114 |
|
4679.3.58
by John Arbash Meinel
Adding a StaticTupleInterner class. |
115 |
def test_initial(self): |
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
116 |
obj = self.module.SimpleSet() |
4679.3.58
by John Arbash Meinel
Adding a StaticTupleInterner class. |
117 |
self.assertEqual(0, len(obj)) |
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
118 |
st = ('foo', 'bar') |
4679.3.60
by John Arbash Meinel
Start working on more of the C api for StaticTupleInterner. |
119 |
self.assertFillState(0, 0, 0x3ff, obj) |
4679.3.58
by John Arbash Meinel
Adding a StaticTupleInterner class. |
120 |
|
121 |
def test__lookup(self): |
|
4679.3.89
by John Arbash Meinel
Switch to using a _Hashable class, rather than using tuples. |
122 |
# These are carefully chosen integers to force hash collisions in the
|
123 |
# algorithm, based on the initial set size of 1024
|
|
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
124 |
obj = self.module.SimpleSet() |
4679.3.91
by John Arbash Meinel
Change the _lookup function to use Quadratic Probing. |
125 |
self.assertLookup(643, '<null>', obj, _Hashable(643)) |
126 |
self.assertLookup(643, '<null>', obj, _Hashable(643 + 1024)) |
|
127 |
self.assertLookup(643, '<null>', obj, _Hashable(643 + 50*1024)) |
|
128 |
||
129 |
def test__lookup_collision(self): |
|
130 |
obj = self.module.SimpleSet() |
|
131 |
k1 = _Hashable(643) |
|
132 |
k2 = _Hashable(643 + 1024) |
|
133 |
self.assertLookup(643, '<null>', obj, k1) |
|
134 |
self.assertLookup(643, '<null>', obj, k2) |
|
135 |
obj.add(k1) |
|
136 |
self.assertLookup(643, k1, obj, k1) |
|
137 |
self.assertLookup(644, '<null>', obj, k2) |
|
138 |
||
139 |
def test__lookup_after_resize(self): |
|
140 |
obj = self.module.SimpleSet() |
|
141 |
k1 = _Hashable(643) |
|
142 |
k2 = _Hashable(643 + 1024) |
|
143 |
obj.add(k1) |
|
144 |
obj.add(k2) |
|
145 |
self.assertLookup(643, k1, obj, k1) |
|
146 |
self.assertLookup(644, k2, obj, k2) |
|
147 |
obj._py_resize(2047) # resized to 2048 |
|
148 |
self.assertEqual(2048, obj.mask + 1) |
|
149 |
self.assertLookup(643, k1, obj, k1) |
|
150 |
self.assertLookup(643+1024, k2, obj, k2) |
|
151 |
obj._py_resize(1023) # resized back to 1024 |
|
152 |
self.assertEqual(1024, obj.mask + 1) |
|
153 |
self.assertLookup(643, k1, obj, k1) |
|
154 |
self.assertLookup(644, k2, obj, k2) |
|
4679.3.58
by John Arbash Meinel
Adding a StaticTupleInterner class. |
155 |
|
156 |
def test_get_set_del_with_collisions(self): |
|
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
157 |
obj = self.module.SimpleSet() |
4679.3.89
by John Arbash Meinel
Switch to using a _Hashable class, rather than using tuples. |
158 |
|
159 |
h1 = 643 |
|
160 |
h2 = 643 + 1024 |
|
161 |
h3 = 643 + 1024*50 |
|
162 |
h4 = 643 + 1024*25 |
|
4679.3.91
by John Arbash Meinel
Change the _lookup function to use Quadratic Probing. |
163 |
h5 = 644 |
164 |
h6 = 644 + 1024 |
|
4679.3.89
by John Arbash Meinel
Switch to using a _Hashable class, rather than using tuples. |
165 |
|
166 |
k1 = _Hashable(h1) |
|
167 |
k2 = _Hashable(h2) |
|
168 |
k3 = _Hashable(h3) |
|
169 |
k4 = _Hashable(h4) |
|
4679.3.91
by John Arbash Meinel
Change the _lookup function to use Quadratic Probing. |
170 |
k5 = _Hashable(h5) |
171 |
k6 = _Hashable(h6) |
|
172 |
self.assertLookup(643, '<null>', obj, k1) |
|
173 |
self.assertLookup(643, '<null>', obj, k2) |
|
174 |
self.assertLookup(643, '<null>', obj, k3) |
|
175 |
self.assertLookup(643, '<null>', obj, k4) |
|
176 |
self.assertLookup(644, '<null>', obj, k5) |
|
177 |
self.assertLookup(644, '<null>', obj, k6) |
|
4679.3.60
by John Arbash Meinel
Start working on more of the C api for StaticTupleInterner. |
178 |
obj.add(k1) |
4679.3.58
by John Arbash Meinel
Adding a StaticTupleInterner class. |
179 |
self.assertIn(k1, obj) |
180 |
self.assertNotIn(k2, obj) |
|
181 |
self.assertNotIn(k3, obj) |
|
182 |
self.assertNotIn(k4, obj) |
|
4679.3.91
by John Arbash Meinel
Change the _lookup function to use Quadratic Probing. |
183 |
self.assertLookup(643, k1, obj, k1) |
184 |
self.assertLookup(644, '<null>', obj, k2) |
|
185 |
self.assertLookup(644, '<null>', obj, k3) |
|
186 |
self.assertLookup(644, '<null>', obj, k4) |
|
187 |
self.assertLookup(644, '<null>', obj, k5) |
|
188 |
self.assertLookup(644, '<null>', obj, k6) |
|
4679.3.58
by John Arbash Meinel
Adding a StaticTupleInterner class. |
189 |
self.assertIs(k1, obj[k1]) |
4679.3.91
by John Arbash Meinel
Change the _lookup function to use Quadratic Probing. |
190 |
self.assertIs(k2, obj.add(k2)) |
4679.3.58
by John Arbash Meinel
Adding a StaticTupleInterner class. |
191 |
self.assertIs(k2, obj[k2]) |
4679.3.91
by John Arbash Meinel
Change the _lookup function to use Quadratic Probing. |
192 |
self.assertLookup(643, k1, obj, k1) |
193 |
self.assertLookup(644, k2, obj, k2) |
|
194 |
self.assertLookup(646, '<null>', obj, k3) |
|
195 |
self.assertLookup(646, '<null>', obj, k4) |
|
196 |
self.assertLookup(645, '<null>', obj, k5) |
|
197 |
self.assertLookup(645, '<null>', obj, k6) |
|
198 |
self.assertLookup(643, k1, obj, _Hashable(h1)) |
|
199 |
self.assertLookup(644, k2, obj, _Hashable(h2)) |
|
200 |
self.assertLookup(646, '<null>', obj, _Hashable(h3)) |
|
201 |
self.assertLookup(646, '<null>', obj, _Hashable(h4)) |
|
202 |
self.assertLookup(645, '<null>', obj, _Hashable(h5)) |
|
203 |
self.assertLookup(645, '<null>', obj, _Hashable(h6)) |
|
4679.3.60
by John Arbash Meinel
Start working on more of the C api for StaticTupleInterner. |
204 |
obj.add(k3) |
4679.3.58
by John Arbash Meinel
Adding a StaticTupleInterner class. |
205 |
self.assertIs(k3, obj[k3]) |
206 |
self.assertIn(k1, obj) |
|
207 |
self.assertIn(k2, obj) |
|
208 |
self.assertIn(k3, obj) |
|
209 |
self.assertNotIn(k4, obj) |
|
210 |
||
4679.3.88
by John Arbash Meinel
Some review comments from Andrew. |
211 |
obj.discard(k1) |
4679.3.91
by John Arbash Meinel
Change the _lookup function to use Quadratic Probing. |
212 |
self.assertLookup(643, '<dummy>', obj, k1) |
213 |
self.assertLookup(644, k2, obj, k2) |
|
214 |
self.assertLookup(646, k3, obj, k3) |
|
215 |
self.assertLookup(643, '<dummy>', obj, k4) |
|
4679.3.58
by John Arbash Meinel
Adding a StaticTupleInterner class. |
216 |
self.assertNotIn(k1, obj) |
217 |
self.assertIn(k2, obj) |
|
218 |
self.assertIn(k3, obj) |
|
219 |
self.assertNotIn(k4, obj) |
|
4679.3.60
by John Arbash Meinel
Start working on more of the C api for StaticTupleInterner. |
220 |
|
221 |
def test_add(self): |
|
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
222 |
obj = self.module.SimpleSet() |
4679.3.60
by John Arbash Meinel
Start working on more of the C api for StaticTupleInterner. |
223 |
self.assertFillState(0, 0, 0x3ff, obj) |
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
224 |
# We use this clumsy notation, because otherwise the refcounts are off.
|
225 |
# I'm guessing the python compiler sees it is a static tuple, and adds
|
|
226 |
# it to the function variables, or somesuch
|
|
227 |
k1 = tuple(['foo']) |
|
4679.3.60
by John Arbash Meinel
Start working on more of the C api for StaticTupleInterner. |
228 |
self.assertRefcount(1, k1) |
229 |
self.assertIs(k1, obj.add(k1)) |
|
230 |
self.assertFillState(1, 1, 0x3ff, obj) |
|
231 |
self.assertRefcount(2, k1) |
|
232 |
ktest = obj[k1] |
|
233 |
self.assertRefcount(3, k1) |
|
234 |
self.assertIs(k1, ktest) |
|
235 |
del ktest |
|
236 |
self.assertRefcount(2, k1) |
|
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
237 |
k2 = tuple(['foo']) |
4679.3.60
by John Arbash Meinel
Start working on more of the C api for StaticTupleInterner. |
238 |
self.assertRefcount(1, k2) |
239 |
self.assertIsNot(k1, k2) |
|
240 |
# doesn't add anything, so the counters shouldn't be adjusted
|
|
241 |
self.assertIs(k1, obj.add(k2)) |
|
242 |
self.assertFillState(1, 1, 0x3ff, obj) |
|
243 |
self.assertRefcount(2, k1) # not changed |
|
244 |
self.assertRefcount(1, k2) # not incremented |
|
245 |
self.assertIs(k1, obj[k1]) |
|
246 |
self.assertIs(k1, obj[k2]) |
|
247 |
self.assertRefcount(2, k1) |
|
248 |
self.assertRefcount(1, k2) |
|
249 |
# Deleting an entry should remove the fill, but not the used
|
|
4679.3.88
by John Arbash Meinel
Some review comments from Andrew. |
250 |
obj.discard(k1) |
4679.3.60
by John Arbash Meinel
Start working on more of the C api for StaticTupleInterner. |
251 |
self.assertFillState(0, 1, 0x3ff, obj) |
252 |
self.assertRefcount(1, k1) |
|
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
253 |
k3 = tuple(['bar']) |
4679.3.60
by John Arbash Meinel
Start working on more of the C api for StaticTupleInterner. |
254 |
self.assertRefcount(1, k3) |
255 |
self.assertIs(k3, obj.add(k3)) |
|
256 |
self.assertFillState(1, 2, 0x3ff, obj) |
|
257 |
self.assertRefcount(2, k3) |
|
258 |
self.assertIs(k2, obj.add(k2)) |
|
259 |
self.assertFillState(2, 2, 0x3ff, obj) |
|
260 |
self.assertRefcount(1, k1) |
|
261 |
self.assertRefcount(2, k2) |
|
262 |
self.assertRefcount(2, k3) |
|
263 |
||
264 |
def test_discard(self): |
|
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
265 |
obj = self.module.SimpleSet() |
266 |
k1 = tuple(['foo']) |
|
267 |
k2 = tuple(['foo']) |
|
268 |
k3 = tuple(['bar']) |
|
4679.3.60
by John Arbash Meinel
Start working on more of the C api for StaticTupleInterner. |
269 |
self.assertRefcount(1, k1) |
270 |
self.assertRefcount(1, k2) |
|
271 |
self.assertRefcount(1, k3) |
|
272 |
obj.add(k1) |
|
273 |
self.assertRefcount(2, k1) |
|
274 |
self.assertEqual(0, obj.discard(k3)) |
|
275 |
self.assertRefcount(1, k3) |
|
276 |
obj.add(k3) |
|
277 |
self.assertRefcount(2, k3) |
|
278 |
self.assertEqual(1, obj.discard(k3)) |
|
279 |
self.assertRefcount(1, k3) |
|
280 |
||
4679.3.63
by John Arbash Meinel
Implement resizing. |
281 |
def test__resize(self): |
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
282 |
obj = self.module.SimpleSet() |
283 |
k1 = ('foo',) |
|
284 |
k2 = ('bar',) |
|
285 |
k3 = ('baz',) |
|
4679.3.63
by John Arbash Meinel
Implement resizing. |
286 |
obj.add(k1) |
287 |
obj.add(k2) |
|
288 |
obj.add(k3) |
|
4679.3.88
by John Arbash Meinel
Some review comments from Andrew. |
289 |
obj.discard(k2) |
4679.3.63
by John Arbash Meinel
Implement resizing. |
290 |
self.assertFillState(2, 3, 0x3ff, obj) |
4679.3.81
by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again. |
291 |
self.assertEqual(1024, obj._py_resize(500)) |
4679.3.64
by John Arbash Meinel
Add functionality for shrinking the table. |
292 |
# Doesn't change the size, but does change the content
|
293 |
self.assertFillState(2, 2, 0x3ff, obj) |
|
294 |
obj.add(k2) |
|
4679.3.88
by John Arbash Meinel
Some review comments from Andrew. |
295 |
obj.discard(k3) |
4679.3.63
by John Arbash Meinel
Implement resizing. |
296 |
self.assertFillState(2, 3, 0x3ff, obj) |
4679.3.81
by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again. |
297 |
self.assertEqual(4096, obj._py_resize(4095)) |
4679.3.63
by John Arbash Meinel
Implement resizing. |
298 |
self.assertFillState(2, 2, 0xfff, obj) |
299 |
self.assertIn(k1, obj) |
|
4679.3.64
by John Arbash Meinel
Add functionality for shrinking the table. |
300 |
self.assertIn(k2, obj) |
301 |
self.assertNotIn(k3, obj) |
|
4679.3.63
by John Arbash Meinel
Implement resizing. |
302 |
obj.add(k2) |
303 |
self.assertIn(k2, obj) |
|
4679.3.88
by John Arbash Meinel
Some review comments from Andrew. |
304 |
obj.discard(k2) |
4679.3.63
by John Arbash Meinel
Implement resizing. |
305 |
self.assertEqual((591, '<dummy>'), obj._test_lookup(k2)) |
4679.3.64
by John Arbash Meinel
Add functionality for shrinking the table. |
306 |
self.assertFillState(1, 2, 0xfff, obj) |
4679.3.81
by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again. |
307 |
self.assertEqual(2048, obj._py_resize(1024)) |
4679.3.64
by John Arbash Meinel
Add functionality for shrinking the table. |
308 |
self.assertFillState(1, 1, 0x7ff, obj) |
4679.3.63
by John Arbash Meinel
Implement resizing. |
309 |
self.assertEqual((591, '<null>'), obj._test_lookup(k2)) |
310 |
||
4679.3.90
by John Arbash Meinel
A bit more error checking in _is_equal. |
311 |
def test_second_hash_failure(self): |
312 |
obj = self.module.SimpleSet() |
|
313 |
k1 = _BadSecondHash(200) |
|
314 |
k2 = _Hashable(200) |
|
315 |
# Should only call hash() one time
|
|
316 |
obj.add(k1) |
|
317 |
self.assertFalse(k1._first) |
|
318 |
self.assertRaises(ValueError, obj.add, k2) |
|
319 |
||
320 |
def test_richcompare_failure(self): |
|
321 |
obj = self.module.SimpleSet() |
|
322 |
k1 = _Hashable(200) |
|
323 |
k2 = _BadCompare(200) |
|
324 |
obj.add(k1) |
|
325 |
# Tries to compare with k1, fails
|
|
326 |
self.assertRaises(RuntimeError, obj.add, k2) |
|
327 |
||
4744.1.1
by John Arbash Meinel
Add a test case for the bug w/ NotImplemented. |
328 |
def test_richcompare_not_implemented(self): |
329 |
obj = self.module.SimpleSet() |
|
330 |
# Even though their hashes are the same, tp_richcompare returns
|
|
331 |
# NotImplemented, which means we treat them as not equal
|
|
332 |
k1 = _NoImplementCompare(200) |
|
333 |
k2 = _NoImplementCompare(200) |
|
334 |
self.assertLookup(200, '<null>', obj, k1) |
|
335 |
self.assertLookup(200, '<null>', obj, k2) |
|
336 |
self.assertIs(k1, obj.add(k1)) |
|
337 |
self.assertLookup(200, k1, obj, k1) |
|
338 |
self.assertLookup(201, '<null>', obj, k2) |
|
339 |
self.assertIs(k2, obj.add(k2)) |
|
340 |
self.assertIs(k1, obj[k1]) |
|
341 |
||
4679.3.64
by John Arbash Meinel
Add functionality for shrinking the table. |
342 |
def test_add_and_remove_lots_of_items(self): |
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
343 |
obj = self.module.SimpleSet() |
4679.3.63
by John Arbash Meinel
Implement resizing. |
344 |
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890' |
345 |
for i in chars: |
|
346 |
for j in chars: |
|
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
347 |
k = (i, j) |
4679.3.63
by John Arbash Meinel
Implement resizing. |
348 |
obj.add(k) |
349 |
num = len(chars)*len(chars) |
|
350 |
self.assertFillState(num, num, 0x1fff, obj) |
|
4679.3.64
by John Arbash Meinel
Add functionality for shrinking the table. |
351 |
# Now delete all of the entries and it should shrink again
|
352 |
for i in chars: |
|
353 |
for j in chars: |
|
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
354 |
k = (i, j) |
4679.3.64
by John Arbash Meinel
Add functionality for shrinking the table. |
355 |
obj.discard(k) |
356 |
# It should be back to 1024 wide mask, though there may still be some
|
|
357 |
# dummy values in there
|
|
358 |
self.assertFillState(0, obj.fill, 0x3ff, obj) |
|
359 |
# but there should be fewer than 1/5th dummy entries
|
|
360 |
self.assertTrue(obj.fill < 1024 / 5) |
|
4679.3.65
by John Arbash Meinel
Add __iter__ support. |
361 |
|
362 |
def test__iter__(self): |
|
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
363 |
obj = self.module.SimpleSet() |
364 |
k1 = ('1',) |
|
365 |
k2 = ('1', '2') |
|
366 |
k3 = ('3', '4') |
|
4679.3.65
by John Arbash Meinel
Add __iter__ support. |
367 |
obj.add(k1) |
368 |
obj.add(k2) |
|
369 |
obj.add(k3) |
|
370 |
all = set() |
|
371 |
for key in obj: |
|
372 |
all.add(key) |
|
373 |
self.assertEqual(sorted([k1, k2, k3]), sorted(all)) |
|
374 |
iterator = iter(obj) |
|
375 |
iterator.next() |
|
4679.3.85
by John Arbash Meinel
Remove everything except for SimpleSet, and clean up its test suite. |
376 |
obj.add(('foo',)) |
4679.3.65
by John Arbash Meinel
Add __iter__ support. |
377 |
# Set changed size
|
378 |
self.assertRaises(RuntimeError, iterator.next) |
|
379 |
# And even removing an item still causes it to fail
|
|
4679.3.88
by John Arbash Meinel
Some review comments from Andrew. |
380 |
obj.discard(k2) |
4679.3.65
by John Arbash Meinel
Add __iter__ support. |
381 |
self.assertRaises(RuntimeError, iterator.next) |