1
# Copyright (C) 2009 Canonical Ltd
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.
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.
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
17
"""Tests for the StaticTuple type."""
30
def load_tests(standard_tests, module, loader):
31
"""Parameterize tests for all versions of groupcompress."""
33
('python', {'module': _static_tuple_py}),
35
suite = loader.suiteClass()
36
if CompiledStaticTuple.available():
37
from bzrlib import _static_tuple_c
38
scenarios.append(('C', {'module': _static_tuple_c}))
40
# the compiled module isn't available, so we add a failing test
41
class FailWithoutFeature(tests.TestCase):
43
self.requireFeature(CompiledStaticTuple)
44
suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
45
result = tests.multiply_tests(standard_tests, scenarios, suite)
49
class _CompiledStaticTuple(tests.Feature):
53
import bzrlib._static_tuple_c
58
def feature_name(self):
59
return 'bzrlib._static_tuple_c'
61
CompiledStaticTuple = _CompiledStaticTuple()
64
class _Meliae(tests.Feature):
68
from meliae import scanner
73
def feature_name(self):
74
return "Meliae - python memory debugger"
79
class TestStaticTuple(tests.TestCase):
81
def assertRefcount(self, count, obj):
82
"""Assert that the refcount for obj is what we expect.
84
Note that this automatically adjusts for the fact that calling
85
assertRefcount actually creates a new pointer, as does calling
86
sys.getrefcount. So pass the expected value *before* the call.
88
# I don't understand why it is getrefcount()-3 here, but it seems to be
89
# correct. If I check in the calling function, with:
90
# self.assertEqual(count, sys.getrefcount(obj)-1)
91
# Then it works fine. Something about passing it to assertRefcount is
92
# actually double-incrementing (and decrementing) the refcount
93
self.assertEqual(count, sys.getrefcount(obj)-3)
95
def test_create(self):
96
k = self.module.StaticTuple('foo')
97
k = self.module.StaticTuple('foo', 'bar')
99
def test_create_bad_args(self):
102
self.assertRaises(ValueError, self.module.StaticTuple, *args_256)
104
self.assertRaises(ValueError, self.module.StaticTuple, *args_300)
106
self.assertRaises(TypeError, self.module.StaticTuple, 10)
108
def test_as_tuple(self):
109
k = self.module.StaticTuple('foo')
111
self.assertEqual(('foo',), t)
112
k = self.module.StaticTuple('foo', 'bar')
114
self.assertEqual(('foo', 'bar'), t)
117
k = self.module.StaticTuple()
118
self.assertEqual(0, len(k))
119
k = self.module.StaticTuple('foo')
120
self.assertEqual(1, len(k))
121
k = self.module.StaticTuple('foo', 'bar')
122
self.assertEqual(2, len(k))
123
k = self.module.StaticTuple('foo', 'bar', 'b', 'b', 'b', 'b', 'b')
124
self.assertEqual(7, len(k))
126
k = self.module.StaticTuple(*args)
127
self.assertEqual(255, len(k))
129
def test_hold_other_static_tuples(self):
130
k = self.module.StaticTuple('foo', 'bar')
131
k2 = self.module.StaticTuple(k, k)
132
self.assertEqual(2, len(k2))
133
self.assertIs(k, k2[0])
134
self.assertIs(k, k2[1])
136
def test_getitem(self):
137
k = self.module.StaticTuple('foo', 'bar', 'b', 'b', 'b', 'b', 'z')
138
self.assertEqual('foo', k[0])
139
self.assertEqual('foo', k[0])
140
self.assertEqual('foo', k[0])
141
self.assertEqual('z', k[6])
142
self.assertEqual('z', k[-1])
144
def test_refcount(self):
146
num_refs = sys.getrefcount(f) - 1 #sys.getrefcount() adds one
147
k = self.module.StaticTuple(f)
148
self.assertRefcount(num_refs + 1, f)
150
self.assertRefcount(num_refs + 2, f)
152
self.assertRefcount(num_refs + 2, f)
154
self.assertRefcount(num_refs + 3, f)
156
self.assertRefcount(num_refs + 1, f)
158
self.assertRefcount(num_refs, f)
160
def test__repr__(self):
161
k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
162
self.assertEqual("StaticTuple('foo', 'bar', 'baz', 'bing')", repr(k))
164
def assertCompareEqual(self, k1, k2):
165
self.assertTrue(k1 == k2)
166
self.assertTrue(k1 <= k2)
167
self.assertTrue(k1 >= k2)
168
self.assertFalse(k1 != k2)
169
self.assertFalse(k1 < k2)
170
self.assertFalse(k1 > k2)
172
def test_compare_same_obj(self):
173
k1 = self.module.StaticTuple('foo', 'bar')
174
self.assertCompareEqual(k1, k1)
175
k2 = self.module.StaticTuple(k1, k1)
176
self.assertCompareEqual(k2, k2)
178
def test_compare_equivalent_obj(self):
179
k1 = self.module.StaticTuple('foo', 'bar')
180
k2 = self.module.StaticTuple('foo', 'bar')
181
self.assertCompareEqual(k1, k2)
182
k3 = self.module.StaticTuple(k1, k2)
183
k4 = self.module.StaticTuple(k2, k1)
184
self.assertCompareEqual(k1, k2)
186
def test_compare_similar_obj(self):
187
k1 = self.module.StaticTuple('foo' + ' bar', 'bar' + ' baz')
188
k2 = self.module.StaticTuple('fo' + 'o bar', 'ba' + 'r baz')
189
self.assertCompareEqual(k1, k2)
190
k3 = self.module.StaticTuple('foo ' + 'bar', 'bar ' + 'baz')
191
k4 = self.module.StaticTuple('f' + 'oo bar', 'b' + 'ar baz')
192
k5 = self.module.StaticTuple(k1, k2)
193
k6 = self.module.StaticTuple(k3, k4)
194
self.assertCompareEqual(k5, k6)
196
def assertCompareDifferent(self, k_small, k_big):
197
self.assertFalse(k_small == k_big)
198
self.assertFalse(k_small >= k_big)
199
self.assertFalse(k_small > k_big)
200
self.assertTrue(k_small != k_big)
201
self.assertTrue(k_small <= k_big)
202
self.assertTrue(k_small < k_big)
204
def test_compare_vs_none(self):
205
k1 = self.module.StaticTuple('baz', 'bing')
206
self.assertCompareDifferent(None, k1)
207
self.assertCompareDifferent(10, k1)
208
# Comparison with a string is poorly-defined, I seem to get failures
209
# regardless of which one comes first...
210
# self.assertCompareDifferent('baz', k1)
212
def test_compare_all_different_same_width(self):
213
k1 = self.module.StaticTuple('baz', 'bing')
214
k2 = self.module.StaticTuple('foo', 'bar')
215
self.assertCompareDifferent(k1, k2)
216
k3 = self.module.StaticTuple(k1, k2)
217
k4 = self.module.StaticTuple(k2, k1)
218
self.assertCompareDifferent(k3, k4)
220
def test_compare_some_different(self):
221
k1 = self.module.StaticTuple('foo', 'bar')
222
k2 = self.module.StaticTuple('foo', 'zzz')
223
self.assertCompareDifferent(k1, k2)
224
k3 = self.module.StaticTuple(k1, k1)
225
k4 = self.module.StaticTuple(k1, k2)
226
self.assertCompareDifferent(k3, k4)
228
def test_compare_diff_width(self):
229
k1 = self.module.StaticTuple('foo')
230
k2 = self.module.StaticTuple('foo', 'bar')
231
self.assertCompareDifferent(k1, k2)
232
k3 = self.module.StaticTuple(k1)
233
k4 = self.module.StaticTuple(k1, k2)
234
self.assertCompareDifferent(k3, k4)
236
def test_compare_to_tuples(self):
237
k1 = self.module.StaticTuple('foo')
238
self.assertCompareEqual(k1, ('foo',))
239
self.assertCompareEqual(('foo',), k1)
240
self.assertCompareDifferent(k1, ('foo', 'bar'))
241
self.assertCompareDifferent(k1, ('foo', 10))
243
k2 = self.module.StaticTuple('foo', 'bar')
244
self.assertCompareEqual(k2, ('foo', 'bar'))
245
self.assertCompareEqual(('foo', 'bar'), k2)
246
self.assertCompareDifferent(k2, ('foo', 'zzz'))
247
self.assertCompareDifferent(('foo',), k2)
248
self.assertCompareDifferent(('foo', 'aaa'), k2)
249
self.assertCompareDifferent(('baz', 'bing'), k2)
250
self.assertCompareDifferent(('foo', 10), k2)
252
k3 = self.module.StaticTuple(k1, k2)
253
self.assertCompareEqual(k3, (('foo',), ('foo', 'bar')))
254
self.assertCompareEqual((('foo',), ('foo', 'bar')), k3)
255
self.assertCompareEqual(k3, (k1, ('foo', 'bar')))
256
self.assertCompareEqual((k1, ('foo', 'bar')), k3)
259
k = self.module.StaticTuple('foo')
260
self.assertEqual(hash(k), hash(('foo',)))
261
k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
262
as_tuple = ('foo', 'bar', 'baz', 'bing')
263
self.assertEqual(hash(k), hash(as_tuple))
265
# Because k == , it replaces the slot, rather than having both
266
# present in the dict.
267
self.assertEqual('foo', x[as_tuple])
269
self.assertEqual({as_tuple: 'bar'}, x)
271
k2 = self.module.StaticTuple(k)
272
as_tuple2 = (('foo', 'bar', 'baz', 'bing'),)
273
self.assertEqual(hash(k2), hash(as_tuple2))
275
def test_slice(self):
276
k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
277
self.assertEqual(('foo', 'bar'), k[:2])
278
self.assertEqual(('baz',), k[2:-1])
280
def test_referents(self):
281
# We implement tp_traverse so that things like 'meliae' can measure the
282
# amount of referenced memory. Unfortunately gc.get_referents() first
283
# checks the IS_GC flag before it traverses anything. So there isn't a
284
# way to expose it that I can see.
285
self.requireFeature(Meliae)
286
from meliae import scanner
287
strs = ['foo', 'bar', 'baz', 'bing']
288
k = self.module.StaticTuple(*strs)
289
if self.module is _static_tuple_py:
290
refs = strs + [self.module.StaticTuple]
293
self.assertEqual(sorted(refs), sorted(scanner.get_referents(k)))
295
def test_nested_referents(self):
296
self.requireFeature(Meliae)
297
from meliae import scanner
298
strs = ['foo', 'bar', 'baz', 'bing']
299
k1 = self.module.StaticTuple(*strs[:2])
300
k2 = self.module.StaticTuple(*strs[2:])
301
k3 = self.module.StaticTuple(k1, k2)
303
if self.module is _static_tuple_py:
304
refs.append(self.module.StaticTuple)
305
self.assertEqual(sorted(refs),
306
sorted(scanner.get_referents(k3)))
308
def test_empty_is_singleton(self):
309
key = self.module.StaticTuple()
310
self.assertIs(key, self.module._empty_tuple)
312
def test_intern(self):
313
unique_str1 = 'unique str ' + osutils.rand_chars(20)
314
unique_str2 = 'unique str ' + osutils.rand_chars(20)
315
key = self.module.StaticTuple(unique_str1, unique_str2)
316
self.assertFalse(key in self.module._interned_tuples)
317
key2 = self.module.StaticTuple(unique_str1, unique_str2)
318
self.assertEqual(key, key2)
319
self.assertIsNot(key, key2)
321
self.assertIs(key, key3)
322
self.assertTrue(key in self.module._interned_tuples)
323
self.assertEqual(key, self.module._interned_tuples[key])
325
self.assertIs(key, key2)
327
def test__c_intern_handles_refcount(self):
328
if self.module is _static_tuple_py:
329
return # Not applicable
330
unique_str1 = 'unique str ' + osutils.rand_chars(20)
331
unique_str2 = 'unique str ' + osutils.rand_chars(20)
332
key = self.module.StaticTuple(unique_str1, unique_str2)
333
self.assertRefcount(1, key)
334
self.assertFalse(key in self.module._interned_tuples)
335
self.assertFalse(key._is_interned())
336
key2 = self.module.StaticTuple(unique_str1, unique_str2)
337
self.assertRefcount(1, key)
338
self.assertRefcount(1, key2)
339
self.assertEqual(key, key2)
340
self.assertIsNot(key, key2)
343
self.assertIs(key, key3)
344
self.assertTrue(key in self.module._interned_tuples)
345
self.assertEqual(key, self.module._interned_tuples[key])
346
# key and key3, but we 'hide' the one in _interned_tuples
347
self.assertRefcount(2, key)
349
self.assertRefcount(1, key)
350
self.assertTrue(key._is_interned())
351
self.assertRefcount(1, key2)
353
# key3 now points to key as well, and *not* to key2
354
self.assertRefcount(2, key)
355
self.assertRefcount(1, key2)
356
self.assertIs(key, key3)
357
self.assertIsNot(key3, key2)
360
self.assertRefcount(1, key)
362
def test__c_keys_are_not_immortal(self):
363
if self.module is _static_tuple_py:
364
return # Not applicable
365
unique_str1 = 'unique str ' + osutils.rand_chars(20)
366
unique_str2 = 'unique str ' + osutils.rand_chars(20)
367
key = self.module.StaticTuple(unique_str1, unique_str2)
368
self.assertFalse(key in self.module._interned_tuples)
369
self.assertRefcount(1, key)
371
self.assertRefcount(1, key)
372
self.assertTrue(key in self.module._interned_tuples)
373
self.assertTrue(key._is_interned())
375
# Create a new entry, which would point to the same location
376
key = self.module.StaticTuple(unique_str1, unique_str2)
377
self.assertRefcount(1, key)
378
# This old entry in _interned_tuples should be gone
379
self.assertFalse(key in self.module._interned_tuples)
380
self.assertFalse(key._is_interned())
382
def test__c_has_C_API(self):
383
if self.module is _static_tuple_py:
385
self.assertIsNot(None, self.module._C_API)