MCPcopy Index your code
hub / github.com/python/cpython / test_sort

Method test_sort

Lib/test/list_tests.py:445–491  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

443 self.assertRaises(TypeError, u.copy, None)
444
445 def test_sort(self):
446 u = self.type2test([1, 0])
447 u.sort()
448 self.assertEqual(u, [0, 1])
449
450 u = self.type2test([2,1,0,-1,-2])
451 u.sort()
452 self.assertEqual(u, self.type2test([-2,-1,0,1,2]))
453
454 self.assertRaises(TypeError, u.sort, 42, 42)
455
456 def revcmp(a, b):
457 if a == b:
458 return 0
459 elif a < b:
460 return 1
461 else: # a > b
462 return -1
463 u.sort(key=cmp_to_key(revcmp))
464 self.assertEqual(u, self.type2test([2,1,0,-1,-2]))
465
466 # The following dumps core in unpatched Python 1.5:
467 def myComparison(x,y):
468 xmod, ymod = x%3, y%7
469 if xmod == ymod:
470 return 0
471 elif xmod < ymod:
472 return -1
473 else: # xmod > ymod
474 return 1
475 z = self.type2test(range(12))
476 z.sort(key=cmp_to_key(myComparison))
477
478 self.assertRaises(TypeError, z.sort, 2)
479
480 def selfmodifyingComparison(x,y):
481 z.append(1)
482 if x == y:
483 return 0
484 elif x < y:
485 return -1
486 else: # x > y
487 return 1
488 self.assertRaises(ValueError, z.sort,
489 key=cmp_to_key(selfmodifyingComparison))
490
491 self.assertRaises(TypeError, z.sort, 42, 42, 42, 42)
492
493 def test_slice(self):
494 u = self.type2test("spam")

Callers

nothing calls this directly

Calls 5

cmp_to_keyFunction · 0.90
type2testMethod · 0.80
sortMethod · 0.45
assertEqualMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected