Skip to content

Commit 0c69391

Browse files
committed
Copy test for numbers.py from v3.12.3
1 parent 91598f9 commit 0c69391

File tree

2 files changed

+161
-82
lines changed

2 files changed

+161
-82
lines changed

Lib/ctypes/test/test_numbers.py

Lines changed: 4 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,6 @@ def test_typeerror(self):
8282
self.assertRaises(TypeError, t, "")
8383
self.assertRaises(TypeError, t, None)
8484

85-
@unittest.skip('test disabled')
86-
def test_valid_ranges(self):
87-
# invalid values of the correct type
88-
# raise ValueError (not OverflowError)
89-
for t, (l, h) in zip(unsigned_types, unsigned_ranges):
90-
self.assertRaises(ValueError, t, l-1)
91-
self.assertRaises(ValueError, t, h+1)
92-
9385
def test_from_param(self):
9486
# the from_param class method attribute always
9587
# returns PyCArgObject instances
@@ -106,7 +98,7 @@ def test_byref(self):
10698
def test_floats(self):
10799
# c_float and c_double can be created from
108100
# Python int and float
109-
class FloatLike(object):
101+
class FloatLike:
110102
def __float__(self):
111103
return 2.0
112104
f = FloatLike()
@@ -117,15 +109,15 @@ def __float__(self):
117109
self.assertEqual(t(f).value, 2.0)
118110

119111
def test_integers(self):
120-
class FloatLike(object):
112+
class FloatLike:
121113
def __float__(self):
122114
return 2.0
123115
f = FloatLike()
124-
class IntLike(object):
116+
class IntLike:
125117
def __int__(self):
126118
return 2
127119
d = IntLike()
128-
class IndexLike(object):
120+
class IndexLike:
129121
def __index__(self):
130122
return 2
131123
i = IndexLike()
@@ -205,19 +197,6 @@ def test_char_from_address(self):
205197
a[0] = ord('?')
206198
self.assertEqual(v.value, b'?')
207199

208-
# array does not support c_bool / 't'
209-
@unittest.skip('test disabled')
210-
def test_bool_from_address(self):
211-
from ctypes import c_bool
212-
from array import array
213-
a = array(c_bool._type_, [True])
214-
v = t.from_address(a.buffer_info()[0])
215-
self.assertEqual(v.value, a[0])
216-
self.assertEqual(type(v) is t)
217-
a[0] = False
218-
self.assertEqual(v.value, a[0])
219-
self.assertEqual(type(v) is t)
220-
221200
def test_init(self):
222201
# c_int() can be initialized from Python's int, and c_int.
223202
# Not from c_long or so, which seems strange, abc should
@@ -234,62 +213,6 @@ def test_float_overflow(self):
234213
if (hasattr(t, "__ctype_le__")):
235214
self.assertRaises(OverflowError, t.__ctype_le__, big_int)
236215

237-
@unittest.skip('test disabled')
238-
def test_perf(self):
239-
check_perf()
240-
241-
from ctypes import _SimpleCData
242-
class c_int_S(_SimpleCData):
243-
_type_ = "i"
244-
__slots__ = []
245-
246-
def run_test(rep, msg, func, arg=None):
247-
## items = [None] * rep
248-
items = range(rep)
249-
from time import perf_counter as clock
250-
if arg is not None:
251-
start = clock()
252-
for i in items:
253-
func(arg); func(arg); func(arg); func(arg); func(arg)
254-
stop = clock()
255-
else:
256-
start = clock()
257-
for i in items:
258-
func(); func(); func(); func(); func()
259-
stop = clock()
260-
print("%15s: %.2f us" % (msg, ((stop-start)*1e6/5/rep)))
261-
262-
def check_perf():
263-
# Construct 5 objects
264-
from ctypes import c_int
265-
266-
REP = 200000
267-
268-
run_test(REP, "int()", int)
269-
run_test(REP, "int(999)", int)
270-
run_test(REP, "c_int()", c_int)
271-
run_test(REP, "c_int(999)", c_int)
272-
run_test(REP, "c_int_S()", c_int_S)
273-
run_test(REP, "c_int_S(999)", c_int_S)
274-
275-
# Python 2.3 -OO, win2k, P4 700 MHz:
276-
#
277-
# int(): 0.87 us
278-
# int(999): 0.87 us
279-
# c_int(): 3.35 us
280-
# c_int(999): 3.34 us
281-
# c_int_S(): 3.23 us
282-
# c_int_S(999): 3.24 us
283-
284-
# Python 2.2 -OO, win2k, P4 700 MHz:
285-
#
286-
# int(): 0.89 us
287-
# int(999): 0.89 us
288-
# c_int(): 9.99 us
289-
# c_int(999): 10.02 us
290-
# c_int_S(): 9.87 us
291-
# c_int_S(999): 9.85 us
292216

293217
if __name__ == '__main__':
294-
## check_perf()
295218
unittest.main()

Lib/test/test_abstract_numbers.py

Lines changed: 157 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
"""Unit tests for numbers.py."""
22

3+
import abc
34
import math
45
import operator
56
import unittest
6-
from numbers import Complex, Real, Rational, Integral
7+
from numbers import Complex, Real, Rational, Integral, Number
8+
9+
10+
def concretize(cls):
11+
def not_implemented(*args, **kwargs):
12+
raise NotImplementedError()
13+
14+
for name in dir(cls):
15+
try:
16+
value = getattr(cls, name)
17+
if value.__isabstractmethod__:
18+
setattr(cls, name, not_implemented)
19+
except AttributeError:
20+
pass
21+
abc.update_abstractmethods(cls)
22+
return cls
23+
724

825
class TestNumbers(unittest.TestCase):
926
def test_int(self):
1027
self.assertTrue(issubclass(int, Integral))
28+
self.assertTrue(issubclass(int, Rational))
29+
self.assertTrue(issubclass(int, Real))
1130
self.assertTrue(issubclass(int, Complex))
31+
self.assertTrue(issubclass(int, Number))
1232

1333
self.assertEqual(7, int(7).real)
1434
self.assertEqual(0, int(7).imag)
@@ -18,17 +38,23 @@ def test_int(self):
1838
self.assertEqual(1, int(7).denominator)
1939

2040
def test_float(self):
41+
self.assertFalse(issubclass(float, Integral))
2142
self.assertFalse(issubclass(float, Rational))
2243
self.assertTrue(issubclass(float, Real))
44+
self.assertTrue(issubclass(float, Complex))
45+
self.assertTrue(issubclass(float, Number))
2346

2447
self.assertEqual(7.3, float(7.3).real)
2548
self.assertEqual(0, float(7.3).imag)
2649
self.assertEqual(7.3, float(7.3).conjugate())
2750
self.assertEqual(-7.3, float(-7.3).conjugate())
2851

2952
def test_complex(self):
53+
self.assertFalse(issubclass(complex, Integral))
54+
self.assertFalse(issubclass(complex, Rational))
3055
self.assertFalse(issubclass(complex, Real))
3156
self.assertTrue(issubclass(complex, Complex))
57+
self.assertTrue(issubclass(complex, Number))
3258

3359
c1, c2 = complex(3, 2), complex(4,1)
3460
# XXX: This is not ideal, but see the comment in math_trunc().
@@ -40,5 +66,135 @@ def test_complex(self):
4066
self.assertRaises(TypeError, int, c1)
4167

4268

69+
class TestNumbersDefaultMethods(unittest.TestCase):
70+
def test_complex(self):
71+
@concretize
72+
class MyComplex(Complex):
73+
def __init__(self, real, imag):
74+
self.r = real
75+
self.i = imag
76+
77+
@property
78+
def real(self):
79+
return self.r
80+
81+
@property
82+
def imag(self):
83+
return self.i
84+
85+
def __add__(self, other):
86+
if isinstance(other, Complex):
87+
return MyComplex(self.imag + other.imag,
88+
self.real + other.real)
89+
raise NotImplementedError
90+
91+
def __neg__(self):
92+
return MyComplex(-self.real, -self.imag)
93+
94+
def __eq__(self, other):
95+
if isinstance(other, Complex):
96+
return self.imag == other.imag and self.real == other.real
97+
if isinstance(other, Number):
98+
return self.imag == 0 and self.real == other.real
99+
100+
# test __bool__
101+
self.assertTrue(bool(MyComplex(1, 1)))
102+
self.assertTrue(bool(MyComplex(0, 1)))
103+
self.assertTrue(bool(MyComplex(1, 0)))
104+
self.assertFalse(bool(MyComplex(0, 0)))
105+
106+
# test __sub__
107+
self.assertEqual(MyComplex(2, 3) - complex(1, 2), MyComplex(1, 1))
108+
109+
# test __rsub__
110+
self.assertEqual(complex(2, 3) - MyComplex(1, 2), MyComplex(1, 1))
111+
112+
def test_real(self):
113+
@concretize
114+
class MyReal(Real):
115+
def __init__(self, n):
116+
self.n = n
117+
118+
def __pos__(self):
119+
return self.n
120+
121+
def __float__(self):
122+
return float(self.n)
123+
124+
def __floordiv__(self, other):
125+
return self.n // other
126+
127+
def __rfloordiv__(self, other):
128+
return other // self.n
129+
130+
def __mod__(self, other):
131+
return self.n % other
132+
133+
def __rmod__(self, other):
134+
return other % self.n
135+
136+
# test __divmod__
137+
self.assertEqual(divmod(MyReal(3), 2), (1, 1))
138+
139+
# test __rdivmod__
140+
self.assertEqual(divmod(3, MyReal(2)), (1, 1))
141+
142+
# test __complex__
143+
self.assertEqual(complex(MyReal(1)), 1+0j)
144+
145+
# test real
146+
self.assertEqual(MyReal(3).real, 3)
147+
148+
# test imag
149+
self.assertEqual(MyReal(3).imag, 0)
150+
151+
# test conjugate
152+
self.assertEqual(MyReal(123).conjugate(), 123)
153+
154+
155+
def test_rational(self):
156+
@concretize
157+
class MyRational(Rational):
158+
def __init__(self, numerator, denominator):
159+
self.n = numerator
160+
self.d = denominator
161+
162+
@property
163+
def numerator(self):
164+
return self.n
165+
166+
@property
167+
def denominator(self):
168+
return self.d
169+
170+
# test__float__
171+
self.assertEqual(float(MyRational(5, 2)), 2.5)
172+
173+
174+
def test_integral(self):
175+
@concretize
176+
class MyIntegral(Integral):
177+
def __init__(self, n):
178+
self.n = n
179+
180+
def __pos__(self):
181+
return self.n
182+
183+
def __int__(self):
184+
return self.n
185+
186+
# test __index__
187+
self.assertEqual(operator.index(MyIntegral(123)), 123)
188+
189+
# test __float__
190+
self.assertEqual(float(MyIntegral(123)), 123.0)
191+
192+
# test numerator
193+
self.assertEqual(MyIntegral(123).numerator, 123)
194+
195+
# test denominator
196+
self.assertEqual(MyIntegral(123).denominator, 1)
197+
198+
43199
if __name__ == "__main__":
44200
unittest.main()

0 commit comments

Comments
 (0)