1 import math 2 from json.tests import PyTest, CTest 3 4 5 class 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 36 class TestPyFloat(TestFloat, PyTest): pass 37 class TestCFloat(TestFloat, CTest): pass 38