Skip to content

Commit

Permalink
Enhance two integration tests (#511)
Browse files Browse the repository at this point in the history
* Enhance two integration tests

Enhance test_append and test_array_creation
1.  add negative tests
2.  add more test cases
3.  refactor test code

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Address comments
1. Create test class for negative testing
2. Refactor out test functions
3. Use parameterize

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Address comments - part2
1. update run_test name to check_array_method
2. use parameterize for step zero cases of arange

* Address comments - Part 3
1. add pytest.mark.xfail for cases with expected failure
2. Small Fix: replace Assert with raising ValueError in deferred.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Address comments - fix a typo

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
robinwnv and pre-commit-ci[bot] authored Aug 16, 2022
1 parent b4fbde3 commit 41a2f90
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 99 deletions.
9 changes: 7 additions & 2 deletions cunumeric/deferred.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,11 @@ def _broadcast(self, shape: NdShape) -> Any:

for dim in range(len(shape)):
if result.shape[dim] != shape[dim]:
assert result.shape[dim] == 1
if result.shape[dim] != 1:
raise ValueError(
f"Shape did not match along dimension {dim} "
"and the value is not equal to 1"
)
result = result.project(dim, 0).promote(dim, shape[dim])

return result
Expand Down Expand Up @@ -1235,7 +1239,8 @@ def _fill(self, value: Any) -> None:

def fill(self, numpy_array: Any) -> None:
assert isinstance(numpy_array, np.ndarray)
assert numpy_array.size == 1
if numpy_array.size != 1:
raise ValueError("Filled value array size is not equal to 1")
assert self.dtype == numpy_array.dtype
# Have to copy the numpy array because this launch is asynchronous
# and we need to make sure the application doesn't mutate the value
Expand Down
88 changes: 46 additions & 42 deletions tests/integration/test_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,10 @@

import numpy as np
import pytest
from utils.utils import check_array_method

import cunumeric as num


def _run_test(arr, values, test_args):
for axis in test_args:
b = np.append(arr, values, axis)
c = num.append(arr, values, axis)
is_equal = True
err_arr = [b, c]

if len(b) != len(c):
is_equal = False
err_arr = [b, c]
else:
for each in zip(b, c):
if not np.array_equal(*each):
err_arr = each
is_equal = False
break
print_msg = (
f"np.append(array({arr.shape}), array({values.shape}), {axis})"
)
assert is_equal, (
f"Failed, {print_msg}\n"
f"numpy result: {err_arr[0]}, {b.shape}\n"
f"cunumeric_result: {err_arr[1]}, {c.shape}\n"
f"cunumeric and numpy shows"
f" different result\n"
)
print(
f"Passed, {print_msg}, np: ({b.shape}, {b.dtype})"
f", cunumeric: ({c.shape}, {c.dtype}"
)


DIM = 10

# test append w/ 1D, 2D and 3D arrays
Expand All @@ -61,6 +29,7 @@ def _run_test(arr, values, test_args):
(1, 1),
(1, 1, 1),
(1, DIM),
(1, DIM, 1),
(DIM, DIM),
(DIM, DIM, DIM),
]
Expand All @@ -69,16 +38,51 @@ def _run_test(arr, values, test_args):
@pytest.mark.parametrize("size", SIZES, ids=str)
def test_append(size):
a = np.random.randint(low=0, high=100, size=size)
test_args = [-1] + list(range(a.ndim))

test_args = list(range(a.ndim)) + [None]

# test the exception for 1D array on append
_run_test(a, a, test_args)

if a.ndim > 1:
# 1D array
b = np.random.randint(low=0, high=100, size=(DIM,))
_run_test(a, b, [None])
for axis in test_args:
size_b = list(size)
size_b[axis] = size[axis] + 10
b = np.random.randint(low=0, high=100, size=size_b)
print_msg = f"np.append(array({a.shape}), array({b.shape}), {axis})"
check_array_method("append", [a, b], {"axis": axis}, print_msg)


@pytest.mark.parametrize("size_b", SIZES, ids=str)
@pytest.mark.parametrize("size_a", SIZES, ids=str)
def test_append_axis_none(size_a, size_b):
axis = None
a = np.random.randint(low=0, high=100, size=size_a)
b = np.random.randint(low=0, high=100, size=size_b)
print_msg = f"np.append(array({a.shape}), array({b.shape}), {axis})"
check_array_method("append", [a, b], {"axis": axis}, print_msg)


class TestAppendErrors:
def setup(self):
size_a = (1, DIM)
self.a = np.random.randint(low=0, high=100, size=size_a)

def test_bad_dimension(self):
size_b = (1, DIM, 1)
b = np.random.randint(low=0, high=100, size=size_b)

msg = (
"All arguments to concatenate must have the "
"same number of dimensions"
)
with pytest.raises(ValueError, match=msg):
num.append(self.a, b, axis=1)

def test_bad_index(self):
with pytest.raises(IndexError):
num.append(self.a, self.a, axis=5)

def test_bad_shape(self):
size_c = (10, DIM)
c = np.random.randint(low=0, high=100, size=size_c)
with pytest.raises(ValueError):
num.append(self.a, c, axis=1)


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 41a2f90

Please sign in to comment.