1
- import numpy as np
1
+ import random
2
+
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
+ # from arrayfire_wrapper.lib.create_and_modify_array.helper_functions import array_to_string
9
10
10
- import random
11
11
12
12
@pytest .mark .parametrize (
13
13
"shape" ,
14
14
[
15
15
(),
16
- (random .randint (1 , 10 ), ),
16
+ (random .randint (1 , 10 ),),
17
17
(random .randint (1 , 10 ), random .randint (1 , 10 )),
18
18
(random .randint (1 , 10 ), random .randint (1 , 10 ), random .randint (1 , 10 )),
19
19
(random .randint (1 , 10 ), random .randint (1 , 10 ), random .randint (1 , 10 ), random .randint (1 , 10 )),
@@ -26,7 +26,8 @@ def test_add_shapes(shape: tuple) -> None:
26
26
27
27
result = wrapper .add (lhs , rhs )
28
28
29
- assert wrapper .get_dims (result )[0 : len (shape )] == shape
29
+ assert wrapper .get_dims (result )[0 : len (shape )] == shape # noqa: E203, W291
30
+
30
31
31
32
def test_add_different_shapes () -> None :
32
33
"""Test if addition handles arrays of different shapes"""
@@ -39,44 +40,47 @@ def test_add_different_shapes() -> None:
39
40
40
41
wrapper .add (lhs , rhs )
41
42
43
+
42
44
dtype_map = {
43
- ' int16' : dtype .s16 ,
44
- ' int32' : dtype .s32 ,
45
- ' int64' : dtype .s64 ,
46
- ' uint8' : dtype .u8 ,
47
- ' uint16' : dtype .u16 ,
48
- ' uint32' : dtype .u32 ,
49
- ' uint64' : dtype .u64 ,
50
- ' float16' : dtype .f16 ,
51
- ' float32' : dtype .f32 ,
45
+ " int16" : dtype .s16 ,
46
+ " int32" : dtype .s32 ,
47
+ " int64" : dtype .s64 ,
48
+ " uint8" : dtype .u8 ,
49
+ " uint16" : dtype .u16 ,
50
+ " uint32" : dtype .u32 ,
51
+ " uint64" : dtype .u64 ,
52
+ " float16" : dtype .f16 ,
53
+ " float32" : dtype .f32 ,
52
54
# 'float64': dtype.f64,
53
55
# 'complex64': dtype.c64,
54
- ' complex32' : dtype .c32 ,
55
- ' bool' : dtype .b8 ,
56
- ' s16' : dtype .s16 ,
57
- ' s32' : dtype .s32 ,
58
- ' s64' : dtype .s64 ,
59
- 'u8' : dtype .u8 ,
60
- ' u16' : dtype .u16 ,
61
- ' u32' : dtype .u32 ,
62
- ' u64' : dtype .u64 ,
63
- ' f16' : dtype .f16 ,
64
- ' f32' : dtype .f32 ,
56
+ " complex32" : dtype .c32 ,
57
+ " bool" : dtype .b8 ,
58
+ " s16" : dtype .s16 ,
59
+ " s32" : dtype .s32 ,
60
+ " s64" : dtype .s64 ,
61
+ "u8" : dtype .u8 ,
62
+ " u16" : dtype .u16 ,
63
+ " u32" : dtype .u32 ,
64
+ " u64" : dtype .u64 ,
65
+ " f16" : dtype .f16 ,
66
+ " f32" : dtype .f32 ,
65
67
# 'f64': dtype.f64,
66
- ' c32' : dtype .c32 ,
68
+ " c32" : dtype .c32 ,
67
69
# 'c64': dtype.c64,
68
- 'b8' : dtype .b8 ,
70
+ "b8" : dtype .b8 ,
69
71
}
70
72
73
+
71
74
@pytest .mark .parametrize ("dtype_name" , dtype_map .values ())
72
- def test_add_supported_dtypes (dtype_name : str ) -> None :
75
+ def test_add_supported_dtypes (dtype_name : dtype . Dtype ) -> None :
73
76
"""Test addition operation across all supported data types."""
74
77
shape = (5 , 5 ) # Using a common shape for simplicity
75
78
lhs = wrapper .randu (shape , dtype_name )
76
79
rhs = wrapper .randu (shape , dtype_name )
77
80
result = wrapper .add (lhs , rhs )
78
81
assert dtype .c_api_value_to_dtype (wrapper .get_type (result )) == dtype_name , f"Failed for dtype: { dtype_name } "
79
82
83
+
80
84
@pytest .mark .parametrize (
81
85
"invdtypes" ,
82
86
[
@@ -93,6 +97,7 @@ def test_add_unsupported_dtypes(invdtypes: dtype.Dtype) -> None:
93
97
result = wrapper .add (lhs , rhs )
94
98
assert dtype .c_api_value_to_dtype (wrapper .get_type (result )) == invdtypes , f"Didn't Fail for Dtype: { invdtypes } "
95
99
100
+
96
101
def test_add_zero_sized_arrays () -> None :
97
102
"""Test addition with arrays where at least one array has zero size."""
98
103
with pytest .raises (RuntimeError ):
@@ -105,35 +110,12 @@ def test_add_zero_sized_arrays() -> None:
105
110
result_lhs_zero = wrapper .add (zero_array , normal_array )
106
111
assert wrapper .get_dims (result_lhs_zero ) == zero_shape
107
112
108
- @pytest .mark .parametrize ("values" , [
109
- (- 5 , 10 ), # Both negative and positive
110
- (10 , - 5 ),
111
- (- 10 , - 5 ), # Both negative
112
- (0 , - 10 ), # Zero and negative
113
- (- 5 , 0 ), # Negative and zero
114
- (0 , 0 ) # Both zero
115
- ])
116
- def test_add_negative_numbers (values : tuple [int , int ]) -> None :
117
- """Test addition with negative numbers."""
118
- shape = (5 , 5 )
119
- lhsV , rhsV = values
120
- # Create one array with negative values
121
- lhs = wrapper .constant (lhsV , shape , dtype .f32 )
122
- rhs = wrapper .constant (rhsV , shape , dtype .f32 )
123
-
124
- result = wrapper .add (lhs , rhs )
125
-
126
- lhs_np = np .full (shape , lhsV )
127
- rhs_np = np .full (shape , rhsV )
128
- np_result = lhs_np + rhs_np
129
- assert wrapper .get_dims (result )[0 : len (shape )] == np .shape (np_result ), f"Test failed for lhs_val={ lhsV } and rhs_val={ rhsV } "
130
- #assert np.allclose(nparr, np_result)
131
113
132
114
@pytest .mark .parametrize (
133
115
"shape" ,
134
116
[
135
117
(),
136
- (random .randint (1 , 10 ), ),
118
+ (random .randint (1 , 10 ),),
137
119
(random .randint (1 , 10 ), random .randint (1 , 10 )),
138
120
(random .randint (1 , 10 ), random .randint (1 , 10 ), random .randint (1 , 10 )),
139
121
(random .randint (1 , 10 ), random .randint (1 , 10 ), random .randint (1 , 10 ), random .randint (1 , 10 )),
@@ -146,7 +128,8 @@ def test_subtract_shapes(shape: tuple) -> None:
146
128
147
129
result = wrapper .sub (lhs , rhs )
148
130
149
- assert wrapper .get_dims (result )[0 : len (shape )] == shape
131
+ assert wrapper .get_dims (result )[0 : len (shape )] == shape # noqa: E203, W291
132
+
150
133
151
134
def test_subtract_different_shapes () -> None :
152
135
"""Test if subtraction handles arrays of different shapes"""
@@ -159,15 +142,17 @@ def test_subtract_different_shapes() -> None:
159
142
160
143
wrapper .sub (lhs , rhs )
161
144
145
+
162
146
@pytest .mark .parametrize ("dtype_name" , dtype_map .values ())
163
- def test_subtract_supported_dtypes (dtype_name : str ) -> None :
147
+ def test_subtract_supported_dtypes (dtype_name : dtype . Dtype ) -> None :
164
148
"""Test subtraction operation across all supported data types."""
165
149
shape = (5 , 5 )
166
150
lhs = wrapper .randu (shape , dtype_name )
167
151
rhs = wrapper .randu (shape , dtype_name )
168
152
result = wrapper .sub (lhs , rhs )
169
153
assert dtype .c_api_value_to_dtype (wrapper .get_type (result )) == dtype_name , f"Failed for dtype: { dtype_name } "
170
154
155
+
171
156
@pytest .mark .parametrize (
172
157
"invdtypes" ,
173
158
[
@@ -182,6 +167,8 @@ def test_subtract_unsupported_dtypes(invdtypes: dtype.Dtype) -> None:
182
167
lhs = wrapper .randu (shape , invdtypes )
183
168
rhs = wrapper .randu (shape , invdtypes )
184
169
result = wrapper .sub (lhs , rhs )
170
+ assert result == invdtypes , f"Didn't Fail for Dtype: { invdtypes } "
171
+
185
172
186
173
def test_subtract_zero_sized_arrays () -> None :
187
174
"""Test subtraction with arrays where at least one array has zero size."""
@@ -193,25 +180,3 @@ def test_subtract_zero_sized_arrays() -> None:
193
180
194
181
result_lhs_zero = wrapper .sub (zero_array , normal_array )
195
182
assert wrapper .get_dims (result_lhs_zero ) == zero_shape
196
-
197
- @pytest .mark .parametrize ("values" , [
198
- (- 5 , 10 ), # Both negative and positive
199
- (10 , - 5 ),
200
- (- 10 , - 5 ), # Both negative
201
- (0 , - 10 ), # Zero and negative
202
- (- 5 , 0 ), # Negative and zero
203
- (0 , 0 ) # Both zero
204
- ])
205
- def test_subtract_negative_numbers (values : tuple [int , int ]) -> None :
206
- """Test subtraction with negative numbers and zero."""
207
- shape = (5 , 5 )
208
- lhsV , rhsV = values
209
- lhs = wrapper .constant (lhsV , shape , dtype .f32 )
210
- rhs = wrapper .constant (rhsV , shape , dtype .f32 )
211
-
212
- result = wrapper .sub (lhs , rhs )
213
-
214
- lhs_np = np .full (shape , lhsV )
215
- rhs_np = np .full (shape , rhsV )
216
- np_result = lhs_np - rhs_np
217
- assert wrapper .get_dims (result )[0 : len (shape )] == np .shape (np_result ), f"Test failed for lhs_val={ lhsV } and rhs_val={ rhsV } "
0 commit comments