Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_float.py @ 39042:c02fa187e0f4 legacy-trunk
Part of SF patch #1484695. This removes dead code. The chksum was
already verified in .frombuf() on the lines above. If there was
a problem an exception is raised, so there was no way this condition
could have been true.
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Mon, 10 Jul 2006 00:23:17 +0000 |
| parents | 275843630522 |
| children | d1146d4db549 8103f0198967 d5fa4afd6620 |
| rev | line source |
|---|---|
| 34972 | 1 |
| 2 import unittest, struct | |
| 3 from test import test_support | |
| 4 | |
| 5 class FormatFunctionsTestCase(unittest.TestCase): | |
| 6 | |
| 7 def setUp(self): | |
| 8 self.save_formats = {'double':float.__getformat__('double'), | |
| 9 'float':float.__getformat__('float')} | |
| 10 | |
| 11 def tearDown(self): | |
| 12 float.__setformat__('double', self.save_formats['double']) | |
| 13 float.__setformat__('float', self.save_formats['float']) | |
| 14 | |
| 15 def test_getformat(self): | |
| 16 self.assert_(float.__getformat__('double') in | |
| 17 ['unknown', 'IEEE, big-endian', 'IEEE, little-endian']) | |
| 18 self.assert_(float.__getformat__('float') in | |
| 19 ['unknown', 'IEEE, big-endian', 'IEEE, little-endian']) | |
| 20 self.assertRaises(ValueError, float.__getformat__, 'chicken') | |
| 21 self.assertRaises(TypeError, float.__getformat__, 1) | |
| 22 | |
| 23 def test_setformat(self): | |
| 24 for t in 'double', 'float': | |
| 25 float.__setformat__(t, 'unknown') | |
| 26 if self.save_formats[t] == 'IEEE, big-endian': | |
| 27 self.assertRaises(ValueError, float.__setformat__, | |
| 28 t, 'IEEE, little-endian') | |
| 29 elif self.save_formats[t] == 'IEEE, little-endian': | |
| 30 self.assertRaises(ValueError, float.__setformat__, | |
| 31 t, 'IEEE, big-endian') | |
| 32 else: | |
| 33 self.assertRaises(ValueError, float.__setformat__, | |
| 34 t, 'IEEE, big-endian') | |
| 35 self.assertRaises(ValueError, float.__setformat__, | |
| 36 t, 'IEEE, little-endian') | |
| 37 self.assertRaises(ValueError, float.__setformat__, | |
| 38 t, 'chicken') | |
| 39 self.assertRaises(ValueError, float.__setformat__, | |
| 40 'chicken', 'unknown') | |
| 41 | |
| 42 BE_DOUBLE_INF = '\x7f\xf0\x00\x00\x00\x00\x00\x00' | |
| 43 LE_DOUBLE_INF = ''.join(reversed(BE_DOUBLE_INF)) | |
| 44 BE_DOUBLE_NAN = '\x7f\xf8\x00\x00\x00\x00\x00\x00' | |
| 45 LE_DOUBLE_NAN = ''.join(reversed(BE_DOUBLE_NAN)) | |
| 46 | |
| 47 BE_FLOAT_INF = '\x7f\x80\x00\x00' | |
| 48 LE_FLOAT_INF = ''.join(reversed(BE_FLOAT_INF)) | |
| 49 BE_FLOAT_NAN = '\x7f\xc0\x00\x00' | |
| 50 LE_FLOAT_NAN = ''.join(reversed(BE_FLOAT_NAN)) | |
| 51 | |
| 52 # on non-IEEE platforms, attempting to unpack a bit pattern | |
| 53 # representing an infinity or a NaN should raise an exception. | |
| 54 | |
| 55 class UnknownFormatTestCase(unittest.TestCase): | |
| 56 def setUp(self): | |
| 57 self.save_formats = {'double':float.__getformat__('double'), | |
| 58 'float':float.__getformat__('float')} | |
| 59 float.__setformat__('double', 'unknown') | |
| 60 float.__setformat__('float', 'unknown') | |
|
35014
275843630522
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34972
diff
changeset
|
61 |
| 34972 | 62 def tearDown(self): |
| 63 float.__setformat__('double', self.save_formats['double']) | |
| 64 float.__setformat__('float', self.save_formats['float']) | |
| 65 | |
| 66 def test_double_specials_dont_unpack(self): | |
| 67 for fmt, data in [('>d', BE_DOUBLE_INF), | |
| 68 ('>d', BE_DOUBLE_NAN), | |
| 69 ('<d', LE_DOUBLE_INF), | |
| 70 ('<d', LE_DOUBLE_NAN)]: | |
| 71 self.assertRaises(ValueError, struct.unpack, fmt, data) | |
| 72 | |
| 73 def test_float_specials_dont_unpack(self): | |
| 74 for fmt, data in [('>f', BE_FLOAT_INF), | |
| 75 ('>f', BE_FLOAT_NAN), | |
| 76 ('<f', LE_FLOAT_INF), | |
| 77 ('<f', LE_FLOAT_NAN)]: | |
| 78 self.assertRaises(ValueError, struct.unpack, fmt, data) | |
| 79 | |
| 80 | |
| 81 # on an IEEE platform, all we guarantee is that bit patterns | |
| 82 # representing infinities or NaNs do not raise an exception; all else | |
| 83 # is accident (today). | |
| 84 | |
| 85 class IEEEFormatTestCase(unittest.TestCase): | |
| 86 if float.__getformat__("double").startswith("IEEE"): | |
| 87 def test_double_specials_do_unpack(self): | |
| 88 for fmt, data in [('>d', BE_DOUBLE_INF), | |
| 89 ('>d', BE_DOUBLE_NAN), | |
| 90 ('<d', LE_DOUBLE_INF), | |
| 91 ('<d', LE_DOUBLE_NAN)]: | |
| 92 struct.unpack(fmt, data) | |
| 93 | |
| 94 if float.__getformat__("float").startswith("IEEE"): | |
| 95 def test_float_specials_do_unpack(self): | |
| 96 for fmt, data in [('>f', BE_FLOAT_INF), | |
| 97 ('>f', BE_FLOAT_NAN), | |
| 98 ('<f', LE_FLOAT_INF), | |
| 99 ('<f', LE_FLOAT_NAN)]: | |
| 100 struct.unpack(fmt, data) | |
| 101 | |
| 102 | |
| 103 def test_main(): | |
| 104 test_support.run_unittest( | |
| 105 FormatFunctionsTestCase, | |
| 106 UnknownFormatTestCase, | |
| 107 IEEEFormatTestCase) | |
| 108 | |
| 109 if __name__ == '__main__': | |
| 110 test_main() |
