79
class TestKeyType(tests.TestCase):
79
class TestStaticTuple(tests.TestCase):
81
81
def test_create(self):
82
k = self.module.Key('foo')
83
k = self.module.Key('foo', 'bar')
82
k = self.module.StaticTuple('foo')
83
k = self.module.StaticTuple('foo', 'bar')
85
85
def test_create_bad_args(self):
86
self.assertRaises(ValueError, self.module.Key)
86
self.assertRaises(ValueError, self.module.StaticTuple)
87
87
lots_of_args = ['a']*300
89
self.assertRaises(ValueError, self.module.Key, *lots_of_args)
89
self.assertRaises(ValueError, self.module.StaticTuple, *lots_of_args)
91
self.assertRaises(TypeError, self.module.Key, 10)
91
self.assertRaises(TypeError, self.module.StaticTuple, 10)
93
93
def test_as_tuple(self):
94
k = self.module.Key('foo')
94
k = self.module.StaticTuple('foo')
96
96
self.assertEqual(('foo',), t)
97
k = self.module.Key('foo', 'bar')
97
k = self.module.StaticTuple('foo', 'bar')
99
99
self.assertEqual(('foo', 'bar'), t)
101
101
def test_len(self):
102
k = self.module.Key('foo')
102
k = self.module.StaticTuple('foo')
103
103
self.assertEqual(1, len(k))
104
k = self.module.Key('foo', 'bar')
104
k = self.module.StaticTuple('foo', 'bar')
105
105
self.assertEqual(2, len(k))
106
k = self.module.Key('foo', 'bar', 'b', 'b', 'b', 'b', 'b')
106
k = self.module.StaticTuple('foo', 'bar', 'b', 'b', 'b', 'b', 'b')
107
107
self.assertEqual(7, len(k))
109
109
def test_getitem(self):
110
k = self.module.Key('foo', 'bar', 'b', 'b', 'b', 'b', 'z')
110
k = self.module.StaticTuple('foo', 'bar', 'b', 'b', 'b', 'b', 'z')
111
111
self.assertEqual('foo', k[0])
112
112
self.assertEqual('foo', k[0])
113
113
self.assertEqual('foo', k[0])
165
165
self.assertTrue(k_small < k_big)
167
167
def test_compare_all_different_same_width(self):
168
k1 = self.module.Key('baz', 'bing')
169
k2 = self.module.Key('foo', 'bar')
168
k1 = self.module.StaticTuple('baz', 'bing')
169
k2 = self.module.StaticTuple('foo', 'bar')
170
170
self.assertCompareDifferent(k1, k2)
172
172
def test_compare_some_different(self):
173
k1 = self.module.Key('foo', 'bar')
174
k2 = self.module.Key('foo', 'zzz')
173
k1 = self.module.StaticTuple('foo', 'bar')
174
k2 = self.module.StaticTuple('foo', 'zzz')
175
175
self.assertCompareDifferent(k1, k2)
177
177
def test_compare_diff_width(self):
178
k1 = self.module.Key('foo')
179
k2 = self.module.Key('foo', 'bar')
178
k1 = self.module.StaticTuple('foo')
179
k2 = self.module.StaticTuple('foo', 'bar')
180
180
self.assertCompareDifferent(k1, k2)
182
182
def test_compare_to_tuples(self):
183
k1 = self.module.Key('foo')
183
k1 = self.module.StaticTuple('foo')
184
184
self.assertCompareEqual(k1, ('foo',))
185
185
self.assertCompareEqual(('foo',), k1)
186
186
self.assertCompareDifferent(k1, ('foo', 'bar'))
187
187
self.assertCompareDifferent(k1, ('foo', 10))
189
k2 = self.module.Key('foo', 'bar')
189
k2 = self.module.StaticTuple('foo', 'bar')
190
190
self.assertCompareEqual(k2, ('foo', 'bar'))
191
191
self.assertCompareEqual(('foo', 'bar'), k2)
192
192
self.assertCompareDifferent(k2, ('foo', 'zzz'))
221
221
self.requireFeature(Meliae)
222
222
from meliae import scanner
223
223
strs = ['foo', 'bar', 'baz', 'bing']
224
k = self.module.Key(*strs)
225
if isinstance(k, _static_tuple_py.Key):
224
k = self.module.StaticTuple(*strs)
225
if isinstance(k, _static_tuple_py.StaticTuple):
226
226
# The python version references objects slightly different than the
227
227
# compiled version
228
self.assertEqual([k._tuple, _static_tuple_py.Key],
228
self.assertEqual([k._tuple, _static_tuple_py.StaticTuple],
229
229
scanner.get_referents(k))
231
231
self.assertEqual(sorted(strs), sorted(scanner.get_referents(k)))
234
class TestKeysType(tests.TestCase):
236
def test_create(self):
237
k = self.module.Keys(1, 'foo', 'bar')
238
k = self.module.Keys(2, 'foo', 'bar')
240
def test_create_bad_args(self):
241
self.assertRaises(TypeError, self.module.Keys)
242
self.assertRaises(TypeError, self.module.Keys, 'foo')
243
self.assertRaises(ValueError, self.module.Keys, 0)
244
self.assertRaises(ValueError, self.module.Keys, -1)
245
self.assertRaises(ValueError, self.module.Keys, -200)
246
self.assertRaises(ValueError, self.module.Keys, 2, 'foo')
247
self.assertRaises(ValueError, self.module.Keys, 257)
248
lots_of_args = ['a']*300
250
self.assertRaises(ValueError, self.module.Keys, 1, *lots_of_args)
251
self.assertRaises(TypeError, self.module.Keys, 1, 'foo', 10)
253
def test_create_and_del_correct_refcount(self):
254
s = 'my custom' + ' foo bar'
255
n_ref = sys.getrefcount(s)
256
k = self.module.Keys(1, s)
257
self.assertEqual(n_ref + 1, sys.getrefcount(s))
259
self.assertEqual(n_ref, sys.getrefcount(s))
261
def test_getitem(self):
263
k = self.module.Keys(1, f, 'bar')
264
self.assertEqual(('foo',), k[0])
265
self.assertEqual(('bar',), k[1])
266
self.assertRaises(IndexError, k.__getitem__, 2)
267
n_refs = sys.getrefcount(f)
269
# The pure-python version returns a tuple it already created, rather
270
# than creating a new one, so the refcount doesn't change
271
self.assertTrue(n_refs + 1 >= sys.getrefcount(f) >= n_refs)
273
# This is the important check, that the final refcount should be
275
self.assertEqual(n_refs, sys.getrefcount(f))
276
self.assertEqual(2, len(k))
278
def test_get_wide_key(self):
279
k = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing')
280
self.assertEqual(('foo', 'bar'), k[0])
281
self.assertEqual(('baz', 'bing'), k[1])
282
self.assertRaises(IndexError, k.__getitem__, 2)
283
self.assertEqual(2, len(k))
285
def test_as_tuple(self):
286
k = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing')
287
if getattr(k, 'as_tuple', None) is not None:
290
t = k # The pure-python form is in tuples already
291
self.assertEqual((('foo', 'bar'), ('baz', 'bing')), t)
294
k = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing')
295
self.assertEqual("(('foo', 'bar'), ('baz', 'bing'))", repr(k))
297
def test_compare(self):
298
k1 = self.module.Keys(2, 'foo', 'bar')
299
k2 = self.module.Keys(2, 'baz', 'bing')
300
k3 = self.module.Keys(2, 'foo', 'zzz')
301
k4 = self.module.Keys(2, 'foo', 'bar')
302
# Comparison should be done on the keys themselves, and not based on
304
self.assertTrue(k1 == k1)
305
self.assertTrue(k1 == k4)
306
self.assertTrue(k2 < k1)
307
self.assertTrue(k2 < k4)
308
self.assertTrue(k3 > k1)
309
self.assertTrue(k3 > k4)
310
# We should also be able to compare against raw tuples
311
self.assertTrue(k1 == (('foo', 'bar'),))
313
def test_sorted(self):
314
k1 = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing', 'foo', 'zzz')
315
self.assertEqual([('baz', 'bing'), ('foo', 'bar'), ('foo', 'zzz')],
318
k1 = self.module.Keys(2, 'foo', 'bar')
319
k2 = self.module.Keys(2, 'baz', 'bing')
320
k3 = self.module.Keys(2, 'foo', 'zzz')
321
self.assertEqual([(('baz', 'bing'),), (('foo', 'bar'),),
322
(('foo', 'zzz'),)], sorted([k1, k2, k3]))
325
k1 = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing', 'foo', 'zzz')
326
as_tuple =(('foo', 'bar'), ('baz', 'bing'), ('foo', 'zzz'))
327
self.assertEqual(hash(k1), hash(as_tuple))
329
# Because k1 == as_tuple, it replaces the slot, rather than having both
330
# present in the dict.
331
self.assertEqual('foo', x[as_tuple])
333
self.assertEqual({as_tuple: 'bar'}, x)
335
def test_referents(self):
336
# We implement tp_traverse so that things like 'meliae' can measure the
337
# amount of referenced memory. Unfortunately gc.get_referents() first
338
# checks the IS_GC flag before it traverses anything. So there isn't a
339
# way to expose it that I can see.
340
self.requireFeature(Meliae)
341
from meliae import scanner
342
strs = ['foo', 'bar', 'baz', 'bing']
343
k = self.module.Keys(2, *strs)
345
self.assertEqual(sorted([('foo', 'bar'), ('baz', 'bing')]),
346
sorted(scanner.get_referents(k)))
348
self.assertEqual(sorted(strs), sorted(scanner.get_referents(k)))
350
233
def test_intern(self):
351
234
unique_str1 = 'unique str ' + osutils.rand_chars(20)
352
235
unique_str2 = 'unique str ' + osutils.rand_chars(20)
353
key = self.module.Key(unique_str1, unique_str2)
236
key = self.module.StaticTuple(unique_str1, unique_str2)
354
237
self.assertFalse(key in self.module._interned_keys)
355
key2 = self.module.Key(unique_str1, unique_str2)
238
key2 = self.module.StaticTuple(unique_str1, unique_str2)
356
239
self.assertEqual(key, key2)
357
240
self.assertIsNot(key, key2)
358
241
key3 = key.intern()