1
# Copyright (C) 2009, 2010 Canonical Ltd
1
# Copyright (C) 2009, 2010, 2011 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
17
17
"""Tests for the StaticTuple type."""
20
import cPickle as pickle
33
def load_tests(standard_tests, module, loader):
32
from breezy.sixish import (
36
from breezy.tests import (
41
def load_tests(loader, standard_tests, pattern):
34
42
"""Parameterize tests for all versions of groupcompress."""
35
43
global compiled_static_tuple_feature
36
44
suite, compiled_static_tuple_feature = tests.permute_tests_for_extension(
37
standard_tests, loader, 'bzrlib._static_tuple_py',
38
'bzrlib._static_tuple_c')
45
standard_tests, loader, 'breezy._static_tuple_py',
46
'breezy._static_tuple_c')
42
class _Meliae(tests.Feature):
46
from meliae import scanner
51
def feature_name(self):
52
return "Meliae - python memory debugger"
57
50
class TestStaticTuple(tests.TestCase):
59
52
def assertRefcount(self, count, obj):
227
220
self.assertRaises(TypeError, self.module.StaticTuple, subint(2))
229
222
def test_holds_long(self):
230
k1 = self.module.StaticTuple(2L**65)
224
self.skipTest("No long type on Python 3")
225
k1 = self.module.StaticTuple(2**65)
231
226
class sublong(long):
233
228
# But not a subclass
240
235
self.assertRaises(TypeError, self.module.StaticTuple, subfloat(1.5))
242
def test_holds_str(self):
243
k1 = self.module.StaticTuple('astring')
237
def test_holds_bytes(self):
238
k1 = self.module.StaticTuple(b'astring')
246
self.assertRaises(TypeError, self.module.StaticTuple, substr('a'))
241
self.assertRaises(TypeError, self.module.StaticTuple, substr(b'a'))
248
243
def test_holds_unicode(self):
249
244
k1 = self.module.StaticTuple(u'\xb5')
250
class subunicode(unicode):
245
class subunicode(text_type):
252
247
self.assertRaises(TypeError, self.module.StaticTuple,
253
248
subunicode(u'\xb5'))
430
425
k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
431
426
self.assertEqual(('foo', 'bar'), k[:2])
432
427
self.assertEqual(('baz',), k[2:-1])
436
# C implementation raises a TypeError, we don't need the
437
# implementation yet, so allow this to pass
440
# Python implementation uses a regular Tuple, so make sure it gives
442
self.assertEqual(('foo', 'baz'), val)
428
self.assertEqual(('foo', 'baz',), k[::2])
429
self.assertRaises(TypeError, k.__getitem__, 'not_slice')
444
431
def test_referents(self):
445
432
# We implement tp_traverse so that things like 'meliae' can measure the
446
433
# amount of referenced memory. Unfortunately gc.get_referents() first
447
434
# checks the IS_GC flag before it traverses anything. We could write a
448
435
# helper func, but that won't work for the generic implementation...
449
self.requireFeature(Meliae)
436
self.requireFeature(features.meliae)
450
437
from meliae import scanner
451
438
strs = ['foo', 'bar', 'baz', 'bing']
452
439
k = self.module.StaticTuple(*strs)
457
444
self.assertEqual(sorted(refs), sorted(scanner.get_referents(k)))
459
446
def test_nested_referents(self):
460
self.requireFeature(Meliae)
447
self.requireFeature(features.meliae)
461
448
from meliae import scanner
462
449
strs = ['foo', 'bar', 'baz', 'bing']
463
450
k1 = self.module.StaticTuple(*strs[:2])
597
584
def test_pickle(self):
598
585
st = self.module.StaticTuple('foo', 'bar')
599
pickled = cPickle.dumps(st)
600
unpickled = cPickle.loads(pickled)
586
pickled = pickle.dumps(st)
587
unpickled = pickle.loads(pickled)
601
588
self.assertEqual(unpickled, st)
603
590
def test_pickle_empty(self):
604
591
st = self.module.StaticTuple()
605
pickled = cPickle.dumps(st)
606
unpickled = cPickle.loads(pickled)
592
pickled = pickle.dumps(st)
593
unpickled = pickle.loads(pickled)
607
594
self.assertIs(st, unpickled)
609
596
def test_pickle_nested(self):
610
597
st = self.module.StaticTuple('foo', self.module.StaticTuple('bar'))
611
pickled = cPickle.dumps(st)
612
unpickled = cPickle.loads(pickled)
598
pickled = pickle.dumps(st)
599
unpickled = pickle.loads(pickled)
613
600
self.assertEqual(unpickled, st)
615
602
def test_static_tuple_thunk(self):
616
603
# Make sure the right implementation is available from
617
# bzrlib.static_tuple.StaticTuple.
604
# breezy.static_tuple.StaticTuple.
618
605
if self.module is _static_tuple_py:
619
606
if compiled_static_tuple_feature.available():
620
607
# We will be using the C version