2
2
from .helper import get_all_dtypes
3
3
4
4
import dpnp
5
+
5
6
import numpy
7
+ from numpy .testing import (
8
+ assert_ ,
9
+ assert_allclose ,
10
+ assert_array_equal ,
11
+ assert_equal ,
12
+ assert_raises ,
13
+ assert_warns
14
+ )
6
15
7
16
8
17
@pytest .mark .usefixtures ("allow_fall_back_on_numpy" )
@@ -14,7 +23,7 @@ def test_asfarray(dtype, data):
14
23
expected = numpy .asfarray (data , dtype )
15
24
result = dpnp .asfarray (data , dtype )
16
25
17
- numpy . testing . assert_array_equal (result , expected )
26
+ assert_array_equal (result , expected )
18
27
19
28
20
29
@pytest .mark .parametrize ("dtype" , get_all_dtypes ())
@@ -24,7 +33,99 @@ def test_asfarray2(dtype, data, data_dtype):
24
33
expected = numpy .asfarray (numpy .array (data , dtype = data_dtype ), dtype )
25
34
result = dpnp .asfarray (dpnp .array (data , dtype = data_dtype ), dtype )
26
35
27
- numpy .testing .assert_array_equal (result , expected )
36
+ assert_array_equal (result , expected )
37
+
38
+
39
+ class TestDims :
40
+ @pytest .mark .parametrize ("dt" , get_all_dtypes ())
41
+ @pytest .mark .parametrize ("sh" ,
42
+ [(0 ,), (1 ,), (3 ,)],
43
+ ids = ['(0,)' , '(1,)' , '(3,)' ])
44
+ def test_broadcast_array (self , sh , dt ):
45
+ np_a = numpy .array (0 , dtype = dt )
46
+ dp_a = dpnp .array (0 , dtype = dt )
47
+ func = lambda xp , a : xp .broadcast_to (a , sh )
48
+
49
+ assert_allclose (func (numpy , np_a ), func (dpnp , dp_a ))
50
+
51
+ @pytest .mark .parametrize ("dt" , get_all_dtypes ())
52
+ @pytest .mark .parametrize ("sh" ,
53
+ [(1 ,), (2 ,), (1 , 2 , 3 )],
54
+ ids = ['(1,)' , '(2,)' , '(1, 2, 3)' ])
55
+ def test_broadcast_ones (self , sh , dt ):
56
+ np_a = numpy .ones (1 , dtype = dt )
57
+ dp_a = dpnp .ones (1 , dtype = dt )
58
+ func = lambda xp , a : xp .broadcast_to (a , sh )
59
+
60
+ assert_allclose (func (numpy , np_a ), func (dpnp , dp_a ))
61
+
62
+ @pytest .mark .parametrize ("dt" , get_all_dtypes (no_bool = True ))
63
+ @pytest .mark .parametrize ("sh" ,
64
+ [(3 ,), (1 , 3 ), (2 , 3 )],
65
+ ids = ['(3,)' , '(1, 3)' , '(2, 3)' ])
66
+ def test_broadcast_arange (self , sh , dt ):
67
+ np_a = numpy .arange (3 , dtype = dt )
68
+ dp_a = dpnp .arange (3 , dtype = dt )
69
+ func = lambda xp , a : xp .broadcast_to (a , sh )
70
+
71
+ assert_allclose (func (numpy , np_a ), func (dpnp , dp_a ))
72
+
73
+ @pytest .mark .parametrize ("dt" , get_all_dtypes ())
74
+ @pytest .mark .parametrize (
75
+ "sh1, sh2" ,
76
+ [
77
+ pytest .param ([0 ], [0 ], id = "(0)" ),
78
+ pytest .param ([1 ], [1 ], id = "(1)" ),
79
+ pytest .param ([1 ], [2 ], id = "(2)" ),
80
+ ],
81
+ )
82
+ def test_broadcast_not_tuple (self , sh1 , sh2 , dt ):
83
+ np_a = numpy .ones (sh1 , dtype = dt )
84
+ dp_a = dpnp .ones (sh1 , dtype = dt )
85
+ func = lambda xp , a : xp .broadcast_to (a , sh2 )
86
+
87
+ assert_allclose (func (numpy , np_a ), func (dpnp , dp_a ))
88
+
89
+ @pytest .mark .parametrize ("dt" , get_all_dtypes ())
90
+ @pytest .mark .parametrize (
91
+ "sh1, sh2" ,
92
+ [
93
+ pytest .param ([1 ], (0 ,), id = "(0,)" ),
94
+ pytest .param ((1 , 2 ), (0 , 2 ), id = "(0, 2)" ),
95
+ pytest .param ((2 , 1 ), (2 , 0 ), id = "(2, 0)" ),
96
+ ],
97
+ )
98
+ def test_broadcast_zero_shape (self , sh1 , sh2 , dt ):
99
+ np_a = numpy .ones (sh1 , dtype = dt )
100
+ dp_a = dpnp .ones (sh1 , dtype = dt )
101
+ func = lambda xp , a : xp .broadcast_to (a , sh2 )
102
+
103
+ assert_allclose (func (numpy , np_a ), func (dpnp , dp_a ))
104
+
105
+ @pytest .mark .parametrize (
106
+ "sh1, sh2" ,
107
+ [
108
+ pytest .param ((0 ,), (), id = "(0,)-()" ),
109
+ pytest .param ((1 ,), (), id = "(1,)-()" ),
110
+ pytest .param ((3 ,), (), id = "(3,)-()" ),
111
+ pytest .param ((3 ,), (1 ,), id = "(3,)-(1,)" ),
112
+ pytest .param ((3 ,), (2 ,), id = "(3,)-(2,)" ),
113
+ pytest .param ((3 ,), (4 ,), id = "(3,)-(4,)" ),
114
+ pytest .param ((1 , 2 ), (2 , 1 ), id = "(1, 2)-(2, 1)" ),
115
+ pytest .param ((1 , 2 ), (1 ,), id = "(1, 2)-(1,)" ),
116
+ pytest .param ((1 ,), - 1 , id = "(1,)--1" ),
117
+ pytest .param ((1 ,), (- 1 ,), id = "(1,)-(-1,)" ),
118
+ pytest .param ((1 , 2 ), (- 1 , 2 ), id = "(1, 2)-(-1, 2)" ),
119
+ ],
120
+ )
121
+ def test_broadcast_raise (self , sh1 , sh2 ):
122
+ np_a = numpy .zeros (sh1 )
123
+ dp_a = dpnp .zeros (sh1 )
124
+ func = lambda xp , a : xp .broadcast_to (a , sh2 )
125
+
126
+ with pytest .raises (ValueError ):
127
+ func (numpy , np_a )
128
+ func (dpnp , dp_a )
28
129
29
130
30
131
@pytest .mark .usefixtures ("allow_fall_back_on_numpy" )
@@ -38,134 +139,134 @@ def test_returns_copy(self):
38
139
def test_large_concatenate_axis_None (self ):
39
140
x = dpnp .arange (1 , 100 )
40
141
r = dpnp .concatenate (x , None )
41
- numpy . testing . assert_array_equal (x , r )
142
+ assert_array_equal (x , r )
42
143
r = dpnp .concatenate (x , 100 )
43
- numpy . testing . assert_array_equal (x , r )
144
+ assert_array_equal (x , r )
44
145
45
146
def test_concatenate (self ):
46
147
# Test concatenate function
47
148
# One sequence returns unmodified (but as array)
48
149
r4 = list (range (4 ))
49
- numpy . testing . assert_array_equal (dpnp .concatenate ((r4 ,)), r4 )
150
+ assert_array_equal (dpnp .concatenate ((r4 ,)), r4 )
50
151
# Any sequence
51
- numpy . testing . assert_array_equal (dpnp .concatenate ((tuple (r4 ),)), r4 )
52
- numpy . testing . assert_array_equal (dpnp .concatenate ((dpnp .array (r4 ),)), r4 )
152
+ assert_array_equal (dpnp .concatenate ((tuple (r4 ),)), r4 )
153
+ assert_array_equal (dpnp .concatenate ((dpnp .array (r4 ),)), r4 )
53
154
# 1D default concatenation
54
155
r3 = list (range (3 ))
55
- numpy . testing . assert_array_equal (dpnp .concatenate ((r4 , r3 )), r4 + r3 )
156
+ assert_array_equal (dpnp .concatenate ((r4 , r3 )), r4 + r3 )
56
157
# Mixed sequence types
57
- numpy . testing . assert_array_equal (dpnp .concatenate ((tuple (r4 ), r3 )), r4 + r3 )
58
- numpy . testing . assert_array_equal (
158
+ assert_array_equal (dpnp .concatenate ((tuple (r4 ), r3 )), r4 + r3 )
159
+ assert_array_equal (
59
160
dpnp .concatenate ((dpnp .array (r4 ), r3 )), r4 + r3
60
161
)
61
162
# Explicit axis specification
62
- numpy . testing . assert_array_equal (dpnp .concatenate ((r4 , r3 ), 0 ), r4 + r3 )
163
+ assert_array_equal (dpnp .concatenate ((r4 , r3 ), 0 ), r4 + r3 )
63
164
# Including negative
64
- numpy . testing . assert_array_equal (dpnp .concatenate ((r4 , r3 ), - 1 ), r4 + r3 )
165
+ assert_array_equal (dpnp .concatenate ((r4 , r3 ), - 1 ), r4 + r3 )
65
166
# 2D
66
167
a23 = dpnp .array ([[10 , 11 , 12 ], [13 , 14 , 15 ]])
67
168
a13 = dpnp .array ([[0 , 1 , 2 ]])
68
169
res = dpnp .array ([[10 , 11 , 12 ], [13 , 14 , 15 ], [0 , 1 , 2 ]])
69
- numpy . testing . assert_array_equal (dpnp .concatenate ((a23 , a13 )), res )
70
- numpy . testing . assert_array_equal (dpnp .concatenate ((a23 , a13 ), 0 ), res )
71
- numpy . testing . assert_array_equal (dpnp .concatenate ((a23 .T , a13 .T ), 1 ), res .T )
72
- numpy . testing . assert_array_equal (dpnp .concatenate ((a23 .T , a13 .T ), - 1 ), res .T )
170
+ assert_array_equal (dpnp .concatenate ((a23 , a13 )), res )
171
+ assert_array_equal (dpnp .concatenate ((a23 , a13 ), 0 ), res )
172
+ assert_array_equal (dpnp .concatenate ((a23 .T , a13 .T ), 1 ), res .T )
173
+ assert_array_equal (dpnp .concatenate ((a23 .T , a13 .T ), - 1 ), res .T )
73
174
# Arrays much match shape
74
- numpy . testing . assert_raises (ValueError , dpnp .concatenate , (a23 .T , a13 .T ), 0 )
175
+ assert_raises (ValueError , dpnp .concatenate , (a23 .T , a13 .T ), 0 )
75
176
# 3D
76
177
res = dpnp .reshape (dpnp .arange (2 * 3 * 7 ), (2 , 3 , 7 ))
77
178
a0 = res [..., :4 ]
78
179
a1 = res [..., 4 :6 ]
79
180
a2 = res [..., 6 :]
80
- numpy . testing . assert_array_equal (dpnp .concatenate ((a0 , a1 , a2 ), 2 ), res )
81
- numpy . testing . assert_array_equal (dpnp .concatenate ((a0 , a1 , a2 ), - 1 ), res )
82
- numpy . testing . assert_array_equal (dpnp .concatenate ((a0 .T , a1 .T , a2 .T ), 0 ), res .T )
181
+ assert_array_equal (dpnp .concatenate ((a0 , a1 , a2 ), 2 ), res )
182
+ assert_array_equal (dpnp .concatenate ((a0 , a1 , a2 ), - 1 ), res )
183
+ assert_array_equal (dpnp .concatenate ((a0 .T , a1 .T , a2 .T ), 0 ), res .T )
83
184
84
185
out = dpnp .copy (res )
85
186
rout = dpnp .concatenate ((a0 , a1 , a2 ), 2 , out = out )
86
- numpy . testing . assert_ (out is rout )
87
- numpy . testing . assert_equal (res , rout )
187
+ assert_ (out is rout )
188
+ assert_equal (res , rout )
88
189
89
190
90
191
class TestHstack :
91
192
def test_non_iterable (self ):
92
- numpy . testing . assert_raises (TypeError , dpnp .hstack , 1 )
193
+ assert_raises (TypeError , dpnp .hstack , 1 )
93
194
94
195
@pytest .mark .usefixtures ("allow_fall_back_on_numpy" )
95
196
def test_empty_input (self ):
96
- numpy . testing . assert_raises (ValueError , dpnp .hstack , ())
197
+ assert_raises (ValueError , dpnp .hstack , ())
97
198
98
199
@pytest .mark .usefixtures ("allow_fall_back_on_numpy" )
99
200
def test_0D_array (self ):
100
201
b = dpnp .array (2 )
101
202
a = dpnp .array (1 )
102
203
res = dpnp .hstack ([a , b ])
103
204
desired = dpnp .array ([1 , 2 ])
104
- numpy . testing . assert_array_equal (res , desired )
205
+ assert_array_equal (res , desired )
105
206
106
207
@pytest .mark .usefixtures ("allow_fall_back_on_numpy" )
107
208
def test_1D_array (self ):
108
209
a = dpnp .array ([1 ])
109
210
b = dpnp .array ([2 ])
110
211
res = dpnp .hstack ([a , b ])
111
212
desired = dpnp .array ([1 , 2 ])
112
- numpy . testing . assert_array_equal (res , desired )
213
+ assert_array_equal (res , desired )
113
214
114
215
@pytest .mark .usefixtures ("allow_fall_back_on_numpy" )
115
216
def test_2D_array (self ):
116
217
a = dpnp .array ([[1 ], [2 ]])
117
218
b = dpnp .array ([[1 ], [2 ]])
118
219
res = dpnp .hstack ([a , b ])
119
220
desired = dpnp .array ([[1 , 1 ], [2 , 2 ]])
120
- numpy . testing . assert_array_equal (res , desired )
221
+ assert_array_equal (res , desired )
121
222
122
223
def test_generator (self ):
123
- with numpy . testing . assert_warns (FutureWarning ):
224
+ with assert_warns (FutureWarning ):
124
225
dpnp .hstack ((numpy .arange (3 ) for _ in range (2 )))
125
- with numpy . testing . assert_warns (FutureWarning ):
226
+ with assert_warns (FutureWarning ):
126
227
dpnp .hstack (map (lambda x : x , numpy .ones ((3 , 2 ))))
127
228
128
229
129
230
class TestVstack :
130
231
def test_non_iterable (self ):
131
- numpy . testing . assert_raises (TypeError , dpnp .vstack , 1 )
232
+ assert_raises (TypeError , dpnp .vstack , 1 )
132
233
133
234
@pytest .mark .usefixtures ("allow_fall_back_on_numpy" )
134
235
def test_empty_input (self ):
135
- numpy . testing . assert_raises (ValueError , dpnp .vstack , ())
236
+ assert_raises (ValueError , dpnp .vstack , ())
136
237
137
238
@pytest .mark .usefixtures ("allow_fall_back_on_numpy" )
138
239
def test_0D_array (self ):
139
240
a = dpnp .array (1 )
140
241
b = dpnp .array (2 )
141
242
res = dpnp .vstack ([a , b ])
142
243
desired = dpnp .array ([[1 ], [2 ]])
143
- numpy . testing . assert_array_equal (res , desired )
244
+ assert_array_equal (res , desired )
144
245
145
246
@pytest .mark .usefixtures ("allow_fall_back_on_numpy" )
146
247
def test_1D_array (self ):
147
248
a = dpnp .array ([1 ])
148
249
b = dpnp .array ([2 ])
149
250
res = dpnp .vstack ([a , b ])
150
251
desired = dpnp .array ([[1 ], [2 ]])
151
- numpy . testing . assert_array_equal (res , desired )
252
+ assert_array_equal (res , desired )
152
253
153
254
@pytest .mark .usefixtures ("allow_fall_back_on_numpy" )
154
255
def test_2D_array (self ):
155
256
a = dpnp .array ([[1 ], [2 ]])
156
257
b = dpnp .array ([[1 ], [2 ]])
157
258
res = dpnp .vstack ([a , b ])
158
259
desired = dpnp .array ([[1 ], [2 ], [1 ], [2 ]])
159
- numpy . testing . assert_array_equal (res , desired )
260
+ assert_array_equal (res , desired )
160
261
161
262
@pytest .mark .usefixtures ("allow_fall_back_on_numpy" )
162
263
def test_2D_array2 (self ):
163
264
a = dpnp .array ([1 , 2 ])
164
265
b = dpnp .array ([1 , 2 ])
165
266
res = dpnp .vstack ([a , b ])
166
267
desired = dpnp .array ([[1 , 2 ], [1 , 2 ]])
167
- numpy . testing . assert_array_equal (res , desired )
268
+ assert_array_equal (res , desired )
168
269
169
270
def test_generator (self ):
170
- with numpy . testing . assert_warns (FutureWarning ):
271
+ with assert_warns (FutureWarning ):
171
272
dpnp .vstack ((numpy .arange (3 ) for _ in range (2 )))
0 commit comments