|
| 1 | +import operator |
| 2 | +import unittest |
| 3 | + |
| 4 | +import numpy |
| 5 | +import pytest |
| 6 | + |
| 7 | +import dpnp as cupy |
| 8 | +from tests.third_party.cupy import testing |
| 9 | + |
| 10 | + |
| 11 | +class TestArrayBoolOp(unittest.TestCase): |
| 12 | + @pytest.mark.skip("The truth value of an empty array is ambiguous") |
| 13 | + @testing.for_all_dtypes() |
| 14 | + def test_bool_empty(self, dtype): |
| 15 | + with testing.assert_warns(DeprecationWarning): |
| 16 | + assert not bool(cupy.array((), dtype=dtype)) |
| 17 | + |
| 18 | + def test_bool_scalar_bool(self): |
| 19 | + assert bool(cupy.array(True, dtype=numpy.bool_)) |
| 20 | + assert not bool(cupy.array(False, dtype=numpy.bool_)) |
| 21 | + |
| 22 | + @testing.for_all_dtypes() |
| 23 | + def test_bool_scalar(self, dtype): |
| 24 | + assert bool(cupy.array(1, dtype=dtype)) |
| 25 | + assert not bool(cupy.array(0, dtype=dtype)) |
| 26 | + |
| 27 | + def test_bool_one_element_bool(self): |
| 28 | + assert bool(cupy.array([True], dtype=numpy.bool_)) |
| 29 | + assert not bool(cupy.array([False], dtype=numpy.bool_)) |
| 30 | + |
| 31 | + @testing.for_all_dtypes() |
| 32 | + def test_bool_one_element(self, dtype): |
| 33 | + assert bool(cupy.array([1], dtype=dtype)) |
| 34 | + assert not bool(cupy.array([0], dtype=dtype)) |
| 35 | + |
| 36 | + @testing.for_all_dtypes() |
| 37 | + def test_bool_two_elements(self, dtype): |
| 38 | + with self.assertRaises(ValueError): |
| 39 | + bool(cupy.array([1, 2], dtype=dtype)) |
| 40 | + |
| 41 | + |
| 42 | +class TestArrayUnaryOp(unittest.TestCase): |
| 43 | + @testing.for_all_dtypes(no_bool=True) |
| 44 | + @testing.numpy_cupy_allclose() |
| 45 | + def check_array_op(self, op, xp, dtype): |
| 46 | + a = testing.shaped_arange((2, 3), xp, dtype) |
| 47 | + return op(a) |
| 48 | + |
| 49 | + @testing.for_all_dtypes() |
| 50 | + @testing.numpy_cupy_allclose() |
| 51 | + def check_array_op_full(self, op, xp, dtype): |
| 52 | + a = testing.shaped_arange((2, 3), xp, dtype) |
| 53 | + return op(a) |
| 54 | + |
| 55 | + @testing.for_all_dtypes(no_bool=True) |
| 56 | + @testing.numpy_cupy_allclose() |
| 57 | + def test_neg_array(self, xp, dtype): |
| 58 | + a = testing.shaped_arange((2, 3), xp, dtype) |
| 59 | + return operator.neg(a) |
| 60 | + |
| 61 | + @testing.for_all_dtypes(no_bool=True) |
| 62 | + @testing.numpy_cupy_allclose() |
| 63 | + def test_pos_array(self, xp, dtype): |
| 64 | + a = testing.shaped_arange((2, 3), xp, dtype) |
| 65 | + assert a is not +a |
| 66 | + return +a |
| 67 | + |
| 68 | + @pytest.mark.skip( |
| 69 | + "dpnp boolean positive, the `+` operator, is not supported" |
| 70 | + ) |
| 71 | + @testing.with_requires("numpy<1.25") |
| 72 | + def test_pos_boolarray(self): |
| 73 | + for xp in (numpy, cupy): |
| 74 | + a = xp.array(True, dtype=xp.bool_) |
| 75 | + with pytest.deprecated_call(): |
| 76 | + assert a is not +a |
| 77 | + |
| 78 | + @testing.with_requires("numpy<1.16") |
| 79 | + def test_pos_array_full(self): |
| 80 | + self.check_array_op_full(operator.pos) |
| 81 | + |
| 82 | + def test_abs_array(self): |
| 83 | + self.check_array_op_full(operator.abs) |
| 84 | + |
| 85 | + @testing.for_all_dtypes(no_bool=True) |
| 86 | + @testing.numpy_cupy_allclose() |
| 87 | + def check_zerodim_op(self, op, xp, dtype): |
| 88 | + a = xp.array(-2).astype(dtype) |
| 89 | + return op(a) |
| 90 | + |
| 91 | + @testing.for_all_dtypes() |
| 92 | + @testing.numpy_cupy_allclose() |
| 93 | + def check_zerodim_op_full(self, op, xp, dtype): |
| 94 | + a = xp.array(-2).astype(dtype) |
| 95 | + return op(a) |
| 96 | + |
| 97 | + @testing.for_all_dtypes(no_bool=True) |
| 98 | + @testing.numpy_cupy_allclose() |
| 99 | + def test_neg_zerodim(self, xp, dtype): |
| 100 | + a = xp.array(-2).astype(dtype) |
| 101 | + return operator.neg(a) |
| 102 | + |
| 103 | + def test_pos_zerodim(self): |
| 104 | + self.check_zerodim_op(operator.pos) |
| 105 | + |
| 106 | + def test_abs_zerodim(self): |
| 107 | + self.check_zerodim_op_full(operator.abs) |
| 108 | + |
| 109 | + def test_abs_zerodim_full(self): |
| 110 | + self.check_zerodim_op_full(operator.abs) |
| 111 | + |
| 112 | + |
| 113 | +class TestArrayIntUnaryOp(unittest.TestCase): |
| 114 | + @testing.for_int_dtypes() |
| 115 | + @testing.numpy_cupy_allclose() |
| 116 | + def check_array_op(self, op, xp, dtype): |
| 117 | + a = testing.shaped_arange((2, 3), xp, dtype) |
| 118 | + return op(a) |
| 119 | + |
| 120 | + def test_invert_array(self): |
| 121 | + self.check_array_op(operator.invert) |
| 122 | + |
| 123 | + @testing.for_all_dtypes() |
| 124 | + @testing.numpy_cupy_allclose(accept_error=TypeError) |
| 125 | + def check_zerodim_op(self, op, xp, dtype): |
| 126 | + a = xp.array(-2).astype(dtype) |
| 127 | + return op(a) |
| 128 | + |
| 129 | + def test_invert_zerodim(self): |
| 130 | + self.check_zerodim_op(operator.invert) |
| 131 | + |
| 132 | + |
| 133 | +@testing.parameterize( |
| 134 | + *testing.product({"xp": [numpy, cupy], "shape": [(3, 2), (), (3, 0, 2)]}) |
| 135 | +) |
| 136 | +class TestBoolNeg(unittest.TestCase): |
| 137 | + def test_bool_neg(self): |
| 138 | + xp = self.xp |
| 139 | + if xp is numpy and not testing.numpy_satisfies(">=1.13.0"): |
| 140 | + raise unittest.SkipTest("NumPy<1.13.0") |
| 141 | + shape = self.shape |
| 142 | + x = testing.shaped_random(shape, xp, dtype=numpy.bool_) |
| 143 | + with pytest.raises(TypeError): |
| 144 | + -x |
0 commit comments