summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-02-22 03:16:42 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-02-22 03:16:42 (GMT)
commit50986cc45bfdfd23fd49cd46148b42ea763cfefd (patch)
tree995d6fcd9f028a5ef65d10f41bb9232fcaa47fe5 /Lib/test
parent12db865a640b87a9e1bb7e821b8d49cab0eb342f (diff)
downloadcpython-50986cc45bfdfd23fd49cd46148b42ea763cfefd.zip
cpython-50986cc45bfdfd23fd49cd46148b42ea763cfefd.tar.gz
cpython-50986cc45bfdfd23fd49cd46148b42ea763cfefd.tar.bz2
First draft for itertools.product(). Docs and other updates forthcoming.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_itertools.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index 9d19228..e65bba7 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -253,6 +253,28 @@ class TestBasicOps(unittest.TestCase):
ids = map(id, list(izip_longest('abc', 'def')))
self.assertEqual(len(dict.fromkeys(ids)), len(ids))
+ def test_product(self):
+ for args, result in [
+ ([], []), # zero iterables ??? is this correct
+ (['ab'], [('a',), ('b',)]), # one iterable
+ ([range(2), range(3)], [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]), # two iterables
+ ([range(0), range(2), range(3)], []), # first iterable with zero length
+ ([range(2), range(0), range(3)], []), # middle iterable with zero length
+ ([range(2), range(3), range(0)], []), # last iterable with zero length
+ ]:
+ self.assertEqual(list(product(*args)), result)
+ self.assertEqual(len(list(product(*[range(7)]*6))), 7**6)
+ self.assertRaises(TypeError, product, range(6), None)
+ argtypes = ['', 'abc', '', xrange(0), xrange(4), dict(a=1, b=2, c=3),
+ set('abcdefg'), range(11), tuple(range(13))]
+ for i in range(100):
+ args = [random.choice(argtypes) for j in range(random.randrange(5))]
+ n = reduce(operator.mul, map(len, args), 1) if args else 0
+ self.assertEqual(len(list(product(*args))), n)
+ args = map(iter, args)
+ self.assertEqual(len(list(product(*args))), n)
+
+
def test_repeat(self):
self.assertEqual(zip(xrange(3),repeat('a')),
[(0, 'a'), (1, 'a'), (2, 'a')])
@@ -623,6 +645,12 @@ class TestVariousIteratorArgs(unittest.TestCase):
self.assertRaises(TypeError, list, chain(N(s)))
self.assertRaises(ZeroDivisionError, list, chain(E(s)))
+ def test_product(self):
+ for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
+ self.assertRaises(TypeError, product, X(s))
+ self.assertRaises(TypeError, product, N(s))
+ self.assertRaises(ZeroDivisionError, product, E(s))
+
def test_cycle(self):
for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
for g in (G, I, Ig, S, L, R):