Skip to content
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

Added unit tests for the pad function and fixed pad function return v… #22

Merged
merged 2 commits into from
Mar 2, 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

return out
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