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])
143
self.assertRaises(IndexError, k.__getitem__, 7)
144
self.assertRaises(IndexError, k.__getitem__, 256+7)
145
self.assertRaises(IndexError, k.__getitem__, 12024)
146
# Python's [] resolver handles the negative arguments, so we can't
147
# really test StaticTuple_item() with negative values.
148
self.assertRaises(TypeError, k.__getitem__, 'not-an-int')
149
self.assertRaises(TypeError, k.__getitem__, '5')
151
def test_refcount(self):
153
num_refs = sys.getrefcount(f) - 1 #sys.getrefcount() adds one
154
k = self.module.StaticTuple(f)
155
self.assertRefcount(num_refs + 1, f)
157
self.assertRefcount(num_refs + 2, f)
159
self.assertRefcount(num_refs + 2, f)
161
self.assertRefcount(num_refs + 3, f)
163
self.assertRefcount(num_refs + 1, f)
165
self.assertRefcount(num_refs, f)
167
def test__repr__(self):
168
k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
169
self.assertEqual("StaticTuple('foo', 'bar', 'baz', 'bing')", repr(k))
171
def assertCompareEqual(self, k1, k2):
172
self.assertTrue(k1 == k2)
173
self.assertTrue(k1 <= k2)
174
self.assertTrue(k1 >= k2)
175
self.assertFalse(k1 != k2)
176
self.assertFalse(k1 < k2)
177
self.assertFalse(k1 > k2)
179
def test_compare_same_obj(self):
180
k1 = self.module.StaticTuple('foo', 'bar')
181
self.assertCompareEqual(k1, k1)
182
k2 = self.module.StaticTuple(k1, k1)
183
self.assertCompareEqual(k2, k2)
185
def test_compare_equivalent_obj(self):
186
k1 = self.module.StaticTuple('foo', 'bar')
187
k2 = self.module.StaticTuple('foo', 'bar')
188
self.assertCompareEqual(k1, k2)
189
k3 = self.module.StaticTuple(k1, k2)
190
k4 = self.module.StaticTuple(k2, k1)
191
self.assertCompareEqual(k1, k2)
193
def test_compare_similar_obj(self):
194
k1 = self.module.StaticTuple('foo' + ' bar', 'bar' + ' baz')
195
k2 = self.module.StaticTuple('fo' + 'o bar', 'ba' + 'r baz')
196
self.assertCompareEqual(k1, k2)
197
k3 = self.module.StaticTuple('foo ' + 'bar', 'bar ' + 'baz')
198
k4 = self.module.StaticTuple('f' + 'oo bar', 'b' + 'ar baz')
199
k5 = self.module.StaticTuple(k1, k2)
200
k6 = self.module.StaticTuple(k3, k4)
201
self.assertCompareEqual(k5, k6)
203
def assertCompareDifferent(self, k_small, k_big):
204
self.assertFalse(k_small == k_big)
205
self.assertFalse(k_small >= k_big)
206
self.assertFalse(k_small > k_big)
207
self.assertTrue(k_small != k_big)
208
self.assertTrue(k_small <= k_big)
209
self.assertTrue(k_small < k_big)
211
def assertCompareNoRelation(self, k1, k2):
212
"""Run the comparison operators, make sure they do something.
214
However, we don't actually care what comes first or second. This is
215
stuff like cross-class comparisons. We don't want to segfault/raise an
216
exception, but we don't care about the sort order.
218
self.assertFalse(k1 == k2)
219
self.assertTrue(k1 != k2)
220
# Do the comparison, but we don't care about the result
226
def test_compare_vs_none(self):
227
k1 = self.module.StaticTuple('baz', 'bing')
228
self.assertCompareDifferent(None, k1)
230
def test_compare_cross_class(self):
231
k1 = self.module.StaticTuple('baz', 'bing')
232
self.assertCompareNoRelation(10, k1)
233
self.assertCompareNoRelation('baz', k1)
235
def test_compare_all_different_same_width(self):
236
k1 = self.module.StaticTuple('baz', 'bing')
237
k2 = self.module.StaticTuple('foo', 'bar')
238
self.assertCompareDifferent(k1, k2)
239
k3 = self.module.StaticTuple(k1, k2)
240
k4 = self.module.StaticTuple(k2, k1)
241
self.assertCompareDifferent(k3, k4)
243
def test_compare_some_different(self):
244
k1 = self.module.StaticTuple('foo', 'bar')
245
k2 = self.module.StaticTuple('foo', 'zzz')
246
self.assertCompareDifferent(k1, k2)
247
k3 = self.module.StaticTuple(k1, k1)
248
k4 = self.module.StaticTuple(k1, k2)
249
self.assertCompareDifferent(k3, k4)
251
def test_compare_diff_width(self):
252
k1 = self.module.StaticTuple('foo')
253
k2 = self.module.StaticTuple('foo', 'bar')
254
self.assertCompareDifferent(k1, k2)
255
k3 = self.module.StaticTuple(k1)
256
k4 = self.module.StaticTuple(k1, k2)
257
self.assertCompareDifferent(k3, k4)
259
def test_compare_to_tuples(self):
260
k1 = self.module.StaticTuple('foo')
261
self.assertCompareEqual(k1, ('foo',))
262
self.assertCompareEqual(('foo',), k1)
263
self.assertCompareDifferent(k1, ('foo', 'bar'))
264
self.assertCompareDifferent(k1, ('foo', 10))
266
k2 = self.module.StaticTuple('foo', 'bar')
267
self.assertCompareEqual(k2, ('foo', 'bar'))
268
self.assertCompareEqual(('foo', 'bar'), k2)
269
self.assertCompareDifferent(k2, ('foo', 'zzz'))
270
self.assertCompareDifferent(('foo',), k2)
271
self.assertCompareDifferent(('foo', 'aaa'), k2)
272
self.assertCompareDifferent(('baz', 'bing'), k2)
273
self.assertCompareDifferent(('foo', 10), k2)
275
k3 = self.module.StaticTuple(k1, k2)
276
self.assertCompareEqual(k3, (('foo',), ('foo', 'bar')))
277
self.assertCompareEqual((('foo',), ('foo', 'bar')), k3)
278
self.assertCompareEqual(k3, (k1, ('foo', 'bar')))
279
self.assertCompareEqual((k1, ('foo', 'bar')), k3)
282
k = self.module.StaticTuple('foo')
283
self.assertEqual(hash(k), hash(('foo',)))
284
k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
285
as_tuple = ('foo', 'bar', 'baz', 'bing')
286
self.assertEqual(hash(k), hash(as_tuple))
288
# Because k == , it replaces the slot, rather than having both
289
# present in the dict.
290
self.assertEqual('foo', x[as_tuple])
292
self.assertEqual({as_tuple: 'bar'}, x)
294
k2 = self.module.StaticTuple(k)
295
as_tuple2 = (('foo', 'bar', 'baz', 'bing'),)
296
self.assertEqual(hash(k2), hash(as_tuple2))
298
def test_slice(self):
299
k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
300
self.assertEqual(('foo', 'bar'), k[:2])
301
self.assertEqual(('baz',), k[2:-1])
305
# C implementation raises a TypeError, we don't need the
306
# implementation yet, so allow this to pass
309
# Python implementation uses a regular Tuple, so make sure it gives
311
self.assertEqual(('foo', 'baz'), val)
313
def test_referents(self):
314
# We implement tp_traverse so that things like 'meliae' can measure the
315
# amount of referenced memory. Unfortunately gc.get_referents() first
316
# checks the IS_GC flag before it traverses anything. We could write a
317
# helper func, but that won't work for the generic implementation...
318
self.requireFeature(Meliae)
319
from meliae import scanner
320
strs = ['foo', 'bar', 'baz', 'bing']
321
k = self.module.StaticTuple(*strs)
322
if self.module is _static_tuple_py:
323
refs = strs + [self.module.StaticTuple]
326
self.assertEqual(sorted(refs), sorted(scanner.get_referents(k)))
328
def test_nested_referents(self):
329
self.requireFeature(Meliae)
330
from meliae import scanner
331
strs = ['foo', 'bar', 'baz', 'bing']
332
k1 = self.module.StaticTuple(*strs[:2])
333
k2 = self.module.StaticTuple(*strs[2:])
334
k3 = self.module.StaticTuple(k1, k2)
336
if self.module is _static_tuple_py:
337
refs.append(self.module.StaticTuple)
338
self.assertEqual(sorted(refs),
339
sorted(scanner.get_referents(k3)))
341
def test_empty_is_singleton(self):
342
key = self.module.StaticTuple()
343
self.assertIs(key, self.module._empty_tuple)
345
def test_intern(self):
346
unique_str1 = 'unique str ' + osutils.rand_chars(20)
347
unique_str2 = 'unique str ' + osutils.rand_chars(20)
348
key = self.module.StaticTuple(unique_str1, unique_str2)
349
self.assertFalse(key in self.module._interned_tuples)
350
key2 = self.module.StaticTuple(unique_str1, unique_str2)
351
self.assertEqual(key, key2)
352
self.assertIsNot(key, key2)
354
self.assertIs(key, key3)
355
self.assertTrue(key in self.module._interned_tuples)
356
self.assertEqual(key, self.module._interned_tuples[key])
358
self.assertIs(key, key2)
360
def test__c_intern_handles_refcount(self):
361
if self.module is _static_tuple_py:
362
return # Not applicable
363
unique_str1 = 'unique str ' + osutils.rand_chars(20)
364
unique_str2 = 'unique str ' + osutils.rand_chars(20)
365
key = self.module.StaticTuple(unique_str1, unique_str2)
366
self.assertRefcount(1, key)
367
self.assertFalse(key in self.module._interned_tuples)
368
self.assertFalse(key._is_interned())
369
key2 = self.module.StaticTuple(unique_str1, unique_str2)
370
self.assertRefcount(1, key)
371
self.assertRefcount(1, key2)
372
self.assertEqual(key, key2)
373
self.assertIsNot(key, key2)
376
self.assertIs(key, key3)
377
self.assertTrue(key in self.module._interned_tuples)
378
self.assertEqual(key, self.module._interned_tuples[key])
379
# key and key3, but we 'hide' the one in _interned_tuples
380
self.assertRefcount(2, key)
382
self.assertRefcount(1, key)
383
self.assertTrue(key._is_interned())
384
self.assertRefcount(1, key2)
386
# key3 now points to key as well, and *not* to key2
387
self.assertRefcount(2, key)
388
self.assertRefcount(1, key2)
389
self.assertIs(key, key3)
390
self.assertIsNot(key3, key2)
393
self.assertRefcount(1, key)
395
def test__c_keys_are_not_immortal(self):
396
if self.module is _static_tuple_py:
397
return # Not applicable
398
unique_str1 = 'unique str ' + osutils.rand_chars(20)
399
unique_str2 = 'unique str ' + osutils.rand_chars(20)
400
key = self.module.StaticTuple(unique_str1, unique_str2)
401
self.assertFalse(key in self.module._interned_tuples)
402
self.assertRefcount(1, key)
404
self.assertRefcount(1, key)
405
self.assertTrue(key in self.module._interned_tuples)
406
self.assertTrue(key._is_interned())
408
# Create a new entry, which would point to the same location
409
key = self.module.StaticTuple(unique_str1, unique_str2)
410
self.assertRefcount(1, key)
411
# This old entry in _interned_tuples should be gone
412
self.assertFalse(key in self.module._interned_tuples)
413
self.assertFalse(key._is_interned())
415
def test__c_has_C_API(self):
416
if self.module is _static_tuple_py:
418
self.assertIsNot(None, self.module._C_API)