|
6 | 6 |
|
7 | 7 | import numpy
|
8 | 8 | from numpy.testing import (
|
9 |
| - assert_array_equal |
| 9 | + assert_, |
| 10 | + assert_array_equal, |
| 11 | + assert_equal |
10 | 12 | )
|
11 | 13 |
|
12 | 14 |
|
| 15 | +class TestIndexing: |
| 16 | + def test_ellipsis_index(self): |
| 17 | + a = dpnp.array([[1, 2, 3], |
| 18 | + [4, 5, 6], |
| 19 | + [7, 8, 9]]) |
| 20 | + assert_(a[...] is not a) |
| 21 | + assert_equal(a[...], a) |
| 22 | + |
| 23 | + # test that slicing with ellipsis doesn't skip an arbitrary number of dimensions |
| 24 | + assert_equal(a[0, ...], a[0]) |
| 25 | + assert_equal(a[0, ...], a[0,:]) |
| 26 | + assert_equal(a[..., 0], a[:, 0]) |
| 27 | + |
| 28 | + # test that slicing with ellipsis always results in an array |
| 29 | + assert_equal(a[0, ..., 1], dpnp.array(2)) |
| 30 | + |
| 31 | + # assignment with `(Ellipsis,)` on 0-d arrays |
| 32 | + b = dpnp.array(1) |
| 33 | + b[(Ellipsis,)] = 2 |
| 34 | + assert_equal(b, 2) |
| 35 | + |
| 36 | + def test_boolean_indexing_list(self): |
| 37 | + a = dpnp.array([1, 2, 3]) |
| 38 | + b = dpnp.array([True, False, True]) |
| 39 | + |
| 40 | + assert_equal(a[b], [1, 3]) |
| 41 | + assert_equal(a[None, b], [[1, 3]]) |
| 42 | + |
| 43 | + def test_indexing_array_weird_strides(self): |
| 44 | + np_x = numpy.ones(10) |
| 45 | + dp_x = dpnp.ones(10) |
| 46 | + |
| 47 | + np_ind = numpy.arange(10)[:, None, None, None] |
| 48 | + np_ind = numpy.broadcast_to(np_ind, (10, 55, 4, 4)) |
| 49 | + |
| 50 | + dp_ind = dpnp.arange(10)[:, None, None, None] |
| 51 | + dp_ind = dpnp.broadcast_to(dp_ind, (10, 55, 4, 4)) |
| 52 | + |
| 53 | + # single advanced index case |
| 54 | + assert_array_equal(dp_x[dp_ind], np_x[np_ind]) |
| 55 | + |
| 56 | + np_x2 = numpy.ones((10, 2)) |
| 57 | + dp_x2 = dpnp.ones((10, 2)) |
| 58 | + |
| 59 | + np_zind = numpy.zeros(4, dtype=numpy.intp) |
| 60 | + dp_zind = dpnp.asarray(np_zind) |
| 61 | + |
| 62 | + # higher dimensional advanced index |
| 63 | + assert_array_equal(dp_x2[dp_ind, dp_zind], np_x2[np_ind, np_zind]) |
| 64 | + |
| 65 | + def test_indexing_array_negative_strides(self): |
| 66 | + arr = dpnp.zeros((4, 4))[::-1, ::-1] |
| 67 | + |
| 68 | + slices = (slice(None), dpnp.array([0, 1, 2, 3])) |
| 69 | + arr[slices] = 10 |
| 70 | + assert_array_equal(arr, 10.) |
| 71 | + |
| 72 | + |
13 | 73 | @pytest.mark.usefixtures("allow_fall_back_on_numpy")
|
14 | 74 | def test_choose():
|
15 | 75 | a = numpy.r_[:4]
|
|
0 commit comments