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."""
32
def load_tests(standard_tests, module, loader):
33
"""Parameterize tests for all versions of groupcompress."""
35
('python', {'module': _static_tuple_py}),
37
suite = loader.suiteClass()
38
if CompiledStaticTuple.available():
39
from bzrlib import _static_tuple_c
40
scenarios.append(('C', {'module': _static_tuple_c}))
42
# the compiled module isn't available, so we add a failing test
43
class FailWithoutFeature(tests.TestCase):
45
self.requireFeature(CompiledStaticTuple)
46
suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
47
result = tests.multiply_tests(standard_tests, scenarios, suite)
51
class _CompiledStaticTuple(tests.Feature):
55
import bzrlib._static_tuple_c
60
def feature_name(self):
61
return 'bzrlib._static_tuple_c'
63
CompiledStaticTuple = _CompiledStaticTuple()
66
class _Meliae(tests.Feature):
70
from meliae import scanner
75
def feature_name(self):
76
return "Meliae - python memory debugger"
81
class TestStaticTuple(tests.TestCase):
83
def assertRefcount(self, count, obj):
84
"""Assert that the refcount for obj is what we expect.
86
Note that this automatically adjusts for the fact that calling
87
assertRefcount actually creates a new pointer, as does calling
88
sys.getrefcount. So pass the expected value *before* the call.
90
# I don't understand why it is getrefcount()-3 here, but it seems to be
91
# correct. If I check in the calling function, with:
92
# self.assertEqual(count, sys.getrefcount(obj)-1)
93
# Then it works fine. Something about passing it to assertRefcount is
94
# actually double-incrementing (and decrementing) the refcount
95
self.assertEqual(count, sys.getrefcount(obj)-3)
97
def test_create(self):
98
k = self.module.StaticTuple('foo')
99
k = self.module.StaticTuple('foo', 'bar')
101
def test_create_bad_args(self):
104
self.assertRaises(ValueError, self.module.StaticTuple, *args_256)
106
self.assertRaises(ValueError, self.module.StaticTuple, *args_300)
108
self.assertRaises(TypeError, self.module.StaticTuple, object())
110
def test_concat(self):
111
st1 = self.module.StaticTuple('foo')
112
st2 = self.module.StaticTuple('bar')
113
st3 = self.module.StaticTuple('foo', 'bar')
115
self.assertEqual(st3, st4)
116
self.assertIsInstance(st4, self.module.StaticTuple)
118
def test_concat_with_tuple(self):
119
st1 = self.module.StaticTuple('foo')
121
st3 = self.module.StaticTuple('foo', 'bar')
122
st4 = self.module.StaticTuple('bar', 'foo')
125
self.assertEqual(st3, st5)
126
self.assertIsInstance(st5, self.module.StaticTuple)
127
self.assertEqual(st4, st6)
128
if self.module is _static_tuple_py:
129
# _static_tuple_py has StaticTuple(tuple), so tuple thinks it
130
# already knows how to concatenate, as such we can't "inject" our
131
# own concatenation...
132
self.assertIsInstance(st6, tuple)
134
self.assertIsInstance(st6, self.module.StaticTuple)
136
def test_concat_with_bad_tuple(self):
137
st1 = self.module.StaticTuple('foo')
139
# Using st1.__add__ doesn't give the same results as doing the '+' form
140
self.assertRaises(TypeError, lambda: st1 + t2)
142
def test_concat_with_non_tuple(self):
143
st1 = self.module.StaticTuple('foo')
144
self.assertRaises(TypeError, lambda: st1 + 10)
146
def test_as_tuple(self):
147
k = self.module.StaticTuple('foo')
149
self.assertEqual(('foo',), t)
150
k = self.module.StaticTuple('foo', 'bar')
152
self.assertEqual(('foo', 'bar'), t)
155
k = self.module.StaticTuple()
156
self.assertEqual(0, len(k))
157
k = self.module.StaticTuple('foo')
158
self.assertEqual(1, len(k))
159
k = self.module.StaticTuple('foo', 'bar')
160
self.assertEqual(2, len(k))
161
k = self.module.StaticTuple('foo', 'bar', 'b', 'b', 'b', 'b', 'b')
162
self.assertEqual(7, len(k))
164
k = self.module.StaticTuple(*args)
165
self.assertEqual(255, len(k))
167
def test_hold_other_static_tuples(self):
168
k = self.module.StaticTuple('foo', 'bar')
169
k2 = self.module.StaticTuple(k, k)
170
self.assertEqual(2, len(k2))
171
self.assertIs(k, k2[0])
172
self.assertIs(k, k2[1])
174
def test_getitem(self):
175
k = self.module.StaticTuple('foo', 'bar', 'b', 'b', 'b', 'b', 'z')
176
self.assertEqual('foo', k[0])
177
self.assertEqual('foo', k[0])
178
self.assertEqual('foo', k[0])
179
self.assertEqual('z', k[6])
180
self.assertEqual('z', k[-1])
181
self.assertRaises(IndexError, k.__getitem__, 7)
182
self.assertRaises(IndexError, k.__getitem__, 256+7)
183
self.assertRaises(IndexError, k.__getitem__, 12024)
184
# Python's [] resolver handles the negative arguments, so we can't
185
# really test StaticTuple_item() with negative values.
186
self.assertRaises(TypeError, k.__getitem__, 'not-an-int')
187
self.assertRaises(TypeError, k.__getitem__, '5')
189
def test_refcount(self):
191
num_refs = sys.getrefcount(f) - 1 #sys.getrefcount() adds one
192
k = self.module.StaticTuple(f)
193
self.assertRefcount(num_refs + 1, f)
195
self.assertRefcount(num_refs + 2, f)
197
self.assertRefcount(num_refs + 2, f)
199
self.assertRefcount(num_refs + 3, f)
201
self.assertRefcount(num_refs + 1, f)
203
self.assertRefcount(num_refs, f)
205
def test__repr__(self):
206
k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
207
self.assertEqual("StaticTuple('foo', 'bar', 'baz', 'bing')", repr(k))
209
def assertCompareEqual(self, k1, k2):
210
self.assertTrue(k1 == k2)
211
self.assertTrue(k1 <= k2)
212
self.assertTrue(k1 >= k2)
213
self.assertFalse(k1 != k2)
214
self.assertFalse(k1 < k2)
215
self.assertFalse(k1 > k2)
217
def test_holds_None(self):
218
k1 = self.module.StaticTuple(None)
219
# You cannot subclass None anyway
221
def test_holds_int(self):
222
k1 = self.module.StaticTuple(1)
225
# But not a subclass, because subint could introduce refcycles
226
self.assertRaises(TypeError, self.module.StaticTuple, subint(2))
228
def test_holds_long(self):
229
k1 = self.module.StaticTuple(2L**65)
233
self.assertRaises(TypeError, self.module.StaticTuple, sublong(1))
235
def test_holds_float(self):
236
k1 = self.module.StaticTuple(1.2)
237
class subfloat(float):
239
self.assertRaises(TypeError, self.module.StaticTuple, subfloat(1.5))
241
def test_holds_str(self):
242
k1 = self.module.StaticTuple('astring')
245
self.assertRaises(TypeError, self.module.StaticTuple, substr('a'))
247
def test_holds_unicode(self):
248
k1 = self.module.StaticTuple(u'\xb5')
249
class subunicode(unicode):
251
self.assertRaises(TypeError, self.module.StaticTuple,
254
def test_hold_bool(self):
255
k1 = self.module.StaticTuple(True)
256
k2 = self.module.StaticTuple(False)
257
# Cannot subclass bool
259
def test_compare_same_obj(self):
260
k1 = self.module.StaticTuple('foo', 'bar')
261
self.assertCompareEqual(k1, k1)
262
k2 = self.module.StaticTuple(k1, k1)
263
self.assertCompareEqual(k2, k2)
264
k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
266
self.assertCompareEqual(k3, k3)
268
def test_compare_equivalent_obj(self):
269
k1 = self.module.StaticTuple('foo', 'bar')
270
k2 = self.module.StaticTuple('foo', 'bar')
271
self.assertCompareEqual(k1, k2)
272
k3 = self.module.StaticTuple(k1, k2)
273
k4 = self.module.StaticTuple(k2, k1)
274
self.assertCompareEqual(k1, k2)
275
k5 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
277
k6 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
279
self.assertCompareEqual(k5, k6)
280
k7 = self.module.StaticTuple(None)
281
k8 = self.module.StaticTuple(None)
282
self.assertCompareEqual(k7, k8)
284
def test_compare_similar_obj(self):
285
k1 = self.module.StaticTuple('foo' + ' bar', 'bar' + ' baz')
286
k2 = self.module.StaticTuple('fo' + 'o bar', 'ba' + 'r baz')
287
self.assertCompareEqual(k1, k2)
288
k3 = self.module.StaticTuple('foo ' + 'bar', 'bar ' + 'baz')
289
k4 = self.module.StaticTuple('f' + 'oo bar', 'b' + 'ar baz')
290
k5 = self.module.StaticTuple(k1, k2)
291
k6 = self.module.StaticTuple(k3, k4)
292
self.assertCompareEqual(k5, k6)
294
def assertCompareDifferent(self, k_small, k_big):
295
self.assertFalse(k_small == k_big)
296
self.assertFalse(k_small >= k_big)
297
self.assertFalse(k_small > k_big)
298
self.assertTrue(k_small != k_big)
299
self.assertTrue(k_small <= k_big)
300
self.assertTrue(k_small < k_big)
302
def assertCompareNoRelation(self, k1, k2):
303
"""Run the comparison operators, make sure they do something.
305
However, we don't actually care what comes first or second. This is
306
stuff like cross-class comparisons. We don't want to segfault/raise an
307
exception, but we don't care about the sort order.
309
self.assertFalse(k1 == k2)
310
self.assertTrue(k1 != k2)
311
# Do the comparison, but we don't care about the result
317
def test_compare_vs_none(self):
318
k1 = self.module.StaticTuple('baz', 'bing')
319
self.assertCompareDifferent(None, k1)
321
def test_compare_cross_class(self):
322
k1 = self.module.StaticTuple('baz', 'bing')
323
self.assertCompareNoRelation(10, k1)
324
self.assertCompareNoRelation('baz', k1)
326
def test_compare_all_different_same_width(self):
327
k1 = self.module.StaticTuple('baz', 'bing')
328
k2 = self.module.StaticTuple('foo', 'bar')
329
self.assertCompareDifferent(k1, k2)
330
k3 = self.module.StaticTuple(k1, k2)
331
k4 = self.module.StaticTuple(k2, k1)
332
self.assertCompareDifferent(k3, k4)
333
k5 = self.module.StaticTuple(1)
334
k6 = self.module.StaticTuple(2)
335
self.assertCompareDifferent(k5, k6)
336
k7 = self.module.StaticTuple(1.2)
337
k8 = self.module.StaticTuple(2.4)
338
self.assertCompareDifferent(k7, k8)
339
k9 = self.module.StaticTuple(u's\xb5')
340
k10 = self.module.StaticTuple(u's\xe5')
341
self.assertCompareDifferent(k9, k10)
343
def test_compare_some_different(self):
344
k1 = self.module.StaticTuple('foo', 'bar')
345
k2 = self.module.StaticTuple('foo', 'zzz')
346
self.assertCompareDifferent(k1, k2)
347
k3 = self.module.StaticTuple(k1, k1)
348
k4 = self.module.StaticTuple(k1, k2)
349
self.assertCompareDifferent(k3, k4)
350
k5 = self.module.StaticTuple('foo', None)
351
self.assertCompareDifferent(k5, k1)
352
self.assertCompareDifferent(k5, k2)
354
def test_compare_diff_width(self):
355
k1 = self.module.StaticTuple('foo')
356
k2 = self.module.StaticTuple('foo', 'bar')
357
self.assertCompareDifferent(k1, k2)
358
k3 = self.module.StaticTuple(k1)
359
k4 = self.module.StaticTuple(k1, k2)
360
self.assertCompareDifferent(k3, k4)
362
def test_compare_different_types(self):
363
k1 = self.module.StaticTuple('foo', 'bar')
364
k2 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
366
self.assertCompareNoRelation(k1, k2)
367
k3 = self.module.StaticTuple('foo')
368
self.assertCompareDifferent(k3, k1)
369
k4 = self.module.StaticTuple(None)
370
self.assertCompareDifferent(k4, k1)
371
k5 = self.module.StaticTuple(1)
372
self.assertCompareNoRelation(k1, k5)
374
def test_compare_to_tuples(self):
375
k1 = self.module.StaticTuple('foo')
376
self.assertCompareEqual(k1, ('foo',))
377
self.assertCompareEqual(('foo',), k1)
378
self.assertCompareDifferent(k1, ('foo', 'bar'))
379
self.assertCompareDifferent(k1, ('foo', 10))
381
k2 = self.module.StaticTuple('foo', 'bar')
382
self.assertCompareEqual(k2, ('foo', 'bar'))
383
self.assertCompareEqual(('foo', 'bar'), k2)
384
self.assertCompareDifferent(k2, ('foo', 'zzz'))
385
self.assertCompareDifferent(('foo',), k2)
386
self.assertCompareDifferent(('foo', 'aaa'), k2)
387
self.assertCompareDifferent(('baz', 'bing'), k2)
388
self.assertCompareDifferent(('foo', 10), k2)
390
k3 = self.module.StaticTuple(k1, k2)
391
self.assertCompareEqual(k3, (('foo',), ('foo', 'bar')))
392
self.assertCompareEqual((('foo',), ('foo', 'bar')), k3)
393
self.assertCompareEqual(k3, (k1, ('foo', 'bar')))
394
self.assertCompareEqual((k1, ('foo', 'bar')), k3)
396
def test_compare_mixed_depths(self):
397
stuple = self.module.StaticTuple
398
k1 = stuple(stuple('a',), stuple('b',))
399
k2 = stuple(stuple(stuple('c',), stuple('d',)),
401
# This requires comparing a StaticTuple to a 'string', and then
402
# interpreting that value in the next higher StaticTuple. This used to
403
# generate a PyErr_BadIternalCall. We now fall back to *something*.
404
self.assertCompareNoRelation(k1, k2)
407
k = self.module.StaticTuple('foo')
408
self.assertEqual(hash(k), hash(('foo',)))
409
k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
410
as_tuple = ('foo', 'bar', 'baz', 'bing')
411
self.assertEqual(hash(k), hash(as_tuple))
413
# Because k == , it replaces the slot, rather than having both
414
# present in the dict.
415
self.assertEqual('foo', x[as_tuple])
417
self.assertEqual({as_tuple: 'bar'}, x)
419
k2 = self.module.StaticTuple(k)
420
as_tuple2 = (('foo', 'bar', 'baz', 'bing'),)
421
self.assertEqual(hash(k2), hash(as_tuple2))
423
k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
425
as_tuple3 = ('foo', 1, None, u'\xb5', 1.2, 2**65, True, k)
426
self.assertEqual(hash(as_tuple3), hash(k3))
428
def test_slice(self):
429
k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
430
self.assertEqual(('foo', 'bar'), k[:2])
431
self.assertEqual(('baz',), k[2:-1])
435
# C implementation raises a TypeError, we don't need the
436
# implementation yet, so allow this to pass
439
# Python implementation uses a regular Tuple, so make sure it gives
441
self.assertEqual(('foo', 'baz'), val)
443
def test_referents(self):
444
# We implement tp_traverse so that things like 'meliae' can measure the
445
# amount of referenced memory. Unfortunately gc.get_referents() first
446
# checks the IS_GC flag before it traverses anything. We could write a
447
# helper func, but that won't work for the generic implementation...
448
self.requireFeature(Meliae)
449
from meliae import scanner
450
strs = ['foo', 'bar', 'baz', 'bing']
451
k = self.module.StaticTuple(*strs)
452
if self.module is _static_tuple_py:
453
refs = strs + [self.module.StaticTuple]
456
self.assertEqual(sorted(refs), sorted(scanner.get_referents(k)))
458
def test_nested_referents(self):
459
self.requireFeature(Meliae)
460
from meliae import scanner
461
strs = ['foo', 'bar', 'baz', 'bing']
462
k1 = self.module.StaticTuple(*strs[:2])
463
k2 = self.module.StaticTuple(*strs[2:])
464
k3 = self.module.StaticTuple(k1, k2)
466
if self.module is _static_tuple_py:
467
refs.append(self.module.StaticTuple)
468
self.assertEqual(sorted(refs),
469
sorted(scanner.get_referents(k3)))
471
def test_empty_is_singleton(self):
472
key = self.module.StaticTuple()
473
self.assertIs(key, self.module._empty_tuple)
475
def test_intern(self):
476
unique_str1 = 'unique str ' + osutils.rand_chars(20)
477
unique_str2 = 'unique str ' + osutils.rand_chars(20)
478
key = self.module.StaticTuple(unique_str1, unique_str2)
479
self.assertFalse(key in self.module._interned_tuples)
480
key2 = self.module.StaticTuple(unique_str1, unique_str2)
481
self.assertEqual(key, key2)
482
self.assertIsNot(key, key2)
484
self.assertIs(key, key3)
485
self.assertTrue(key in self.module._interned_tuples)
486
self.assertEqual(key, self.module._interned_tuples[key])
488
self.assertIs(key, key2)
490
def test__c_intern_handles_refcount(self):
491
if self.module is _static_tuple_py:
492
return # Not applicable
493
unique_str1 = 'unique str ' + osutils.rand_chars(20)
494
unique_str2 = 'unique str ' + osutils.rand_chars(20)
495
key = self.module.StaticTuple(unique_str1, unique_str2)
496
self.assertRefcount(1, key)
497
self.assertFalse(key in self.module._interned_tuples)
498
self.assertFalse(key._is_interned())
499
key2 = self.module.StaticTuple(unique_str1, unique_str2)
500
self.assertRefcount(1, key)
501
self.assertRefcount(1, key2)
502
self.assertEqual(key, key2)
503
self.assertIsNot(key, key2)
506
self.assertIs(key, key3)
507
self.assertTrue(key in self.module._interned_tuples)
508
self.assertEqual(key, self.module._interned_tuples[key])
509
# key and key3, but we 'hide' the one in _interned_tuples
510
self.assertRefcount(2, key)
512
self.assertRefcount(1, key)
513
self.assertTrue(key._is_interned())
514
self.assertRefcount(1, key2)
516
# key3 now points to key as well, and *not* to key2
517
self.assertRefcount(2, key)
518
self.assertRefcount(1, key2)
519
self.assertIs(key, key3)
520
self.assertIsNot(key3, key2)
523
self.assertRefcount(1, key)
525
def test__c_keys_are_not_immortal(self):
526
if self.module is _static_tuple_py:
527
return # Not applicable
528
unique_str1 = 'unique str ' + osutils.rand_chars(20)
529
unique_str2 = 'unique str ' + osutils.rand_chars(20)
530
key = self.module.StaticTuple(unique_str1, unique_str2)
531
self.assertFalse(key in self.module._interned_tuples)
532
self.assertRefcount(1, key)
534
self.assertRefcount(1, key)
535
self.assertTrue(key in self.module._interned_tuples)
536
self.assertTrue(key._is_interned())
538
# Create a new entry, which would point to the same location
539
key = self.module.StaticTuple(unique_str1, unique_str2)
540
self.assertRefcount(1, key)
541
# This old entry in _interned_tuples should be gone
542
self.assertFalse(key in self.module._interned_tuples)
543
self.assertFalse(key._is_interned())
545
def test__c_has_C_API(self):
546
if self.module is _static_tuple_py:
548
self.assertIsNot(None, self.module._C_API)
550
def test_from_sequence_tuple(self):
551
st = self.module.StaticTuple.from_sequence(('foo', 'bar'))
552
self.assertIsInstance(st, self.module.StaticTuple)
553
self.assertEqual(('foo', 'bar'), st)
555
def test_from_sequence_str(self):
556
st = self.module.StaticTuple.from_sequence('foo')
557
self.assertIsInstance(st, self.module.StaticTuple)
558
self.assertEqual(('f', 'o', 'o'), st)
560
def test_from_sequence_list(self):
561
st = self.module.StaticTuple.from_sequence(['foo', 'bar'])
562
self.assertIsInstance(st, self.module.StaticTuple)
563
self.assertEqual(('foo', 'bar'), st)
565
def test_from_sequence_static_tuple(self):
566
st = self.module.StaticTuple('foo', 'bar')
567
st2 = self.module.StaticTuple.from_sequence(st)
568
# If the source is a StaticTuple already, we return the exact object
569
self.assertIs(st, st2)
571
def test_from_sequence_not_sequence(self):
572
self.assertRaises(TypeError,
573
self.module.StaticTuple.from_sequence, object())
574
self.assertRaises(TypeError,
575
self.module.StaticTuple.from_sequence, 10)
577
def test_from_sequence_incorrect_args(self):
578
self.assertRaises(TypeError,
579
self.module.StaticTuple.from_sequence, object(), 'a')
580
self.assertRaises(TypeError,
581
self.module.StaticTuple.from_sequence, foo='a')
583
def test_from_sequence_iterable(self):
584
st = self.module.StaticTuple.from_sequence(iter(['foo', 'bar']))
585
self.assertIsInstance(st, self.module.StaticTuple)
586
self.assertEqual(('foo', 'bar'), st)
588
def test_from_sequence_generator(self):
589
def generate_tuple():
592
st = self.module.StaticTuple.from_sequence(generate_tuple())
593
self.assertIsInstance(st, self.module.StaticTuple)
594
self.assertEqual(('foo', 'bar'), st)
596
def test_pickle(self):
597
st = self.module.StaticTuple('foo', 'bar')
598
pickled = cPickle.dumps(st)
599
unpickled = cPickle.loads(pickled)
600
self.assertEqual(unpickled, st)
602
def test_pickle_empty(self):
603
st = self.module.StaticTuple()
604
pickled = cPickle.dumps(st)
605
unpickled = cPickle.loads(pickled)
606
self.assertIs(st, unpickled)
608
def test_pickle_nested(self):
609
st = self.module.StaticTuple('foo', self.module.StaticTuple('bar'))
610
pickled = cPickle.dumps(st)
611
unpickled = cPickle.loads(pickled)
612
self.assertEqual(unpickled, st)
614
def test_static_tuple_thunk(self):
615
# Make sure the right implementation is available from
616
# bzrlib.static_tuple.StaticTuple.
617
if self.module is _static_tuple_py:
618
if CompiledStaticTuple.available():
619
# We will be using the C version
621
self.assertIs(static_tuple.StaticTuple,
622
self.module.StaticTuple)