1 | n/a | import math |
---|
2 | n/a | from test.test_json import PyTest, CTest |
---|
3 | n/a | |
---|
4 | n/a | |
---|
5 | n/a | class TestFloat: |
---|
6 | n/a | def test_floats(self): |
---|
7 | n/a | for num in [1617161771.7650001, math.pi, math.pi**100, math.pi**-100, 3.1]: |
---|
8 | n/a | self.assertEqual(float(self.dumps(num)), num) |
---|
9 | n/a | self.assertEqual(self.loads(self.dumps(num)), num) |
---|
10 | n/a | |
---|
11 | n/a | def test_ints(self): |
---|
12 | n/a | for num in [1, 1<<32, 1<<64]: |
---|
13 | n/a | self.assertEqual(self.dumps(num), str(num)) |
---|
14 | n/a | self.assertEqual(int(self.dumps(num)), num) |
---|
15 | n/a | |
---|
16 | n/a | def test_out_of_range(self): |
---|
17 | n/a | self.assertEqual(self.loads('[23456789012E666]'), [float('inf')]) |
---|
18 | n/a | self.assertEqual(self.loads('[-23456789012E666]'), [float('-inf')]) |
---|
19 | n/a | |
---|
20 | n/a | def test_allow_nan(self): |
---|
21 | n/a | for val in (float('inf'), float('-inf'), float('nan')): |
---|
22 | n/a | out = self.dumps([val]) |
---|
23 | n/a | if val == val: # inf |
---|
24 | n/a | self.assertEqual(self.loads(out), [val]) |
---|
25 | n/a | else: # nan |
---|
26 | n/a | res = self.loads(out) |
---|
27 | n/a | self.assertEqual(len(res), 1) |
---|
28 | n/a | self.assertNotEqual(res[0], res[0]) |
---|
29 | n/a | self.assertRaises(ValueError, self.dumps, [val], allow_nan=False) |
---|
30 | n/a | |
---|
31 | n/a | |
---|
32 | n/a | class TestPyFloat(TestFloat, PyTest): pass |
---|
33 | n/a | class TestCFloat(TestFloat, CTest): pass |
---|