287
288
k6 = self.module.StaticTuple(k3, k4)
288
289
self.assertCompareEqual(k5, k6)
290
def assertCompareDifferent(self, k_small, k_big):
291
def check_strict_compare(self, k1, k2, mismatched_types):
292
"""True if on Python 3 and stricter comparison semantics are used."""
293
if PY3 and mismatched_types:
294
for op in ("ge", "gt", "le", "lt"):
295
self.assertRaises(TypeError, getattr(operator, op), k1, k2)
299
def assertCompareDifferent(self, k_small, k_big, mismatched_types=False):
291
300
self.assertFalse(k_small == k_big)
292
self.assertFalse(k_small >= k_big)
293
self.assertFalse(k_small > k_big)
294
301
self.assertTrue(k_small != k_big)
295
self.assertTrue(k_small <= k_big)
296
self.assertTrue(k_small < k_big)
302
if not self.check_strict_compare(k_small, k_big, mismatched_types):
303
self.assertFalse(k_small >= k_big)
304
self.assertFalse(k_small > k_big)
305
self.assertTrue(k_small <= k_big)
306
self.assertTrue(k_small < k_big)
298
def assertCompareNoRelation(self, k1, k2):
308
def assertCompareNoRelation(self, k1, k2, mismatched_types=False):
299
309
"""Run the comparison operators, make sure they do something.
301
311
However, we don't actually care what comes first or second. This is
305
315
self.assertFalse(k1 == k2)
306
316
self.assertTrue(k1 != k2)
307
# Do the comparison, but we don't care about the result
317
if not self.check_strict_compare(k1, k2, mismatched_types):
318
# Do the comparison, but we don't care about the result
313
324
def test_compare_vs_none(self):
314
325
k1 = self.module.StaticTuple('baz', 'bing')
315
self.assertCompareDifferent(None, k1)
326
self.assertCompareDifferent(None, k1, mismatched_types=True)
317
328
def test_compare_cross_class(self):
318
329
k1 = self.module.StaticTuple('baz', 'bing')
319
self.assertCompareNoRelation(10, k1)
320
self.assertCompareNoRelation('baz', k1)
330
self.assertCompareNoRelation(10, k1, mismatched_types=True)
331
self.assertCompareNoRelation('baz', k1, mismatched_types=True)
322
333
def test_compare_all_different_same_width(self):
323
334
k1 = self.module.StaticTuple('baz', 'bing')
344
355
k4 = self.module.StaticTuple(k1, k2)
345
356
self.assertCompareDifferent(k3, k4)
346
357
k5 = self.module.StaticTuple('foo', None)
347
self.assertCompareDifferent(k5, k1)
348
self.assertCompareDifferent(k5, k2)
358
self.assertCompareDifferent(k5, k1, mismatched_types=True)
359
self.assertCompareDifferent(k5, k2, mismatched_types=True)
350
361
def test_compare_diff_width(self):
351
362
k1 = self.module.StaticTuple('foo')
359
370
k1 = self.module.StaticTuple('foo', 'bar')
360
371
k2 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
362
self.assertCompareNoRelation(k1, k2)
373
self.assertCompareNoRelation(k1, k2, mismatched_types=True)
363
374
k3 = self.module.StaticTuple('foo')
364
375
self.assertCompareDifferent(k3, k1)
365
376
k4 = self.module.StaticTuple(None)
366
self.assertCompareDifferent(k4, k1)
377
self.assertCompareDifferent(k4, k1, mismatched_types=True)
367
378
k5 = self.module.StaticTuple(1)
368
self.assertCompareNoRelation(k1, k5)
379
self.assertCompareNoRelation(k1, k5, mismatched_types=True)
370
381
def test_compare_to_tuples(self):
371
382
k1 = self.module.StaticTuple('foo')
381
392
self.assertCompareDifferent(('foo',), k2)
382
393
self.assertCompareDifferent(('foo', 'aaa'), k2)
383
394
self.assertCompareDifferent(('baz', 'bing'), k2)
384
self.assertCompareDifferent(('foo', 10), k2)
395
self.assertCompareDifferent(('foo', 10), k2, mismatched_types=True)
386
397
k3 = self.module.StaticTuple(k1, k2)
387
398
self.assertCompareEqual(k3, (('foo',), ('foo', 'bar')))
397
408
# This requires comparing a StaticTuple to a 'string', and then
398
409
# interpreting that value in the next higher StaticTuple. This used to
399
410
# generate a PyErr_BadIternalCall. We now fall back to *something*.
400
self.assertCompareNoRelation(k1, k2)
411
self.assertCompareNoRelation(k1, k2, mismatched_types=True)
402
413
def test_hash(self):
403
414
k = self.module.StaticTuple('foo')