diff --git a/dpnp/dpnp_array.py b/dpnp/dpnp_array.py index a60464583cab..473e08c83fc8 100644 --- a/dpnp/dpnp_array.py +++ b/dpnp/dpnp_array.py @@ -253,7 +253,10 @@ def __radd__(self, other): # '__rdivmod__', # '__reduce__', # '__reduce_ex__', - # '__repr__', + + def __repr__(self): + return dpt.usm_ndarray_repr(self._array_obj, prefix="array") + # '__rfloordiv__', # '__rlshift__', @@ -298,8 +301,7 @@ def __str__(self): """ - return str(self.asnumpy()) - + return self._array_obj.__str__() def __sub__(self, other): return dpnp.subtract(self, other) diff --git a/tests/test_dparray.py b/tests/test_dparray.py index 50eaa2e46eb8..62a0120f8a33 100644 --- a/tests/test_dparray.py +++ b/tests/test_dparray.py @@ -69,6 +69,111 @@ def test_flags_strides(dtype, order, strides): assert numpy_array.flags.c_contiguous == dpnp_array.flags.c_contiguous assert numpy_array.flags.f_contiguous == dpnp_array.flags.f_contiguous +def test_print_dpnp_int(): + result = repr(dpnp.array([1, 0, 2, -3, -1, 2, 21, -9], dtype='i4')) + expected = "array([ 1, 0, 2, -3, -1, 2, 21, -9], dtype=int32)" + assert(result==expected) + + result = str(dpnp.array([1, 0, 2, -3, -1, 2, 21, -9], dtype='i4')) + expected = "[ 1 0 2 -3 -1 2 21 -9]" + assert(result==expected) +# int32 + result = repr(dpnp.array([1, -1, 21], dtype=dpnp.int32)) + expected = "array([ 1, -1, 21], dtype=int32)" + assert(result==expected) + + result = str(dpnp.array([1, -1, 21], dtype=dpnp.int32)) + expected = "[ 1 -1 21]" + assert(result==expected) +# uint8 + result = repr(dpnp.array([1, 0, 3], dtype=numpy.uint8)) + expected = "array([1, 0, 3], dtype=uint8)" + assert(result==expected) + + result = str(dpnp.array([1, 0, 3], dtype=numpy.uint8)) + expected = "[1 0 3]" + assert(result==expected) + +def test_print_dpnp_float(): + result = repr(dpnp.array([1, -1, 21], dtype=float)) + expected = "array([ 1., -1., 21.])" + assert(result==expected) + + result = str(dpnp.array([1, -1, 21], dtype=float)) + expected = "[ 1. -1. 21.]" + assert(result==expected) +# float32 + result = repr(dpnp.array([1, -1, 21], dtype=dpnp.float32)) + expected = "array([ 1., -1., 21.], dtype=float32)" + assert(result==expected) + + result = str(dpnp.array([1, -1, 21], dtype=dpnp.float32)) + expected = "[ 1. -1. 21.]" + assert(result==expected) + +def test_print_dpnp_complex(): + result = repr(dpnp.array([1, -1, 21], dtype=complex)) + expected = "array([ 1.+0.j, -1.+0.j, 21.+0.j])" + assert(result==expected) + + result = str(dpnp.array([1, -1, 21], dtype=complex)) + expected = "[ 1.+0.j -1.+0.j 21.+0.j]" + assert(result==expected) + +def test_print_dpnp_boolean(): + result = repr(dpnp.array([1, 0, 3], dtype=bool)) + expected = "array([ True, False, True])" + assert(result==expected) + + result = str(dpnp.array([1, 0, 3], dtype=bool)) + expected = "[ True False True]" + assert(result==expected) + +def test_print_dpnp_special_character(): +# NaN + result = repr(dpnp.array([1., 0., dpnp.nan, 3.])) + expected = "array([ 1., 0., nan, 3.])" + assert(result==expected) + + result = str(dpnp.array([1., 0., dpnp.nan, 3.])) + expected = "[ 1. 0. nan 3.]" + assert(result==expected) +# inf + result = repr(dpnp.array([1., 0., numpy.inf, 3.])) + expected = "array([ 1., 0., inf, 3.])" + assert(result==expected) + + result = str(dpnp.array([1., 0., numpy.inf, 3.])) + expected = "[ 1. 0. inf 3.]" + assert(result==expected) + +def test_print_dpnp_nd(): +# 1D + result = repr(dpnp.arange(10000, dtype='float32')) + expected = "array([0.000e+00, 1.000e+00, 2.000e+00, ..., 9.997e+03, 9.998e+03,\n 9.999e+03], dtype=float32)" + assert(result==expected) + + result = str(dpnp.arange(10000, dtype='float32')) + expected = "[0.000e+00 1.000e+00 2.000e+00 ... 9.997e+03 9.998e+03 9.999e+03]" + assert(result==expected) + +# 2D + result = repr(dpnp.array([[1, 2], [3, 4]], dtype=float)) + expected = "array([[1., 2.],\n [3., 4.]])" + assert(result==expected) + + result = str(dpnp.array([[1, 2], [3, 4]])) + expected = "[[1 2]\n [3 4]]" + assert(result==expected) + +# 0 shape + result = repr(dpnp.empty( shape=(0, 0) )) + expected = "array([])" + assert(result==expected) + + result = str(dpnp.empty( shape=(0, 0) )) + expected = "[]" + assert(result==expected) @pytest.mark.parametrize("func", [bool, float, int, complex]) @pytest.mark.parametrize("shape", [tuple(), (1,), (1, 1), (1, 1, 1)])