bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
4679.3.1
by John Arbash Meinel
Start working on a Keys type. |
1 |
# Copyright (C) 2009 Canonical Ltd
|
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 |
||
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
17 |
"""Tests for the StaticTuple type."""
|
|
4679.3.1
by John Arbash Meinel
Start working on a Keys type. |
18 |
|
|
4759.2.16
by Matt Nordhoff
Add a test |
19 |
import cPickle |
|
4679.3.20
by John Arbash Meinel
Implement tp_traverse, and add a soft dependency on meliae to test it. |
20 |
import gc |
|
4679.3.2
by John Arbash Meinel
Add a get_key() function, which then returns tuples for the given items. |
21 |
import sys |
22 |
||
|
4679.3.1
by John Arbash Meinel
Start working on a Keys type. |
23 |
from bzrlib import ( |
|
4679.3.40
by John Arbash Meinel
Switch away from the name Key instead start branding as StaticTuple. |
24 |
_static_tuple_py, |
|
4668.3.1
by John Arbash Meinel
Fix bug #471193, allow tuples into the CHK code. |
25 |
debug, |
|
4679.3.1
by John Arbash Meinel
Start working on a Keys type. |
26 |
errors, |
|
4679.3.29
by John Arbash Meinel
Start work on implementing a Key.intern() function. |
27 |
osutils, |
|
4679.8.3
by John Arbash Meinel
Expose bzrlib.static_tuple.StaticTuple as a thunk |
28 |
static_tuple, |
|
4679.3.1
by John Arbash Meinel
Start working on a Keys type. |
29 |
tests, |
30 |
)
|
|
31 |
||
32 |
||
33 |
def load_tests(standard_tests, module, loader): |
|
34 |
"""Parameterize tests for all versions of groupcompress.""" |
|
35 |
scenarios = [ |
|
|
4679.3.40
by John Arbash Meinel
Switch away from the name Key instead start branding as StaticTuple. |
36 |
('python', {'module': _static_tuple_py}), |
|
4679.3.1
by John Arbash Meinel
Start working on a Keys type. |
37 |
]
|
38 |
suite = loader.suiteClass() |
|
|
4913.2.7
by John Arbash Meinel
Switch CompiledStaticTuple |
39 |
if compiled_static_tuple.available(): |
40 |
scenarios.append(('C', {'module': compiled_static_tuple.module})) |
|
|
4679.3.1
by John Arbash Meinel
Start working on a Keys type. |
41 |
else: |
42 |
# the compiled module isn't available, so we add a failing test
|
|
43 |
class FailWithoutFeature(tests.TestCase): |
|
44 |
def test_fail(self): |
|
|
4913.2.7
by John Arbash Meinel
Switch CompiledStaticTuple |
45 |
self.requireFeature(compiled_static_tuple) |
|
4679.3.1
by John Arbash Meinel
Start working on a Keys type. |
46 |
suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature)) |
47 |
result = tests.multiply_tests(standard_tests, scenarios, suite) |
|
48 |
return result |
|
49 |
||
50 |
||
|
4913.2.7
by John Arbash Meinel
Switch CompiledStaticTuple |
51 |
compiled_static_tuple = tests.ModuleAvailableFeature('bzrlib._static_tuple_c') |
|
4679.3.1
by John Arbash Meinel
Start working on a Keys type. |
52 |
|
53 |
||
|
4679.3.29
by John Arbash Meinel
Start work on implementing a Key.intern() function. |
54 |
class _Meliae(tests.Feature): |
55 |
||
56 |
def _probe(self): |
|
57 |
try: |
|
58 |
from meliae import scanner |
|
59 |
except ImportError: |
|
60 |
return False |
|
61 |
return True |
|
62 |
||
63 |
def feature_name(self): |
|
64 |
return "Meliae - python memory debugger" |
|
65 |
||
66 |
Meliae = _Meliae() |
|
67 |
||
68 |
||
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
69 |
class TestStaticTuple(tests.TestCase): |
|
4679.3.16
by John Arbash Meinel
Initial work for a Key class. |
70 |
|
|
4679.3.81
by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again. |
71 |
def assertRefcount(self, count, obj): |
72 |
"""Assert that the refcount for obj is what we expect. |
|
73 |
||
74 |
Note that this automatically adjusts for the fact that calling
|
|
75 |
assertRefcount actually creates a new pointer, as does calling
|
|
76 |
sys.getrefcount. So pass the expected value *before* the call.
|
|
77 |
"""
|
|
78 |
# I don't understand why it is getrefcount()-3 here, but it seems to be
|
|
79 |
# correct. If I check in the calling function, with:
|
|
80 |
# self.assertEqual(count, sys.getrefcount(obj)-1)
|
|
81 |
# Then it works fine. Something about passing it to assertRefcount is
|
|
82 |
# actually double-incrementing (and decrementing) the refcount
|
|
83 |
self.assertEqual(count, sys.getrefcount(obj)-3) |
|
84 |
||
|
4679.3.16
by John Arbash Meinel
Initial work for a Key class. |
85 |
def test_create(self): |
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
86 |
k = self.module.StaticTuple('foo') |
87 |
k = self.module.StaticTuple('foo', 'bar') |
|
|
4679.3.16
by John Arbash Meinel
Initial work for a Key class. |
88 |
|
89 |
def test_create_bad_args(self): |
|
|
4679.3.42
by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects. |
90 |
args_256 = ['a']*256 |
|
4679.3.16
by John Arbash Meinel
Initial work for a Key class. |
91 |
# too many args
|
|
4679.3.42
by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects. |
92 |
self.assertRaises(ValueError, self.module.StaticTuple, *args_256) |
93 |
args_300 = ['a']*300 |
|
94 |
self.assertRaises(ValueError, self.module.StaticTuple, *args_300) |
|
|
4679.3.16
by John Arbash Meinel
Initial work for a Key class. |
95 |
# not a string
|
|
4759.2.8
by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types. |
96 |
self.assertRaises(TypeError, self.module.StaticTuple, object()) |
|
4759.2.2
by John Arbash Meinel
Update _static_tuple_py.py with the same concatenation behavior |
97 |
|
98 |
def test_concat(self): |
|
99 |
st1 = self.module.StaticTuple('foo') |
|
100 |
st2 = self.module.StaticTuple('bar') |
|
101 |
st3 = self.module.StaticTuple('foo', 'bar') |
|
102 |
st4 = st1 + st2 |
|
103 |
self.assertEqual(st3, st4) |
|
104 |
self.assertIsInstance(st4, self.module.StaticTuple) |
|
105 |
||
106 |
def test_concat_with_tuple(self): |
|
107 |
st1 = self.module.StaticTuple('foo') |
|
108 |
t2 = ('bar',) |
|
109 |
st3 = self.module.StaticTuple('foo', 'bar') |
|
110 |
st4 = self.module.StaticTuple('bar', 'foo') |
|
111 |
st5 = st1 + t2 |
|
112 |
st6 = t2 + st1 |
|
113 |
self.assertEqual(st3, st5) |
|
114 |
self.assertIsInstance(st5, self.module.StaticTuple) |
|
115 |
self.assertEqual(st4, st6) |
|
116 |
if self.module is _static_tuple_py: |
|
117 |
# _static_tuple_py has StaticTuple(tuple), so tuple thinks it
|
|
118 |
# already knows how to concatenate, as such we can't "inject" our
|
|
119 |
# own concatenation...
|
|
120 |
self.assertIsInstance(st6, tuple) |
|
121 |
else: |
|
122 |
self.assertIsInstance(st6, self.module.StaticTuple) |
|
123 |
||
124 |
def test_concat_with_bad_tuple(self): |
|
125 |
st1 = self.module.StaticTuple('foo') |
|
126 |
t2 = (object(),) |
|
|
4759.2.11
by John Arbash Meinel
Review feedback from Andrew. |
127 |
# Using st1.__add__ doesn't give the same results as doing the '+' form
|
128 |
self.assertRaises(TypeError, lambda: st1 + t2) |
|
|
4759.2.5
by John Arbash Meinel
Add a bit more tests. |
129 |
|
130 |
def test_concat_with_non_tuple(self): |
|
131 |
st1 = self.module.StaticTuple('foo') |
|
|
4759.2.11
by John Arbash Meinel
Review feedback from Andrew. |
132 |
self.assertRaises(TypeError, lambda: st1 + 10) |
|
4679.3.16
by John Arbash Meinel
Initial work for a Key class. |
133 |
|
134 |
def test_as_tuple(self): |
|
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
135 |
k = self.module.StaticTuple('foo') |
|
4679.3.29
by John Arbash Meinel
Start work on implementing a Key.intern() function. |
136 |
t = k.as_tuple() |
|
4679.3.16
by John Arbash Meinel
Initial work for a Key class. |
137 |
self.assertEqual(('foo',), t) |
|
4789.28.3
by John Arbash Meinel
Add a static_tuple.as_tuples() helper. |
138 |
self.assertIsInstance(t, tuple) |
139 |
self.assertFalse(isinstance(t, self.module.StaticTuple)) |
|
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
140 |
k = self.module.StaticTuple('foo', 'bar') |
|
4679.3.29
by John Arbash Meinel
Start work on implementing a Key.intern() function. |
141 |
t = k.as_tuple() |
|
4679.3.16
by John Arbash Meinel
Initial work for a Key class. |
142 |
self.assertEqual(('foo', 'bar'), t) |
|
4789.28.3
by John Arbash Meinel
Add a static_tuple.as_tuples() helper. |
143 |
k2 = self.module.StaticTuple(1, k) |
144 |
t = k2.as_tuple() |
|
145 |
self.assertIsInstance(t, tuple) |
|
146 |
# For pickling to work, we need to keep the sub-items as StaticTuple so
|
|
147 |
# that it knows that they also need to be converted.
|
|
148 |
self.assertIsInstance(t[1], self.module.StaticTuple) |
|
149 |
self.assertEqual((1, ('foo', 'bar')), t) |
|
150 |
||
151 |
def test_as_tuples(self): |
|
152 |
k1 = self.module.StaticTuple('foo', 'bar') |
|
153 |
t = static_tuple.as_tuples(k1) |
|
154 |
self.assertIsInstance(t, tuple) |
|
155 |
self.assertEqual(('foo', 'bar'), t) |
|
156 |
k2 = self.module.StaticTuple(1, k1) |
|
157 |
t = static_tuple.as_tuples(k2) |
|
158 |
self.assertIsInstance(t, tuple) |
|
159 |
self.assertIsInstance(t[1], tuple) |
|
160 |
self.assertEqual((1, ('foo', 'bar')), t) |
|
161 |
mixed = (1, k1) |
|
162 |
t = static_tuple.as_tuples(mixed) |
|
163 |
self.assertIsInstance(t, tuple) |
|
164 |
self.assertIsInstance(t[1], tuple) |
|
165 |
self.assertEqual((1, ('foo', 'bar')), t) |
|
|
4679.3.16
by John Arbash Meinel
Initial work for a Key class. |
166 |
|
|
4679.3.17
by John Arbash Meinel
Implement some of the sequence items. |
167 |
def test_len(self): |
|
4679.3.42
by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects. |
168 |
k = self.module.StaticTuple() |
169 |
self.assertEqual(0, len(k)) |
|
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
170 |
k = self.module.StaticTuple('foo') |
|
4679.3.17
by John Arbash Meinel
Implement some of the sequence items. |
171 |
self.assertEqual(1, len(k)) |
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
172 |
k = self.module.StaticTuple('foo', 'bar') |
|
4679.3.17
by John Arbash Meinel
Implement some of the sequence items. |
173 |
self.assertEqual(2, len(k)) |
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
174 |
k = self.module.StaticTuple('foo', 'bar', 'b', 'b', 'b', 'b', 'b') |
|
4679.3.17
by John Arbash Meinel
Implement some of the sequence items. |
175 |
self.assertEqual(7, len(k)) |
|
4679.3.42
by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects. |
176 |
args = ['foo']*255 |
177 |
k = self.module.StaticTuple(*args) |
|
178 |
self.assertEqual(255, len(k)) |
|
179 |
||
180 |
def test_hold_other_static_tuples(self): |
|
181 |
k = self.module.StaticTuple('foo', 'bar') |
|
182 |
k2 = self.module.StaticTuple(k, k) |
|
183 |
self.assertEqual(2, len(k2)) |
|
184 |
self.assertIs(k, k2[0]) |
|
185 |
self.assertIs(k, k2[1]) |
|
|
4679.3.17
by John Arbash Meinel
Implement some of the sequence items. |
186 |
|
187 |
def test_getitem(self): |
|
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
188 |
k = self.module.StaticTuple('foo', 'bar', 'b', 'b', 'b', 'b', 'z') |
|
4679.3.17
by John Arbash Meinel
Implement some of the sequence items. |
189 |
self.assertEqual('foo', k[0]) |
190 |
self.assertEqual('foo', k[0]) |
|
191 |
self.assertEqual('foo', k[0]) |
|
192 |
self.assertEqual('z', k[6]) |
|
193 |
self.assertEqual('z', k[-1]) |
|
|
4679.5.5
by John Arbash Meinel
Review feedback from Andrew Bennetts. |
194 |
self.assertRaises(IndexError, k.__getitem__, 7) |
195 |
self.assertRaises(IndexError, k.__getitem__, 256+7) |
|
196 |
self.assertRaises(IndexError, k.__getitem__, 12024) |
|
197 |
# Python's [] resolver handles the negative arguments, so we can't
|
|
198 |
# really test StaticTuple_item() with negative values.
|
|
199 |
self.assertRaises(TypeError, k.__getitem__, 'not-an-int') |
|
200 |
self.assertRaises(TypeError, k.__getitem__, '5') |
|
|
4679.3.17
by John Arbash Meinel
Implement some of the sequence items. |
201 |
|
202 |
def test_refcount(self): |
|
203 |
f = 'fo' + 'oo' |
|
|
4679.3.81
by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again. |
204 |
num_refs = sys.getrefcount(f) - 1 #sys.getrefcount() adds one |
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
205 |
k = self.module.StaticTuple(f) |
|
4679.3.81
by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again. |
206 |
self.assertRefcount(num_refs + 1, f) |
207 |
b = k[0] |
|
208 |
self.assertRefcount(num_refs + 2, f) |
|
209 |
b = k[0] |
|
210 |
self.assertRefcount(num_refs + 2, f) |
|
|
4679.3.17
by John Arbash Meinel
Implement some of the sequence items. |
211 |
c = k[0] |
|
4679.3.81
by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again. |
212 |
self.assertRefcount(num_refs + 3, f) |
|
4679.3.17
by John Arbash Meinel
Implement some of the sequence items. |
213 |
del b, c |
|
4679.3.81
by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again. |
214 |
self.assertRefcount(num_refs + 1, f) |
|
4679.3.17
by John Arbash Meinel
Implement some of the sequence items. |
215 |
del k |
|
4679.3.81
by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again. |
216 |
self.assertRefcount(num_refs, f) |
|
4679.3.17
by John Arbash Meinel
Implement some of the sequence items. |
217 |
|
|
4679.3.18
by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests. |
218 |
def test__repr__(self): |
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
219 |
k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing') |
|
4679.3.79
by John Arbash Meinel
Change the repr to print out 'StaticTuple' |
220 |
self.assertEqual("StaticTuple('foo', 'bar', 'baz', 'bing')", repr(k)) |
|
4679.3.18
by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests. |
221 |
|
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
222 |
def assertCompareEqual(self, k1, k2): |
223 |
self.assertTrue(k1 == k2) |
|
224 |
self.assertTrue(k1 <= k2) |
|
225 |
self.assertTrue(k1 >= k2) |
|
226 |
self.assertFalse(k1 != k2) |
|
227 |
self.assertFalse(k1 < k2) |
|
228 |
self.assertFalse(k1 > k2) |
|
229 |
||
|
4759.2.8
by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types. |
230 |
def test_holds_None(self): |
231 |
k1 = self.module.StaticTuple(None) |
|
|
4759.2.11
by John Arbash Meinel
Review feedback from Andrew. |
232 |
# You cannot subclass None anyway
|
|
4759.2.8
by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types. |
233 |
|
234 |
def test_holds_int(self): |
|
235 |
k1 = self.module.StaticTuple(1) |
|
|
4759.2.11
by John Arbash Meinel
Review feedback from Andrew. |
236 |
class subint(int): |
237 |
pass
|
|
238 |
# But not a subclass, because subint could introduce refcycles
|
|
239 |
self.assertRaises(TypeError, self.module.StaticTuple, subint(2)) |
|
|
4759.2.8
by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types. |
240 |
|
|
4759.2.9
by John Arbash Meinel
Implement support for lots of types. |
241 |
def test_holds_long(self): |
242 |
k1 = self.module.StaticTuple(2L**65) |
|
|
4759.2.11
by John Arbash Meinel
Review feedback from Andrew. |
243 |
class sublong(long): |
244 |
pass
|
|
245 |
# But not a subclass
|
|
246 |
self.assertRaises(TypeError, self.module.StaticTuple, sublong(1)) |
|
|
4759.2.9
by John Arbash Meinel
Implement support for lots of types. |
247 |
|
|
4759.2.8
by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types. |
248 |
def test_holds_float(self): |
249 |
k1 = self.module.StaticTuple(1.2) |
|
|
4759.2.11
by John Arbash Meinel
Review feedback from Andrew. |
250 |
class subfloat(float): |
251 |
pass
|
|
252 |
self.assertRaises(TypeError, self.module.StaticTuple, subfloat(1.5)) |
|
253 |
||
254 |
def test_holds_str(self): |
|
255 |
k1 = self.module.StaticTuple('astring') |
|
256 |
class substr(str): |
|
257 |
pass
|
|
258 |
self.assertRaises(TypeError, self.module.StaticTuple, substr('a')) |
|
|
4759.2.8
by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types. |
259 |
|
260 |
def test_holds_unicode(self): |
|
261 |
k1 = self.module.StaticTuple(u'\xb5') |
|
|
4759.2.11
by John Arbash Meinel
Review feedback from Andrew. |
262 |
class subunicode(unicode): |
263 |
pass
|
|
264 |
self.assertRaises(TypeError, self.module.StaticTuple, |
|
265 |
subunicode(u'\xb5')) |
|
|
4759.2.8
by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types. |
266 |
|
267 |
def test_hold_bool(self): |
|
268 |
k1 = self.module.StaticTuple(True) |
|
269 |
k2 = self.module.StaticTuple(False) |
|
|
4759.2.11
by John Arbash Meinel
Review feedback from Andrew. |
270 |
# Cannot subclass bool
|
|
4759.2.8
by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types. |
271 |
|
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
272 |
def test_compare_same_obj(self): |
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
273 |
k1 = self.module.StaticTuple('foo', 'bar') |
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
274 |
self.assertCompareEqual(k1, k1) |
|
4679.3.42
by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects. |
275 |
k2 = self.module.StaticTuple(k1, k1) |
276 |
self.assertCompareEqual(k2, k2) |
|
|
4759.2.9
by John Arbash Meinel
Implement support for lots of types. |
277 |
k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True, |
278 |
k1) |
|
|
4759.2.8
by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types. |
279 |
self.assertCompareEqual(k3, k3) |
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
280 |
|
281 |
def test_compare_equivalent_obj(self): |
|
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
282 |
k1 = self.module.StaticTuple('foo', 'bar') |
283 |
k2 = self.module.StaticTuple('foo', 'bar') |
|
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
284 |
self.assertCompareEqual(k1, k2) |
|
4679.3.42
by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects. |
285 |
k3 = self.module.StaticTuple(k1, k2) |
286 |
k4 = self.module.StaticTuple(k2, k1) |
|
287 |
self.assertCompareEqual(k1, k2) |
|
|
4759.2.9
by John Arbash Meinel
Implement support for lots of types. |
288 |
k5 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True, |
289 |
k1) |
|
290 |
k6 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True, |
|
291 |
k1) |
|
|
4759.2.8
by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types. |
292 |
self.assertCompareEqual(k5, k6) |
293 |
k7 = self.module.StaticTuple(None) |
|
294 |
k8 = self.module.StaticTuple(None) |
|
295 |
self.assertCompareEqual(k7, k8) |
|
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
296 |
|
297 |
def test_compare_similar_obj(self): |
|
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
298 |
k1 = self.module.StaticTuple('foo' + ' bar', 'bar' + ' baz') |
299 |
k2 = self.module.StaticTuple('fo' + 'o bar', 'ba' + 'r baz') |
|
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
300 |
self.assertCompareEqual(k1, k2) |
|
4679.3.42
by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects. |
301 |
k3 = self.module.StaticTuple('foo ' + 'bar', 'bar ' + 'baz') |
302 |
k4 = self.module.StaticTuple('f' + 'oo bar', 'b' + 'ar baz') |
|
303 |
k5 = self.module.StaticTuple(k1, k2) |
|
304 |
k6 = self.module.StaticTuple(k3, k4) |
|
305 |
self.assertCompareEqual(k5, k6) |
|
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
306 |
|
307 |
def assertCompareDifferent(self, k_small, k_big): |
|
308 |
self.assertFalse(k_small == k_big) |
|
309 |
self.assertFalse(k_small >= k_big) |
|
310 |
self.assertFalse(k_small > k_big) |
|
311 |
self.assertTrue(k_small != k_big) |
|
312 |
self.assertTrue(k_small <= k_big) |
|
313 |
self.assertTrue(k_small < k_big) |
|
314 |
||
|
4679.5.8
by John Arbash Meinel
Add some tests that we *can* compare to strings, even if we don't care |
315 |
def assertCompareNoRelation(self, k1, k2): |
316 |
"""Run the comparison operators, make sure they do something. |
|
317 |
||
318 |
However, we don't actually care what comes first or second. This is
|
|
319 |
stuff like cross-class comparisons. We don't want to segfault/raise an
|
|
320 |
exception, but we don't care about the sort order.
|
|
321 |
"""
|
|
322 |
self.assertFalse(k1 == k2) |
|
323 |
self.assertTrue(k1 != k2) |
|
324 |
# Do the comparison, but we don't care about the result
|
|
325 |
k1 >= k2 |
|
326 |
k1 > k2 |
|
327 |
k1 <= k2 |
|
328 |
k1 < k2 |
|
329 |
||
|
4679.3.45
by John Arbash Meinel
Do some work to handle comparison to object that aren't tuples or strings. |
330 |
def test_compare_vs_none(self): |
331 |
k1 = self.module.StaticTuple('baz', 'bing') |
|
332 |
self.assertCompareDifferent(None, k1) |
|
|
4679.5.8
by John Arbash Meinel
Add some tests that we *can* compare to strings, even if we don't care |
333 |
|
334 |
def test_compare_cross_class(self): |
|
335 |
k1 = self.module.StaticTuple('baz', 'bing') |
|
336 |
self.assertCompareNoRelation(10, k1) |
|
337 |
self.assertCompareNoRelation('baz', k1) |
|
|
4679.3.45
by John Arbash Meinel
Do some work to handle comparison to object that aren't tuples or strings. |
338 |
|
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
339 |
def test_compare_all_different_same_width(self): |
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
340 |
k1 = self.module.StaticTuple('baz', 'bing') |
341 |
k2 = self.module.StaticTuple('foo', 'bar') |
|
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
342 |
self.assertCompareDifferent(k1, k2) |
|
4679.3.42
by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects. |
343 |
k3 = self.module.StaticTuple(k1, k2) |
344 |
k4 = self.module.StaticTuple(k2, k1) |
|
345 |
self.assertCompareDifferent(k3, k4) |
|
|
4759.2.8
by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types. |
346 |
k5 = self.module.StaticTuple(1) |
347 |
k6 = self.module.StaticTuple(2) |
|
348 |
self.assertCompareDifferent(k5, k6) |
|
349 |
k7 = self.module.StaticTuple(1.2) |
|
350 |
k8 = self.module.StaticTuple(2.4) |
|
351 |
self.assertCompareDifferent(k7, k8) |
|
352 |
k9 = self.module.StaticTuple(u's\xb5') |
|
353 |
k10 = self.module.StaticTuple(u's\xe5') |
|
354 |
self.assertCompareDifferent(k9, k10) |
|
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
355 |
|
356 |
def test_compare_some_different(self): |
|
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
357 |
k1 = self.module.StaticTuple('foo', 'bar') |
358 |
k2 = self.module.StaticTuple('foo', 'zzz') |
|
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
359 |
self.assertCompareDifferent(k1, k2) |
|
4679.3.42
by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects. |
360 |
k3 = self.module.StaticTuple(k1, k1) |
361 |
k4 = self.module.StaticTuple(k1, k2) |
|
362 |
self.assertCompareDifferent(k3, k4) |
|
|
4759.2.8
by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types. |
363 |
k5 = self.module.StaticTuple('foo', None) |
364 |
self.assertCompareDifferent(k5, k1) |
|
365 |
self.assertCompareDifferent(k5, k2) |
|
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
366 |
|
367 |
def test_compare_diff_width(self): |
|
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
368 |
k1 = self.module.StaticTuple('foo') |
369 |
k2 = self.module.StaticTuple('foo', 'bar') |
|
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
370 |
self.assertCompareDifferent(k1, k2) |
|
4679.3.42
by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects. |
371 |
k3 = self.module.StaticTuple(k1) |
372 |
k4 = self.module.StaticTuple(k1, k2) |
|
373 |
self.assertCompareDifferent(k3, k4) |
|
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
374 |
|
|
4759.2.8
by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types. |
375 |
def test_compare_different_types(self): |
376 |
k1 = self.module.StaticTuple('foo', 'bar') |
|
|
4759.2.9
by John Arbash Meinel
Implement support for lots of types. |
377 |
k2 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True, |
378 |
k1) |
|
|
4759.2.8
by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types. |
379 |
self.assertCompareNoRelation(k1, k2) |
380 |
k3 = self.module.StaticTuple('foo') |
|
381 |
self.assertCompareDifferent(k3, k1) |
|
382 |
k4 = self.module.StaticTuple(None) |
|
383 |
self.assertCompareDifferent(k4, k1) |
|
384 |
k5 = self.module.StaticTuple(1) |
|
385 |
self.assertCompareNoRelation(k1, k5) |
|
386 |
||
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
387 |
def test_compare_to_tuples(self): |
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
388 |
k1 = self.module.StaticTuple('foo') |
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
389 |
self.assertCompareEqual(k1, ('foo',)) |
390 |
self.assertCompareEqual(('foo',), k1) |
|
391 |
self.assertCompareDifferent(k1, ('foo', 'bar')) |
|
392 |
self.assertCompareDifferent(k1, ('foo', 10)) |
|
393 |
||
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
394 |
k2 = self.module.StaticTuple('foo', 'bar') |
|
4679.3.21
by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples. |
395 |
self.assertCompareEqual(k2, ('foo', 'bar')) |
396 |
self.assertCompareEqual(('foo', 'bar'), k2) |
|
397 |
self.assertCompareDifferent(k2, ('foo', 'zzz')) |
|
398 |
self.assertCompareDifferent(('foo',), k2) |
|
399 |
self.assertCompareDifferent(('foo', 'aaa'), k2) |
|
400 |
self.assertCompareDifferent(('baz', 'bing'), k2) |
|
401 |
self.assertCompareDifferent(('foo', 10), k2) |
|
|
4679.3.18
by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests. |
402 |
|
|
4679.3.42
by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects. |
403 |
k3 = self.module.StaticTuple(k1, k2) |
404 |
self.assertCompareEqual(k3, (('foo',), ('foo', 'bar'))) |
|
405 |
self.assertCompareEqual((('foo',), ('foo', 'bar')), k3) |
|
406 |
self.assertCompareEqual(k3, (k1, ('foo', 'bar'))) |
|
407 |
self.assertCompareEqual((k1, ('foo', 'bar')), k3) |
|
408 |
||
|
4679.8.11
by John Arbash Meinel
Handle the case where the recursive call ends up returning NotImplemented. |
409 |
def test_compare_mixed_depths(self): |
410 |
stuple = self.module.StaticTuple |
|
|
4679.8.13
by John Arbash Meinel
clean up the test case a little bit. |
411 |
k1 = stuple(stuple('a',), stuple('b',)) |
412 |
k2 = stuple(stuple(stuple('c',), stuple('d',)), |
|
413 |
stuple('b',)) |
|
|
4679.8.11
by John Arbash Meinel
Handle the case where the recursive call ends up returning NotImplemented. |
414 |
# This requires comparing a StaticTuple to a 'string', and then
|
415 |
# interpreting that value in the next higher StaticTuple. This used to
|
|
416 |
# generate a PyErr_BadIternalCall. We now fall back to *something*.
|
|
417 |
self.assertCompareNoRelation(k1, k2) |
|
418 |
||
|
4679.3.18
by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests. |
419 |
def test_hash(self): |
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
420 |
k = self.module.StaticTuple('foo') |
|
4679.3.18
by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests. |
421 |
self.assertEqual(hash(k), hash(('foo',))) |
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
422 |
k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing') |
|
4679.3.18
by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests. |
423 |
as_tuple = ('foo', 'bar', 'baz', 'bing') |
424 |
self.assertEqual(hash(k), hash(as_tuple)) |
|
425 |
x = {k: 'foo'} |
|
426 |
# Because k == , it replaces the slot, rather than having both
|
|
427 |
# present in the dict.
|
|
428 |
self.assertEqual('foo', x[as_tuple]) |
|
429 |
x[as_tuple] = 'bar' |
|
430 |
self.assertEqual({as_tuple: 'bar'}, x) |
|
431 |
||
|
4679.3.42
by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects. |
432 |
k2 = self.module.StaticTuple(k) |
433 |
as_tuple2 = (('foo', 'bar', 'baz', 'bing'),) |
|
434 |
self.assertEqual(hash(k2), hash(as_tuple2)) |
|
435 |
||
|
4759.2.9
by John Arbash Meinel
Implement support for lots of types. |
436 |
k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True, |
437 |
k) |
|
438 |
as_tuple3 = ('foo', 1, None, u'\xb5', 1.2, 2**65, True, k) |
|
|
4759.2.8
by John Arbash Meinel
Set up a test suite for hash() and richcompare against lots of acceptable types. |
439 |
self.assertEqual(hash(as_tuple3), hash(k3)) |
440 |
||
|
4679.3.19
by John Arbash Meinel
implement slicing as a tuple thunk. |
441 |
def test_slice(self): |
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
442 |
k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing') |
|
4679.3.19
by John Arbash Meinel
implement slicing as a tuple thunk. |
443 |
self.assertEqual(('foo', 'bar'), k[:2]) |
444 |
self.assertEqual(('baz',), k[2:-1]) |
|
|
4679.5.5
by John Arbash Meinel
Review feedback from Andrew Bennetts. |
445 |
try: |
446 |
val = k[::2] |
|
447 |
except TypeError: |
|
448 |
# C implementation raises a TypeError, we don't need the
|
|
449 |
# implementation yet, so allow this to pass
|
|
450 |
pass
|
|
451 |
else: |
|
452 |
# Python implementation uses a regular Tuple, so make sure it gives
|
|
453 |
# the right result
|
|
454 |
self.assertEqual(('foo', 'baz'), val) |
|
|
4679.3.19
by John Arbash Meinel
implement slicing as a tuple thunk. |
455 |
|
|
4679.3.20
by John Arbash Meinel
Implement tp_traverse, and add a soft dependency on meliae to test it. |
456 |
def test_referents(self): |
457 |
# We implement tp_traverse so that things like 'meliae' can measure the
|
|
458 |
# amount of referenced memory. Unfortunately gc.get_referents() first
|
|
|
4679.5.5
by John Arbash Meinel
Review feedback from Andrew Bennetts. |
459 |
# checks the IS_GC flag before it traverses anything. We could write a
|
460 |
# helper func, but that won't work for the generic implementation...
|
|
|
4679.3.29
by John Arbash Meinel
Start work on implementing a Key.intern() function. |
461 |
self.requireFeature(Meliae) |
462 |
from meliae import scanner |
|
|
4679.3.20
by John Arbash Meinel
Implement tp_traverse, and add a soft dependency on meliae to test it. |
463 |
strs = ['foo', 'bar', 'baz', 'bing'] |
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
464 |
k = self.module.StaticTuple(*strs) |
|
4679.3.45
by John Arbash Meinel
Do some work to handle comparison to object that aren't tuples or strings. |
465 |
if self.module is _static_tuple_py: |
|
4679.3.78
by John Arbash Meinel
Change the pure-python version of StaticTuple |
466 |
refs = strs + [self.module.StaticTuple] |
|
4679.3.29
by John Arbash Meinel
Start work on implementing a Key.intern() function. |
467 |
else: |
|
4679.3.78
by John Arbash Meinel
Change the pure-python version of StaticTuple |
468 |
refs = strs |
469 |
self.assertEqual(sorted(refs), sorted(scanner.get_referents(k))) |
|
|
4679.3.20
by John Arbash Meinel
Implement tp_traverse, and add a soft dependency on meliae to test it. |
470 |
|
|
4679.3.45
by John Arbash Meinel
Do some work to handle comparison to object that aren't tuples or strings. |
471 |
def test_nested_referents(self): |
472 |
self.requireFeature(Meliae) |
|
|
4679.3.78
by John Arbash Meinel
Change the pure-python version of StaticTuple |
473 |
from meliae import scanner |
474 |
strs = ['foo', 'bar', 'baz', 'bing'] |
|
475 |
k1 = self.module.StaticTuple(*strs[:2]) |
|
476 |
k2 = self.module.StaticTuple(*strs[2:]) |
|
477 |
k3 = self.module.StaticTuple(k1, k2) |
|
478 |
refs = [k1, k2] |
|
479 |
if self.module is _static_tuple_py: |
|
480 |
refs.append(self.module.StaticTuple) |
|
481 |
self.assertEqual(sorted(refs), |
|
482 |
sorted(scanner.get_referents(k3))) |
|
|
4679.3.45
by John Arbash Meinel
Do some work to handle comparison to object that aren't tuples or strings. |
483 |
|
|
4679.3.44
by John Arbash Meinel
Special case the empty tuple as a singleton. |
484 |
def test_empty_is_singleton(self): |
485 |
key = self.module.StaticTuple() |
|
486 |
self.assertIs(key, self.module._empty_tuple) |
|
487 |
||
|
4679.3.29
by John Arbash Meinel
Start work on implementing a Key.intern() function. |
488 |
def test_intern(self): |
489 |
unique_str1 = 'unique str ' + osutils.rand_chars(20) |
|
490 |
unique_str2 = 'unique str ' + osutils.rand_chars(20) |
|
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
491 |
key = self.module.StaticTuple(unique_str1, unique_str2) |
|
4679.3.51
by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code. |
492 |
self.assertFalse(key in self.module._interned_tuples) |
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
493 |
key2 = self.module.StaticTuple(unique_str1, unique_str2) |
|
4679.3.29
by John Arbash Meinel
Start work on implementing a Key.intern() function. |
494 |
self.assertEqual(key, key2) |
495 |
self.assertIsNot(key, key2) |
|
|
4679.3.33
by John Arbash Meinel
Change Key away from being a PyVarObject. |
496 |
key3 = key.intern() |
497 |
self.assertIs(key, key3) |
|
|
4679.3.51
by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code. |
498 |
self.assertTrue(key in self.module._interned_tuples) |
499 |
self.assertEqual(key, self.module._interned_tuples[key]) |
|
|
4679.3.33
by John Arbash Meinel
Change Key away from being a PyVarObject. |
500 |
key2 = key2.intern() |
501 |
self.assertIs(key, key2) |
|
|
4679.3.29
by John Arbash Meinel
Start work on implementing a Key.intern() function. |
502 |
|
|
4679.3.33
by John Arbash Meinel
Change Key away from being a PyVarObject. |
503 |
def test__c_intern_handles_refcount(self): |
|
4679.3.40
by John Arbash Meinel
Switch away from the name Key instead start branding as StaticTuple. |
504 |
if self.module is _static_tuple_py: |
|
4679.3.33
by John Arbash Meinel
Change Key away from being a PyVarObject. |
505 |
return # Not applicable |
506 |
unique_str1 = 'unique str ' + osutils.rand_chars(20) |
|
507 |
unique_str2 = 'unique str ' + osutils.rand_chars(20) |
|
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
508 |
key = self.module.StaticTuple(unique_str1, unique_str2) |
|
4679.3.81
by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again. |
509 |
self.assertRefcount(1, key) |
|
4679.3.51
by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code. |
510 |
self.assertFalse(key in self.module._interned_tuples) |
|
4679.3.35
by John Arbash Meinel
Work on making intern() not generate immortal Key objects. |
511 |
self.assertFalse(key._is_interned()) |
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
512 |
key2 = self.module.StaticTuple(unique_str1, unique_str2) |
|
4679.3.81
by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again. |
513 |
self.assertRefcount(1, key) |
514 |
self.assertRefcount(1, key2) |
|
|
4679.3.33
by John Arbash Meinel
Change Key away from being a PyVarObject. |
515 |
self.assertEqual(key, key2) |
516 |
self.assertIsNot(key, key2) |
|
|
4679.3.30
by John Arbash Meinel
Interning with a regular 'dict' is a tradeoff for bzr.dev of: |
517 |
|
518 |
key3 = key.intern() |
|
519 |
self.assertIs(key, key3) |
|
|
4679.3.51
by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code. |
520 |
self.assertTrue(key in self.module._interned_tuples) |
521 |
self.assertEqual(key, self.module._interned_tuples[key]) |
|
|
4679.3.81
by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again. |
522 |
# key and key3, but we 'hide' the one in _interned_tuples
|
523 |
self.assertRefcount(2, key) |
|
|
4679.3.30
by John Arbash Meinel
Interning with a regular 'dict' is a tradeoff for bzr.dev of: |
524 |
del key3 |
|
4679.3.81
by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again. |
525 |
self.assertRefcount(1, key) |
|
4679.3.35
by John Arbash Meinel
Work on making intern() not generate immortal Key objects. |
526 |
self.assertTrue(key._is_interned()) |
|
4679.3.81
by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again. |
527 |
self.assertRefcount(1, key2) |
528 |
key3 = key2.intern() |
|
529 |
# key3 now points to key as well, and *not* to key2
|
|
530 |
self.assertRefcount(2, key) |
|
531 |
self.assertRefcount(1, key2) |
|
532 |
self.assertIs(key, key3) |
|
533 |
self.assertIsNot(key3, key2) |
|
534 |
del key2 |
|
535 |
del key3 |
|
536 |
self.assertRefcount(1, key) |
|
|
4679.3.38
by John Arbash Meinel
add a test that Key.intern() doesn't create an immortal object. |
537 |
|
538 |
def test__c_keys_are_not_immortal(self): |
|
|
4679.3.40
by John Arbash Meinel
Switch away from the name Key instead start branding as StaticTuple. |
539 |
if self.module is _static_tuple_py: |
|
4679.3.38
by John Arbash Meinel
add a test that Key.intern() doesn't create an immortal object. |
540 |
return # Not applicable |
541 |
unique_str1 = 'unique str ' + osutils.rand_chars(20) |
|
542 |
unique_str2 = 'unique str ' + osutils.rand_chars(20) |
|
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
543 |
key = self.module.StaticTuple(unique_str1, unique_str2) |
|
4679.3.51
by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code. |
544 |
self.assertFalse(key in self.module._interned_tuples) |
|
4679.3.81
by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again. |
545 |
self.assertRefcount(1, key) |
|
4679.3.38
by John Arbash Meinel
add a test that Key.intern() doesn't create an immortal object. |
546 |
key = key.intern() |
|
4679.3.81
by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again. |
547 |
self.assertRefcount(1, key) |
|
4679.3.51
by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code. |
548 |
self.assertTrue(key in self.module._interned_tuples) |
|
4679.3.38
by John Arbash Meinel
add a test that Key.intern() doesn't create an immortal object. |
549 |
self.assertTrue(key._is_interned()) |
550 |
del key |
|
551 |
# Create a new entry, which would point to the same location
|
|
|
4679.3.41
by John Arbash Meinel
Finish switching the naming to StaticTuple. |
552 |
key = self.module.StaticTuple(unique_str1, unique_str2) |
|
4679.3.81
by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again. |
553 |
self.assertRefcount(1, key) |
|
4679.3.51
by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code. |
554 |
# This old entry in _interned_tuples should be gone
|
555 |
self.assertFalse(key in self.module._interned_tuples) |
|
|
4679.3.38
by John Arbash Meinel
add a test that Key.intern() doesn't create an immortal object. |
556 |
self.assertFalse(key._is_interned()) |
|
4679.3.47
by John Arbash Meinel
Work out how to expose the C api using the Python PyCObject interface. |
557 |
|
558 |
def test__c_has_C_API(self): |
|
559 |
if self.module is _static_tuple_py: |
|
560 |
return
|
|
561 |
self.assertIsNot(None, self.module._C_API) |
|
|
4679.8.3
by John Arbash Meinel
Expose bzrlib.static_tuple.StaticTuple as a thunk |
562 |
|
|
4739.4.1
by John Arbash Meinel
Implement StaticTuple.from_sequence() |
563 |
def test_from_sequence_tuple(self): |
564 |
st = self.module.StaticTuple.from_sequence(('foo', 'bar')) |
|
565 |
self.assertIsInstance(st, self.module.StaticTuple) |
|
566 |
self.assertEqual(('foo', 'bar'), st) |
|
567 |
||
568 |
def test_from_sequence_str(self): |
|
569 |
st = self.module.StaticTuple.from_sequence('foo') |
|
570 |
self.assertIsInstance(st, self.module.StaticTuple) |
|
571 |
self.assertEqual(('f', 'o', 'o'), st) |
|
572 |
||
573 |
def test_from_sequence_list(self): |
|
574 |
st = self.module.StaticTuple.from_sequence(['foo', 'bar']) |
|
575 |
self.assertIsInstance(st, self.module.StaticTuple) |
|
576 |
self.assertEqual(('foo', 'bar'), st) |
|
577 |
||
578 |
def test_from_sequence_static_tuple(self): |
|
579 |
st = self.module.StaticTuple('foo', 'bar') |
|
580 |
st2 = self.module.StaticTuple.from_sequence(st) |
|
581 |
# If the source is a StaticTuple already, we return the exact object
|
|
582 |
self.assertIs(st, st2) |
|
583 |
||
584 |
def test_from_sequence_not_sequence(self): |
|
585 |
self.assertRaises(TypeError, |
|
586 |
self.module.StaticTuple.from_sequence, object()) |
|
|
4771.2.2
by John Arbash Meinel
Clean up the C code a bit, using a goto. |
587 |
self.assertRaises(TypeError, |
588 |
self.module.StaticTuple.from_sequence, 10) |
|
|
4739.4.1
by John Arbash Meinel
Implement StaticTuple.from_sequence() |
589 |
|
590 |
def test_from_sequence_incorrect_args(self): |
|
591 |
self.assertRaises(TypeError, |
|
592 |
self.module.StaticTuple.from_sequence, object(), 'a') |
|
593 |
self.assertRaises(TypeError, |
|
594 |
self.module.StaticTuple.from_sequence, foo='a') |
|
|
4739.4.2
by John Arbash Meinel
Merge bzr.dev resolve test conflict. |
595 |
|
|
4771.2.1
by Matt Nordhoff
_static_tuple_c.StaticTuple.from_sequence() now supports arbitrary iterables (by converting them to tuples first). |
596 |
def test_from_sequence_iterable(self): |
|
4771.2.2
by John Arbash Meinel
Clean up the C code a bit, using a goto. |
597 |
st = self.module.StaticTuple.from_sequence(iter(['foo', 'bar'])) |
598 |
self.assertIsInstance(st, self.module.StaticTuple) |
|
599 |
self.assertEqual(('foo', 'bar'), st) |
|
600 |
||
601 |
def test_from_sequence_generator(self): |
|
602 |
def generate_tuple(): |
|
603 |
yield 'foo' |
|
604 |
yield 'bar' |
|
605 |
st = self.module.StaticTuple.from_sequence(generate_tuple()) |
|
|
4771.2.1
by Matt Nordhoff
_static_tuple_c.StaticTuple.from_sequence() now supports arbitrary iterables (by converting them to tuples first). |
606 |
self.assertIsInstance(st, self.module.StaticTuple) |
607 |
self.assertEqual(('foo', 'bar'), st) |
|
608 |
||
|
4759.2.16
by Matt Nordhoff
Add a test |
609 |
def test_pickle(self): |
610 |
st = self.module.StaticTuple('foo', 'bar') |
|
611 |
pickled = cPickle.dumps(st) |
|
612 |
unpickled = cPickle.loads(pickled) |
|
613 |
self.assertEqual(unpickled, st) |
|
|
4759.2.20
by Matt Nordhoff
Review: Add a Py_INCREF, and test pickling a nested StaticTuple. |
614 |
|
615 |
def test_pickle_empty(self): |
|
|
4759.2.17
by Matt Nordhoff
Test pickling the empty StaticTuple too |
616 |
st = self.module.StaticTuple() |
617 |
pickled = cPickle.dumps(st) |
|
618 |
unpickled = cPickle.loads(pickled) |
|
619 |
self.assertIs(st, unpickled) |
|
|
4759.2.16
by Matt Nordhoff
Add a test |
620 |
|
|
4759.2.20
by Matt Nordhoff
Review: Add a Py_INCREF, and test pickling a nested StaticTuple. |
621 |
def test_pickle_nested(self): |
622 |
st = self.module.StaticTuple('foo', self.module.StaticTuple('bar')) |
|
623 |
pickled = cPickle.dumps(st) |
|
624 |
unpickled = cPickle.loads(pickled) |
|
625 |
self.assertEqual(unpickled, st) |
|
626 |
||
|
4679.8.3
by John Arbash Meinel
Expose bzrlib.static_tuple.StaticTuple as a thunk |
627 |
def test_static_tuple_thunk(self): |
628 |
# Make sure the right implementation is available from
|
|
629 |
# bzrlib.static_tuple.StaticTuple.
|
|
630 |
if self.module is _static_tuple_py: |
|
|
4913.2.7
by John Arbash Meinel
Switch CompiledStaticTuple |
631 |
if compiled_static_tuple.available(): |
|
4679.8.3
by John Arbash Meinel
Expose bzrlib.static_tuple.StaticTuple as a thunk |
632 |
# We will be using the C version
|
633 |
return
|
|
|
4739.4.2
by John Arbash Meinel
Merge bzr.dev resolve test conflict. |
634 |
self.assertIs(static_tuple.StaticTuple, |
|
4679.8.3
by John Arbash Meinel
Expose bzrlib.static_tuple.StaticTuple as a thunk |
635 |
self.module.StaticTuple) |
|
4668.3.1
by John Arbash Meinel
Fix bug #471193, allow tuples into the CHK code. |
636 |
|
637 |
||
638 |
class TestEnsureStaticTuple(tests.TestCase): |
|
639 |
||
640 |
def test_is_static_tuple(self): |
|
641 |
st = static_tuple.StaticTuple('foo') |
|
642 |
st2 = static_tuple.expect_static_tuple(st) |
|
643 |
self.assertIs(st, st2) |
|
644 |
||
645 |
def test_is_tuple(self): |
|
646 |
t = ('foo',) |
|
647 |
st = static_tuple.expect_static_tuple(t) |
|
648 |
self.assertIsInstance(st, static_tuple.StaticTuple) |
|
649 |
self.assertEqual(t, st) |
|
650 |
||
651 |
def test_flagged_is_static_tuple(self): |
|
652 |
debug.debug_flags.add('static_tuple') |
|
653 |
st = static_tuple.StaticTuple('foo') |
|
654 |
st2 = static_tuple.expect_static_tuple(st) |
|
655 |
self.assertIs(st, st2) |
|
656 |
||
657 |
def test_flagged_is_tuple(self): |
|
658 |
debug.debug_flags.add('static_tuple') |
|
659 |
t = ('foo',) |
|
660 |
self.assertRaises(TypeError, static_tuple.expect_static_tuple, t) |