/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__static_tuple.py

Track down a few more import typos.

Show diffs side-by-side

added added

removed removed

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