mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Copy test for numbers.py from v3.12.3
This commit is contained in:
85
Lib/ctypes/test/test_numbers.py
vendored
85
Lib/ctypes/test/test_numbers.py
vendored
@@ -82,14 +82,6 @@ class NumberTestCase(unittest.TestCase):
|
||||
self.assertRaises(TypeError, t, "")
|
||||
self.assertRaises(TypeError, t, None)
|
||||
|
||||
@unittest.skip('test disabled')
|
||||
def test_valid_ranges(self):
|
||||
# invalid values of the correct type
|
||||
# raise ValueError (not OverflowError)
|
||||
for t, (l, h) in zip(unsigned_types, unsigned_ranges):
|
||||
self.assertRaises(ValueError, t, l-1)
|
||||
self.assertRaises(ValueError, t, h+1)
|
||||
|
||||
def test_from_param(self):
|
||||
# the from_param class method attribute always
|
||||
# returns PyCArgObject instances
|
||||
@@ -106,7 +98,7 @@ class NumberTestCase(unittest.TestCase):
|
||||
def test_floats(self):
|
||||
# c_float and c_double can be created from
|
||||
# Python int and float
|
||||
class FloatLike(object):
|
||||
class FloatLike:
|
||||
def __float__(self):
|
||||
return 2.0
|
||||
f = FloatLike()
|
||||
@@ -117,15 +109,15 @@ class NumberTestCase(unittest.TestCase):
|
||||
self.assertEqual(t(f).value, 2.0)
|
||||
|
||||
def test_integers(self):
|
||||
class FloatLike(object):
|
||||
class FloatLike:
|
||||
def __float__(self):
|
||||
return 2.0
|
||||
f = FloatLike()
|
||||
class IntLike(object):
|
||||
class IntLike:
|
||||
def __int__(self):
|
||||
return 2
|
||||
d = IntLike()
|
||||
class IndexLike(object):
|
||||
class IndexLike:
|
||||
def __index__(self):
|
||||
return 2
|
||||
i = IndexLike()
|
||||
@@ -205,19 +197,6 @@ class NumberTestCase(unittest.TestCase):
|
||||
a[0] = ord('?')
|
||||
self.assertEqual(v.value, b'?')
|
||||
|
||||
# array does not support c_bool / 't'
|
||||
@unittest.skip('test disabled')
|
||||
def test_bool_from_address(self):
|
||||
from ctypes import c_bool
|
||||
from array import array
|
||||
a = array(c_bool._type_, [True])
|
||||
v = t.from_address(a.buffer_info()[0])
|
||||
self.assertEqual(v.value, a[0])
|
||||
self.assertEqual(type(v) is t)
|
||||
a[0] = False
|
||||
self.assertEqual(v.value, a[0])
|
||||
self.assertEqual(type(v) is t)
|
||||
|
||||
def test_init(self):
|
||||
# c_int() can be initialized from Python's int, and c_int.
|
||||
# Not from c_long or so, which seems strange, abc should
|
||||
@@ -234,62 +213,6 @@ class NumberTestCase(unittest.TestCase):
|
||||
if (hasattr(t, "__ctype_le__")):
|
||||
self.assertRaises(OverflowError, t.__ctype_le__, big_int)
|
||||
|
||||
@unittest.skip('test disabled')
|
||||
def test_perf(self):
|
||||
check_perf()
|
||||
|
||||
from ctypes import _SimpleCData
|
||||
class c_int_S(_SimpleCData):
|
||||
_type_ = "i"
|
||||
__slots__ = []
|
||||
|
||||
def run_test(rep, msg, func, arg=None):
|
||||
## items = [None] * rep
|
||||
items = range(rep)
|
||||
from time import perf_counter as clock
|
||||
if arg is not None:
|
||||
start = clock()
|
||||
for i in items:
|
||||
func(arg); func(arg); func(arg); func(arg); func(arg)
|
||||
stop = clock()
|
||||
else:
|
||||
start = clock()
|
||||
for i in items:
|
||||
func(); func(); func(); func(); func()
|
||||
stop = clock()
|
||||
print("%15s: %.2f us" % (msg, ((stop-start)*1e6/5/rep)))
|
||||
|
||||
def check_perf():
|
||||
# Construct 5 objects
|
||||
from ctypes import c_int
|
||||
|
||||
REP = 200000
|
||||
|
||||
run_test(REP, "int()", int)
|
||||
run_test(REP, "int(999)", int)
|
||||
run_test(REP, "c_int()", c_int)
|
||||
run_test(REP, "c_int(999)", c_int)
|
||||
run_test(REP, "c_int_S()", c_int_S)
|
||||
run_test(REP, "c_int_S(999)", c_int_S)
|
||||
|
||||
# Python 2.3 -OO, win2k, P4 700 MHz:
|
||||
#
|
||||
# int(): 0.87 us
|
||||
# int(999): 0.87 us
|
||||
# c_int(): 3.35 us
|
||||
# c_int(999): 3.34 us
|
||||
# c_int_S(): 3.23 us
|
||||
# c_int_S(999): 3.24 us
|
||||
|
||||
# Python 2.2 -OO, win2k, P4 700 MHz:
|
||||
#
|
||||
# int(): 0.89 us
|
||||
# int(999): 0.89 us
|
||||
# c_int(): 9.99 us
|
||||
# c_int(999): 10.02 us
|
||||
# c_int_S(): 9.87 us
|
||||
# c_int_S(999): 9.85 us
|
||||
|
||||
if __name__ == '__main__':
|
||||
## check_perf()
|
||||
unittest.main()
|
||||
|
||||
158
Lib/test/test_abstract_numbers.py
vendored
158
Lib/test/test_abstract_numbers.py
vendored
@@ -1,14 +1,34 @@
|
||||
"""Unit tests for numbers.py."""
|
||||
|
||||
import abc
|
||||
import math
|
||||
import operator
|
||||
import unittest
|
||||
from numbers import Complex, Real, Rational, Integral
|
||||
from numbers import Complex, Real, Rational, Integral, Number
|
||||
|
||||
|
||||
def concretize(cls):
|
||||
def not_implemented(*args, **kwargs):
|
||||
raise NotImplementedError()
|
||||
|
||||
for name in dir(cls):
|
||||
try:
|
||||
value = getattr(cls, name)
|
||||
if value.__isabstractmethod__:
|
||||
setattr(cls, name, not_implemented)
|
||||
except AttributeError:
|
||||
pass
|
||||
abc.update_abstractmethods(cls)
|
||||
return cls
|
||||
|
||||
|
||||
class TestNumbers(unittest.TestCase):
|
||||
def test_int(self):
|
||||
self.assertTrue(issubclass(int, Integral))
|
||||
self.assertTrue(issubclass(int, Rational))
|
||||
self.assertTrue(issubclass(int, Real))
|
||||
self.assertTrue(issubclass(int, Complex))
|
||||
self.assertTrue(issubclass(int, Number))
|
||||
|
||||
self.assertEqual(7, int(7).real)
|
||||
self.assertEqual(0, int(7).imag)
|
||||
@@ -18,8 +38,11 @@ class TestNumbers(unittest.TestCase):
|
||||
self.assertEqual(1, int(7).denominator)
|
||||
|
||||
def test_float(self):
|
||||
self.assertFalse(issubclass(float, Integral))
|
||||
self.assertFalse(issubclass(float, Rational))
|
||||
self.assertTrue(issubclass(float, Real))
|
||||
self.assertTrue(issubclass(float, Complex))
|
||||
self.assertTrue(issubclass(float, Number))
|
||||
|
||||
self.assertEqual(7.3, float(7.3).real)
|
||||
self.assertEqual(0, float(7.3).imag)
|
||||
@@ -27,8 +50,11 @@ class TestNumbers(unittest.TestCase):
|
||||
self.assertEqual(-7.3, float(-7.3).conjugate())
|
||||
|
||||
def test_complex(self):
|
||||
self.assertFalse(issubclass(complex, Integral))
|
||||
self.assertFalse(issubclass(complex, Rational))
|
||||
self.assertFalse(issubclass(complex, Real))
|
||||
self.assertTrue(issubclass(complex, Complex))
|
||||
self.assertTrue(issubclass(complex, Number))
|
||||
|
||||
c1, c2 = complex(3, 2), complex(4,1)
|
||||
# XXX: This is not ideal, but see the comment in math_trunc().
|
||||
@@ -40,5 +66,135 @@ class TestNumbers(unittest.TestCase):
|
||||
self.assertRaises(TypeError, int, c1)
|
||||
|
||||
|
||||
class TestNumbersDefaultMethods(unittest.TestCase):
|
||||
def test_complex(self):
|
||||
@concretize
|
||||
class MyComplex(Complex):
|
||||
def __init__(self, real, imag):
|
||||
self.r = real
|
||||
self.i = imag
|
||||
|
||||
@property
|
||||
def real(self):
|
||||
return self.r
|
||||
|
||||
@property
|
||||
def imag(self):
|
||||
return self.i
|
||||
|
||||
def __add__(self, other):
|
||||
if isinstance(other, Complex):
|
||||
return MyComplex(self.imag + other.imag,
|
||||
self.real + other.real)
|
||||
raise NotImplementedError
|
||||
|
||||
def __neg__(self):
|
||||
return MyComplex(-self.real, -self.imag)
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Complex):
|
||||
return self.imag == other.imag and self.real == other.real
|
||||
if isinstance(other, Number):
|
||||
return self.imag == 0 and self.real == other.real
|
||||
|
||||
# test __bool__
|
||||
self.assertTrue(bool(MyComplex(1, 1)))
|
||||
self.assertTrue(bool(MyComplex(0, 1)))
|
||||
self.assertTrue(bool(MyComplex(1, 0)))
|
||||
self.assertFalse(bool(MyComplex(0, 0)))
|
||||
|
||||
# test __sub__
|
||||
self.assertEqual(MyComplex(2, 3) - complex(1, 2), MyComplex(1, 1))
|
||||
|
||||
# test __rsub__
|
||||
self.assertEqual(complex(2, 3) - MyComplex(1, 2), MyComplex(1, 1))
|
||||
|
||||
def test_real(self):
|
||||
@concretize
|
||||
class MyReal(Real):
|
||||
def __init__(self, n):
|
||||
self.n = n
|
||||
|
||||
def __pos__(self):
|
||||
return self.n
|
||||
|
||||
def __float__(self):
|
||||
return float(self.n)
|
||||
|
||||
def __floordiv__(self, other):
|
||||
return self.n // other
|
||||
|
||||
def __rfloordiv__(self, other):
|
||||
return other // self.n
|
||||
|
||||
def __mod__(self, other):
|
||||
return self.n % other
|
||||
|
||||
def __rmod__(self, other):
|
||||
return other % self.n
|
||||
|
||||
# test __divmod__
|
||||
self.assertEqual(divmod(MyReal(3), 2), (1, 1))
|
||||
|
||||
# test __rdivmod__
|
||||
self.assertEqual(divmod(3, MyReal(2)), (1, 1))
|
||||
|
||||
# test __complex__
|
||||
self.assertEqual(complex(MyReal(1)), 1+0j)
|
||||
|
||||
# test real
|
||||
self.assertEqual(MyReal(3).real, 3)
|
||||
|
||||
# test imag
|
||||
self.assertEqual(MyReal(3).imag, 0)
|
||||
|
||||
# test conjugate
|
||||
self.assertEqual(MyReal(123).conjugate(), 123)
|
||||
|
||||
|
||||
def test_rational(self):
|
||||
@concretize
|
||||
class MyRational(Rational):
|
||||
def __init__(self, numerator, denominator):
|
||||
self.n = numerator
|
||||
self.d = denominator
|
||||
|
||||
@property
|
||||
def numerator(self):
|
||||
return self.n
|
||||
|
||||
@property
|
||||
def denominator(self):
|
||||
return self.d
|
||||
|
||||
# test__float__
|
||||
self.assertEqual(float(MyRational(5, 2)), 2.5)
|
||||
|
||||
|
||||
def test_integral(self):
|
||||
@concretize
|
||||
class MyIntegral(Integral):
|
||||
def __init__(self, n):
|
||||
self.n = n
|
||||
|
||||
def __pos__(self):
|
||||
return self.n
|
||||
|
||||
def __int__(self):
|
||||
return self.n
|
||||
|
||||
# test __index__
|
||||
self.assertEqual(operator.index(MyIntegral(123)), 123)
|
||||
|
||||
# test __float__
|
||||
self.assertEqual(float(MyIntegral(123)), 123.0)
|
||||
|
||||
# test numerator
|
||||
self.assertEqual(MyIntegral(123).numerator, 123)
|
||||
|
||||
# test denominator
|
||||
self.assertEqual(MyIntegral(123).denominator, 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user