blob: 11f169054a9e1f139790314228fd8a62f72d546b [file] [log] [blame]
Michael W. Hudsonba283e22005-05-27 15:23:20 +00001
2import unittest, struct
Christian Heimes284d9272007-12-10 22:28:56 +00003import os
Michael W. Hudsonba283e22005-05-27 15:23:20 +00004from test import test_support
5
Christian Heimes0a8143f2007-12-18 23:22:54 +00006def isinf(x):
7 return x * 0.5 == x
8
9def isnan(x):
10 return x != x
11
Michael W. Hudsonba283e22005-05-27 15:23:20 +000012class FormatFunctionsTestCase(unittest.TestCase):
13
14 def setUp(self):
15 self.save_formats = {'double':float.__getformat__('double'),
16 'float':float.__getformat__('float')}
17
18 def tearDown(self):
19 float.__setformat__('double', self.save_formats['double'])
20 float.__setformat__('float', self.save_formats['float'])
21
22 def test_getformat(self):
23 self.assert_(float.__getformat__('double') in
24 ['unknown', 'IEEE, big-endian', 'IEEE, little-endian'])
25 self.assert_(float.__getformat__('float') in
26 ['unknown', 'IEEE, big-endian', 'IEEE, little-endian'])
27 self.assertRaises(ValueError, float.__getformat__, 'chicken')
28 self.assertRaises(TypeError, float.__getformat__, 1)
29
30 def test_setformat(self):
31 for t in 'double', 'float':
32 float.__setformat__(t, 'unknown')
33 if self.save_formats[t] == 'IEEE, big-endian':
34 self.assertRaises(ValueError, float.__setformat__,
35 t, 'IEEE, little-endian')
36 elif self.save_formats[t] == 'IEEE, little-endian':
37 self.assertRaises(ValueError, float.__setformat__,
38 t, 'IEEE, big-endian')
39 else:
40 self.assertRaises(ValueError, float.__setformat__,
41 t, 'IEEE, big-endian')
42 self.assertRaises(ValueError, float.__setformat__,
43 t, 'IEEE, little-endian')
44 self.assertRaises(ValueError, float.__setformat__,
45 t, 'chicken')
46 self.assertRaises(ValueError, float.__setformat__,
47 'chicken', 'unknown')
48
49BE_DOUBLE_INF = '\x7f\xf0\x00\x00\x00\x00\x00\x00'
50LE_DOUBLE_INF = ''.join(reversed(BE_DOUBLE_INF))
51BE_DOUBLE_NAN = '\x7f\xf8\x00\x00\x00\x00\x00\x00'
52LE_DOUBLE_NAN = ''.join(reversed(BE_DOUBLE_NAN))
53
54BE_FLOAT_INF = '\x7f\x80\x00\x00'
55LE_FLOAT_INF = ''.join(reversed(BE_FLOAT_INF))
56BE_FLOAT_NAN = '\x7f\xc0\x00\x00'
57LE_FLOAT_NAN = ''.join(reversed(BE_FLOAT_NAN))
58
59# on non-IEEE platforms, attempting to unpack a bit pattern
60# representing an infinity or a NaN should raise an exception.
61
62class UnknownFormatTestCase(unittest.TestCase):
63 def setUp(self):
64 self.save_formats = {'double':float.__getformat__('double'),
65 'float':float.__getformat__('float')}
66 float.__setformat__('double', 'unknown')
67 float.__setformat__('float', 'unknown')
Tim Peters5d36a552005-06-03 22:40:27 +000068
Michael W. Hudsonba283e22005-05-27 15:23:20 +000069 def tearDown(self):
70 float.__setformat__('double', self.save_formats['double'])
71 float.__setformat__('float', self.save_formats['float'])
72
73 def test_double_specials_dont_unpack(self):
74 for fmt, data in [('>d', BE_DOUBLE_INF),
75 ('>d', BE_DOUBLE_NAN),
76 ('<d', LE_DOUBLE_INF),
77 ('<d', LE_DOUBLE_NAN)]:
78 self.assertRaises(ValueError, struct.unpack, fmt, data)
79
80 def test_float_specials_dont_unpack(self):
81 for fmt, data in [('>f', BE_FLOAT_INF),
82 ('>f', BE_FLOAT_NAN),
83 ('<f', LE_FLOAT_INF),
84 ('<f', LE_FLOAT_NAN)]:
85 self.assertRaises(ValueError, struct.unpack, fmt, data)
86
87
88# on an IEEE platform, all we guarantee is that bit patterns
89# representing infinities or NaNs do not raise an exception; all else
90# is accident (today).
Alex Martellid8672aa2007-08-22 21:14:17 +000091# let's also try to guarantee that -0.0 and 0.0 don't get confused.
Michael W. Hudsonba283e22005-05-27 15:23:20 +000092
93class IEEEFormatTestCase(unittest.TestCase):
94 if float.__getformat__("double").startswith("IEEE"):
95 def test_double_specials_do_unpack(self):
96 for fmt, data in [('>d', BE_DOUBLE_INF),
97 ('>d', BE_DOUBLE_NAN),
98 ('<d', LE_DOUBLE_INF),
99 ('<d', LE_DOUBLE_NAN)]:
100 struct.unpack(fmt, data)
101
102 if float.__getformat__("float").startswith("IEEE"):
103 def test_float_specials_do_unpack(self):
104 for fmt, data in [('>f', BE_FLOAT_INF),
105 ('>f', BE_FLOAT_NAN),
106 ('<f', LE_FLOAT_INF),
107 ('<f', LE_FLOAT_NAN)]:
108 struct.unpack(fmt, data)
109
Alex Martellid8672aa2007-08-22 21:14:17 +0000110 if float.__getformat__("double").startswith("IEEE"):
111 def test_negative_zero(self):
112 import math
113 def pos_pos():
114 return 0.0, math.atan2(0.0, -1)
115 def pos_neg():
116 return 0.0, math.atan2(-0.0, -1)
117 def neg_pos():
118 return -0.0, math.atan2(0.0, -1)
119 def neg_neg():
120 return -0.0, math.atan2(-0.0, -1)
121 self.assertEquals(pos_pos(), neg_pos())
122 self.assertEquals(pos_neg(), neg_neg())
123
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000124
Christian Heimes284d9272007-12-10 22:28:56 +0000125class ReprTestCase(unittest.TestCase):
126 def test_repr(self):
127 floats_file = open(os.path.join(os.path.split(__file__)[0],
128 'floating_points.txt'))
129 for line in floats_file:
130 line = line.strip()
131 if not line or line.startswith('#'):
132 continue
133 v = eval(line)
134 self.assertEqual(v, eval(repr(v)))
135 floats_file.close()
136
Christian Heimes0a8143f2007-12-18 23:22:54 +0000137# Beginning with Python 2.6 float has cross platform compatible
138# ways to create and representate inf and nan
139class InfNanTest(unittest.TestCase):
140 def test_inf_from_str(self):
141 self.assert_(isinf(float("inf")))
142 self.assert_(isinf(float("+inf")))
143 self.assert_(isinf(float("-inf")))
144
145 self.assertEqual(repr(float("inf")), "inf")
146 self.assertEqual(repr(float("+inf")), "inf")
147 self.assertEqual(repr(float("-inf")), "-inf")
148
149 self.assertEqual(repr(float("INF")), "inf")
150 self.assertEqual(repr(float("+Inf")), "inf")
151 self.assertEqual(repr(float("-iNF")), "-inf")
152
153 self.assertEqual(str(float("inf")), "inf")
154 self.assertEqual(str(float("+inf")), "inf")
155 self.assertEqual(str(float("-inf")), "-inf")
156
157 self.assertRaises(ValueError, float, "info")
158 self.assertRaises(ValueError, float, "+info")
159 self.assertRaises(ValueError, float, "-info")
160 self.assertRaises(ValueError, float, "in")
161 self.assertRaises(ValueError, float, "+in")
162 self.assertRaises(ValueError, float, "-in")
163
164 def test_inf_as_str(self):
165 self.assertEqual(repr(1e300 * 1e300), "inf")
166 self.assertEqual(repr(-1e300 * 1e300), "-inf")
167
168 self.assertEqual(str(1e300 * 1e300), "inf")
169 self.assertEqual(str(-1e300 * 1e300), "-inf")
170
171 def test_nan_from_str(self):
172 self.assert_(isnan(float("nan")))
173 self.assert_(isnan(float("+nan")))
174 self.assert_(isnan(float("-nan")))
175
176 self.assertEqual(repr(float("nan")), "nan")
177 self.assertEqual(repr(float("+nan")), "nan")
178 self.assertEqual(repr(float("-nan")), "nan")
179
180 self.assertEqual(repr(float("NAN")), "nan")
181 self.assertEqual(repr(float("+NAn")), "nan")
182 self.assertEqual(repr(float("-NaN")), "nan")
183
184 self.assertEqual(str(float("nan")), "nan")
185 self.assertEqual(str(float("+nan")), "nan")
186 self.assertEqual(str(float("-nan")), "nan")
187
188 self.assertRaises(ValueError, float, "nana")
189 self.assertRaises(ValueError, float, "+nana")
190 self.assertRaises(ValueError, float, "-nana")
191 self.assertRaises(ValueError, float, "na")
192 self.assertRaises(ValueError, float, "+na")
193 self.assertRaises(ValueError, float, "-na")
194
195 def test_nan_as_str(self):
196 self.assertEqual(repr(1e300 * 1e300 * 0), "nan")
197 self.assertEqual(repr(-1e300 * 1e300 * 0), "nan")
198
199 self.assertEqual(str(1e300 * 1e300 * 0), "nan")
200 self.assertEqual(str(-1e300 * 1e300 * 0), "nan")
Christian Heimes284d9272007-12-10 22:28:56 +0000201
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000202def test_main():
203 test_support.run_unittest(
204 FormatFunctionsTestCase,
205 UnknownFormatTestCase,
Christian Heimes284d9272007-12-10 22:28:56 +0000206 IEEEFormatTestCase,
Christian Heimes0a8143f2007-12-18 23:22:54 +0000207 ReprTestCase,
208 InfNanTest,
Christian Heimesf15c66e2007-12-11 00:54:34 +0000209 )
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000210
211if __name__ == '__main__':
212 test_main()