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

Method test_combinatorics

Lib/test/test_itertools.py:441–472  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

439 self.assertNotEqual(len(set(map(id, list(permutations('abcde', 3))))), 1)
440
441 def test_combinatorics(self):
442 # Test relationships between product(), permutations(),
443 # combinations() and combinations_with_replacement().
444
445 for n in range(6):
446 s = 'ABCDEFG'[:n]
447 for r in range(8):
448 prod = list(product(s, repeat=r))
449 cwr = list(combinations_with_replacement(s, r))
450 perm = list(permutations(s, r))
451 comb = list(combinations(s, r))
452
453 # Check size
454 self.assertEqual(len(prod), n**r)
455 self.assertEqual(len(cwr), (fact(n+r-1) / fact(r)/ fact(n-1)) if n else (not r))
456 self.assertEqual(len(perm), 0 if r>n else fact(n) / fact(n-r))
457 self.assertEqual(len(comb), 0 if r>n else fact(n) / fact(r) / fact(n-r))
458
459 # Check lexicographic order without repeated tuples
460 self.assertEqual(prod, sorted(set(prod)))
461 self.assertEqual(cwr, sorted(set(cwr)))
462 self.assertEqual(perm, sorted(set(perm)))
463 self.assertEqual(comb, sorted(set(comb)))
464
465 # Check interrelationships
466 self.assertEqual(cwr, [t for t in prod if sorted(t)==list(t)]) # cwr: prods which are sorted
467 self.assertEqual(perm, [t for t in prod if len(set(t))==r]) # perm: prods with no dups
468 self.assertEqual(comb, [t for t in perm if sorted(t)==list(t)]) # comb: perms that are sorted
469 self.assertEqual(comb, [t for t in cwr if len(set(t))==r]) # comb: cwrs without dups
470 self.assertEqual(comb, list(filter(set(cwr).__contains__, perm))) # comb: perm that is a cwr
471 self.assertEqual(comb, list(filter(set(perm).__contains__, cwr))) # comb: cwr that is a perm
472 self.assertEqual(comb, sorted(set(cwr) & set(perm))) # comb: both a cwr and a perm
473
474 def test_compress(self):
475 self.assertEqual(list(compress(data='ABCDEF', selectors=[1,0,1,0,1,1])), list('ACEF'))

Callers

nothing calls this directly

Calls 5

listClass · 0.85
factFunction · 0.85
setFunction · 0.85
filterFunction · 0.50
assertEqualMethod · 0.45

Tested by

no test coverage detected