Skip to content

Up to Date Master Branch #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ def pad(arr: AFArray, begin_shape: tuple[int, ...], end_shape: tuple[int, ...],
end_c_shape.c_array,
border_type.value,
)
return NotImplemented
return out
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def get_scalar(arr: AFArray, dtype: Dtype, /) -> int | float | complex | bool |
out = dtype.c_type()
call_from_clib(get_scalar.__name__, ctypes.pointer(out), arr)
if dtype == c32 or dtype == c64:
return complex(out[0], out[1]) # type: ignore
return complex(out[0], out[1]) # type: ignore
else:
return cast(int | float | complex | bool | None, out.value)

Expand Down
43 changes: 43 additions & 0 deletions tests/test_diag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pytest

import arrayfire_wrapper.dtypes as dtypes
import arrayfire_wrapper.lib as wrapper


@pytest.mark.parametrize("diagonal_shape", [(2,), (10,), (100,), (1000,)])
def test_diagonal_shape(diagonal_shape: tuple) -> None:
"""Test if diagonal array is keeping the shape of the passed into the input array"""
in_arr = wrapper.constant(1, diagonal_shape, dtypes.s16)
diag_array = wrapper.diag_create(in_arr, 0)

extracted_diagonal = wrapper.diag_extract(diag_array, 0)

assert wrapper.get_dims(extracted_diagonal)[0 : len(diagonal_shape)] == diagonal_shape # noqa: E203


@pytest.mark.parametrize("diagonal_shape", [(2,), (10,), (100,), (1000,)])
def test_diagonal_val(diagonal_shape: tuple) -> None:
"""Test if diagonal array is keeping the same value as that of the values passed into the input array"""
dtype = dtypes.s16
in_arr = wrapper.constant(1, diagonal_shape, dtype)
diag_array = wrapper.diag_create(in_arr, 0)

extracted_diagonal = wrapper.diag_extract(diag_array, 0)

assert wrapper.get_scalar(extracted_diagonal, dtype) == wrapper.get_scalar(in_arr, dtype)


@pytest.mark.parametrize(
"diagonal_shape",
[
(10, 10, 10),
(100, 100, 100, 100),
],
)
def test_invalid_diagonal(diagonal_shape: tuple) -> None:
"""Test if an invalid diagonal shape is being properly handled"""
with pytest.raises(RuntimeError):
in_arr = wrapper.constant(1, diagonal_shape, dtypes.s16)
diag_array = wrapper.diag_create(in_arr, 0)

wrapper.diag_extract(diag_array, 0)
70 changes: 70 additions & 0 deletions tests/test_identity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import random

import pytest

import arrayfire_wrapper.dtypes as dtypes
import arrayfire_wrapper.lib as wrapper


@pytest.mark.parametrize(
"shape",
[
(),
(random.randint(1, 10), 1),
(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_identity_shape(shape: tuple) -> None:
"""Test if identity creates an array with the correct shape"""
dtype = dtypes.s16

result = wrapper.identity(shape, dtype)

assert wrapper.get_dims(result)[0 : len(shape)] == shape # noqa: E203


def test_identity_invalid_shape() -> None:
"""Test if identity handles a shape with greater than 4 dimensions"""
with pytest.raises(TypeError) as excinfo:
invalid_shape = (
random.randint(1, 10),
random.randint(1, 10),
random.randint(1, 10),
random.randint(1, 10),
random.randint(1, 10),
)
dtype = dtypes.s16

wrapper.identity(invalid_shape, dtype)

assert f"CShape.__init__() takes from 1 to 5 positional arguments but {len(invalid_shape) + 1} were given" in str(
excinfo.value
)


def test_identity_nonsquare_shape() -> None:
dtype = dtypes.s16
shape = (5, 6)

result = wrapper.identity(shape, dtype)

assert wrapper.get_dims(result)[0 : len(shape)] == shape # noqa: E203


@pytest.mark.parametrize(
"dtype_index",
[i for i in range(13)],
)
def test_identity_dtype(dtype_index: int) -> None:
"""Test if identity creates an array with the correct dtype"""
if dtype_index in [2, 3] and not wrapper.get_dbl_support():
pytest.skip()

shape = (5, 5)
dtype = dtypes.c_api_value_to_dtype(dtype_index)

result = wrapper.identity(shape, dtype)

assert dtypes.c_api_value_to_dtype(wrapper.get_type(result)) == dtype
125 changes: 125 additions & 0 deletions tests/test_iota.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import random

import numpy as np
import pytest

import arrayfire_wrapper.dtypes as dtypes
import arrayfire_wrapper.lib as wrapper


@pytest.mark.parametrize(
"shape",
[
(),
(random.randint(1, 10), 1),
(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_iota_shape(shape: tuple) -> None:
"""Test if identity creates an array with the correct shape"""
dtype = dtypes.s16
t_shape = (1, 1)

result = wrapper.iota(shape, t_shape, dtype)

assert wrapper.get_dims(result)[0 : len(shape)] == shape # noqa: E203


def test_iota_invalid_shape() -> None:
"""Test if iota handles a shape with greater than 4 dimensions"""
with pytest.raises(TypeError) as excinfo:
invalid_shape = (
random.randint(1, 10),
random.randint(1, 10),
random.randint(1, 10),
random.randint(1, 10),
random.randint(1, 10),
)
dtype = dtypes.s16
t_shape = ()

wrapper.iota(invalid_shape, t_shape, dtype)

assert f"CShape.__init__() takes from 1 to 5 positional arguments but {len(invalid_shape) + 1} were given" in str(
excinfo.value
)


@pytest.mark.parametrize(
"t_shape",
[
(1,),
(random.randint(1, 10), 1),
(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_iota_tshape(t_shape: tuple) -> None:
"""Test if iota properly uses t_shape to change the size of the array and result in correct dimensions"""
shape = np.array([2, 2])
dtype = dtypes.s64

if len(shape.shape) < len(t_shape):
shape = np.append(shape, np.ones(len(t_shape) - len(shape), dtype=int))

result_shape = shape * t_shape

result = wrapper.iota(tuple(shape), t_shape, dtype)

result_dims = tuple(int(value) for value in wrapper.get_dims(result))

assert (result_dims[0 : len(result_shape)] == result_shape).all() # noqa: E203


@pytest.mark.parametrize(
"t_shape",
[
(0,),
(-1, -1),
],
)
def test_iota_tshape_zero(t_shape: tuple) -> None:
"""Test it iota properly handles negative or zero t_shapes"""
with pytest.raises(RuntimeError):
shape = (2, 2)

dtype = dtypes.s16

wrapper.iota(shape, t_shape, dtype)


def test_iota_tshape_invalid() -> None:
"""Test it iota properly handles a tshape with greater than 4 dimensions"""
with pytest.raises(TypeError):
shape = (2, 2)
invalid_tshape = (
random.randint(1, 10),
random.randint(1, 10),
random.randint(1, 10),
random.randint(1, 10),
random.randint(1, 10),
)
dtype = dtypes.s16

wrapper.iota(shape, invalid_tshape, dtype)


@pytest.mark.parametrize(
"dtype_index",
[i for i in range(13)],
)
def test_iota_dtype(dtype_index: int) -> None:
"""Test if iota creates an array with the correct dtype"""
if (dtype_index in [1, 4]) or (dtype_index in [2, 3] and not wrapper.get_dbl_support()):
pytest.skip()

shape = (5, 5)
t_shape = (2, 2)
dtype = dtypes.c_api_value_to_dtype(dtype_index)

result = wrapper.iota(shape, t_shape, dtype)

assert dtypes.c_api_value_to_dtype(wrapper.get_type(result)) == dtype
45 changes: 45 additions & 0 deletions tests/test_lower.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import pytest

import arrayfire_wrapper.dtypes as dtypes
import arrayfire_wrapper.lib as wrapper


@pytest.mark.parametrize(
"shape",
[
(3, 3),
(3, 3, 3),
(3, 3, 3, 3),
],
)
def test_diag_is_unit(shape: tuple) -> None:
"""Test if when is_unit_diag in lower returns an array with a unit diagonal"""
dtype = dtypes.s64
constant_array = wrapper.constant(3, shape, dtype)

lower_array = wrapper.lower(constant_array, True)
diagonal = wrapper.diag_extract(lower_array, 0)
diagonal_value = wrapper.get_scalar(diagonal, dtype)

assert diagonal_value == 1


@pytest.mark.parametrize(
"shape",
[
(3, 3),
(3, 3, 3),
(3, 3, 3, 3),
],
)
def test_is_original(shape: tuple) -> None:
"""Test if is_original keeps the diagonal the same as the original array"""
dtype = dtypes.s64
constant_array = wrapper.constant(3, shape, dtype)
original_value = wrapper.get_scalar(constant_array, dtype)

lower_array = wrapper.lower(constant_array, False)
diagonal = wrapper.diag_extract(lower_array, 0)
diagonal_value = wrapper.get_scalar(diagonal, dtype)

assert original_value == diagonal_value
71 changes: 71 additions & 0 deletions tests/test_pad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import random

import numpy as np
import pytest

import arrayfire_wrapper.dtypes as dtypes
import arrayfire_wrapper.lib as wrapper


@pytest.mark.parametrize(
"original_shape",
[
(random.randint(1, 100),),
(random.randint(1, 100), random.randint(1, 100)),
(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100)),
(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100), random.randint(1, 100)),
],
)
def test_zero_padding(original_shape: tuple) -> None:
"""Test if pad creates an array with no padding if no padding is given"""
original_array = wrapper.constant(2, original_shape, dtypes.s64)
padding = wrapper.Pad(0)

zero_shape = tuple(0 for _ in range(len(original_shape)))
result = wrapper.pad(original_array, zero_shape, zero_shape, padding)

assert wrapper.get_dims(result)[0 : len(original_shape)] == original_shape # noqa: E203


@pytest.mark.parametrize(
"original_shape",
[
(random.randint(1, 100),),
(random.randint(1, 100), random.randint(1, 100)),
(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100)),
(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100), random.randint(1, 100)),
],
)
def test_negative_padding(original_shape: tuple) -> None:
"""Test if pad can properly handle if negative padding is given"""
with pytest.raises(RuntimeError):
original_array = wrapper.constant(2, original_shape, dtypes.s64)
padding = wrapper.Pad(0)

neg_shape = tuple(-1 for _ in range(len(original_shape)))
result = wrapper.pad(original_array, neg_shape, neg_shape, padding)

assert wrapper.get_dims(result)[0 : len(original_shape)] == original_shape # noqa: E203


@pytest.mark.parametrize(
"original_shape",
[
(random.randint(1, 100),),
(random.randint(1, 100), random.randint(1, 100)),
(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100)),
(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100), random.randint(1, 100)),
],
)
def test_padding_shape(original_shape: tuple) -> None:
"""Test if pad outputs the correct shape when a padding is adding to the original array"""
original_array = wrapper.constant(2, original_shape, dtypes.s64)
padding = wrapper.Pad(0)

beg_shape = tuple(random.randint(1, 10) for _ in range(len(original_shape)))
end_shape = tuple(random.randint(1, 10) for _ in range(len(original_shape)))

result = wrapper.pad(original_array, beg_shape, end_shape, padding)
new_shape = np.array(beg_shape) + np.array(end_shape) + np.array(original_shape)

assert wrapper.get_dims(result)[0 : len(original_shape)] == tuple(new_shape) # noqa: E203
Loading