summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_itertools.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2009-01-27 10:39:42 (GMT)
committerRaymond Hettinger <python@rcn.com>2009-01-27 10:39:42 (GMT)
commitda6bc5214f90a8a5ada001e4715d9797ab1984de (patch)
treea4ba160e13cf624075e8178fe50ce0ad805e0af0 /Lib/test/test_itertools.py
parent0f05517d85efcce6fc57cec63ec54a4612ee5382 (diff)
downloadcpython-da6bc5214f90a8a5ada001e4715d9797ab1984de.zip
cpython-da6bc5214f90a8a5ada001e4715d9797ab1984de.tar.gz
cpython-da6bc5214f90a8a5ada001e4715d9797ab1984de.tar.bz2
More exhaustive combinatoric checks.
Diffstat (limited to 'Lib/test/test_itertools.py')
-rw-r--r--Lib/test/test_itertools.py44
1 files changed, 28 insertions, 16 deletions
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index 1b1fe09..01409b3 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -278,22 +278,34 @@ class TestBasicOps(unittest.TestCase):
# Test relationships between product(), permutations(),
# combinations() and combinations_with_replacement().
- s = 'ABCDE'
- for r in range(8):
- prod = list(product(s, repeat=r))
- cwr = list(combinations_with_replacement(s, r))
- perm = list(permutations(s, r))
- comb = list(combinations(s, r))
-
- self.assertEquals(len(prod), len(s)**r)
- self.assertEquals(prod, sorted(set(prod))) # prod in lexicographic order without repeats
- 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, list(filter(set(cwr).__contains__, perm))) # comb: perm that is a cwr
- self.assertEqual(comb, list(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
+ for n in range(6):
+ s = 'ABCDEFG'[:n]
+ for r in range(8):
+ prod = list(product(s, repeat=r))
+ cwr = list(combinations_with_replacement(s, r))
+ perm = list(permutations(s, r))
+ comb = list(combinations(s, r))
+
+ # Check size
+ self.assertEquals(len(prod), n**r)
+ self.assertEquals(len(cwr), (fact(n+r-1) / fact(r)/ fact(n-1)) if n else (not r))
+ self.assertEquals(len(perm), 0 if r>n else fact(n) / fact(n-r))
+ self.assertEquals(len(comb), 0 if r>n else fact(n) / fact(r) / fact(n-r))
+
+ # Check lexicographic order without repeated tuples
+ self.assertEquals(prod, sorted(set(prod)))
+ 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, list(filter(set(cwr).__contains__, perm))) # comb: perm that is a cwr
+ self.assertEqual(comb, list(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):
self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF'))