| 1 |
import math |
| 2 |
import sys |
| 3 |
import unittest |
| 4 |
import warnings |
| 5 |
|
| 6 |
from test.test_capi.test_getargs import (Float, FloatSubclass, FloatSubclass2, |
| 7 |
BadIndex2, BadFloat2, Index, BadIndex, |
| 8 |
BadFloat) |
| 9 |
from test.support import import_helper |
| 10 |
|
| 11 |
_testcapi = import_helper.import_module('_testcapi') |
| 12 |
_testlimitedcapi = import_helper.import_module('_testlimitedcapi') |
| 13 |
|
| 14 |
NULL = None |
| 15 |
|
| 16 |
# For PyFloat_Pack/Unpack* |
| 17 |
BIG_ENDIAN = 0 |
| 18 |
LITTLE_ENDIAN = 1 |
| 19 |
EPSILON = { |
| 20 |
2: 2.0 ** -11, # binary16 |
| 21 |
4: 2.0 ** -24, # binary32 |
| 22 |
8: 2.0 ** -53, # binary64 |
| 23 |
} |
| 24 |
|
| 25 |
HAVE_IEEE_754 = float.__getformat__("double").startswith("IEEE") |
| 26 |
INF = float("inf") |
| 27 |
NAN = float("nan") |
| 28 |
|
| 29 |
|
| 30 |
class CAPIFloatTest(unittest.TestCase): |
| 31 |
def test_check(self): |
| 32 |
# Test PyFloat_Check() |
| 33 |
check = _testlimitedcapi.float_check |
| 34 |
|
| 35 |
self.assertTrue(check(4.25)) |
| 36 |
self.assertTrue(check(FloatSubclass(4.25))) |
| 37 |
self.assertFalse(check(Float())) |
| 38 |
self.assertFalse(check(3)) |
| 39 |
self.assertFalse(check(object())) |
| 40 |
|
| 41 |
# CRASHES check(NULL) |
| 42 |
|
| 43 |
def test_checkexact(self): |
| 44 |
# Test PyFloat_CheckExact() |
| 45 |
checkexact = _testlimitedcapi.float_checkexact |
| 46 |
|
| 47 |
self.assertTrue(checkexact(4.25)) |
| 48 |
self.assertFalse(checkexact(FloatSubclass(4.25))) |
| 49 |
self.assertFalse(checkexact(Float())) |
| 50 |
self.assertFalse(checkexact(3)) |
| 51 |
self.assertFalse(checkexact(object())) |
| 52 |
|
| 53 |
# CRASHES checkexact(NULL) |
| 54 |
|
| 55 |
def test_fromstring(self): |
| 56 |
# Test PyFloat_FromString() |
| 57 |
fromstring = _testlimitedcapi.float_fromstring |
| 58 |
|
| 59 |
self.assertEqual(fromstring("4.25"), 4.25) |
| 60 |
self.assertEqual(fromstring(b"4.25"), 4.25) |
| 61 |
self.assertRaises(ValueError, fromstring, "4.25\0") |
| 62 |
self.assertRaises(ValueError, fromstring, b"4.25\0") |
| 63 |
|
| 64 |
self.assertEqual(fromstring(bytearray(b"4.25")), 4.25) |
| 65 |
|
| 66 |
self.assertEqual(fromstring(memoryview(b"4.25")), 4.25) |
| 67 |
self.assertEqual(fromstring(memoryview(b"4.255")[:-1]), 4.25) |
| 68 |
self.assertRaises(TypeError, fromstring, memoryview(b"4.25")[::2]) |
| 69 |
|
| 70 |
self.assertRaises(TypeError, fromstring, 4.25) |
| 71 |
|
| 72 |
# CRASHES fromstring(NULL) |
| 73 |
|
| 74 |
def test_fromdouble(self): |
| 75 |
# Test PyFloat_FromDouble() |
| 76 |
fromdouble = _testlimitedcapi.float_fromdouble |
| 77 |
|
| 78 |
self.assertEqual(fromdouble(4.25), 4.25) |
| 79 |
|
| 80 |
def test_asdouble(self): |
| 81 |
# Test PyFloat_AsDouble() |
| 82 |
asdouble = _testlimitedcapi.float_asdouble |
| 83 |
|
| 84 |
class BadFloat3: |
| 85 |
def __float__(self): |
| 86 |
raise RuntimeError |
| 87 |
|
| 88 |
self.assertEqual(asdouble(4.25), 4.25) |
| 89 |
self.assertEqual(asdouble(-1.0), -1.0) |
| 90 |
self.assertEqual(asdouble(42), 42.0) |
| 91 |
self.assertEqual(asdouble(-1), -1.0) |
| 92 |
self.assertEqual(asdouble(2**1000), float(2**1000)) |
| 93 |
|
| 94 |
self.assertEqual(asdouble(FloatSubclass(4.25)), 4.25) |
| 95 |
self.assertEqual(asdouble(FloatSubclass2(4.25)), 4.25) |
| 96 |
self.assertEqual(asdouble(Index()), 99.) |
| 97 |
|
| 98 |
self.assertRaises(TypeError, asdouble, BadIndex()) |
| 99 |
self.assertRaises(TypeError, asdouble, BadFloat()) |
| 100 |
self.assertRaises(RuntimeError, asdouble, BadFloat3()) |
| 101 |
with self.assertWarns(DeprecationWarning): |
| 102 |
self.assertEqual(asdouble(BadIndex2()), 1.) |
| 103 |
with self.assertWarns(DeprecationWarning): |
| 104 |
self.assertEqual(asdouble(BadFloat2()), 4.25) |
| 105 |
with warnings.catch_warnings(): |
| 106 |
warnings.simplefilter("error", DeprecationWarning) |
| 107 |
self.assertRaises(DeprecationWarning, asdouble, BadFloat2()) |
| 108 |
self.assertRaises(TypeError, asdouble, object()) |
| 109 |
self.assertRaises(TypeError, asdouble, NULL) |
| 110 |
|
| 111 |
def test_getinfo(self): |
| 112 |
# Test PyFloat_GetInfo() |
| 113 |
getinfo = _testlimitedcapi.float_getinfo |
| 114 |
|
| 115 |
self.assertEqual(getinfo(), sys.float_info) |
| 116 |
|
| 117 |
def test_getmax(self): |
| 118 |
# Test PyFloat_GetMax() |
| 119 |
getmax = _testlimitedcapi.float_getmax |
| 120 |
|
| 121 |
self.assertEqual(getmax(), sys.float_info.max) |
| 122 |
|
| 123 |
def test_getmin(self): |
| 124 |
# Test PyFloat_GetMax() |
| 125 |
getmin = _testlimitedcapi.float_getmin |
| 126 |
|
| 127 |
self.assertEqual(getmin(), sys.float_info.min) |
| 128 |
|
| 129 |
def test_pack(self): |
| 130 |
# Test PyFloat_Pack2(), PyFloat_Pack4() and PyFloat_Pack8() |
| 131 |
pack = _testcapi.float_pack |
| 132 |
|
| 133 |
self.assertEqual(pack(2, 1.5, BIG_ENDIAN), b'>\x00') |
| 134 |
self.assertEqual(pack(4, 1.5, BIG_ENDIAN), b'?\xc0\x00\x00') |
| 135 |
self.assertEqual(pack(8, 1.5, BIG_ENDIAN), |
| 136 |
b'?\xf8\x00\x00\x00\x00\x00\x00') |
| 137 |
self.assertEqual(pack(2, 1.5, LITTLE_ENDIAN), b'\x00>') |
| 138 |
self.assertEqual(pack(4, 1.5, LITTLE_ENDIAN), b'\x00\x00\xc0?') |
| 139 |
self.assertEqual(pack(8, 1.5, LITTLE_ENDIAN), |
| 140 |
b'\x00\x00\x00\x00\x00\x00\xf8?') |
| 141 |
|
| 142 |
def test_unpack(self): |
| 143 |
# Test PyFloat_Unpack2(), PyFloat_Unpack4() and PyFloat_Unpack8() |
| 144 |
unpack = _testcapi.float_unpack |
| 145 |
|
| 146 |
self.assertEqual(unpack(b'>\x00', BIG_ENDIAN), 1.5) |
| 147 |
self.assertEqual(unpack(b'?\xc0\x00\x00', BIG_ENDIAN), 1.5) |
| 148 |
self.assertEqual(unpack(b'?\xf8\x00\x00\x00\x00\x00\x00', BIG_ENDIAN), |
| 149 |
1.5) |
| 150 |
self.assertEqual(unpack(b'\x00>', LITTLE_ENDIAN), 1.5) |
| 151 |
self.assertEqual(unpack(b'\x00\x00\xc0?', LITTLE_ENDIAN), 1.5) |
| 152 |
self.assertEqual(unpack(b'\x00\x00\x00\x00\x00\x00\xf8?', LITTLE_ENDIAN), |
| 153 |
1.5) |
| 154 |
|
| 155 |
def test_pack_unpack_roundtrip(self): |
| 156 |
pack = _testcapi.float_pack |
| 157 |
unpack = _testcapi.float_unpack |
| 158 |
|
| 159 |
large = 2.0 ** 100 |
| 160 |
values = [1.0, 1.5, large, 1.0/7, math.pi] |
| 161 |
if HAVE_IEEE_754: |
| 162 |
values.extend((INF, NAN)) |
| 163 |
for value in values: |
| 164 |
for size in (2, 4, 8,): |
| 165 |
if size == 2 and value == large: |
| 166 |
# too large for 16-bit float |
| 167 |
continue |
| 168 |
rel_tol = EPSILON[size] |
| 169 |
for endian in (BIG_ENDIAN, LITTLE_ENDIAN): |
| 170 |
with self.subTest(value=value, size=size, endian=endian): |
| 171 |
data = pack(size, value, endian) |
| 172 |
value2 = unpack(data, endian) |
| 173 |
if math.isnan(value): |
| 174 |
self.assertTrue(math.isnan(value2), (value, value2)) |
| 175 |
elif size < 8: |
| 176 |
self.assertTrue(math.isclose(value2, value, rel_tol=rel_tol), |
| 177 |
(value, value2)) |
| 178 |
else: |
| 179 |
self.assertEqual(value2, value) |
| 180 |
|
| 181 |
|
| 182 |
if __name__ == "__main__": |
| 183 |
unittest.main() |