1import math
2from json.tests import PyTest, CTest
3
4
5class TestFloat(object):
6    def test_floats(self):
7        for num in [1617161771.7650001, math.pi, math.pi**100,
8                    math.pi**-100, 3.1]:
9            self.assertEqual(float(self.dumps(num)), num)
10            self.assertEqual(self.loads(self.dumps(num)), num)
11            self.assertEqual(self.loads(unicode(self.dumps(num))), num)
12
13    def test_ints(self):
14        for num in [1, 1L, 1<<32, 1<<64]:
15            self.assertEqual(self.dumps(num), str(num))
16            self.assertEqual(int(self.dumps(num)), num)
17            self.assertEqual(self.loads(self.dumps(num)), num)
18            self.assertEqual(self.loads(unicode(self.dumps(num))), num)
19
20    def test_out_of_range(self):
21        self.assertEqual(self.loads('[23456789012E666]'), [float('inf')])
22        self.assertEqual(self.loads('[-23456789012E666]'), [float('-inf')])
23
24    def test_allow_nan(self):
25        for val in (float('inf'), float('-inf'), float('nan')):
26            out = self.dumps([val])
27            if val == val:  # inf
28                self.assertEqual(self.loads(out), [val])
29            else:  # nan
30                res = self.loads(out)
31                self.assertEqual(len(res), 1)
32                self.assertNotEqual(res[0], res[0])
33            self.assertRaises(ValueError, self.dumps, [val], allow_nan=False)
34
35    def test_float_subclasses_use_float_repr(self):
36        # Issue 27934.
37        class PeculiarFloat(float):
38            def __repr__(self):
39                return "I'm not valid JSON"
40            def __str__(self):
41                return "Neither am I"
42
43        val = PeculiarFloat(3.2)
44        self.assertEqual(self.loads(self.dumps(val)), val)
45
46
47class TestPyFloat(TestFloat, PyTest): pass
48class TestCFloat(TestFloat, CTest): pass
49