|
1 | 1 | import array
|
| 2 | +import ctypes |
2 | 3 | import struct
|
3 | 4 | import sys
|
4 | 5 | import unittest
|
| 6 | +from itertools import combinations |
| 7 | +from math import copysign, isnan |
5 | 8 | from operator import truth
|
6 | 9 | from ctypes import (byref, sizeof, alignment,
|
7 | 10 | c_char, c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint,
|
@@ -38,8 +41,55 @@ def valid_ranges(*types):
|
38 | 41 | signed_ranges = valid_ranges(*signed_types)
|
39 | 42 | bool_values = [True, False, 0, 1, -1, 5000, 'test', [], [1]]
|
40 | 43 |
|
| 44 | +class IntLike: |
| 45 | + def __int__(self): |
| 46 | + return 2 |
| 47 | + |
| 48 | +class IndexLike: |
| 49 | + def __index__(self): |
| 50 | + return 2 |
| 51 | + |
| 52 | +class FloatLike: |
| 53 | + def __float__(self): |
| 54 | + return 2.0 |
| 55 | + |
| 56 | +class ComplexLike: |
| 57 | + def __complex__(self): |
| 58 | + return 1+1j |
| 59 | + |
| 60 | + |
| 61 | +INF = float("inf") |
| 62 | +NAN = float("nan") |
| 63 | + |
41 | 64 |
|
42 | 65 | class NumberTestCase(unittest.TestCase):
|
| 66 | + # from Lib/test/test_complex.py |
| 67 | + def assertFloatsAreIdentical(self, x, y): |
| 68 | + """assert that floats x and y are identical, in the sense that: |
| 69 | + (1) both x and y are nans, or |
| 70 | + (2) both x and y are infinities, with the same sign, or |
| 71 | + (3) both x and y are zeros, with the same sign, or |
| 72 | + (4) x and y are both finite and nonzero, and x == y |
| 73 | +
|
| 74 | + """ |
| 75 | + msg = 'floats {!r} and {!r} are not identical' |
| 76 | + |
| 77 | + if isnan(x) or isnan(y): |
| 78 | + if isnan(x) and isnan(y): |
| 79 | + return |
| 80 | + elif x == y: |
| 81 | + if x != 0.0: |
| 82 | + return |
| 83 | + # both zero; check that signs match |
| 84 | + elif copysign(1.0, x) == copysign(1.0, y): |
| 85 | + return |
| 86 | + else: |
| 87 | + msg += ': zeros have different signs' |
| 88 | + self.fail(msg.format(x, y)) |
| 89 | + |
| 90 | + def assertComplexesAreIdentical(self, x, y): |
| 91 | + self.assertFloatsAreIdentical(x.real, y.real) |
| 92 | + self.assertFloatsAreIdentical(x.imag, y.imag) |
43 | 93 |
|
44 | 94 | def test_default_init(self):
|
45 | 95 | # default values are set to zero
|
@@ -86,28 +136,39 @@ def test_byref(self):
|
86 | 136 | def test_floats(self):
|
87 | 137 | # c_float and c_double can be created from
|
88 | 138 | # Python int and float
|
89 |
| - class FloatLike: |
90 |
| - def __float__(self): |
91 |
| - return 2.0 |
92 | 139 | f = FloatLike()
|
93 | 140 | for t in float_types:
|
94 | 141 | self.assertEqual(t(2.0).value, 2.0)
|
95 | 142 | self.assertEqual(t(2).value, 2.0)
|
96 | 143 | self.assertEqual(t(2).value, 2.0)
|
97 | 144 | self.assertEqual(t(f).value, 2.0)
|
98 | 145 |
|
| 146 | + @unittest.skipUnless(hasattr(ctypes, "c_double_complex"), |
| 147 | + "requires C11 complex type") |
| 148 | + def test_complex(self): |
| 149 | + for t in [ctypes.c_double_complex]: |
| 150 | + self.assertEqual(t(1).value, 1+0j) |
| 151 | + self.assertEqual(t(1.0).value, 1+0j) |
| 152 | + self.assertEqual(t(1+0.125j).value, 1+0.125j) |
| 153 | + self.assertEqual(t(IndexLike()).value, 2+0j) |
| 154 | + self.assertEqual(t(FloatLike()).value, 2+0j) |
| 155 | + self.assertEqual(t(ComplexLike()).value, 1+1j) |
| 156 | + |
| 157 | + @unittest.skipUnless(hasattr(ctypes, "c_double_complex"), |
| 158 | + "requires C11 complex type") |
| 159 | + def test_complex_round_trip(self): |
| 160 | + # Ensure complexes transformed exactly. The CMPLX macro should |
| 161 | + # preserve special components (like inf/nan or signed zero). |
| 162 | + values = [complex(*_) for _ in combinations([1, -1, 0.0, -0.0, 2, |
| 163 | + -3, INF, -INF, NAN], 2)] |
| 164 | + for z in values: |
| 165 | + with self.subTest(z=z): |
| 166 | + z2 = ctypes.c_double_complex(z).value |
| 167 | + self.assertComplexesAreIdentical(z, z2) |
| 168 | + |
99 | 169 | def test_integers(self):
|
100 |
| - class FloatLike: |
101 |
| - def __float__(self): |
102 |
| - return 2.0 |
103 | 170 | f = FloatLike()
|
104 |
| - class IntLike: |
105 |
| - def __int__(self): |
106 |
| - return 2 |
107 | 171 | d = IntLike()
|
108 |
| - class IndexLike: |
109 |
| - def __index__(self): |
110 |
| - return 2 |
111 | 172 | i = IndexLike()
|
112 | 173 | # integers cannot be constructed from floats,
|
113 | 174 | # but from integer-like objects
|
|
0 commit comments