1- import numpy as np
1+ import random
2+
3+ # import numpy as np
24import pytest
35
46import arrayfire_wrapper .dtypes as dtype
57import 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
88
9+ # from arrayfire_wrapper.lib.create_and_modify_array.helper_functions import array_to_string
910
10- import random
1111
1212@pytest .mark .parametrize (
1313 "shape" ,
1414 [
1515 (),
16- (random .randint (1 , 10 ), ),
16+ (random .randint (1 , 10 ),),
1717 (random .randint (1 , 10 ), random .randint (1 , 10 )),
1818 (random .randint (1 , 10 ), random .randint (1 , 10 ), random .randint (1 , 10 )),
1919 (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:
2626
2727 result = wrapper .add (lhs , rhs )
2828
29- assert wrapper .get_dims (result )[0 : len (shape )] == shape
29+ assert wrapper .get_dims (result )[0 : len (shape )] == shape # noqa: E203, W291
30+
3031
3132def test_add_different_shapes () -> None :
3233 """Test if addition handles arrays of different shapes"""
@@ -39,44 +40,47 @@ def test_add_different_shapes() -> None:
3940
4041 wrapper .add (lhs , rhs )
4142
43+
4244dtype_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 ,
5254 # 'float64': dtype.f64,
5355 # '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 ,
6567 # 'f64': dtype.f64,
66- ' c32' : dtype .c32 ,
68+ " c32" : dtype .c32 ,
6769 # 'c64': dtype.c64,
68- 'b8' : dtype .b8 ,
70+ "b8" : dtype .b8 ,
6971}
7072
73+
7174@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 :
7376 """Test addition operation across all supported data types."""
7477 shape = (5 , 5 ) # Using a common shape for simplicity
7578 lhs = wrapper .randu (shape , dtype_name )
7679 rhs = wrapper .randu (shape , dtype_name )
7780 result = wrapper .add (lhs , rhs )
7881 assert dtype .c_api_value_to_dtype (wrapper .get_type (result )) == dtype_name , f"Failed for dtype: { dtype_name } "
7982
83+
8084@pytest .mark .parametrize (
8185 "invdtypes" ,
8286 [
@@ -93,6 +97,7 @@ def test_add_unsupported_dtypes(invdtypes: dtype.Dtype) -> None:
9397 result = wrapper .add (lhs , rhs )
9498 assert dtype .c_api_value_to_dtype (wrapper .get_type (result )) == invdtypes , f"Didn't Fail for Dtype: { invdtypes } "
9599
100+
96101def test_add_zero_sized_arrays () -> None :
97102 """Test addition with arrays where at least one array has zero size."""
98103 with pytest .raises (RuntimeError ):
@@ -105,35 +110,12 @@ def test_add_zero_sized_arrays() -> None:
105110 result_lhs_zero = wrapper .add (zero_array , normal_array )
106111 assert wrapper .get_dims (result_lhs_zero ) == zero_shape
107112
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)
131113
132114@pytest .mark .parametrize (
133115 "shape" ,
134116 [
135117 (),
136- (random .randint (1 , 10 ), ),
118+ (random .randint (1 , 10 ),),
137119 (random .randint (1 , 10 ), random .randint (1 , 10 )),
138120 (random .randint (1 , 10 ), random .randint (1 , 10 ), random .randint (1 , 10 )),
139121 (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:
146128
147129 result = wrapper .sub (lhs , rhs )
148130
149- assert wrapper .get_dims (result )[0 : len (shape )] == shape
131+ assert wrapper .get_dims (result )[0 : len (shape )] == shape # noqa: E203, W291
132+
150133
151134def test_subtract_different_shapes () -> None :
152135 """Test if subtraction handles arrays of different shapes"""
@@ -159,15 +142,17 @@ def test_subtract_different_shapes() -> None:
159142
160143 wrapper .sub (lhs , rhs )
161144
145+
162146@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 :
164148 """Test subtraction operation across all supported data types."""
165149 shape = (5 , 5 )
166150 lhs = wrapper .randu (shape , dtype_name )
167151 rhs = wrapper .randu (shape , dtype_name )
168152 result = wrapper .sub (lhs , rhs )
169153 assert dtype .c_api_value_to_dtype (wrapper .get_type (result )) == dtype_name , f"Failed for dtype: { dtype_name } "
170154
155+
171156@pytest .mark .parametrize (
172157 "invdtypes" ,
173158 [
@@ -182,6 +167,8 @@ def test_subtract_unsupported_dtypes(invdtypes: dtype.Dtype) -> None:
182167 lhs = wrapper .randu (shape , invdtypes )
183168 rhs = wrapper .randu (shape , invdtypes )
184169 result = wrapper .sub (lhs , rhs )
170+ assert result == invdtypes , f"Didn't Fail for Dtype: { invdtypes } "
171+
185172
186173def test_subtract_zero_sized_arrays () -> None :
187174 """Test subtraction with arrays where at least one array has zero size."""
@@ -193,25 +180,3 @@ def test_subtract_zero_sized_arrays() -> None:
193180
194181 result_lhs_zero = wrapper .sub (zero_array , normal_array )
195182 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