1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
# Copyright (C) 2009 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Tests for the StaticTuple type."""
import gc
import sys
from bzrlib import (
_static_tuple_py,
errors,
osutils,
tests,
)
def load_tests(standard_tests, module, loader):
"""Parameterize tests for all versions of groupcompress."""
scenarios = [
('python', {'module': _static_tuple_py}),
]
suite = loader.suiteClass()
if CompiledStaticTuple.available():
from bzrlib import _static_tuple_c
scenarios.append(('C', {'module': _static_tuple_c}))
else:
# the compiled module isn't available, so we add a failing test
class FailWithoutFeature(tests.TestCase):
def test_fail(self):
self.requireFeature(CompiledStaticTuple)
suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
result = tests.multiply_tests(standard_tests, scenarios, suite)
return result
class _CompiledStaticTuple(tests.Feature):
def _probe(self):
try:
import bzrlib._static_tuple_c
except ImportError:
return False
return True
def feature_name(self):
return 'bzrlib._static_tuple_c'
CompiledStaticTuple = _CompiledStaticTuple()
class _Meliae(tests.Feature):
def _probe(self):
try:
from meliae import scanner
except ImportError:
return False
return True
def feature_name(self):
return "Meliae - python memory debugger"
Meliae = _Meliae()
class TestStaticTuple(tests.TestCase):
def assertRefcount(self, count, obj):
"""Assert that the refcount for obj is what we expect.
Note that this automatically adjusts for the fact that calling
assertRefcount actually creates a new pointer, as does calling
sys.getrefcount. So pass the expected value *before* the call.
"""
# I don't understand why it is getrefcount()-3 here, but it seems to be
# correct. If I check in the calling function, with:
# self.assertEqual(count, sys.getrefcount(obj)-1)
# Then it works fine. Something about passing it to assertRefcount is
# actually double-incrementing (and decrementing) the refcount
self.assertEqual(count, sys.getrefcount(obj)-3)
def test_create(self):
k = self.module.StaticTuple('foo')
k = self.module.StaticTuple('foo', 'bar')
def test_create_bad_args(self):
args_256 = ['a']*256
# too many args
self.assertRaises(ValueError, self.module.StaticTuple, *args_256)
args_300 = ['a']*300
self.assertRaises(ValueError, self.module.StaticTuple, *args_300)
# not a string
self.assertRaises(TypeError, self.module.StaticTuple, 10)
def test_as_tuple(self):
k = self.module.StaticTuple('foo')
t = k.as_tuple()
self.assertEqual(('foo',), t)
k = self.module.StaticTuple('foo', 'bar')
t = k.as_tuple()
self.assertEqual(('foo', 'bar'), t)
def test_len(self):
k = self.module.StaticTuple()
self.assertEqual(0, len(k))
k = self.module.StaticTuple('foo')
self.assertEqual(1, len(k))
k = self.module.StaticTuple('foo', 'bar')
self.assertEqual(2, len(k))
k = self.module.StaticTuple('foo', 'bar', 'b', 'b', 'b', 'b', 'b')
self.assertEqual(7, len(k))
args = ['foo']*255
k = self.module.StaticTuple(*args)
self.assertEqual(255, len(k))
def test_hold_other_static_tuples(self):
k = self.module.StaticTuple('foo', 'bar')
k2 = self.module.StaticTuple(k, k)
self.assertEqual(2, len(k2))
self.assertIs(k, k2[0])
self.assertIs(k, k2[1])
def test_getitem(self):
k = self.module.StaticTuple('foo', 'bar', 'b', 'b', 'b', 'b', 'z')
self.assertEqual('foo', k[0])
self.assertEqual('foo', k[0])
self.assertEqual('foo', k[0])
self.assertEqual('z', k[6])
self.assertEqual('z', k[-1])
self.assertRaises(IndexError, k.__getitem__, 7)
self.assertRaises(IndexError, k.__getitem__, 256+7)
self.assertRaises(IndexError, k.__getitem__, 12024)
# Python's [] resolver handles the negative arguments, so we can't
# really test StaticTuple_item() with negative values.
self.assertRaises(TypeError, k.__getitem__, 'not-an-int')
self.assertRaises(TypeError, k.__getitem__, '5')
def test_refcount(self):
f = 'fo' + 'oo'
num_refs = sys.getrefcount(f) - 1 #sys.getrefcount() adds one
k = self.module.StaticTuple(f)
self.assertRefcount(num_refs + 1, f)
b = k[0]
self.assertRefcount(num_refs + 2, f)
b = k[0]
self.assertRefcount(num_refs + 2, f)
c = k[0]
self.assertRefcount(num_refs + 3, f)
del b, c
self.assertRefcount(num_refs + 1, f)
del k
self.assertRefcount(num_refs, f)
def test__repr__(self):
k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
self.assertEqual("StaticTuple('foo', 'bar', 'baz', 'bing')", repr(k))
def assertCompareEqual(self, k1, k2):
self.assertTrue(k1 == k2)
self.assertTrue(k1 <= k2)
self.assertTrue(k1 >= k2)
self.assertFalse(k1 != k2)
self.assertFalse(k1 < k2)
self.assertFalse(k1 > k2)
def test_compare_same_obj(self):
k1 = self.module.StaticTuple('foo', 'bar')
self.assertCompareEqual(k1, k1)
k2 = self.module.StaticTuple(k1, k1)
self.assertCompareEqual(k2, k2)
def test_compare_equivalent_obj(self):
k1 = self.module.StaticTuple('foo', 'bar')
k2 = self.module.StaticTuple('foo', 'bar')
self.assertCompareEqual(k1, k2)
k3 = self.module.StaticTuple(k1, k2)
k4 = self.module.StaticTuple(k2, k1)
self.assertCompareEqual(k1, k2)
def test_compare_similar_obj(self):
k1 = self.module.StaticTuple('foo' + ' bar', 'bar' + ' baz')
k2 = self.module.StaticTuple('fo' + 'o bar', 'ba' + 'r baz')
self.assertCompareEqual(k1, k2)
k3 = self.module.StaticTuple('foo ' + 'bar', 'bar ' + 'baz')
k4 = self.module.StaticTuple('f' + 'oo bar', 'b' + 'ar baz')
k5 = self.module.StaticTuple(k1, k2)
k6 = self.module.StaticTuple(k3, k4)
self.assertCompareEqual(k5, k6)
def assertCompareDifferent(self, k_small, k_big):
self.assertFalse(k_small == k_big)
self.assertFalse(k_small >= k_big)
self.assertFalse(k_small > k_big)
self.assertTrue(k_small != k_big)
self.assertTrue(k_small <= k_big)
self.assertTrue(k_small < k_big)
def assertCompareNoRelation(self, k1, k2):
"""Run the comparison operators, make sure they do something.
However, we don't actually care what comes first or second. This is
stuff like cross-class comparisons. We don't want to segfault/raise an
exception, but we don't care about the sort order.
"""
self.assertFalse(k1 == k2)
self.assertTrue(k1 != k2)
# Do the comparison, but we don't care about the result
k1 >= k2
k1 > k2
k1 <= k2
k1 < k2
def test_compare_vs_none(self):
k1 = self.module.StaticTuple('baz', 'bing')
self.assertCompareDifferent(None, k1)
def test_compare_cross_class(self):
k1 = self.module.StaticTuple('baz', 'bing')
self.assertCompareNoRelation(10, k1)
self.assertCompareNoRelation('baz', k1)
def test_compare_all_different_same_width(self):
k1 = self.module.StaticTuple('baz', 'bing')
k2 = self.module.StaticTuple('foo', 'bar')
self.assertCompareDifferent(k1, k2)
k3 = self.module.StaticTuple(k1, k2)
k4 = self.module.StaticTuple(k2, k1)
self.assertCompareDifferent(k3, k4)
def test_compare_some_different(self):
k1 = self.module.StaticTuple('foo', 'bar')
k2 = self.module.StaticTuple('foo', 'zzz')
self.assertCompareDifferent(k1, k2)
k3 = self.module.StaticTuple(k1, k1)
k4 = self.module.StaticTuple(k1, k2)
self.assertCompareDifferent(k3, k4)
def test_compare_diff_width(self):
k1 = self.module.StaticTuple('foo')
k2 = self.module.StaticTuple('foo', 'bar')
self.assertCompareDifferent(k1, k2)
k3 = self.module.StaticTuple(k1)
k4 = self.module.StaticTuple(k1, k2)
self.assertCompareDifferent(k3, k4)
def test_compare_to_tuples(self):
k1 = self.module.StaticTuple('foo')
self.assertCompareEqual(k1, ('foo',))
self.assertCompareEqual(('foo',), k1)
self.assertCompareDifferent(k1, ('foo', 'bar'))
self.assertCompareDifferent(k1, ('foo', 10))
k2 = self.module.StaticTuple('foo', 'bar')
self.assertCompareEqual(k2, ('foo', 'bar'))
self.assertCompareEqual(('foo', 'bar'), k2)
self.assertCompareDifferent(k2, ('foo', 'zzz'))
self.assertCompareDifferent(('foo',), k2)
self.assertCompareDifferent(('foo', 'aaa'), k2)
self.assertCompareDifferent(('baz', 'bing'), k2)
self.assertCompareDifferent(('foo', 10), k2)
k3 = self.module.StaticTuple(k1, k2)
self.assertCompareEqual(k3, (('foo',), ('foo', 'bar')))
self.assertCompareEqual((('foo',), ('foo', 'bar')), k3)
self.assertCompareEqual(k3, (k1, ('foo', 'bar')))
self.assertCompareEqual((k1, ('foo', 'bar')), k3)
def test_hash(self):
k = self.module.StaticTuple('foo')
self.assertEqual(hash(k), hash(('foo',)))
k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
as_tuple = ('foo', 'bar', 'baz', 'bing')
self.assertEqual(hash(k), hash(as_tuple))
x = {k: 'foo'}
# Because k == , it replaces the slot, rather than having both
# present in the dict.
self.assertEqual('foo', x[as_tuple])
x[as_tuple] = 'bar'
self.assertEqual({as_tuple: 'bar'}, x)
k2 = self.module.StaticTuple(k)
as_tuple2 = (('foo', 'bar', 'baz', 'bing'),)
self.assertEqual(hash(k2), hash(as_tuple2))
def test_slice(self):
k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
self.assertEqual(('foo', 'bar'), k[:2])
self.assertEqual(('baz',), k[2:-1])
try:
val = k[::2]
except TypeError:
# C implementation raises a TypeError, we don't need the
# implementation yet, so allow this to pass
pass
else:
# Python implementation uses a regular Tuple, so make sure it gives
# the right result
self.assertEqual(('foo', 'baz'), val)
def test_referents(self):
# We implement tp_traverse so that things like 'meliae' can measure the
# amount of referenced memory. Unfortunately gc.get_referents() first
# checks the IS_GC flag before it traverses anything. We could write a
# helper func, but that won't work for the generic implementation...
self.requireFeature(Meliae)
from meliae import scanner
strs = ['foo', 'bar', 'baz', 'bing']
k = self.module.StaticTuple(*strs)
if self.module is _static_tuple_py:
refs = strs + [self.module.StaticTuple]
else:
refs = strs
self.assertEqual(sorted(refs), sorted(scanner.get_referents(k)))
def test_nested_referents(self):
self.requireFeature(Meliae)
from meliae import scanner
strs = ['foo', 'bar', 'baz', 'bing']
k1 = self.module.StaticTuple(*strs[:2])
k2 = self.module.StaticTuple(*strs[2:])
k3 = self.module.StaticTuple(k1, k2)
refs = [k1, k2]
if self.module is _static_tuple_py:
refs.append(self.module.StaticTuple)
self.assertEqual(sorted(refs),
sorted(scanner.get_referents(k3)))
def test_empty_is_singleton(self):
key = self.module.StaticTuple()
self.assertIs(key, self.module._empty_tuple)
def test_intern(self):
unique_str1 = 'unique str ' + osutils.rand_chars(20)
unique_str2 = 'unique str ' + osutils.rand_chars(20)
key = self.module.StaticTuple(unique_str1, unique_str2)
self.assertFalse(key in self.module._interned_tuples)
key2 = self.module.StaticTuple(unique_str1, unique_str2)
self.assertEqual(key, key2)
self.assertIsNot(key, key2)
key3 = key.intern()
self.assertIs(key, key3)
self.assertTrue(key in self.module._interned_tuples)
self.assertEqual(key, self.module._interned_tuples[key])
key2 = key2.intern()
self.assertIs(key, key2)
def test__c_intern_handles_refcount(self):
if self.module is _static_tuple_py:
return # Not applicable
unique_str1 = 'unique str ' + osutils.rand_chars(20)
unique_str2 = 'unique str ' + osutils.rand_chars(20)
key = self.module.StaticTuple(unique_str1, unique_str2)
self.assertRefcount(1, key)
self.assertFalse(key in self.module._interned_tuples)
self.assertFalse(key._is_interned())
key2 = self.module.StaticTuple(unique_str1, unique_str2)
self.assertRefcount(1, key)
self.assertRefcount(1, key2)
self.assertEqual(key, key2)
self.assertIsNot(key, key2)
key3 = key.intern()
self.assertIs(key, key3)
self.assertTrue(key in self.module._interned_tuples)
self.assertEqual(key, self.module._interned_tuples[key])
# key and key3, but we 'hide' the one in _interned_tuples
self.assertRefcount(2, key)
del key3
self.assertRefcount(1, key)
self.assertTrue(key._is_interned())
self.assertRefcount(1, key2)
key3 = key2.intern()
# key3 now points to key as well, and *not* to key2
self.assertRefcount(2, key)
self.assertRefcount(1, key2)
self.assertIs(key, key3)
self.assertIsNot(key3, key2)
del key2
del key3
self.assertRefcount(1, key)
def test__c_keys_are_not_immortal(self):
if self.module is _static_tuple_py:
return # Not applicable
unique_str1 = 'unique str ' + osutils.rand_chars(20)
unique_str2 = 'unique str ' + osutils.rand_chars(20)
key = self.module.StaticTuple(unique_str1, unique_str2)
self.assertFalse(key in self.module._interned_tuples)
self.assertRefcount(1, key)
key = key.intern()
self.assertRefcount(1, key)
self.assertTrue(key in self.module._interned_tuples)
self.assertTrue(key._is_interned())
del key
# Create a new entry, which would point to the same location
key = self.module.StaticTuple(unique_str1, unique_str2)
self.assertRefcount(1, key)
# This old entry in _interned_tuples should be gone
self.assertFalse(key in self.module._interned_tuples)
self.assertFalse(key._is_interned())
def test__c_has_C_API(self):
if self.module is _static_tuple_py:
return
self.assertIsNot(None, self.module._C_API)
|