11"""Unit tests for numbers.py."""
22
3+ import abc
34import math
45import operator
56import 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
825class 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+
43199if __name__ == "__main__" :
44200 unittest .main ()
0 commit comments