1
1
from ._array_module import (asarray , arange , ceil , empty , empty_like , eye , full ,
2
2
full_like , equal , all , linspace , ones , ones_like ,
3
- zeros , zeros_like , isnan )
3
+ zeros , zeros_like , isnan , float32 )
4
4
from .array_helpers import (is_integer_dtype , dtype_ranges ,
5
5
assert_exactly_equal , isintegral , is_float_dtype )
6
6
from .hypothesis_helpers import (numeric_dtypes , dtypes , MAX_ARRAY_SIZE ,
7
7
shapes , sizes , sqrt_sizes , shared_dtypes ,
8
- scalars , xps )
8
+ scalars , xps , kwargs )
9
9
10
10
from hypothesis import assume , given
11
11
from hypothesis .strategies import integers , floats , one_of , none , booleans , just , shared
@@ -74,7 +74,7 @@ def test_arange(start, stop, step, dtype):
74
74
def test_empty (shape , dtype ):
75
75
if dtype is None :
76
76
a = empty (shape )
77
- assert is_float_dtype (a .dtype ), "empty() should produce an array with the default floating point dtype"
77
+ assert is_float_dtype (a .dtype ), "empty() should returned an array with the default floating point dtype"
78
78
else :
79
79
a = empty (shape , dtype = dtype )
80
80
assert a .dtype == dtype
@@ -85,23 +85,17 @@ def test_empty(shape, dtype):
85
85
86
86
87
87
@given (
88
- x = xps .arrays (
89
- dtype = dtypes ,
90
- shape = shapes ,
91
- ),
92
- dtype = optional_dtypes ,
88
+ x = xps .arrays (dtype = xps .scalar_dtypes (), shape = shapes ),
89
+ kw = kwargs (dtype = none () | xps .scalar_dtypes ())
93
90
)
94
- def test_empty_like (x , dtype ):
95
- kwargs = {} if dtype is None else {'dtype' : dtype }
96
-
97
- x_like = empty_like (x , ** kwargs )
98
-
99
- if dtype is None :
100
- assert x_like .dtype == x .dtype , f"{ x .dtype = !s} , but empty_like() did not produce a { x .dtype } array - instead was { x_like .dtype } "
91
+ def test_empty_like (x , kw ):
92
+ out = empty_like (x , ** kw )
93
+ dtype = kw .get ("dtype" , None ) or x .dtype
94
+ if kw .get ("dtype" , None ) is None :
95
+ assert out .dtype == x .dtype , f"{ x .dtype = !s} , but empty_like() returned an array with dtype { out .dtype } "
101
96
else :
102
- assert x_like .dtype == dtype , f"{ dtype = !s} , but empty_like() did not produce a { dtype } array - instead was { x_like .dtype } "
103
-
104
- assert x_like .shape == x .shape , "empty_like() produced an array with an incorrect shape"
97
+ assert out .dtype == dtype , f"{ dtype = !s} , but empty_like() returned an array with dtype { out .dtype } "
98
+ assert out .shape == x .shape , "empty_like() produced an array with an incorrect shape"
105
99
106
100
107
101
# TODO: Use this method for all optional arguments
@@ -117,7 +111,7 @@ def test_eye(n_rows, n_cols, k, dtype):
117
111
else :
118
112
a = eye (n_rows , n_cols , ** kwargs )
119
113
if dtype is None :
120
- assert is_float_dtype (a .dtype ), "eye() should produce an array with the default floating point dtype"
114
+ assert is_float_dtype (a .dtype ), "eye() should returned an array with the default floating point dtype"
121
115
else :
122
116
assert a .dtype == dtype , "eye() did not produce the correct dtype"
123
117
@@ -142,7 +136,7 @@ def test_full(shape, fill_value, dtype):
142
136
143
137
if dtype is None :
144
138
# TODO: Should it actually match the fill_value?
145
- # assert a.dtype in _floating_dtypes, "eye() should produce an array with the default floating point dtype"
139
+ # assert a.dtype in _floating_dtypes, "eye() should returned an array with the default floating point dtype"
146
140
pass
147
141
else :
148
142
assert a .dtype == dtype
@@ -157,21 +151,20 @@ def test_full(shape, fill_value, dtype):
157
151
@given (
158
152
x = xps .arrays (dtype = shared_dtypes , shape = shapes ),
159
153
fill_value = shared_dtypes .flatmap (xps .from_dtype ),
160
- dtype = shared_optional_dtypes ,
154
+ kw = kwargs ( dtype = none () | shared_dtypes ) ,
161
155
)
162
- def test_full_like (x , fill_value , dtype ):
163
- x_like = full_like (x , fill_value , dtype = dtype )
164
-
165
- if dtype is None :
166
- assert x_like .dtype == x .dtype , f"{ x .dtype = !s} , but full_like() did not produce a { x . dtype } array - instead was { x_like .dtype } "
156
+ def test_full_like (x , fill_value , kw ):
157
+ out = full_like (x , fill_value , ** kw )
158
+ dtype = kw . get ( "dtype" , None ) or x . dtype
159
+ if kw . get ( " dtype" , None ) is None :
160
+ assert out .dtype == x .dtype , f"{ x .dtype = !s} , but full_like() returned an array with dtype { out .dtype } "
167
161
else :
168
- assert x_like .dtype == dtype , f"{ dtype = !s} , but full_like() did not produce a { dtype } array - instead was { x_like .dtype } "
169
-
170
- assert x_like .shape == x .shape , "full_like() produced an array with incorrect shape"
171
- if is_float_dtype (x_like .dtype ) and isnan (asarray (fill_value )):
172
- assert all (isnan (x_like )), "full_like() array did not equal the fill value"
162
+ assert out .dtype == dtype , f"{ dtype = !s} , but full_like() returned an array with dtype { out .dtype } "
163
+ assert out .shape == x .shape , "{x.shape=}, but full_like() returned an array with shape {out.shape}"
164
+ if is_float_dtype (dtype ) and isnan (asarray (fill_value )):
165
+ assert all (isnan (out )), "full_like() array did not equal the fill value"
173
166
else :
174
- assert all (equal (x_like , asarray (fill_value , dtype = x_like . dtype ))), "full_like() array did not equal the fill value"
167
+ assert all (equal (out , asarray (fill_value , dtype = dtype ))), "full_like() array did not equal the fill value"
175
168
176
169
177
170
@given (scalars (shared_dtypes , finite = True ),
@@ -192,11 +185,11 @@ def test_linspace(start, stop, num, dtype, endpoint):
192
185
a = linspace (start , stop , num , ** kwargs )
193
186
194
187
if dtype is None :
195
- assert is_float_dtype (a .dtype ), "linspace() should produce an array with the default floating point dtype"
188
+ assert is_float_dtype (a .dtype ), "linspace() should returned an array with the default floating point dtype"
196
189
else :
197
190
assert a .dtype == dtype , "linspace() did not produce the correct dtype"
198
191
199
- assert a .shape == (num ,), "linspace() did not produce an array with the correct shape"
192
+ assert a .shape == (num ,), "linspace() did not returned an array with the correct shape"
200
193
201
194
if endpoint in [None , True ]:
202
195
if num > 1 :
@@ -217,96 +210,75 @@ def test_linspace(start, stop, num, dtype, endpoint):
217
210
# for i in range(1, num):
218
211
# assert all(equal(a[i], full((), i*(stop - start)/n + start, dtype=dtype))), f"linspace() produced an array with an incorrect value at index {i}"
219
212
220
- @given (shapes , one_of (none (), dtypes ))
221
- def test_ones (shape , dtype ):
222
- kwargs = {} if dtype is None else {'dtype' : dtype }
223
- if dtype is None or is_float_dtype (dtype ):
224
- ONE = 1.0
213
+
214
+ def make_one (dtype ):
215
+ if kwargs is None or is_float_dtype (dtype ):
216
+ return 1.0
225
217
elif is_integer_dtype (dtype ):
226
- ONE = 1
218
+ return 1
227
219
else :
228
- ONE = True
220
+ return True
229
221
230
- a = ones (shape , ** kwargs )
231
222
232
- if dtype is None :
233
- # TODO: Should it actually match the fill_value?
234
- # assert a.dtype in _floating_dtypes, "eye() should produce an array with the default floating point dtype"
235
- pass
223
+ @given (shapes , kwargs (dtype = none () | xps .scalar_dtypes ()))
224
+ def test_ones (shape , kw ):
225
+ out = ones (shape , ** kw )
226
+ dtype = kw .get ("dtype" , None ) or float32
227
+ if kw .get ("dtype" , None ) is None :
228
+ assert is_float_dtype (out .dtype ), "ones() returned an array with dtype {x.dtype}, but should be the default float dtype"
236
229
else :
237
- assert a .dtype == dtype
238
-
239
- assert a .shape == shape , "ones() produced an array with incorrect shape"
240
- assert all (equal (a , full ((), ONE , ** kwargs ))), "ones() array did not equal 1"
230
+ assert out .dtype == dtype , f"{ dtype = !s} , but ones() returned an array with dtype { out .dtype } "
231
+ assert out .shape == shape , "ones() produced an array with incorrect shape"
232
+ assert all (equal (out , full ((), make_one (dtype ), dtype = dtype ))), "ones() array did not equal 1"
241
233
242
234
243
235
@given (
244
236
x = xps .arrays (dtype = dtypes , shape = shapes ),
245
- dtype = optional_dtypes ,
237
+ kw = kwargs ( dtype = none () | xps . scalar_dtypes ()) ,
246
238
)
247
- def test_ones_like (x , dtype ):
248
- kwargs = {} if dtype is None else {'dtype' : dtype }
249
- if kwargs is None or is_float_dtype (x .dtype ):
250
- ONE = 1.0
251
- elif is_integer_dtype (x .dtype ):
252
- ONE = 1
239
+ def test_ones_like (x , kw ):
240
+ out = ones_like (x , ** kw )
241
+ dtype = kw .get ("dtype" , None ) or x .dtype
242
+ if kw .get ("dtype" , None ) is None :
243
+ assert out .dtype == x .dtype , f"{ x .dtype = !s} , but ones_like() returned an array with dtype { out .dtype } "
253
244
else :
254
- ONE = True
245
+ assert out .dtype == dtype , f"{ dtype = !s} , but ones_like() returned an array with dtype { out .dtype } "
246
+ assert out .shape == x .shape , "{x.shape=}, but ones_like() returned an array with shape {out.shape}"
247
+ assert all (equal (out , full ((), make_one (dtype ), dtype = dtype ))), "ones_like() array elements did not equal 1"
255
248
256
- x_like = ones_like (x , ** kwargs )
257
249
258
- if dtype is None :
259
- assert x_like .dtype == x .dtype , f"{ x .dtype = !s} , but ones_like() did not produce a { x .dtype } array - instead was { x_like .dtype } "
260
- else :
261
- assert x_like .dtype == dtype , f"{ dtype = !s} , but ones_like() did not produce a { dtype } array - instead was { x_like .dtype } "
262
-
263
- assert x_like .shape == x .shape , "ones_like() produced an array with an incorrect shape"
264
- assert all (equal (x_like , full ((), ONE , dtype = x_like .dtype ))), "ones_like() array did not equal 1"
265
-
266
-
267
- @given (shapes , one_of (none (), dtypes ))
268
- def test_zeros (shape , dtype ):
269
- kwargs = {} if dtype is None else {'dtype' : dtype }
270
- if dtype is None or is_float_dtype (dtype ):
271
- ZERO = 0.0
250
+ def make_zero (dtype ):
251
+ if is_float_dtype (dtype ):
252
+ return 0.0
272
253
elif is_integer_dtype (dtype ):
273
- ZERO = 0
254
+ return 0
274
255
else :
275
- ZERO = False
256
+ return False
276
257
277
- a = zeros (shape , ** kwargs )
278
258
279
- if dtype is None :
280
- # TODO: Should it actually match the fill_value?
281
- # assert a.dtype in _floating_dtypes, "eye() should produce an array with the default floating point dtype"
282
- pass
259
+ @given (shapes , kwargs (dtype = none () | xps .scalar_dtypes ()))
260
+ def test_zeros (shape , kw ):
261
+ out = zeros (shape , ** kw )
262
+ dtype = kw .get ("dtype" , None ) or float32
263
+ if kw .get ("dtype" , None ) is None :
264
+ assert is_float_dtype (out .dtype ), "zeros() returned an array with dtype {out.dtype}, but should be the default float dtype"
283
265
else :
284
- assert a .dtype == dtype
285
-
286
- assert a .shape == shape , "zeros() produced an array with incorrect shape"
287
- assert all (equal (a , full ((), ZERO , ** kwargs ))), "zeros() array did not equal 0"
266
+ assert out .dtype == dtype , f"{ dtype = !s} , but zeros() returned an array with dtype { out .dtype } "
267
+ assert out .shape == shape , "zeros() produced an array with incorrect shape"
268
+ assert all (equal (out , full ((), make_zero (dtype ), dtype = dtype ))), "zeros() array did not equal 0"
288
269
289
270
290
271
@given (
291
272
x = xps .arrays (dtype = dtypes , shape = shapes ),
292
- dtype = optional_dtypes ,
273
+ kw = kwargs ( dtype = none () | xps . scalar_dtypes ()) ,
293
274
)
294
- def test_zeros_like (x , dtype ):
295
- kwargs = {} if dtype is None else {'dtype' : dtype }
296
- if dtype is None or is_float_dtype (x .dtype ):
297
- ZERO = 0.0
298
- elif is_integer_dtype (x .dtype ):
299
- ZERO = 0
300
- else :
301
- ZERO = False
302
-
303
- x_like = zeros_like (x , ** kwargs )
304
-
305
- if dtype is None :
306
- assert x_like .dtype == x .dtype , f"{ x .dtype = !s} , but zeros_like() did not produce a { x .dtype } array - instead was { x_like .dtype } "
275
+ def test_zeros_like (x , kw ):
276
+ out = zeros_like (x , ** kw )
277
+ dtype = kw .get ("dtype" , None ) or x .dtype
278
+ if kw .get ("dtype" , None ) is None :
279
+ assert out .dtype == x .dtype , f"{ x .dtype = !s} , but zeros_like() returned an array with dtype { out .dtype } "
307
280
else :
308
- assert x_like .dtype == dtype , f"{ dtype = !s} , but zeros_like() did not produce a { dtype } array - instead was { x_like .dtype } "
309
-
310
- assert x_like .shape == x .shape , "zeros_like() produced an array with an incorrect shape"
311
- assert all (equal (x_like , full ((), ZERO , dtype = x_like .dtype ))), "zeros_like() array did not equal 0"
281
+ assert out .dtype == dtype , f"{ dtype = !s} , but zeros_like() returned an array with dtype { out .dtype } "
282
+ assert out .shape == x .shape , "{x.shape=}, but zeros_like() returned an array with shape {out.shape}"
283
+ assert all (equal (out , full ((), make_zero (dtype ), dtype = out .dtype ))), "zeros_like() array elements did not all equal 0"
312
284
0 commit comments