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