More exhaustive combinatoric checks.

This commit is contained in:
Raymond Hettinger 2009-01-27 10:36:14 +00:00
parent ecf252abac
commit 2f6c2e03a8
1 changed files with 27 additions and 15 deletions

View File

@ -274,22 +274,34 @@ class TestBasicOps(unittest.TestCase):
# Test relationships between product(), permutations(), # Test relationships between product(), permutations(),
# combinations() and combinations_with_replacement(). # combinations() and combinations_with_replacement().
s = 'ABCDE' for n in range(6):
for r in range(8): s = 'ABCDEFG'[:n]
prod = list(product(s, repeat=r)) for r in range(8):
cwr = list(combinations_with_replacement(s, r)) prod = list(product(s, repeat=r))
perm = list(permutations(s, r)) cwr = list(combinations_with_replacement(s, r))
comb = list(combinations(s, r)) perm = list(permutations(s, r))
comb = list(combinations(s, r))
self.assertEquals(len(prod), len(s)**r) # Check size
self.assertEquals(prod, sorted(set(prod))) # prod in lexicographic order without repeats self.assertEquals(len(prod), n**r)
self.assertEquals(cwr, [t for t in prod if sorted(t)==list(t)]) # cwr: prods which are sorted self.assertEquals(len(cwr), (fact(n+r-1) / fact(r)/ fact(n-1)) if n else (not r))
self.assertEquals(perm, [t for t in prod if len(set(t))==r]) # perm: prods with no dups self.assertEquals(len(perm), 0 if r>n else fact(n) / fact(n-r))
self.assertEqual(comb, [t for t in perm if sorted(t)==list(t)]) # comb: perms that are sorted self.assertEquals(len(comb), 0 if r>n else fact(n) / fact(r) / fact(n-r))
self.assertEqual(comb, [t for t in cwr if len(set(t))==r]) # comb: cwrs without dups
self.assertEqual(comb, filter(set(cwr).__contains__, perm)) # comb: perm that is a cwr # Check lexicographic order without repeated tuples
self.assertEqual(comb, filter(set(perm).__contains__, cwr)) # comb: cwr that is a perm self.assertEquals(prod, sorted(set(prod)))
self.assertEqual(comb, sorted(set(cwr) & set(perm))) # comb: both a cwr and a perm self.assertEquals(cwr, sorted(set(cwr)))
self.assertEquals(perm, sorted(set(perm)))
self.assertEquals(comb, sorted(set(comb)))
# Check interrelationships
self.assertEquals(cwr, [t for t in prod if sorted(t)==list(t)]) # cwr: prods which are sorted
self.assertEquals(perm, [t for t in prod if len(set(t))==r]) # perm: prods with no dups
self.assertEqual(comb, [t for t in perm if sorted(t)==list(t)]) # comb: perms that are sorted
self.assertEqual(comb, [t for t in cwr if len(set(t))==r]) # comb: cwrs without dups
self.assertEqual(comb, filter(set(cwr).__contains__, perm)) # comb: perm that is a cwr
self.assertEqual(comb, filter(set(perm).__contains__, cwr)) # comb: cwr that is a perm
self.assertEqual(comb, sorted(set(cwr) & set(perm))) # comb: both a cwr and a perm
def test_compress(self): def test_compress(self):
self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF')) self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF'))