1
+ import random
2
+
1
3
import numpy as np
2
4
import pytest
3
5
4
6
import arrayfire_wrapper .dtypes as dtype
5
7
import arrayfire_wrapper .lib as wrapper
6
- import arrayfire_wrapper .lib .mathematical_functions as ops
7
- from arrayfire_wrapper .lib .create_and_modify_array .helper_functions import array_to_string
8
8
9
-
10
- import random
9
+ # import arrayfire_wrapper.lib.mathematical_functions as ops
10
+ from arrayfire_wrapper . lib . create_and_modify_array . helper_functions import array_to_string
11
11
12
12
dtype_map = {
13
- ' int16' : dtype .s16 ,
14
- ' int32' : dtype .s32 ,
15
- ' int64' : dtype .s64 ,
16
- ' uint8' : dtype .u8 ,
17
- ' uint16' : dtype .u16 ,
18
- ' uint32' : dtype .u32 ,
19
- ' uint64' : dtype .u64 ,
20
- ' float16' : dtype .f16 ,
21
- ' float32' : dtype .f32 ,
13
+ " int16" : dtype .s16 ,
14
+ " int32" : dtype .s32 ,
15
+ " int64" : dtype .s64 ,
16
+ " uint8" : dtype .u8 ,
17
+ " uint16" : dtype .u16 ,
18
+ " uint32" : dtype .u32 ,
19
+ " uint64" : dtype .u64 ,
20
+ " float16" : dtype .f16 ,
21
+ " float32" : dtype .f32 ,
22
22
# 'float64': dtype.f64,
23
23
# 'complex64': dtype.c64,
24
- ' complex32' : dtype .c32 ,
25
- ' bool' : dtype .b8 ,
26
- ' s16' : dtype .s16 ,
27
- ' s32' : dtype .s32 ,
28
- ' s64' : dtype .s64 ,
29
- 'u8' : dtype .u8 ,
30
- ' u16' : dtype .u16 ,
31
- ' u32' : dtype .u32 ,
32
- ' u64' : dtype .u64 ,
33
- ' f16' : dtype .f16 ,
34
- ' f32' : dtype .f32 ,
24
+ " complex32" : dtype .c32 ,
25
+ " bool" : dtype .b8 ,
26
+ " s16" : dtype .s16 ,
27
+ " s32" : dtype .s32 ,
28
+ " s64" : dtype .s64 ,
29
+ "u8" : dtype .u8 ,
30
+ " u16" : dtype .u16 ,
31
+ " u32" : dtype .u32 ,
32
+ " u64" : dtype .u64 ,
33
+ " f16" : dtype .f16 ,
34
+ " f32" : dtype .f32 ,
35
35
# 'f64': dtype.f64,
36
- ' c32' : dtype .c32 ,
36
+ " c32" : dtype .c32 ,
37
37
# 'c64': dtype.c64,
38
- 'b8' : dtype .b8 ,
38
+ "b8" : dtype .b8 ,
39
39
}
40
40
41
+
41
42
@pytest .mark .parametrize (
42
43
"shape" ,
43
44
[
44
45
(),
45
- (random .randint (1 , 10 ), ),
46
+ (random .randint (1 , 10 ),),
46
47
(random .randint (1 , 10 ), random .randint (1 , 10 )),
47
48
(random .randint (1 , 10 ), random .randint (1 , 10 ), random .randint (1 , 10 )),
48
49
(random .randint (1 , 10 ), random .randint (1 , 10 ), random .randint (1 , 10 ), random .randint (1 , 10 )),
@@ -55,7 +56,8 @@ def test_multiply_shapes(shape: tuple) -> None:
55
56
56
57
result = wrapper .mul (lhs , rhs )
57
58
58
- assert wrapper .get_dims (result )[0 : len (shape )] == shape
59
+ assert wrapper .get_dims (result )[0 : len (shape )] == shape # noqa
60
+
59
61
60
62
def test_multiply_different_shapes () -> None :
61
63
"""Test if multiplication handles arrays of different shapes"""
@@ -66,8 +68,10 @@ def test_multiply_different_shapes() -> None:
66
68
lhs = wrapper .randu (lhs_shape , dtypes )
67
69
rhs = wrapper .randu (rhs_shape , dtypes )
68
70
result = wrapper .mul (lhs , rhs )
69
- expected_shape = np .broadcast (np .empty (lhs ), np .empty (rhs )).shape
70
- assert wrapper .get_dims (result )[0 : len (expected_shape )] == expected_shape , f"Failed for shapes { lhs_shape } and { rhs_shape } "
71
+ assert (
72
+ wrapper .get_dims (result )[0 : len (lhs_shape )] == lhs_shape # noqa
73
+ ), f"Failed for shapes { lhs_shape } and { rhs_shape } "
74
+
71
75
72
76
def test_multiply_negative_shapes () -> None :
73
77
"""Test if multiplication handles arrays of negative shapes"""
@@ -78,18 +82,21 @@ def test_multiply_negative_shapes() -> None:
78
82
lhs = wrapper .randu (lhs_shape , dtypes )
79
83
rhs = wrapper .randu (rhs_shape , dtypes )
80
84
result = wrapper .mul (lhs , rhs )
81
- expected_shape = np .broadcast (np .empty (lhs ), np .empty (rhs )).shape
82
- assert wrapper .get_dims (result )[0 : len (expected_shape )] == expected_shape , f"Failed for shapes { lhs_shape } and { rhs_shape } "
85
+ assert (
86
+ wrapper .get_dims (result )[0 : len (lhs_shape )] == lhs_shape # noqa
87
+ ), f"Failed for shapes { lhs_shape } and { rhs_shape } "
88
+
83
89
84
90
@pytest .mark .parametrize ("dtype_name" , dtype_map .values ())
85
- def test_multiply_supported_dtypes (dtype_name : str ) -> None :
91
+ def test_multiply_supported_dtypes (dtype_name : dtype . Dtype ) -> None :
86
92
"""Test multiplication operation across all supported data types."""
87
93
shape = (5 , 5 )
88
94
lhs = wrapper .randu (shape , dtype_name )
89
95
rhs = wrapper .randu (shape , dtype_name )
90
96
result = wrapper .mul (lhs , rhs )
91
97
assert dtype .c_api_value_to_dtype (wrapper .get_type (result )) == dtype_name , f"Failed for dtype: { dtype_name } "
92
98
99
+
93
100
@pytest .mark .parametrize (
94
101
"invdtypes" ,
95
102
[
@@ -105,6 +112,7 @@ def test_multiply_unsupported_dtypes(invdtypes: dtype.Dtype) -> None:
105
112
rhs = wrapper .randu (shape , invdtypes )
106
113
wrapper .mul (lhs , rhs )
107
114
115
+
108
116
def test_multiply_zero_sized_arrays () -> None :
109
117
"""Test multiplication with arrays where at least one array has zero size."""
110
118
with pytest .raises (RuntimeError ):
@@ -115,10 +123,11 @@ def test_multiply_zero_sized_arrays() -> None:
115
123
116
124
result_rhs_zero = wrapper .mul (normal_array , zero_array )
117
125
assert wrapper .get_dims (result_rhs_zero ) == normal_shape
118
-
126
+
119
127
result_lhs_zero = wrapper .mul (zero_array , normal_array )
120
128
assert wrapper .get_dims (result_lhs_zero ) == zero_shape
121
129
130
+
122
131
@pytest .mark .parametrize (
123
132
"shape_a, shape_b" ,
124
133
[
@@ -136,13 +145,16 @@ def test_multiply_varying_dimensionality(shape_a: tuple, shape_b: tuple) -> None
136
145
137
146
result = wrapper .mul (lhs , rhs )
138
147
expected_shape = np .broadcast (np .empty (shape_a ), np .empty (shape_b )).shape
139
- assert wrapper .get_dims (result )[0 : len (expected_shape )] == expected_shape , f"Failed for shapes { shape_a } and { shape_b } "
148
+ assert (
149
+ wrapper .get_dims (result )[0 : len (expected_shape )] == expected_shape # noqa
150
+ ), f"Failed for shapes { shape_a } and { shape_b } "
151
+
140
152
141
153
@pytest .mark .parametrize (
142
154
"shape" ,
143
155
[
144
156
(),
145
- (random .randint (1 , 10 ), ),
157
+ (random .randint (1 , 10 ),),
146
158
(random .randint (1 , 10 ), random .randint (1 , 10 )),
147
159
(random .randint (1 , 10 ), random .randint (1 , 10 ), random .randint (1 , 10 )),
148
160
(random .randint (1 , 10 ), random .randint (1 , 10 ), random .randint (1 , 10 ), random .randint (1 , 10 )),
@@ -157,7 +169,8 @@ def test_divide_shapes(shape: tuple) -> None:
157
169
158
170
result = wrapper .div (lhs , rhs )
159
171
160
- assert wrapper .get_dims (result )[0 : len (shape )] == shape
172
+ assert wrapper .get_dims (result )[0 : len (shape )] == shape # noqa
173
+
161
174
162
175
def test_divide_different_shapes () -> None :
163
176
"""Test if division handles arrays of different shapes"""
@@ -169,7 +182,10 @@ def test_divide_different_shapes() -> None:
169
182
rhs = wrapper .randu (rhs_shape , dtypes )
170
183
result = wrapper .div (lhs , rhs )
171
184
expected_shape = np .broadcast (np .empty (lhs_shape ), np .empty (rhs_shape )).shape
172
- assert wrapper .get_dims (result )[0 : len (expected_shape )] == expected_shape , f"Failed for shapes { lhs_shape } and { rhs_shape } "
185
+ assert (
186
+ wrapper .get_dims (result )[0 : len (expected_shape )] == expected_shape # noqa
187
+ ), f"Failed for shapes { lhs_shape } and { rhs_shape } "
188
+
173
189
174
190
def test_divide_negative_shapes () -> None :
175
191
"""Test if division handles arrays of negative shapes"""
@@ -181,10 +197,13 @@ def test_divide_negative_shapes() -> None:
181
197
rhs = wrapper .randu (rhs_shape , dtypes )
182
198
result = wrapper .div (lhs , rhs )
183
199
expected_shape = np .broadcast (np .empty (lhs_shape ), np .empty (rhs_shape )).shape
184
- assert wrapper .get_dims (result )[0 : len (expected_shape )] == expected_shape , f"Failed for shapes { lhs_shape } and { rhs_shape } "
200
+ assert (
201
+ wrapper .get_dims (result )[0 : len (expected_shape )] == expected_shape # noqa
202
+ ), f"Failed for shapes { lhs_shape } and { rhs_shape } "
203
+
185
204
186
205
@pytest .mark .parametrize ("dtype_name" , dtype_map .values ())
187
- def test_divide_supported_dtypes (dtype_name : str ) -> None :
206
+ def test_divide_supported_dtypes (dtype_name : dtype . Dtype ) -> None :
188
207
"""Test division operation across all supported data types."""
189
208
shape = (5 , 5 )
190
209
lhs = wrapper .randu (shape , dtype_name )
@@ -195,6 +214,7 @@ def test_divide_supported_dtypes(dtype_name: str) -> None:
195
214
result = wrapper .div (lhs , rhs )
196
215
assert dtype .c_api_value_to_dtype (wrapper .get_type (result )) == dtype_name , f"Failed for dtype: { dtype_name } "
197
216
217
+
198
218
def test_divide_by0 () -> None :
199
219
"""Test division operation for undefined error type."""
200
220
shape = (2 , 2 )
@@ -208,10 +228,11 @@ def test_divide_by0() -> None:
208
228
divOut = wrapper .div (lhs , rhs )
209
229
print (array_to_string ("" , divOut , 3 , False ))
210
230
wrapper .div (lhs , rhs )
211
-
231
+
212
232
# result = wrapper.div(lhs, rhs)
213
233
# assert dtype.c_api_value_to_dtype(wrapper.get_type(result)) == dtype_name, f"Failed for dtype: {dtype_name}"
214
234
235
+
215
236
@pytest .mark .parametrize (
216
237
"invdtypes" ,
217
238
[
@@ -230,6 +251,7 @@ def test_divide_unsupported_dtypes(invdtypes: dtype.Dtype) -> None:
230
251
231
252
wrapper .div (lhs , rhs )
232
253
254
+
233
255
def test_divide_zero_sized_arrays () -> None :
234
256
"""Test division with arrays where at least one array has zero size."""
235
257
with pytest .raises (RuntimeError ):
@@ -240,10 +262,11 @@ def test_divide_zero_sized_arrays() -> None:
240
262
241
263
result_rhs_zero = wrapper .div (normal_array , zero_array )
242
264
assert wrapper .get_dims (result_rhs_zero ) == normal_shape
243
-
265
+
244
266
result_lhs_zero = wrapper .div (zero_array , normal_array )
245
267
assert wrapper .get_dims (result_lhs_zero ) == zero_shape
246
268
269
+
247
270
@pytest .mark .parametrize (
248
271
"shape_a, shape_b" ,
249
272
[
@@ -263,4 +286,6 @@ def test_divide_varying_dimensionality(shape_a: tuple, shape_b: tuple) -> None:
263
286
264
287
result = wrapper .div (lhs , rhs )
265
288
expected_shape = np .broadcast (np .empty (shape_a ), np .empty (shape_b )).shape
266
- assert wrapper .get_dims (result )[0 : len (expected_shape )] == expected_shape , f"Failed for shapes { shape_a } and { shape_b } "
289
+ assert (
290
+ wrapper .get_dims (result )[0 : len (expected_shape )] == expected_shape # noqa
291
+ ), f"Failed for shapes { shape_a } and { shape_b } "
0 commit comments