summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_collections.py34
-rw-r--r--Lib/test/test_dict.py11
-rw-r--r--Lib/test/test_float.py73
3 files changed, 115 insertions, 3 deletions
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index 5a4029d..fdc82fc 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -49,6 +49,7 @@ class TestNamedTuple(unittest.TestCase):
self.assertEqual(repr(p), 'Point(x=11, y=22)')
self.assert_('__dict__' not in dir(p)) # verify instance has no dict
self.assert_('__weakref__' not in dir(p))
+ self.assertEqual(p, Point._cast([11, 22])) # test _cast classmethod
self.assertEqual(p._fields, ('x', 'y')) # test _fields attribute
self.assertEqual(p._replace(x=1), (1, 22)) # test _replace method
self.assertEqual(p._asdict(), dict(x=11, y=22)) # test _asdict method
@@ -93,9 +94,40 @@ class TestNamedTuple(unittest.TestCase):
def test_odd_sizes(self):
Zero = namedtuple('Zero', '')
self.assertEqual(Zero(), ())
+ self.assertEqual(Zero._cast([]), ())
+ self.assertEqual(repr(Zero()), 'Zero()')
+ self.assertEqual(Zero()._asdict(), {})
+ self.assertEqual(Zero()._fields, ())
+
Dot = namedtuple('Dot', 'd')
self.assertEqual(Dot(1), (1,))
-
+ self.assertEqual(Dot._cast([1]), (1,))
+ self.assertEqual(Dot(1).d, 1)
+ self.assertEqual(repr(Dot(1)), 'Dot(d=1)')
+ self.assertEqual(Dot(1)._asdict(), {'d':1})
+ self.assertEqual(Dot(1)._replace(d=999), (999,))
+ self.assertEqual(Dot(1)._fields, ('d',))
+
+ # n = 10000
+ n = 254 # SyntaxError: more than 255 arguments:
+ import string, random
+ names = [''.join([random.choice(string.ascii_letters) for j in range(10)]) for i in range(n)]
+ Big = namedtuple('Big', names)
+ b = Big(*range(n))
+ self.assertEqual(b, tuple(range(n)))
+ self.assertEqual(Big._cast(range(n)), tuple(range(n)))
+ for pos, name in enumerate(names):
+ self.assertEqual(getattr(b, name), pos)
+ repr(b) # make sure repr() doesn't blow-up
+ d = b._asdict()
+ d_expected = dict(zip(names, range(n)))
+ self.assertEqual(d, d_expected)
+ b2 = b._replace(**dict([(names[1], 999),(names[-5], 42)]))
+ b2_expected = list(range(n))
+ b2_expected[1] = 999
+ b2_expected[-5] = 42
+ self.assertEqual(b2, tuple(b2_expected))
+ self.assertEqual(b._fields, tuple(names))
class TestOneTrickPonyABCs(unittest.TestCase):
diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py
index 56ce722..53e6446 100644
--- a/Lib/test/test_dict.py
+++ b/Lib/test/test_dict.py
@@ -1,7 +1,7 @@
import unittest
from test import test_support
-import sys, UserDict
+import sys, UserDict, random, string
class DictTest(unittest.TestCase):
@@ -11,6 +11,15 @@ class DictTest(unittest.TestCase):
self.assertEqual(dict(), {})
self.assert_(dict() is not {})
+ def test_literal_constructor(self):
+ # check literal constructor for different sized dicts (to exercise the BUILD_MAP oparg
+ items = []
+ for n in range(400):
+ dictliteral = '{' + ', '.join('%r: %d' % item for item in items) + '}'
+ self.assertEqual(eval(dictliteral), dict(items))
+ items.append((''.join([random.choice(string.ascii_letters) for j in range(8)]), n))
+ random.shuffle(items)
+
def test_bool(self):
self.assert_(not {})
self.assert_({1: 2})
diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py
index 922de7a..0de65cb 100644
--- a/Lib/test/test_float.py
+++ b/Lib/test/test_float.py
@@ -3,6 +3,12 @@ import unittest, struct
import os
from test import test_support
+def isinf(x):
+ return x * 0.5 == x
+
+def isnan(x):
+ return x != x
+
class FormatFunctionsTestCase(unittest.TestCase):
def setUp(self):
@@ -159,6 +165,70 @@ class ReprTestCase(unittest.TestCase):
self.assertEqual(v, eval(repr(v)))
floats_file.close()
+# Beginning with Python 2.6 float has cross platform compatible
+# ways to create and representate inf and nan
+class InfNanTest(unittest.TestCase):
+ def test_inf_from_str(self):
+ self.assert_(isinf(float("inf")))
+ self.assert_(isinf(float("+inf")))
+ self.assert_(isinf(float("-inf")))
+
+ self.assertEqual(repr(float("inf")), "inf")
+ self.assertEqual(repr(float("+inf")), "inf")
+ self.assertEqual(repr(float("-inf")), "-inf")
+
+ self.assertEqual(repr(float("INF")), "inf")
+ self.assertEqual(repr(float("+Inf")), "inf")
+ self.assertEqual(repr(float("-iNF")), "-inf")
+
+ self.assertEqual(str(float("inf")), "inf")
+ self.assertEqual(str(float("+inf")), "inf")
+ self.assertEqual(str(float("-inf")), "-inf")
+
+ self.assertRaises(ValueError, float, "info")
+ self.assertRaises(ValueError, float, "+info")
+ self.assertRaises(ValueError, float, "-info")
+ self.assertRaises(ValueError, float, "in")
+ self.assertRaises(ValueError, float, "+in")
+ self.assertRaises(ValueError, float, "-in")
+
+ def test_inf_as_str(self):
+ self.assertEqual(repr(1e300 * 1e300), "inf")
+ self.assertEqual(repr(-1e300 * 1e300), "-inf")
+
+ self.assertEqual(str(1e300 * 1e300), "inf")
+ self.assertEqual(str(-1e300 * 1e300), "-inf")
+
+ def test_nan_from_str(self):
+ self.assert_(isnan(float("nan")))
+ self.assert_(isnan(float("+nan")))
+ self.assert_(isnan(float("-nan")))
+
+ self.assertEqual(repr(float("nan")), "nan")
+ self.assertEqual(repr(float("+nan")), "nan")
+ self.assertEqual(repr(float("-nan")), "nan")
+
+ self.assertEqual(repr(float("NAN")), "nan")
+ self.assertEqual(repr(float("+NAn")), "nan")
+ self.assertEqual(repr(float("-NaN")), "nan")
+
+ self.assertEqual(str(float("nan")), "nan")
+ self.assertEqual(str(float("+nan")), "nan")
+ self.assertEqual(str(float("-nan")), "nan")
+
+ self.assertRaises(ValueError, float, "nana")
+ self.assertRaises(ValueError, float, "+nana")
+ self.assertRaises(ValueError, float, "-nana")
+ self.assertRaises(ValueError, float, "na")
+ self.assertRaises(ValueError, float, "+na")
+ self.assertRaises(ValueError, float, "-na")
+
+ def test_nan_as_str(self):
+ self.assertEqual(repr(1e300 * 1e300 * 0), "nan")
+ self.assertEqual(repr(-1e300 * 1e300 * 0), "nan")
+
+ self.assertEqual(str(1e300 * 1e300 * 0), "nan")
+ self.assertEqual(str(-1e300 * 1e300 * 0), "nan")
def test_main():
test_support.run_unittest(
@@ -166,7 +236,8 @@ def test_main():
UnknownFormatTestCase,
IEEEFormatTestCase,
FormatTestCase,
- #ReprTestCase
+ ReprTestCase,
+ InfNanTest,
)
if __name__ == '__main__':