Skip to content

Commit

Permalink
enhance test_setflags (nv-legate#962)
Browse files Browse the repository at this point in the history
* enhance test_setflags
  • Loading branch information
XiaLuNV authored Jun 14, 2023
1 parent e510d05 commit 3a616bf
Show file tree
Hide file tree
Showing 2 changed files with 220 additions and 24 deletions.
90 changes: 66 additions & 24 deletions tests/integration/test_repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@
"array", (None, [], 4, [2, 3], mk_seq_array(num, (3, 4, 2)))
)
def test_repeats_none(array):
with pytest.raises(TypeError):
expected_exc = TypeError
with pytest.raises(expected_exc):
np.repeat(array, None)
with pytest.raises(expected_exc):
num.repeat(array, None)


@pytest.mark.parametrize("repeats", (-3, [], [-3], [2, 3]))
def test_array_none_invalid(repeats):
with pytest.raises(ValueError):
expected_exc = ValueError
with pytest.raises(expected_exc):
np.repeat(None, repeats)
with pytest.raises(expected_exc):
num.repeat(None, repeats)


Expand All @@ -48,13 +54,17 @@ def test_array_empty_repeats_valid(repeats):
assert np.array_equal(res_np, res_num)


@pytest.mark.xfail
@pytest.mark.parametrize("repeats", ([3, 4], [1, 2, 3]))
def test_array_empty_repeats_invalid_negative(repeats):
# numpy raises:
# ValueError: operands could not be broadcast together with shape (0,) (2,)
# while cunumeric is pass with the result []
res_num = num.repeat([], repeats)
assert np.array_equal(res_num, [])
expected_exc = ValueError
with pytest.raises(expected_exc):
np.repeat([], repeats)
# numpy raises: ValueError: operands could not be broadcast
# together with shape (0,) (2,)
with pytest.raises(expected_exc):
num.repeat([], repeats)
# while cunumeric is pass with the result []


@pytest.mark.xfail
Expand All @@ -74,13 +84,28 @@ def test_array_empty_axis_valid(repeats):

@pytest.mark.parametrize("repeats", (-3, 0, 3, 4.7, [], [-3], [0], [3], [4.7]))
def test_array_empty_axis_invalid(repeats):
with pytest.raises(ValueError):
expected_exc = ValueError
with pytest.raises(expected_exc):
np.repeat([], repeats, axis=1)
with pytest.raises(expected_exc):
num.repeat([], repeats, axis=1)


@pytest.mark.parametrize("axis", (-3, 3))
def test_array_int_axis_negative(axis):
expected_exc = ValueError
with pytest.raises(expected_exc):
np.repeat(3, 3, axis=axis)
with pytest.raises(expected_exc):
num.repeat(3, 3, axis=axis)


@pytest.mark.parametrize("repeats", (-3, [-3]))
def test_array_int_repeats_negative(repeats):
with pytest.raises(ValueError):
expected_exc = ValueError
with pytest.raises(expected_exc):
np.repeat(3, repeats)
with pytest.raises(expected_exc):
num.repeat(3, repeats)


Expand All @@ -93,8 +118,10 @@ def test_array_int_repeats_valid(repeats):

@pytest.mark.parametrize("repeats", ([], [1, 2]))
def test_array_int_repeats_invalid(repeats):
msg = r"scalar"
with pytest.raises(ValueError, match=msg):
expected_exc = ValueError
with pytest.raises(expected_exc):
np.repeat(3, repeats)
with pytest.raises(expected_exc):
num.repeat(3, repeats)


Expand All @@ -109,7 +136,10 @@ def test_array_1d_repeats_valid(repeats):
@pytest.mark.parametrize("repeats", ([], [2, 3]))
def test_array_1d_repeats_invalid(repeats):
anp = np.array([1, 2, 3])
with pytest.raises(ValueError):
expected_exc = ValueError
with pytest.raises(expected_exc):
np.repeat(anp, repeats)
with pytest.raises(expected_exc):
num.repeat(anp, repeats)


Expand All @@ -124,7 +154,10 @@ def test_array_2d_repeats_valid(repeats):
@pytest.mark.parametrize("repeats", ([], [2, 3]))
def test_array_2d_repeats_invalid(repeats):
anp = np.array([[1, 3], [2, 4]])
with pytest.raises(ValueError):
expected_exc = ValueError
with pytest.raises(expected_exc):
np.repeat(anp, repeats)
with pytest.raises(expected_exc):
num.repeat(anp, repeats)


Expand All @@ -133,9 +166,14 @@ def test_array_2d_repeats_invalid(repeats):
@pytest.mark.parametrize("repeats", (-3, [-3]))
def test_array_1d_repeats_fatal_error(arr, repeats):
anp = np.array(arr)
# numpy raises "ValueError: negative dimensions are not allowed"
# while cunumeric got "Fatal Python error: Aborted"
num.repeat(anp, repeats)
anum = num.array(arr)
expected_exc = ValueError
with pytest.raises(expected_exc):
np.repeat(anp, repeats)
# numpy raises "ValueError: negative dimensions are not allowed"
with pytest.raises(expected_exc):
num.repeat(anum, repeats)
# cuNumeric got "Fatal Python error: Aborted"


@pytest.mark.parametrize("arr", (None, [], 3, [1, 2, 3], [[1, 3], [2, 4]]))
Expand All @@ -145,25 +183,29 @@ def test_array_1d_repeats_fatal_error(arr, repeats):
)
def test_repeats_nd(arr, repeats):
anp = np.array(arr)
msg = r"should be scalar or 1D array"
with pytest.raises(ValueError, match=msg):
expected_exc = ValueError
with pytest.raises(expected_exc):
np.repeat(anp, repeats)
with pytest.raises(expected_exc):
num.repeat(anp, repeats)


@pytest.mark.parametrize(("arr", "repeats"), ((3, 3), ([1, 2, 3], [1, 2, 3])))
@pytest.mark.parametrize("axis", ("hello", 0.9))
def test_axis_string(arr, repeats, axis):
msg = r"integer"
with pytest.raises(TypeError, match=msg):
expected_exc = TypeError
with pytest.raises(expected_exc):
np.repeat(arr, repeats, axis=axis)
with pytest.raises(expected_exc):
num.repeat(arr, repeats, axis=axis)


def test_array_axis_out_bound():
anp = np.array([1, 2, 3, 4, 5])
# np.repeat(anp, 4, 2)
# numpy.AxisError: axis 2 is out of bounds for array of dimension 1
msg = r"dimension"
with pytest.raises(ValueError, match=msg):
expected_exc = np.AxisError
with pytest.raises(expected_exc):
np.repeat(anp, 4, 2)
with pytest.raises(expected_exc):
num.repeat(anp, 4, 2)


Expand Down
154 changes: 154 additions & 0 deletions tests/integration/test_setflags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# Copyright 2021-2022 NVIDIA Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import numpy as np
import pytest
from legate.core import LEGATE_MAX_DIM

import cunumeric as num


@pytest.mark.parametrize("write", (None, False, True, 1, -1, 100, "11"))
def test_set_value(write):
array_np = np.array(3)
array_num = num.array(3)
assert array_np.flags == array_num.flags
array_np.setflags(write)
array_num.setflags(write)
assert array_np.flags == array_num.flags


def test_array_default_flags():
array_np = np.array([0, 0, 0, 0, 0])
array_num = num.array([0, 0, 0, 0, 0])
assert array_np.flags["C_CONTIGUOUS"] == array_num.flags["C_CONTIGUOUS"]
assert array_np.flags["F_CONTIGUOUS"] == array_num.flags["F_CONTIGUOUS"]
assert array_np.flags["WRITEABLE"] == array_num.flags["WRITEABLE"]
assert array_np.flags["ALIGNED"] == array_num.flags["ALIGNED"]
assert (
array_np.flags["WRITEBACKIFCOPY"] == array_num.flags["WRITEBACKIFCOPY"]
)
# array_np.flags
# C_CONTIGUOUS : True
# F_CONTIGUOUS : True
# OWNDATA : True
# WRITEABLE : True
# ALIGNED : True
# WRITEBACKIFCOPY : False
# array_num.flags
# C_CONTIGUOUS : True
# F_CONTIGUOUS : True
# OWNDATA : False
# WRITEABLE : True
# ALIGNED : True
# WRITEBACKIFCOPY : False


@pytest.mark.xfail
def test_no_writable():
array_np = np.array([0, 0, 0, 0, 0])
array_num = num.array([0, 0, 0, 0, 0])
array_np.setflags(0)
array_num.setflags(0)
expected_exc = ValueError
with pytest.raises(expected_exc):
array_np[2] = 1
# Numpy raises ValueError: assignment destination is read-only
expected_exc = ValueError
with pytest.raises(expected_exc):
array_num[2] = 1
# cuNumeric set value as [0 0 1 0 0]


@pytest.mark.xfail
def test_writeable():
array_np = np.array([0, 0, 0, 0, 0])
array_num = num.array([0, 0, 0, 0, 0])
array_np.setflags(1)
array_num.setflags(1)
# cuNumeric raises ValueError: cannot set WRITEABLE flag to
# True of this array
array_np[2] = 1
array_num[2] = 1
assert array_np.flags == array_num.flags
assert np.array_equal(array_np, array_num)


def test_logic():
shape = (3, 3)
array_np = np.random.randint(1, 100, shape, dtype=int)
array_num = num.array(array_np)
array_np.setflags(write=False, align=False)
array_num.setflags(write=False, align=False)

expected_exc = ValueError
with pytest.raises(expected_exc):
array_np.setflags(uic=True)
# Numpy raises ValueError: cannot set WRITEBACKIFCOPY flag to True
expected_exc = ValueError
with pytest.raises(expected_exc):
array_num.setflags(uic=True)
# cuNumeric raises ValueError: cannot set WRITEBACKIFCOPY flag to True


@pytest.mark.xfail
@pytest.mark.parametrize("ndim", range(1, LEGATE_MAX_DIM + 1))
def test_set_write_true(ndim):
shape = (3,) * ndim
array_np = np.random.randint(1, 100, shape, dtype=int)
array_num = num.array(array_np)
array_np.setflags(write=True)
array_num.setflags(write=True)
# cuNumeric raises ValueError: cannot set WRITEABLE flag to True
# of this array
assert array_np.flags["WRITEABLE"] == array_num.flags["WRITEABLE"]


@pytest.mark.xfail
@pytest.mark.parametrize("ndim", range(1, LEGATE_MAX_DIM + 1))
def test_set_write_false(ndim):
shape = (3,) * ndim
array_np = np.random.randint(1, 100, shape, dtype=int)
array_num = num.array(array_np)
array_np.setflags(write=False)
array_num.setflags(write=False)
assert array_np.flags["WRITEABLE"] == array_num.flags["WRITEABLE"]


@pytest.mark.parametrize("ndim", range(1, LEGATE_MAX_DIM + 1))
def test_set_align_true(ndim):
shape = (3,) * ndim
array_np = np.random.randint(1, 100, shape, dtype=int)
array_num = num.array(array_np)
array_np.setflags(align=True)
array_num.setflags(align=True)
assert array_np.flags["ALIGNED"] == array_num.flags["ALIGNED"]


@pytest.mark.xfail
@pytest.mark.parametrize("ndim", range(1, LEGATE_MAX_DIM + 1))
def test_set_align_false(ndim):
shape = (3,) * ndim
array_np = np.random.randint(1, 100, shape, dtype=int)
array_num = num.array(array_np)
array_np.setflags(align=False)
array_num.setflags(align=False)
assert array_np.flags["ALIGNED"] == array_num.flags["ALIGNED"]


if __name__ == "__main__":
import sys

np.random.seed(12345)
sys.exit(pytest.main(sys.argv))

0 comments on commit 3a616bf

Please sign in to comment.