22
33from __future__ import division
44from numpy import *
5+ from numpy .testing import assert_
56from rational import *
67
78R = rational
89
910def test_misc ():
1011 x = R ()
11- y = R (7 )
12+ y = R (7 )
1213 z = R (- 6 ,- 10 )
13- assert not x
14- assert y and z
15- assert z .n is 3
16- assert z .d is 5
17- assert str (y )== '7'
18- assert str (z )== '3/5'
19- assert repr (y )== 'rational(7)'
20- assert repr (z )== 'rational(3,5)'
14+ assert_ ( not x )
15+ assert_ ( y and z )
16+ assert_ ( z .n is 3 )
17+ assert_ ( z .d is 5 )
18+ assert_ ( str (y )== '7' )
19+ assert_ ( str (z )== '3/5' )
20+ assert_ ( repr (y )== 'rational(7)' )
21+ assert_ ( repr (z )== 'rational(3,5)' )
2122
2223def test_parse ():
23- assert rational ("4" )== 4
24- assert rational (" -4 " )== - 4
25- assert rational ("3/5" )== R (3 ,5 )
26- assert rational (" -3/5 " )== R (- 3 ,5 )
24+ assert_ ( rational ("4" )== 4 )
25+ assert_ ( rational (" -4 " )== - 4 )
26+ assert_ ( rational ("3/5" )== R (3 ,5 ) )
27+ assert_ ( rational (" -3/5 " )== R (- 3 ,5 ) )
2728 for s in '-4 5' ,'1/0' ,'1/-1' ,'1/' :
2829 try :
2930 rational (s )
30- assert False
31+ assert_ ( False )
3132 except ValueError :
3233 pass
3334
@@ -37,82 +38,82 @@ def test_compare():
3738 xn ,yn = random .randint (- 10 ,10 ,2 )
3839 xd ,yd = random .randint (1 ,10 ,2 )
3940 x ,y = R (xn ,xd ),R (yn ,yd )
40- assert bool (x )== bool (xn )
41- assert ( x == y )== (xn * yd == yn * xd )
42- assert ( x < y )== (xn * yd < yn * xd )
43- assert ( x > y )== (xn * yd > yn * xd )
44- assert ( x <= y )== (xn * yd <= yn * xd )
45- assert ( x >= y )== (xn * yd >= yn * xd )
41+ assert_ ( bool (x )== bool (xn ) )
42+ assert_ (( x == y )== (xn * yd == yn * xd ) )
43+ assert_ (( x < y )== (xn * yd < yn * xd ) )
44+ assert_ (( x > y )== (xn * yd > yn * xd ) )
45+ assert_ (( x <= y )== (xn * yd <= yn * xd ) )
46+ assert_ (( x >= y )== (xn * yd >= yn * xd ) )
4647 # Not true in general, but should be for this sample size
47- assert ( hash (x )== hash (y ))== (x == y )
48+ assert_ (( hash (x )== hash (y ))== (x == y ) )
4849
4950def test_arithmetic ():
5051 random .seed (1262081 )
5152 for _ in xrange (100 ):
5253 xn ,yn ,zn = random .randint (- 100 ,100 ,3 )
5354 xd ,yd ,zd = [n if n else 1 for n in random .randint (- 100 ,100 ,3 )]
5455 x ,y ,z = R (xn ,xd ),R (yn ,yd ),R (zn ,zd )
55- assert - x == R (- xn ,xd )
56- assert + x is x
57- assert - - x == x
58- assert x + y == R (xn * yd + yn * xd ,xd * yd )
59- assert x + y == x - - y == R (xn * yd + yn * xd ,xd * yd )
60- assert - x + y == - (x - y )
61- assert ( x + y )+ z == x + (y + z )
62- assert x * y == R (xn * yn ,xd * yd )
63- assert ( x * y )* z == x * (y * z )
64- assert - (x * y )== (- x )* y
65- assert x * y == y * x
66- assert x * (y + z )== x * y + x * z
56+ assert_ ( - x == R (- xn ,xd ) )
57+ assert_ ( + x is x )
58+ assert_ ( - - x == x )
59+ assert_ ( x + y == R (xn * yd + yn * xd ,xd * yd ) )
60+ assert_ ( x + y == x - - y == R (xn * yd + yn * xd ,xd * yd ) )
61+ assert_ ( - x + y == - (x - y ) )
62+ assert_ (( x + y )+ z == x + (y + z ) )
63+ assert_ ( x * y == R (xn * yn ,xd * yd ) )
64+ assert_ (( x * y )* z == x * (y * z ) )
65+ assert_ ( - (x * y )== (- x )* y )
66+ assert_ ( x * y == y * x )
67+ assert_ ( x * (y + z )== x * y + x * z )
6768 if y :
68- assert x / y == R (xn * yd ,xd * yn )
69- assert x / y * y == x
70- assert x // y == xn * yd // (xd * yn )
71- assert x % y == x - x // y * y
72- assert x + 7 == 7 + x == x + R (7 )
73- assert x * 7 == 7 * x == x * R (7 )
74- assert int (x )== int (xn / xd )
75- assert allclose (float (x ),xn / xd )
76- assert abs (x )== R (abs (xn ),abs (xd ))
69+ assert_ ( x / y == R (xn * yd ,xd * yn ) )
70+ assert_ ( x / y * y == x )
71+ assert_ ( x // y == xn * yd // (xd * yn ) )
72+ assert_ ( x % y == x - x // y * y )
73+ assert_ ( x + 7 == 7 + x == x + R (7 ) )
74+ assert_ ( x * 7 == 7 * x == x * R (7 ) )
75+ assert_ ( int (x )== int (xn / xd ) )
76+ assert_ ( allclose (float (x ),xn / xd ) )
77+ assert_ ( abs (x )== R (abs (xn ),abs (xd ) ))
7778 # TODO: test floor, ceil, abs
7879
7980def test_errors ():
8081 # Check invalid constructions
8182 for args in (R (3 ,2 ),4 ),(1.2 ,),(1 ,2 ,3 ):
8283 try :
8384 R (* args )
84- assert False
85+ assert_ ( False )
8586 except TypeError :
8687 pass
8788 for args in (1 << 80 ,),(2 ,1 << 80 ):
8889 try :
8990 R (* args )
90- assert False
91+ assert_ ( False )
9192 except OverflowError :
9293 pass
9394 # Check for zero divisions
9495 try :
9596 R (1 ,0 )
96- assert False
97+ assert_ ( False )
9798 except ZeroDivisionError :
9899 pass
99100 try :
100101 R (7 )/ R ()
101- assert False
102+ assert_ ( False )
102103 except ZeroDivisionError :
103104 pass
104105 # Check for LONG_MIN overflows
105106 for args in (- 1 << 63 ,- 1 ),(1 << 63 ,):
106107 try :
107108 R (* args )
108- assert False
109+ assert_ ( False )
109110 except OverflowError :
110111 pass
111112 # Check for overflow in addition
112113 r = R (1 << 30 )
113114 try :
114115 r + r
115- assert False
116+ assert_ ( False )
116117 except OverflowError :
117118 pass
118119 # Check for overflow in multiplication
@@ -122,80 +123,80 @@ def test_errors():
122123 r *= p
123124 try :
124125 r * p
125- assert False
126+ assert_ ( False )
126127 except OverflowError :
127128 pass
128129 # Float/rational arithmetic should fail
129130 for x ,y in (.2 ,R (3 ,2 )),(R (3 ,2 ),.2 ):
130131 try :
131132 x + y
132- assert False
133+ assert_ ( False )
133134 except TypeError :
134135 pass
135136
136137def test_numpy_basic ():
137138 d = dtype (rational )
138- assert d .itemsize == 8
139+ assert_ ( d .itemsize == 8 )
139140 x = zeros (5 ,d )
140- assert type (x [2 ]) is rational
141- assert x [3 ]== 0
142- assert ones (5 ,d )[3 ]== 1
141+ assert_ ( type (x [2 ]) is rational )
142+ assert_ ( x [3 ]== 0 )
143+ assert_ ( ones (5 ,d )[3 ]== 1 )
143144 x [2 ] = 2
144- assert x [2 ]== 2
145+ assert_ ( x [2 ]== 2 )
145146 x [3 ] = R (4 ,5 )
146- assert 5 * x [3 ]== 4
147+ assert_ ( 5 * x [3 ]== 4 )
147148 try :
148149 x [4 ] = 1.2
149- assert False
150+ assert_ ( False )
150151 except TypeError :
151152 pass
152153 i = arange (R (1 ,3 ),R (5 ,3 ),R (1 ,3 ))
153- assert i .dtype is d
154- assert all (i == [R (1 ,3 ),R (2 ,3 ),R (3 ,3 ),R (4 ,3 )])
155- assert numerator (i ).dtype == denominator (i ).dtype == dtype (int64 )
156- assert all (numerator (i )== [1 ,2 ,1 ,4 ])
157- assert all (denominator (i )== [3 ,3 ,1 ,3 ])
154+ assert_ ( i .dtype is d )
155+ assert_ ( all (i == [R (1 ,3 ),R (2 ,3 ),R (3 ,3 ),R (4 ,3 )]) )
156+ assert_ ( numerator (i ).dtype == denominator (i ).dtype == dtype (int64 ) )
157+ assert_ ( all (numerator (i )== [1 ,2 ,1 ,4 ]) )
158+ assert_ ( all (denominator (i )== [3 ,3 ,1 ,3 ]) )
158159 y = zeros (4 ,d )
159160 y [1 :3 ] = i [1 :3 ] # Test unstride copyswapn
160- assert all (y == [0 ,R (2 ,3 ),R (3 ,3 ),0 ])
161- assert all (nonzero (y )[0 ]== (1 ,2 ))
161+ assert_ ( all (y == [0 ,R (2 ,3 ),R (3 ,3 ),0 ]) )
162+ assert_ ( all (nonzero (y )[0 ]== (1 ,2 ) ))
162163 y [::3 ] = i [:2 ] # Test strided copyswapn
163- assert all (y == [R (1 ,3 ),R (2 ,3 ),R (3 ,3 ),R (2 ,3 )])
164- assert searchsorted (arange (0 ,20 ),R (7 ,2 ))== 4 # Test compare
165- assert argmin (y )== 0
166- assert argmax (y )== 2
167- assert y .min ()== R (1 ,3 )
168- assert y .max ()== 1
169- assert dot (i ,y )== R (22 ,9 )
164+ assert_ ( all (y == [R (1 ,3 ),R (2 ,3 ),R (3 ,3 ),R (2 ,3 )]) )
165+ assert_ ( searchsorted (arange (0 ,20 ),R (7 ,2 ))== 4 ) # Test compare
166+ assert_ ( argmin (y )== 0 )
167+ assert_ ( argmax (y )== 2 )
168+ assert_ ( y .min ()== R (1 ,3 ) )
169+ assert_ ( y .max ()== 1 )
170+ assert_ ( dot (i ,y )== R (22 ,9 ) )
170171 y [:] = 7 # Test fillwithscalar
171- assert all (y == 7 )
172+ assert_ ( all (y == 7 ) )
172173
173174def test_numpy_cast ():
174175 r = arange (R (10 ,3 ),step = R (1 ,3 ),dtype = rational )
175176 # Check integer to rational conversion
176177 for T in int8 ,int32 ,int64 :
177178 n = arange (10 ,dtype = T )
178- assert all (n .astype (rational )== 3 * r )
179- assert all (n + r == 4 * r )
179+ assert_ ( all (n .astype (rational )== 3 * r ) )
180+ assert_ ( all (n + r == 4 * r ) )
180181 # Check rational to integer conversion
181- assert all (r .astype (int )== r .astype (float ).astype (int ))
182+ assert_ ( all (r .astype (int )== r .astype (float ).astype (int ) ))
182183 # Check detection of overflow during casts
183184 for x in array (1 << 40 ),array ([1 << 40 ]):
184185 try :
185186 x .astype (int64 ).astype (rational )
186- assert False
187+ assert_ ( False )
187188 except OverflowError :
188189 pass
189190 # Check conversion to and from floating point
190191 for T in float ,double :
191192 f = arange (10 ,dtype = float )/ 3
192- assert allclose (r .astype (float ),f )
193+ assert_ ( allclose (r .astype (float ),f ) )
193194 rf = r + f
194- assert rf .dtype == dtype (float )
195- assert allclose (rf ,2 * f )
195+ assert_ ( rf .dtype == dtype (float ) )
196+ assert_ ( allclose (rf ,2 * f ) )
196197 try :
197198 f .astype (rational )
198- assert False
199+ assert_ ( False )
199200 except ValueError :
200201 pass
201202
@@ -211,50 +212,50 @@ def test_numpy_ufunc():
211212 for f in add ,subtract ,multiply ,minimum ,maximum ,divide ,true_divide :
212213 z = s if f in (divide ,true_divide ) else y
213214 fxy = f (x ,z )
214- assert fxy .dtype is d
215- assert allclose (fxy ,f (xf ,z ))
216- assert all (x // s == floor (x / s ))
217- assert all (x % s == x - x // s * s )
215+ assert_ ( fxy .dtype is d )
216+ assert_ ( allclose (fxy ,f (xf ,z ) ))
217+ assert_ ( all (x // s == floor (x / s ) ))
218+ assert_ ( all (x % s == x - x // s * s ) )
218219 xn ,yn = numerator (x ),numerator (y )
219220 xd ,yd = denominator (x ),denominator (y )
220221 for f in equal ,not_equal ,less ,greater ,less_equal ,greater_equal :
221- assert all (f (x ,y )== f (xn * yd ,yn * xd ))
222+ assert_ ( all (f (x ,y )== f (xn * yd ,yn * xd ) ))
222223 for f in negative ,absolute ,floor ,ceil ,trunc ,square ,sign :
223224 fx = f (x )
224- assert fx .dtype is d
225- assert allclose (fx ,f (xf ))
226- assert all (denominator (rint (x ))== 1 )
227- assert all (absolute (rint (x )- x )<= R (1 ,2 ))
228- assert all (reciprocal (s )* s == 1 )
225+ assert_ ( fx .dtype is d )
226+ assert_ ( allclose (fx ,f (xf ) ))
227+ assert_ ( all (denominator (rint (x ))== 1 ) )
228+ assert_ ( all (absolute (rint (x )- x )<= R (1 ,2 ) ))
229+ assert_ ( all (reciprocal (s )* s == 1 ) )
229230 # Check that missing ufuncs promote to float
230231 r = array ([R (5 ,3 )])
231- assert r .dtype == dtype (rational )
232- assert sin (r ).dtype == dtype (float )
232+ assert_ ( r .dtype == dtype (rational ) )
233+ assert_ ( sin (r ).dtype == dtype (float ) )
233234
234235def test_gcd_lcm ():
235236 x = arange (- 10 ,10 ).reshape (- 1 ,1 )
236237 y = x .reshape (1 ,- 1 )
237238 z = x .reshape (- 1 ,1 ,1 )
238239 g = gcd (x ,y )
239240 l = lcm (x ,y )
240- assert all (g * l == absolute (x * y ))
241- assert all (gcd (x ,lcm (y ,z ))== lcm (gcd (x ,y ),gcd (x ,z )))
242- assert all (gcd (2 ,[1 ,2 ,3 ,4 ,5 ,6 ])== [1 ,2 ,1 ,2 ,1 ,2 ])
243- assert all (lcm (2 ,[1 ,2 ,3 ,4 ,5 ,6 ])== [2 ,2 ,6 ,4 ,10 ,6 ])
244- assert lcm .reduce (arange (1 ,10 ))== 2520
241+ assert_ ( all (g * l == absolute (x * y ) ))
242+ assert_ ( all (gcd (x ,lcm (y ,z ))== lcm (gcd (x ,y ),gcd (x ,z ) )))
243+ assert_ ( all (gcd (2 ,[1 ,2 ,3 ,4 ,5 ,6 ])== [1 ,2 ,1 ,2 ,1 ,2 ]) )
244+ assert_ ( all (lcm (2 ,[1 ,2 ,3 ,4 ,5 ,6 ])== [2 ,2 ,6 ,4 ,10 ,6 ]) )
245+ assert_ ( lcm .reduce (arange (1 ,10 ))== 2520 )
245246
246247def test_numpy_errors ():
247248 # Check that exceptions inside ufuncs are detected
248249 r = array ([1 << 30 ]).astype (rational )
249250 try :
250251 r + r
251- assert False
252+ assert_ ( False )
252253 except OverflowError :
253254 pass
254255 r = zeros (3 ,rational )
255256 try :
256257 reciprocal (r )
257- assert False
258+ assert_ ( False )
258259 except ZeroDivisionError :
259260 pass
260261
0 commit comments