code: def test_np_ndarray_indexing():
      def np_int(index, int_type=_np.int32):
          """
          Helper function for testing indexing that converts slices to slices of ints or None, and tuples to
          tuples of ints or None.
          """
  
          def convert(num):
              if num is None:
                  return num
              else:
                  return int_type(num)
  
          if isinstance(index, slice):
              return slice(
                  convert(index.start),
                  convert(index.stop),
                  convert(index.step),
              )
          elif isinstance(index, tuple):  # tuple of slices and integers
              ret = []
              for elem in index:
                  if isinstance(elem, slice):
                      ret.append(
                          slice(
                              convert(elem.start),
                              convert(elem.stop),
                              convert(elem.step),
                          )
                      )
                  else:
                      ret.append(convert(elem))
              return tuple(ret)
          else:
              assert False
  
      # Copied from test_ndarray.py. Under construction.
      def test_getitem(np_array, index):
          np_index = index
          if (
              type(index) == mx.nd.NDArray
          ):  # use of NDArray is prohibited
              assert False
          if isinstance(index, np.ndarray):
              np_index = index.asnumpy()
          if isinstance(index, tuple):
              np_index = tuple(
                  [
                      idx.asnumpy()
                      if isinstance(idx, mx.nd.NDArray)
                      else idx
                      for idx in index
                  ]
              )
          np_indexed_array = np_array[np_index]
          mx_np_array = np.array(np_array, dtype=np_array.dtype)
          for autograd in [True, False]:
              try:
                  if autograd:
                      with mx.autograd.record():
                          mx_indexed_array = mx_np_array[index]
                  else:
                      mx_indexed_array = mx_np_array[index]
              except Exception as e:
                  print("Failed with index = {}".format(index))
                  raise e
              mx_indexed_array = mx_indexed_array.asnumpy()
              assert same(
                  np_indexed_array, mx_indexed_array
              ), "Failed with index = {}".format(index)
  
      def test_getitem_slice_bound():
          mx_array = np.arange(10)
          np_array = mx_array.asnumpy()
          assert_almost_equal(mx_array[100:], np_array[100:])
          assert_almost_equal(mx_array[:100], np_array[:100])
          assert_almost_equal(mx_array[-100:], np_array[-100:])
          assert_almost_equal(mx_array[:-100], np_array[:-100])
  
          mx_array = np.arange(81).reshape(3, 3, 3, 3)
          np_array = mx_array.asnumpy()
          assert_almost_equal(mx_array[100:], np_array[100:])
          assert_almost_equal(mx_array[:100], np_array[:100])
          assert_almost_equal(mx_array[-100:], np_array[-100:])
          assert_almost_equal(mx_array[:-100], np_array[:-100])
  
      def test_setitem(np_array, index):
          def assert_same(
              np_array,
              np_index,
              mx_array,
              mx_index,
              mx_value,
              np_value=None,
          ):
              if np_value is not None:
                  np_array[np_index] = np_value
              elif isinstance(mx_value, np.ndarray):
                  np_array[np_index] = mx_value.asnumpy()
              else:
                  np_array[np_index] = mx_value
              try:
                  mx_array[mx_index] = mx_value
              except Exception as e:
                  print(
                      "Failed with index = {}, value.shape = {}".format(
                          mx_index, mx_value.shape
                      )
                  )
                  raise e
  
              assert same(np_array, mx_array.asnumpy())
  
          def _is_basic_index(index):
              if isinstance(index, (integer_types, py_slice)):
                  return True
              if isinstance(index, tuple) and all(
                  isinstance(i, (integer_types, py_slice))
                  for i in index
              ):
                  return True
              return False
  
          np_index = index  # keep this native numpy type
          if isinstance(index, np.ndarray):
              np_index = index.asnumpy()
          if isinstance(index, tuple):
              np_index = []
              for idx in index:
                  if isinstance(idx, np.ndarray):
                      np_index.append(idx.asnumpy())
                  else:
                      np_index.append(idx)
              np_index = tuple(np_index)
  
          mx_array = np.array(
              np_array, dtype=np_array.dtype
          )  # mxnet.np.ndarray
          np_array = mx_array.asnumpy()  # native numpy array
          indexed_array_shape = np_array[np_index].shape
          np_indexed_array = _np.random.randint(
              low=-10000, high=0, size=indexed_array_shape
          )
          # test value is a native numpy array without broadcast
          assert_same(
              np_array, np_index, mx_array, index, np_indexed_array
          )
          # test value is a list without broadcast
          assert_same(
              np_array,
              np_index,
              mx_array,
              index,
              np_indexed_array.tolist(),
          )
          # test value is a mxnet numpy array without broadcast
          assert_same(
              np_array,
              np_index,
              mx_array,
              index,
              np.array(np_indexed_array),
          )
          # test value is an numeric_type
          assert_same(
              np_array,
              np_index,
              mx_array,
              index,
              _np.random.randint(low=-10000, high=0),
          )
  
          np_value = _np.random.randint(
              low=-10000,
              high=0,
              size=(indexed_array_shape[-1],)
              if len(indexed_array_shape) > 0
              else (),
          )
          # test mxnet ndarray with broadcast
          assert_same(
              np_array, np_index, mx_array, index, np.array(np_value)
          )
          # test native numpy array with broadcast
          assert_same(np_array, np_index, mx_array, index, np_value)
          # test python list with broadcast
          assert_same(
              np_array, np_index, mx_array, index, np_value.tolist()
          )
  
          # test value shape are expanded to be longer than index array's shape
          # this is currently only supported in basic indexing
          if _is_basic_index(index):
              expanded_value_shape = (1, 1) + np_value.shape
              assert_same(
                  np_array,
                  np_index,
                  mx_array,
                  index,
                  np.array(np_value.reshape(expanded_value_shape)),
              )
              assert_same(
                  np_array,
                  np_index,
                  mx_array,
                  index,
                  np_value.reshape(expanded_value_shape),
              )
              if len(expanded_value_shape) <= np_array[index].ndim:
                  # NumPy does not allow value.ndim > np_array[index].ndim when value is a python list.
                  # It may be a bug of NumPy.
                  assert_same(
                      np_array,
                      np_index,
                      mx_array,
                      index,
                      np_value.reshape(expanded_value_shape).tolist(),
                  )
  
          # test list with broadcast
          assert_same(
              np_array,
              np_index,
              mx_array,
              index,
              [_np.random.randint(low=-10000, high=0)]
              * indexed_array_shape[-1]
              if len(indexed_array_shape) > 0
              else _np.random.randint(low=-10000, high=0),
          )
  
      def test_getitem_autograd(np_array, index):
          """
          np_array: native numpy array.
          """
          x = np.array(np_array, dtype=np_array.dtype)
          x.attach_grad()
          with mx.autograd.record():
              y = x[index]
          y.backward()
          value = np.ones_like(y)
          x_grad = np.zeros_like(x)
          x_grad[index] = value
          assert same(x_grad.asnumpy(), x.grad.asnumpy())
  
      def test_setitem_autograd(np_array, index):
          """
          np_array: native numpy array.
          """
          x = np.array(np_array, dtype=np_array.dtype)
          out_shape = x[index].shape
          y = np.array(_np.random.uniform(size=out_shape))
          y.attach_grad()
          try:
              with mx.autograd.record():
                  x[index] = y
                  x.backward()
                  y_grad = np.ones_like(y)
                  assert same(y_grad.asnumpy(), y.grad.asnumpy())
          except mx.base.MXNetError as err:
              assert (
                  str(err).find(
                      "Inplace operations (+=, -=, x[:]=, etc) are not supported when recording with"
                  )
                  != -1
              )
  
      shape = (8, 16, 9, 9)
      np_array = _np.arange(
          _np.prod(_np.array(shape)), dtype="int32"
      ).reshape(
          shape
      )  # native np array
  
      # Test sliced output being ndarray:
      index_list = [
          (),
          # Basic indexing
          # Single int as index
          0,
          _np.int32(0),
          _np.int64(0),
          np.array(0, dtype="int32"),
          np.array(0, dtype="int64"),
          5,
          _np.int32(5),
          _np.int64(5),
          np.array(5, dtype="int32"),
          np.array(5, dtype="int64"),
          -1,
          _np.int32(-1),
          _np.int64(-1),
          np.array(-1, dtype="int32"),
          np.array(-1, dtype="int64"),
          # Slicing as index
          slice(5),
          np_int(slice(5), _np.int32),
          np_int(slice(5), _np.int64),
          slice(1, 5),
          np_int(slice(1, 5), _np.int32),
          np_int(slice(1, 5), _np.int64),
          slice(1, 5, 2),
          slice(1, 2, 2),
          np_int(slice(1, 5, 2), _np.int32),
          np_int(slice(1, 5, 2), _np.int64),
          slice(7, 0, -1),
          np_int(slice(7, 0, -1)),
          np_int(slice(7, 0, -1), _np.int64),
          slice(None, 6),
          np_int(slice(None, 6)),
          np_int(slice(None, 6), _np.int64),
          slice(None, 6, 3),
          np_int(slice(None, 6, 3)),
          np_int(slice(None, 6, 3), _np.int64),
          slice(1, None),
          np_int(slice(1, None)),
          np_int(slice(1, None), _np.int64),
          slice(1, None, 3),
          np_int(slice(1, None, 3)),
          np_int(slice(1, None, 3), _np.int64),
          slice(None, None, 2),
          np_int(slice(None, None, 2)),
          np_int(slice(None, None, 2), _np.int64),
          slice(None, None, -1),
          np_int(slice(None, None, -1)),
          np_int(slice(None, None, -1), _np.int64),
          slice(None, None, -2),
          np_int(slice(None, None, -2), _np.int32),
          np_int(slice(None, None, -2), _np.int64),
          # Multiple ints as indices
          (1, 2, 3),
          np_int((1, 2, 3)),
          np_int((1, 2, 3), _np.int64),
          (-1, -2, -3),
          np_int((-1, -2, -3)),
          np_int((-1, -2, -3), _np.int64),
          (1, 2, 3, 4),
          np_int((1, 2, 3, 4)),
          np_int((1, 2, 3, 4), _np.int64),
          (-4, -3, -2, -1),
          (-4, mx.np.array(-3, dtype="int32"), -2, -1),
          (-4, mx.np.array(-3, dtype="int64"), -2, -1),
          np_int((-4, -3, -2, -1)),
          np_int((-4, -3, -2, -1), _np.int64),
          # slice(None) as indices
          (slice(None), slice(None), 1, 8),
          (slice(None), slice(None), np.array(1, dtype="int32"), 8),
          (slice(None), slice(None), np.array(1, dtype="int64"), 8),
          (slice(None), slice(None), -1, 8),
          (slice(None), slice(None), 1, -8),
          (slice(None), slice(None), -1, -8),
          np_int((slice(None), slice(None), 1, 8)),
          np_int((slice(None), slice(None), 1, 8), _np.int64),
          (slice(None), slice(None), 1, 8),
          np_int((slice(None), slice(None), -1, -8)),
          np_int((slice(None), slice(None), -1, -8), _np.int64),
          (slice(None), 2, slice(1, 5), 1),
          np_int((slice(None), 2, slice(1, 5), 1)),
          np_int((slice(None), 2, slice(1, 5), 1), _np.int64),
          # Mixture of ints and slices as indices
          (slice(None, None, -1), 2, slice(1, 5), 1),
          np_int((slice(None, None, -1), 2, slice(1, 5), 1)),
          np_int((slice(None, None, -1), 2, slice(1, 5), 1), _np.int64),
          (slice(None, None, -1), 2, slice(1, 7, 2), 1),
          np_int((slice(None, None, -1), 2, slice(1, 7, 2), 1)),
          np_int(
              (slice(None, None, -1), 2, slice(1, 7, 2), 1), _np.int64
          ),
          (
              slice(1, 8, 2),
              slice(14, 2, -2),
              slice(3, 8),
              slice(0, 7, 3),
          ),
          np_int(
              (
                  slice(1, 8, 2),
                  slice(14, 2, -2),
                  slice(3, 8),
                  slice(0, 7, 3),
              )
          ),
          np_int(
              (
                  slice(1, 8, 2),
                  slice(14, 2, -2),
                  slice(3, 8),
                  slice(0, 7, 3),
              ),
              _np.int64,
          ),
          (slice(1, 8, 2), 1, slice(3, 8), 2),
          np_int((slice(1, 8, 2), 1, slice(3, 8), 2)),
          np_int((slice(1, 8, 2), 1, slice(3, 8), 2), _np.int64),
          # Test Ellipsis ('...')
          (1, Ellipsis, -1),
          (slice(2), Ellipsis, None, 0),
          # Test newaxis
          None,
          (1, None, -2, 3, -4),
          (1, slice(2, 5), None),
          (slice(None), slice(1, 4), None, slice(2, 3)),
          (slice(1, 3), slice(1, 3), slice(1, 3), slice(1, 3), None),
          (slice(1, 3), slice(1, 3), None, slice(1, 3), slice(1, 3)),
          (None, slice(1, 2), 3, None),
          (1, None, 2, 3, None, None, 4),
          # Advanced indexing
          ([1, 2], slice(3, 5), None, None, [3, 4]),
          (slice(None), slice(3, 5), None, None, [2, 3], [3, 4]),
          (slice(None), slice(3, 5), None, [2, 3], None, [3, 4]),
          (None, slice(None), slice(3, 5), [2, 3], None, [3, 4]),
          [1],
          [1, 2],
          [2, 1, 3],
          [7, 5, 0, 3, 6, 2, 1],
          np.array([6, 3], dtype=np.int32),
          np.array([[3, 4], [0, 6]], dtype=np.int32),
          np.array([[7, 3], [2, 6], [0, 5], [4, 1]], dtype=np.int32),
          np.array([[7, 3], [2, 6], [0, 5], [4, 1]], dtype=np.int64),
          np.array([[2], [0], [1]], dtype=np.int32),
          np.array([[2], [0], [1]], dtype=np.int64),
          np.array([4, 7], dtype=np.int32),
          np.array([4, 7], dtype=np.int64),
          np.array([[3, 6], [2, 1]], dtype=np.int32),
          np.array([[3, 6], [2, 1]], dtype=np.int64),
          np.array([[7, 3], [2, 6], [0, 5], [4, 1]], dtype=np.int32),
          np.array([[7, 3], [2, 6], [0, 5], [4, 1]], dtype=np.int64),
          (1, [2, 3]),
          (1, [2, 3], np.array([[3], [0]], dtype=np.int32)),
          (1, [2, 3]),
          (1, [2, 3], np.array([[3], [0]], dtype=np.int64)),
          (1, [2], np.array([[5], [3]], dtype=np.int32), slice(None)),
          (1, [2], np.array([[5], [3]], dtype=np.int64), slice(None)),
          (
              1,
              [2, 3],
              np.array([[6], [0]], dtype=np.int32),
              slice(2, 5),
          ),
          (
              1,
              [2, 3],
              np.array([[6], [0]], dtype=np.int64),
              slice(2, 5),
          ),
          (
              1,
              [2, 3],
              np.array([[4], [7]], dtype=np.int32),
              slice(2, 5, 2),
          ),
          (
              1,
              [2, 3],
              np.array([[4], [7]], dtype=np.int64),
              slice(2, 5, 2),
          ),
          (
              1,
              [2],
              np.array([[3]], dtype=np.int32),
              slice(None, None, -1),
          ),
          (
              1,
              [2],
              np.array([[3]], dtype=np.int64),
              slice(None, None, -1),
          ),
          (
              1,
              [2],
              np.array([[3]], dtype=np.int32),
              np.array([[5, 7], [2, 4]], dtype=np.int64),
          ),
          (
              1,
              [2],
              np.array([[4]], dtype=np.int32),
              np.array([[1, 3], [5, 7]], dtype="int64"),
          ),
          [0],
          [0, 1],
          [1, 2, 3],
          [2, 0, 5, 6],
          ([1, 1], [2, 3]),
          ([1], [4], [5]),
          ([1], [4], [5], [6]),
          ([[1]], [[2]]),
          ([[1]], [[2]], [[3]], [[4]]),
          (slice(0, 2), [[1], [6]], slice(0, 2), slice(0, 5, 2)),
          ([[[[1]]]], [[1]], slice(0, 3), [1, 5]),
          ([[[[1]]]], 3, slice(0, 3), [1, 3]),
          ([[[[1]]]], 3, slice(0, 3), 0),
          ([[[[1]]]], [[2], [12]], slice(0, 3), slice(None)),
          ([1, 2], slice(3, 5), [2, 3], [3, 4]),
          ([1, 2], slice(3, 5), (2, 3), [3, 4]),
          range(4),
          range(3, 0, -1),
          (
              range(
                  4,
              ),
              [1],
          ),
          (1, 1, slice(None), 1),
          (1, 1, slice(None, 3), 1),
          (1, 1, slice(None, 8, 3), 1),
      ]
      for index in index_list:
          test_getitem(np_array, index)
          test_setitem(np_array, index)
          test_getitem_autograd(np_array, index)
          test_setitem_autograd(np_array, index)
  
      # Test indexing to zero-size tensors
      index_list = [
          (slice(0, 0), slice(0, 0), 1, 2),
          (slice(0, 0), slice(0, 0), slice(0, 0), slice(0, 0)),
      ]
      for index in index_list:
          test_getitem(np_array, index)
          test_setitem(np_array, index)
          test_getitem_autograd(np_array, index)
          test_setitem_autograd(np_array, index)
  
      # test zero-size tensors get and setitem
      shapes_indices = [
          ((0), [slice(None, None, None)]),
          (
              (3, 0),
              [
                  2,
                  (slice(None, None, None)),
                  (slice(None, None, None), None),
              ],
          ),
      ]
      for shape, indices in shapes_indices:
          np_array = _np.zeros(shape)
          for index in indices:
              test_getitem(np_array, index)
              test_setitem(np_array, index)
              test_getitem_autograd(np_array, index)
              test_setitem_autograd(np_array, index)
      test_getitem_slice_bound()
  
imports: from __future__ import absolute_import
  from __future__ import division
  import itertools
  import os
  import pytest
  import operator
  import numpy
  import mxnet
  from mxnet import np, npx, autograd
  from mxnet.gluon import HybridBlock
  from mxnet.test_utils import same, assert_almost_equal, rand_shape_nd, rand_ndarray, use_np
  from common import retry, TemporaryDirectory, xfail_when_nonstandard_decimal_separator
  from mxnet.test_utils import verify_generator, gen_buckets_probs_with_ppf, assert_exception, is_op_runnable, collapse_sum_like
  from mxnet.ndarray.ndarray import py_slice
  from mxnet.base import integer_types
label_desc: Use image node_id 12 for calling a global function with example usage: test_np_ndarray_indexing() and returns: slice, False, num, int_type, tuple, True, True