From f32651bc5ec70215f6c8012fe8ba25d6588ea8a6 Mon Sep 17 00:00:00 2001 From: AzeezIsh Date: Mon, 18 Mar 2024 17:14:31 -0400 Subject: [PATCH 1/7] Tested all the bitshift operations in Python. Covered cases like 0 shifts, negative shifts, and shifts greater than number of bits. ```bash --- tests/test_bitshift.py | 201 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 tests/test_bitshift.py diff --git a/tests/test_bitshift.py b/tests/test_bitshift.py new file mode 100644 index 0000000..dc95423 --- /dev/null +++ b/tests/test_bitshift.py @@ -0,0 +1,201 @@ +import pytest + +import arrayfire_wrapper.dtypes as dtype +import arrayfire_wrapper.lib as wrapper +from arrayfire_wrapper.lib.create_and_modify_array.helper_functions import array_to_string + +dtype_map = { + 'int16': dtype.s16, + 'int32': dtype.s32, + 'int64': dtype.s64, + 'uint8': dtype.u8, + 'uint16': dtype.u16, + 'uint32': dtype.u32, + 'uint64': dtype.u64, + # 'float16': dtype.f16, + # 'float32': dtype.f32, + # 'float64': dtype.f64, + # 'complex64': dtype.c64, + # 'complex32': dtype.c32, + 'bool': dtype.b8, + 's16': dtype.s16, + 's32': dtype.s32, + 's64': dtype.s64, + 'u8': dtype.u8, + 'u16': dtype.u16, + 'u32': dtype.u32, + 'u64': dtype.u64, + # 'f16': dtype.f16, + # 'f32': dtype.f32, + # 'f64': dtype.f64, + # 'c32': dtype.c32, + # 'c64': dtype.c64, + 'b8': dtype.b8, +} +@pytest.mark.parametrize("dtype_name", dtype_map.values()) +def test_bitshiftl_dtypes(dtype_name: dtype.Dtype) -> None: + """Test bit shift operation across all supported data types.""" + shape = (5, 5) + values = wrapper.randu(shape, dtype_name) + bits_to_shift = wrapper.constant(1, shape, dtype_name) + + result = wrapper.bitshiftl(values, bits_to_shift) + + assert dtype.c_api_value_to_dtype(wrapper.get_type(result)) == dtype_name, f"Failed for dtype: {dtype_name}" +@pytest.mark.parametrize( + "invdtypes", + [ + dtype.c64, + dtype.f64, + ], +) +def test_bitshiftl_supported_dtypes(invdtypes: dtype.Dtype) -> None: + """Test bitshift operations for unsupported integer data types.""" + shape = (5, 5) + with pytest.raises(RuntimeError): + value = wrapper.randu(shape, invdtypes) + shift_amount = 1 + + result = wrapper.bitshiftl(value, shift_amount) + assert dtype.c_api_value_to_dtype(wrapper.get_type(result)) == invdtypes, f"Failed for dtype: {invdtypes}" +@pytest.mark.parametrize("input_size", [8, 10, 12]) +def test_bitshiftl_varying_input_size(input_size): + """Test bitshift left operation with varying input sizes""" + shape = (input_size, input_size) + value = wrapper.randu(shape, dtype.int16) + shift_amount = wrapper.constant(1, shape, dtype.int16) # Fixed shift amount for simplicity + + result = wrapper.bitshiftl(value, shift_amount) + + assert wrapper.get_dims(result)[0 : len(shape)] == shape + +@pytest.mark.parametrize( + "shape", + [ + (10, ), + (5, 5), + (2, 3, 4), + ], +) +def test_bitshiftl_varying_shapes(shape: tuple) -> None: + """Test left bit shifting with arrays of varying shapes.""" + values = wrapper.randu(shape, dtype.int16) + bits_to_shift = wrapper.constant(1, shape, dtype.int16) + + result = wrapper.bitshiftl(values, bits_to_shift) + + assert wrapper.get_dims(result)[0 : len(shape)] == shape + +@pytest.mark.parametrize("shift_amount", [0, 2, 30]) +def test_bitshift_left_varying_shift_amount(shift_amount): + """Test bitshift left operation with varying shift amounts.""" + shape = (5, 5) + value = wrapper.randu(shape, dtype.int16) + shift_amount_arr = wrapper.constant(shift_amount, shape, dtype.int16) + + result = wrapper.bitshiftl(value, shift_amount_arr) + + assert wrapper.get_dims(result)[0 : len(shape)] == shape + +@pytest.mark.parametrize( + "shape_a, shape_b", + [ + ((1, 5), (5, 1)), # 2D with 2D inverse + ((5, 5), (5, 1)), # 2D with 2D + ((5, 5), (1, 1)), # 2D with 2D + ((1, 1, 1), (5, 5, 5)), # 3D with 3D + ], +) +def test_bitshiftl_different_shapes(shape_a: tuple, shape_b: tuple) -> None: + """Test if left bit shifting handles arrays of different shapes""" + with pytest.raises(RuntimeError): + values = wrapper.randu(shape_a, dtype.int16) + bits_to_shift = wrapper.constant(1, shape_b, dtype.int16) + result = wrapper.bitshiftl(values, bits_to_shift) + print(array_to_string("", result, 3, False)) + assert wrapper.get_dims(result)[0 : len(shape_a)] == shape_a, f"Failed for shapes {shape_a} and {shape_b}" + +@pytest.mark.parametrize("shift_amount", [0, 2, 30]) +def test_bitshift_right_varying_shift_amount(shift_amount): + """Test bitshift right operation with varying shift amounts.""" + shape = (5, 5) + value = wrapper.randu(shape, dtype.int16) + shift_amount_arr = wrapper.constant(shift_amount, shape, dtype.int16) + + result = wrapper.bitshiftr(value, shift_amount_arr) + + assert wrapper.get_dims(result)[0 : len(shape)] == shape + +@pytest.mark.parametrize("dtype_name", dtype_map.values()) +def test_bitshiftr_dtypes(dtype_name: dtype.Dtype) -> None: + """Test bit shift operation across all supported data types.""" + shape = (5, 5) + values = wrapper.randu(shape, dtype_name) + bits_to_shift = wrapper.constant(1, shape, dtype_name) + + result = wrapper.bitshiftr(values, bits_to_shift) + + assert dtype.c_api_value_to_dtype(wrapper.get_type(result)) == dtype_name, f"Failed for dtype: {dtype_name}" +@pytest.mark.parametrize( + "invdtypes", + [ + dtype.c64, + dtype.f64, + ], +) +def test_bitshiftr_supported_dtypes(invdtypes: dtype.Dtype) -> None: + """Test bitshift operations for unsupported integer data types.""" + shape = (5, 5) + with pytest.raises(RuntimeError): + value = wrapper.randu(shape, invdtypes) + shift_amount = 1 + + result = wrapper.bitshiftr(value, shift_amount) + assert dtype.c_api_value_to_dtype(wrapper.get_type(result)) == invdtypes, f"Failed for dtype: {invdtypes}" + +@pytest.mark.parametrize("input_size", [8, 10, 12]) +def test_bitshift_right_varying_input_size(input_size): + """Test bitshift right operation with varying input sizes""" + shape = (input_size, input_size) + value = wrapper.randu(shape, dtype.int16) + shift_amount = wrapper.constant(1, shape, dtype.int16) # Fixed shift amount for simplicity + + result = wrapper.bitshiftr(value, shift_amount) + + assert wrapper.get_dims(result)[0 : len(shape)] == shape +@pytest.mark.parametrize( + "shape", + [ + (10, ), + (5, 5), + (2, 3, 4), + ], +) +def test_bitshiftr_varying_shapes(shape: tuple) -> None: + """Test right bit shifting with arrays of varying shapes.""" + values = wrapper.randu(shape, dtype.int16) + bits_to_shift = wrapper.constant(1, shape, dtype.int16) + + result = wrapper.bitshiftr(values, bits_to_shift) + + assert wrapper.get_dims(result)[0 : len(shape)] == shape + + + +@pytest.mark.parametrize( + "shape_a, shape_b", + [ + ((1, 5), (5, 1)), # 2D with 2D inverse + ((5, 5), (5, 1)), # 2D with 2D + ((5, 5), (1, 1)), # 2D with 2D + ((1, 1, 1), (5, 5, 5)), # 3D with 3D + ], +) +def test_bitshiftr_different_shapes(shape_a: tuple, shape_b: tuple) -> None: + """Test if right bit shifting handles arrays of different shapes""" + with pytest.raises(RuntimeError): + values = wrapper.randu(shape_a, dtype.int16) + bits_to_shift = wrapper.constant(1, shape_b, dtype.int16) + result = wrapper.bitshiftr(values, bits_to_shift) + print(array_to_string("", result, 3, False)) + assert wrapper.get_dims(result)[0 : len(shape_a)] == shape_a, f"Failed for shapes {shape_a} and {shape_b}" \ No newline at end of file From 83f2d15359c6cf3bb417710ca4dab92e9de30b56 Mon Sep 17 00:00:00 2001 From: AzeezIsh Date: Mon, 18 Mar 2024 17:28:18 -0400 Subject: [PATCH 2/7] Ensured negatives were tested as well. --- tests/test_bitshift.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_bitshift.py b/tests/test_bitshift.py index dc95423..6d63402 100644 --- a/tests/test_bitshift.py +++ b/tests/test_bitshift.py @@ -86,7 +86,7 @@ def test_bitshiftl_varying_shapes(shape: tuple) -> None: assert wrapper.get_dims(result)[0 : len(shape)] == shape -@pytest.mark.parametrize("shift_amount", [0, 2, 30]) +@pytest.mark.parametrize("shift_amount", [-1, 0, 2, 30]) def test_bitshift_left_varying_shift_amount(shift_amount): """Test bitshift left operation with varying shift amounts.""" shape = (5, 5) @@ -115,7 +115,7 @@ def test_bitshiftl_different_shapes(shape_a: tuple, shape_b: tuple) -> None: print(array_to_string("", result, 3, False)) assert wrapper.get_dims(result)[0 : len(shape_a)] == shape_a, f"Failed for shapes {shape_a} and {shape_b}" -@pytest.mark.parametrize("shift_amount", [0, 2, 30]) +@pytest.mark.parametrize("shift_amount", [-1, 0, 2, 30]) def test_bitshift_right_varying_shift_amount(shift_amount): """Test bitshift right operation with varying shift amounts.""" shape = (5, 5) From 5e1600f51787e97b391d72d63a651e884ba19d02 Mon Sep 17 00:00:00 2001 From: AzeezIsh Date: Mon, 18 Mar 2024 18:06:54 -0400 Subject: [PATCH 3/7] Applied all checkstyle requirements. --- tests/test_bitshift.py | 95 +++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 38 deletions(-) diff --git a/tests/test_bitshift.py b/tests/test_bitshift.py index 6d63402..705c81d 100644 --- a/tests/test_bitshift.py +++ b/tests/test_bitshift.py @@ -5,43 +5,47 @@ from arrayfire_wrapper.lib.create_and_modify_array.helper_functions import array_to_string dtype_map = { - 'int16': dtype.s16, - 'int32': dtype.s32, - 'int64': dtype.s64, - 'uint8': dtype.u8, - 'uint16': dtype.u16, - 'uint32': dtype.u32, - 'uint64': dtype.u64, + "int16": dtype.s16, + "int32": dtype.s32, + "int64": dtype.s64, + "uint8": dtype.u8, + "uint16": dtype.u16, + "uint32": dtype.u32, + "uint64": dtype.u64, # 'float16': dtype.f16, # 'float32': dtype.f32, # 'float64': dtype.f64, # 'complex64': dtype.c64, # 'complex32': dtype.c32, - 'bool': dtype.b8, - 's16': dtype.s16, - 's32': dtype.s32, - 's64': dtype.s64, - 'u8': dtype.u8, - 'u16': dtype.u16, - 'u32': dtype.u32, - 'u64': dtype.u64, + "bool": dtype.b8, + "s16": dtype.s16, + "s32": dtype.s32, + "s64": dtype.s64, + "u8": dtype.u8, + "u16": dtype.u16, + "u32": dtype.u32, + "u64": dtype.u64, # 'f16': dtype.f16, # 'f32': dtype.f32, # 'f64': dtype.f64, # 'c32': dtype.c32, # 'c64': dtype.c64, - 'b8': dtype.b8, + "b8": dtype.b8, } + + @pytest.mark.parametrize("dtype_name", dtype_map.values()) def test_bitshiftl_dtypes(dtype_name: dtype.Dtype) -> None: """Test bit shift operation across all supported data types.""" shape = (5, 5) values = wrapper.randu(shape, dtype_name) bits_to_shift = wrapper.constant(1, shape, dtype_name) - + result = wrapper.bitshiftl(values, bits_to_shift) assert dtype.c_api_value_to_dtype(wrapper.get_type(result)) == dtype_name, f"Failed for dtype: {dtype_name}" + + @pytest.mark.parametrize( "invdtypes", [ @@ -54,12 +58,14 @@ def test_bitshiftl_supported_dtypes(invdtypes: dtype.Dtype) -> None: shape = (5, 5) with pytest.raises(RuntimeError): value = wrapper.randu(shape, invdtypes) - shift_amount = 1 + bits_to_shift = wrapper.constant(1, shape, invdtypes) - result = wrapper.bitshiftl(value, shift_amount) + result = wrapper.bitshiftl(value, bits_to_shift) assert dtype.c_api_value_to_dtype(wrapper.get_type(result)) == invdtypes, f"Failed for dtype: {invdtypes}" + + @pytest.mark.parametrize("input_size", [8, 10, 12]) -def test_bitshiftl_varying_input_size(input_size): +def test_bitshiftl_varying_input_size(input_size: int) -> None: """Test bitshift left operation with varying input sizes""" shape = (input_size, input_size) value = wrapper.randu(shape, dtype.int16) @@ -67,12 +73,13 @@ def test_bitshiftl_varying_input_size(input_size): result = wrapper.bitshiftl(value, shift_amount) - assert wrapper.get_dims(result)[0 : len(shape)] == shape + assert (wrapper.get_dims(result)[0], wrapper.get_dims(result)[1]) == shape + @pytest.mark.parametrize( "shape", [ - (10, ), + (10,), (5, 5), (2, 3, 4), ], @@ -81,13 +88,14 @@ def test_bitshiftl_varying_shapes(shape: tuple) -> None: """Test left bit shifting with arrays of varying shapes.""" values = wrapper.randu(shape, dtype.int16) bits_to_shift = wrapper.constant(1, shape, dtype.int16) - + result = wrapper.bitshiftl(values, bits_to_shift) - assert wrapper.get_dims(result)[0 : len(shape)] == shape + assert wrapper.get_dims(result)[0 : len(shape)] == shape # noqa + @pytest.mark.parametrize("shift_amount", [-1, 0, 2, 30]) -def test_bitshift_left_varying_shift_amount(shift_amount): +def test_bitshift_left_varying_shift_amount(shift_amount: int) -> None: """Test bitshift left operation with varying shift amounts.""" shape = (5, 5) value = wrapper.randu(shape, dtype.int16) @@ -95,7 +103,8 @@ def test_bitshift_left_varying_shift_amount(shift_amount): result = wrapper.bitshiftl(value, shift_amount_arr) - assert wrapper.get_dims(result)[0 : len(shape)] == shape + assert (wrapper.get_dims(result)[0], wrapper.get_dims(result)[1]) == shape + @pytest.mark.parametrize( "shape_a, shape_b", @@ -113,10 +122,13 @@ def test_bitshiftl_different_shapes(shape_a: tuple, shape_b: tuple) -> None: bits_to_shift = wrapper.constant(1, shape_b, dtype.int16) result = wrapper.bitshiftl(values, bits_to_shift) print(array_to_string("", result, 3, False)) - assert wrapper.get_dims(result)[0 : len(shape_a)] == shape_a, f"Failed for shapes {shape_a} and {shape_b}" + assert ( + wrapper.get_dims(result)[0 : len(shape_a)] == shape_a # noqa + ), f"Failed for shapes {shape_a} and {shape_b}" + @pytest.mark.parametrize("shift_amount", [-1, 0, 2, 30]) -def test_bitshift_right_varying_shift_amount(shift_amount): +def test_bitshift_right_varying_shift_amount(shift_amount: int) -> None: """Test bitshift right operation with varying shift amounts.""" shape = (5, 5) value = wrapper.randu(shape, dtype.int16) @@ -124,7 +136,8 @@ def test_bitshift_right_varying_shift_amount(shift_amount): result = wrapper.bitshiftr(value, shift_amount_arr) - assert wrapper.get_dims(result)[0 : len(shape)] == shape + assert (wrapper.get_dims(result)[0], wrapper.get_dims(result)[1]) == shape + @pytest.mark.parametrize("dtype_name", dtype_map.values()) def test_bitshiftr_dtypes(dtype_name: dtype.Dtype) -> None: @@ -132,10 +145,12 @@ def test_bitshiftr_dtypes(dtype_name: dtype.Dtype) -> None: shape = (5, 5) values = wrapper.randu(shape, dtype_name) bits_to_shift = wrapper.constant(1, shape, dtype_name) - + result = wrapper.bitshiftr(values, bits_to_shift) assert dtype.c_api_value_to_dtype(wrapper.get_type(result)) == dtype_name, f"Failed for dtype: {dtype_name}" + + @pytest.mark.parametrize( "invdtypes", [ @@ -148,13 +163,14 @@ def test_bitshiftr_supported_dtypes(invdtypes: dtype.Dtype) -> None: shape = (5, 5) with pytest.raises(RuntimeError): value = wrapper.randu(shape, invdtypes) - shift_amount = 1 + shift_amount = wrapper.constant(1, shape, invdtypes) result = wrapper.bitshiftr(value, shift_amount) assert dtype.c_api_value_to_dtype(wrapper.get_type(result)) == invdtypes, f"Failed for dtype: {invdtypes}" + @pytest.mark.parametrize("input_size", [8, 10, 12]) -def test_bitshift_right_varying_input_size(input_size): +def test_bitshift_right_varying_input_size(input_size: int) -> None: """Test bitshift right operation with varying input sizes""" shape = (input_size, input_size) value = wrapper.randu(shape, dtype.int16) @@ -162,11 +178,13 @@ def test_bitshift_right_varying_input_size(input_size): result = wrapper.bitshiftr(value, shift_amount) - assert wrapper.get_dims(result)[0 : len(shape)] == shape + assert (wrapper.get_dims(result)[0], wrapper.get_dims(result)[1]) == shape + + @pytest.mark.parametrize( "shape", [ - (10, ), + (10,), (5, 5), (2, 3, 4), ], @@ -175,11 +193,10 @@ def test_bitshiftr_varying_shapes(shape: tuple) -> None: """Test right bit shifting with arrays of varying shapes.""" values = wrapper.randu(shape, dtype.int16) bits_to_shift = wrapper.constant(1, shape, dtype.int16) - - result = wrapper.bitshiftr(values, bits_to_shift) - assert wrapper.get_dims(result)[0 : len(shape)] == shape + result = wrapper.bitshiftr(values, bits_to_shift) + assert wrapper.get_dims(result)[0 : len(shape)] == shape # noqa @pytest.mark.parametrize( @@ -198,4 +215,6 @@ def test_bitshiftr_different_shapes(shape_a: tuple, shape_b: tuple) -> None: bits_to_shift = wrapper.constant(1, shape_b, dtype.int16) result = wrapper.bitshiftr(values, bits_to_shift) print(array_to_string("", result, 3, False)) - assert wrapper.get_dims(result)[0 : len(shape_a)] == shape_a, f"Failed for shapes {shape_a} and {shape_b}" \ No newline at end of file + assert ( + wrapper.get_dims(result)[0 : len(shape_a)] == shape_a # noqa + ), f"Failed for shapes {shape_a} and {shape_b}" From b5f88c6c2c39f79c081074bdef0332d4b794b160 Mon Sep 17 00:00:00 2001 From: AzeezIsh Date: Tue, 19 Mar 2024 13:35:02 -0400 Subject: [PATCH 4/7] Adhered to all checkstyle and checked for dtypes and shapes for all complex math functions. --- tests/complex_testing.py | 185 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 tests/complex_testing.py diff --git a/tests/complex_testing.py b/tests/complex_testing.py new file mode 100644 index 0000000..82afb4a --- /dev/null +++ b/tests/complex_testing.py @@ -0,0 +1,185 @@ +import random + +# import numpy as np +import pytest + +import arrayfire_wrapper.dtypes as dtype +import arrayfire_wrapper.lib as wrapper + +dtype_map = { + # "int16": dtype.s16, + # "int32": dtype.s32, + # "int64": dtype.s64, + # "uint8": dtype.u8, + # "uint16": dtype.u16, + # "uint32": dtype.u32, + # "uint64": dtype.u64, + # "float16": dtype.f16, + "float32": dtype.f32, + # 'float64': dtype.f64, + # 'complex64': dtype.c64, + # "complex32": dtype.c32, + # "bool": dtype.b8, + # "s16": dtype.s16, + # "s32": dtype.s32, + # "s64": dtype.s64, + # "u8": dtype.u8, + # "u16": dtype.u16, + # "u32": dtype.u32, + # "u64": dtype.u64, + # "f16": dtype.f16, + "f32": dtype.f32, + # 'f64': dtype.f64, + # "c32": dtype.c32, + # 'c64': dtype.c64, + # "b8": dtype.b8, +} + + +@pytest.mark.parametrize( + "shape", + [ + (), + (random.randint(1, 10),), + (random.randint(1, 10), random.randint(1, 10)), + (random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)), + (random.randint(1, 10), random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)), + ], +) +@pytest.mark.parametrize("dtype_name", dtype_map.values()) +def test_complex_supported_dtypes(shape: tuple, dtype_name: dtype.Dtype) -> None: + """Test complex operation across all supported data types.""" + tester = wrapper.randu(shape, dtype_name) + result = wrapper.cplx(tester) + assert wrapper.is_complex(result), f"Failed for dtype: {dtype_name}" + + +@pytest.mark.parametrize( + "invdtypes", + [ + dtype.c64, + dtype.f64, + ], +) +def test_complex_unsupported_dtypes(invdtypes: dtype.Dtype) -> None: + """Test complex operation for unsupported data types.""" + with pytest.raises(RuntimeError): + shape = (5, 5) + out = wrapper.randu(shape, invdtypes) + wrapper.cplx(out) + + +@pytest.mark.parametrize( + "shape", + [ + (), + (random.randint(1, 10),), + (random.randint(1, 10), random.randint(1, 10)), + (random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)), + (random.randint(1, 10), random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)), + ], +) +@pytest.mark.parametrize("dtype_name", dtype_map.values()) +def test_complex2_supported_dtypes(shape: tuple, dtype_name: dtype.Dtype) -> None: + """Test complex2 operation across all supported data types.""" + lhs = wrapper.randu(shape, dtype_name) + rhs = wrapper.randu(shape, dtype_name) + result = wrapper.cplx2(lhs, rhs) + assert wrapper.is_complex(result), f"Failed for dtype: {dtype_name}" + + +@pytest.mark.parametrize( + "invdtypes", + [ + dtype.c64, + dtype.f64, + ], +) +def test_complex2_unsupported_dtypes(invdtypes: dtype.Dtype) -> None: + """Test complex2 operation for unsupported data types.""" + with pytest.raises(RuntimeError): + shape = (5, 5) + lhs = wrapper.randu(shape, invdtypes) + rhs = wrapper.randu(shape, invdtypes) + wrapper.cplx2(lhs, rhs) + + +@pytest.mark.parametrize( + "shape", + [ + (), + (random.randint(1, 10),), + (random.randint(1, 10), random.randint(1, 10)), + (random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)), + (random.randint(1, 10), random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)), + ], +) +def test_conj_supported_dtypes(shape: tuple) -> None: + """Test conjugate operation for supported data types.""" + arr = wrapper.constant(7, shape, dtype.c32) + result = wrapper.conjg(arr) + assert wrapper.is_complex(result), f"Failed for shape: {shape}" + + +@pytest.mark.parametrize( + "invdtypes", + [ + dtype.c64, + dtype.f64, + ], +) +def test_conj_unsupported_dtypes(invdtypes: dtype.Dtype) -> None: + """Test conjugate operation for unsupported data types.""" + with pytest.raises(RuntimeError): + shape = (5, 5) + arr = wrapper.randu(shape, invdtypes) + wrapper.conjg(arr) + + +@pytest.mark.parametrize( + "shape", + [ + (), + (random.randint(1, 10),), + (random.randint(1, 10), random.randint(1, 10)), + (random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)), + (random.randint(1, 10), random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)), + ], +) +def test_imag_real_supported_dtypes(shape: tuple) -> None: + """Test imaginary and real operations for supported data types.""" + arr = wrapper.randu(shape, dtype.c32) + imaginary = wrapper.imag(arr) + real = wrapper.real(arr) + assert not wrapper.is_empty(imaginary), f"Failed for shape: {shape}" + assert not wrapper.is_empty(real), f"Failed for shape: {shape}" + + +@pytest.mark.parametrize( + "invdtypes", + [ + dtype.c64, + dtype.f64, + ], +) +def test_imag_unsupported_dtypes(invdtypes: dtype.Dtype) -> None: + """Test conjugate operation for unsupported data types.""" + with pytest.raises(RuntimeError): + shape = (5, 5) + arr = wrapper.randu(shape, invdtypes) + wrapper.imag(arr) + + +@pytest.mark.parametrize( + "invdtypes", + [ + dtype.c64, + dtype.f64, + ], +) +def test_real_unsupported_dtypes(invdtypes: dtype.Dtype) -> None: + """Test real operation for unsupported data types.""" + with pytest.raises(RuntimeError): + shape = (5, 5) + arr = wrapper.randu(shape, invdtypes) + wrapper.real(arr) From 28ee287af6caf7aedb16f11c39855359e9dc9149 Mon Sep 17 00:00:00 2001 From: AzeezIsh Date: Tue, 26 Mar 2024 12:41:56 -0400 Subject: [PATCH 5/7] Adhered to checkstyle and fixed dtype checking. --- tests/{complex_testing.py => test_complex.py} | 116 +++++------------- 1 file changed, 33 insertions(+), 83 deletions(-) rename tests/{complex_testing.py => test_complex.py} (56%) diff --git a/tests/complex_testing.py b/tests/test_complex.py similarity index 56% rename from tests/complex_testing.py rename to tests/test_complex.py index 82afb4a..8db17b7 100644 --- a/tests/complex_testing.py +++ b/tests/test_complex.py @@ -1,39 +1,10 @@ import random -# import numpy as np import pytest import arrayfire_wrapper.dtypes as dtype import arrayfire_wrapper.lib as wrapper - -dtype_map = { - # "int16": dtype.s16, - # "int32": dtype.s32, - # "int64": dtype.s64, - # "uint8": dtype.u8, - # "uint16": dtype.u16, - # "uint32": dtype.u32, - # "uint64": dtype.u64, - # "float16": dtype.f16, - "float32": dtype.f32, - # 'float64': dtype.f64, - # 'complex64': dtype.c64, - # "complex32": dtype.c32, - # "bool": dtype.b8, - # "s16": dtype.s16, - # "s32": dtype.s32, - # "s64": dtype.s64, - # "u8": dtype.u8, - # "u16": dtype.u16, - # "u32": dtype.u32, - # "u64": dtype.u64, - # "f16": dtype.f16, - "f32": dtype.f32, - # 'f64': dtype.f64, - # "c32": dtype.c32, - # 'c64': dtype.c64, - # "b8": dtype.b8, -} +from tests.utility_functions import check_type_supported, get_all_types, get_float_types, get_real_types @pytest.mark.parametrize( @@ -46,9 +17,12 @@ (random.randint(1, 10), random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)), ], ) -@pytest.mark.parametrize("dtype_name", dtype_map.values()) +@pytest.mark.parametrize("dtype_name", get_float_types()) def test_complex_supported_dtypes(shape: tuple, dtype_name: dtype.Dtype) -> None: """Test complex operation across all supported data types.""" + check_type_supported(dtype_name) + if dtype_name == dtype.f16: + pytest.skip() tester = wrapper.randu(shape, dtype_name) result = wrapper.cplx(tester) assert wrapper.is_complex(result), f"Failed for dtype: {dtype_name}" @@ -57,8 +31,8 @@ def test_complex_supported_dtypes(shape: tuple, dtype_name: dtype.Dtype) -> None @pytest.mark.parametrize( "invdtypes", [ - dtype.c64, - dtype.f64, + dtype.int32, + dtype.complex32, ], ) def test_complex_unsupported_dtypes(invdtypes: dtype.Dtype) -> None: @@ -79,9 +53,10 @@ def test_complex_unsupported_dtypes(invdtypes: dtype.Dtype) -> None: (random.randint(1, 10), random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)), ], ) -@pytest.mark.parametrize("dtype_name", dtype_map.values()) +@pytest.mark.parametrize("dtype_name", get_real_types()) def test_complex2_supported_dtypes(shape: tuple, dtype_name: dtype.Dtype) -> None: """Test complex2 operation across all supported data types.""" + check_type_supported(dtype_name) lhs = wrapper.randu(shape, dtype_name) rhs = wrapper.randu(shape, dtype_name) result = wrapper.cplx2(lhs, rhs) @@ -91,8 +66,7 @@ def test_complex2_supported_dtypes(shape: tuple, dtype_name: dtype.Dtype) -> Non @pytest.mark.parametrize( "invdtypes", [ - dtype.c64, - dtype.f64, + dtype.c32, ], ) def test_complex2_unsupported_dtypes(invdtypes: dtype.Dtype) -> None: @@ -114,26 +88,13 @@ def test_complex2_unsupported_dtypes(invdtypes: dtype.Dtype) -> None: (random.randint(1, 10), random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)), ], ) -def test_conj_supported_dtypes(shape: tuple) -> None: +@pytest.mark.parametrize("dtypes", get_all_types()) +def test_conj_supported_dtypes(shape: tuple, dtypes: dtype.Dtype) -> None: """Test conjugate operation for supported data types.""" - arr = wrapper.constant(7, shape, dtype.c32) + check_type_supported(dtypes) + arr = wrapper.constant(7, shape, dtypes) result = wrapper.conjg(arr) - assert wrapper.is_complex(result), f"Failed for shape: {shape}" - - -@pytest.mark.parametrize( - "invdtypes", - [ - dtype.c64, - dtype.f64, - ], -) -def test_conj_unsupported_dtypes(invdtypes: dtype.Dtype) -> None: - """Test conjugate operation for unsupported data types.""" - with pytest.raises(RuntimeError): - shape = (5, 5) - arr = wrapper.randu(shape, invdtypes) - wrapper.conjg(arr) + assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"Failed for shape: {shape}, and dtype: {dtypes}" # noqa @pytest.mark.parametrize( @@ -146,40 +107,29 @@ def test_conj_unsupported_dtypes(invdtypes: dtype.Dtype) -> None: (random.randint(1, 10), random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)), ], ) -def test_imag_real_supported_dtypes(shape: tuple) -> None: +@pytest.mark.parametrize("dtypes", get_all_types()) +def test_imag_supported_dtypes(shape: tuple, dtypes: dtype.Dtype) -> None: """Test imaginary and real operations for supported data types.""" - arr = wrapper.randu(shape, dtype.c32) - imaginary = wrapper.imag(arr) + check_type_supported(dtypes) + arr = wrapper.randu(shape, dtypes) real = wrapper.real(arr) - assert not wrapper.is_empty(imaginary), f"Failed for shape: {shape}" - assert not wrapper.is_empty(real), f"Failed for shape: {shape}" - - -@pytest.mark.parametrize( - "invdtypes", - [ - dtype.c64, - dtype.f64, - ], -) -def test_imag_unsupported_dtypes(invdtypes: dtype.Dtype) -> None: - """Test conjugate operation for unsupported data types.""" - with pytest.raises(RuntimeError): - shape = (5, 5) - arr = wrapper.randu(shape, invdtypes) - wrapper.imag(arr) + assert wrapper.is_real(real), f"Failed for shape: {shape}" @pytest.mark.parametrize( - "invdtypes", + "shape", [ - dtype.c64, - dtype.f64, + (), + (random.randint(1, 10),), + (random.randint(1, 10), random.randint(1, 10)), + (random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)), + (random.randint(1, 10), random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)), ], ) -def test_real_unsupported_dtypes(invdtypes: dtype.Dtype) -> None: - """Test real operation for unsupported data types.""" - with pytest.raises(RuntimeError): - shape = (5, 5) - arr = wrapper.randu(shape, invdtypes) - wrapper.real(arr) +@pytest.mark.parametrize("dtypes", get_all_types()) +def test_real_supported_dtypes(shape: tuple, dtypes: dtype.Dtype) -> None: + """Test imaginary and real operations for supported data types.""" + check_type_supported(dtypes) + arr = wrapper.randu(shape, dtypes) + real = wrapper.real(arr) + assert wrapper.is_real(real), f"Failed for shape: {shape}" From a5fc38bedb08b7e465c3f0947aea437cf04dad45 Mon Sep 17 00:00:00 2001 From: AzeezIsh Date: Tue, 26 Mar 2024 14:20:49 -0400 Subject: [PATCH 6/7] Removed dtype map, added further dtype coverage. --- tests/test_bitshift.py | 45 +++++++++++------------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/tests/test_bitshift.py b/tests/test_bitshift.py index 705c81d..a651c57 100644 --- a/tests/test_bitshift.py +++ b/tests/test_bitshift.py @@ -3,40 +3,14 @@ import arrayfire_wrapper.dtypes as dtype import arrayfire_wrapper.lib as wrapper from arrayfire_wrapper.lib.create_and_modify_array.helper_functions import array_to_string +from tests.utility_functions import check_type_supported, get_all_types, get_float_types, get_real_types -dtype_map = { - "int16": dtype.s16, - "int32": dtype.s32, - "int64": dtype.s64, - "uint8": dtype.u8, - "uint16": dtype.u16, - "uint32": dtype.u32, - "uint64": dtype.u64, - # 'float16': dtype.f16, - # 'float32': dtype.f32, - # 'float64': dtype.f64, - # 'complex64': dtype.c64, - # 'complex32': dtype.c32, - "bool": dtype.b8, - "s16": dtype.s16, - "s32": dtype.s32, - "s64": dtype.s64, - "u8": dtype.u8, - "u16": dtype.u16, - "u32": dtype.u32, - "u64": dtype.u64, - # 'f16': dtype.f16, - # 'f32': dtype.f32, - # 'f64': dtype.f64, - # 'c32': dtype.c32, - # 'c64': dtype.c64, - "b8": dtype.b8, -} - - -@pytest.mark.parametrize("dtype_name", dtype_map.values()) +@pytest.mark.parametrize("dtype_name", get_real_types()) def test_bitshiftl_dtypes(dtype_name: dtype.Dtype) -> None: """Test bit shift operation across all supported data types.""" + check_type_supported(dtype_name) + if dtype_name == dtype.f16 or dtype_name == dtype.f32: + pytest.skip() shape = (5, 5) values = wrapper.randu(shape, dtype_name) bits_to_shift = wrapper.constant(1, shape, dtype_name) @@ -49,7 +23,7 @@ def test_bitshiftl_dtypes(dtype_name: dtype.Dtype) -> None: @pytest.mark.parametrize( "invdtypes", [ - dtype.c64, + dtype.c32, dtype.f64, ], ) @@ -139,9 +113,12 @@ def test_bitshift_right_varying_shift_amount(shift_amount: int) -> None: assert (wrapper.get_dims(result)[0], wrapper.get_dims(result)[1]) == shape -@pytest.mark.parametrize("dtype_name", dtype_map.values()) +@pytest.mark.parametrize("dtype_name", get_real_types()) def test_bitshiftr_dtypes(dtype_name: dtype.Dtype) -> None: """Test bit shift operation across all supported data types.""" + check_type_supported(dtype_name) + if dtype_name == dtype.f16 or dtype_name == dtype.f32: + pytest.skip() shape = (5, 5) values = wrapper.randu(shape, dtype_name) bits_to_shift = wrapper.constant(1, shape, dtype_name) @@ -154,7 +131,7 @@ def test_bitshiftr_dtypes(dtype_name: dtype.Dtype) -> None: @pytest.mark.parametrize( "invdtypes", [ - dtype.c64, + dtype.c32, dtype.f64, ], ) From 99092da1c164f789d9c4edb1b4184296aa0b5ab2 Mon Sep 17 00:00:00 2001 From: AzeezIsh Date: Tue, 26 Mar 2024 14:23:03 -0400 Subject: [PATCH 7/7] Addedd checkstyle and fixed dtype coverage. --- tests/test_bitshift.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_bitshift.py b/tests/test_bitshift.py index a651c57..fa005b5 100644 --- a/tests/test_bitshift.py +++ b/tests/test_bitshift.py @@ -3,7 +3,8 @@ import arrayfire_wrapper.dtypes as dtype import arrayfire_wrapper.lib as wrapper from arrayfire_wrapper.lib.create_and_modify_array.helper_functions import array_to_string -from tests.utility_functions import check_type_supported, get_all_types, get_float_types, get_real_types +from tests.utility_functions import check_type_supported, get_real_types + @pytest.mark.parametrize("dtype_name", get_real_types()) def test_bitshiftl_dtypes(dtype_name: dtype.Dtype) -> None: