summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_itertools.py29
-rw-r--r--Lib/test/test_types.py38
2 files changed, 60 insertions, 7 deletions
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index ae5eb90..380d121 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -5,6 +5,7 @@ from weakref import proxy
import sys
import operator
import random
+from functools import reduce
maxsize = test_support.MAX_Py_ssize_t
minsize = -maxsize-1
@@ -261,6 +262,28 @@ class TestBasicOps(unittest.TestCase):
ids = list(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', '', range(0), range(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(lzip(range(3),repeat('a')),
[(0, 'a'), (1, 'a'), (2, 'a')])
@@ -636,6 +659,12 @@ class TestVariousIteratorArgs(unittest.TestCase):
self.assertRaises(TypeError, chain, N(s))
self.assertRaises(ZeroDivisionError, list, chain(E(s)))
+ def test_product(self):
+ for s in ("123", "", range(1000), ('do', 1.2), range(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), range(2000,2200,5)):
for g in (G, I, Ig, S, L, R):
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index f82fe30..654bc11 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -89,6 +89,29 @@ class TypesTests(unittest.TestCase):
if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass
else: self.fail('float() does not work properly')
+ def test_float_to_string(self):
+ def test(f, result):
+ self.assertEqual(f.__format__('e'), result)
+ self.assertEqual('%e' % f, result)
+
+ # test all 2 digit exponents, both with __format__ and with
+ # '%' formatting
+ for i in range(-99, 100):
+ test(float('1.5e'+str(i)), '1.500000e{0:+03d}'.format(i))
+
+ # test some 3 digit exponents
+ self.assertEqual(1.5e100.__format__('e'), '1.500000e+100')
+ self.assertEqual('%e' % 1.5e100, '1.500000e+100')
+
+ self.assertEqual(1.5e101.__format__('e'), '1.500000e+101')
+ self.assertEqual('%e' % 1.5e101, '1.500000e+101')
+
+ self.assertEqual(1.5e-100.__format__('e'), '1.500000e-100')
+ self.assertEqual('%e' % 1.5e-100, '1.500000e-100')
+
+ self.assertEqual(1.5e-101.__format__('e'), '1.500000e-101')
+ self.assertEqual('%e' % 1.5e-101, '1.500000e-101')
+
def test_normal_integers(self):
# Ensure the first 256 integers are shared
a = 256
@@ -416,16 +439,17 @@ class TypesTests(unittest.TestCase):
test(-1.0, ' f', '-1.000000')
test( 1.0, '+f', '+1.000000')
test(-1.0, '+f', '-1.000000')
+ test(1.1234e90, 'f', '1.1234e+90')
+ test(1.1234e90, 'F', '1.1234e+90')
test(1.1234e200, 'f', '1.1234e+200')
test(1.1234e200, 'F', '1.1234e+200')
- # temporarily removed. see issue 1600
- # test( 1.0, 'e', '1.000000e+00')
- # test(-1.0, 'e', '-1.000000e+00')
- # test( 1.0, 'E', '1.000000E+00')
- # test(-1.0, 'E', '-1.000000E+00')
- # test(1.1234e20, 'e', '1.123400e+20')
- # test(1.1234e20, 'E', '1.123400E+20')
+ test( 1.0, 'e', '1.000000e+00')
+ test(-1.0, 'e', '-1.000000e+00')
+ test( 1.0, 'E', '1.000000E+00')
+ test(-1.0, 'E', '-1.000000E+00')
+ test(1.1234e20, 'e', '1.123400e+20')
+ test(1.1234e20, 'E', '1.123400E+20')
# % formatting
test(-1.0, '%', '-100.000000%')