Repository: numpy/numpy
File: numpy/_core/tests/test_multiarray.py (Python)

Code before:
        assert_c(a.copy('A'))

    @pytest.mark.parametrize("dtype", ['O', np.int32, 'i,O'])
    def test__deepcopy__(self, dtype):
        # Force the entry of NULLs into array
        a = np.empty(4, dtype=dtype)
        ctypes.memset(a.ctypes.data, 0, a.nbytes)

        # Ensure no error is raised, see gh-21833
        b = a.__deepcopy__({})

        a[0] = 42
        with pytest.raises(AssertionError):
            assert_array_equal(a, b)

    def test__deepcopy___void_scalar(self, dtype):
        # see comments in gh-29643
        value = np.void('Rex', dtype=[('name', 'U10')])
        value_deepcopy = value.__deepcopy__(None)
        value[0] = None
        assert value_deepcopy[0] == 'Rex'

    @pytest.mark.parametrize("dtype", [np.int64, np.float32, np.float64])
    def test__deepcopy__scalar(self):
        # test optimization from gh-29656
        value = dtype(1.1)
        value_deepcopy = value.__deepcopy__(None)
        assert value is value_deepcopy

    def test__deepcopy__catches_failure(self):
        class MyObj:
            def __deepcopy__(self, *args, **kwargs):
                raise RuntimeError

        arr = np.array([1, MyObj(), 3], dtype='O')
        with pytest.raises(RuntimeError):
            arr.__deepcopy__({})

    def test_sort_order(self):
        # Test sorting an array with fields
        x1 = np.array([21, 32, 14])
        x2 = np.array(['my', 'first', 'name'])
        x3 = np.array([3.1, 4.5, 6.2])
        r = np.rec.fromarrays([x1, x2, x3], names='id,word,number')

        r.sort(order=['id'])
        assert_equal(r.id, np.array([14, 21, 32]))
        assert_equal(r.word, np.array(['name', 'my', 'first']))
        assert_equal(r.number, np.array([6.2, 3.1, 4.5]))

        r.sort(order=['word'])

Review comment (suggestion):
```suggestion
    @pytest.mark.parametrize("sctype", [np.int64, np.float32, np.float64])
    def test__deepcopy__scalar(self, sctype):
        # test optimization from gh-29656
        value = sctype(1.1)
```

Code after:
        assert_c(a.copy('A'))

    @pytest.mark.parametrize("dtype", ['O', np.int32, 'i,O'])
    def test__deepcopy__(self, dtype):
        # Force the entry of NULLs into array
        a = np.empty(4, dtype=dtype)
        ctypes.memset(a.ctypes.data, 0, a.nbytes)

        # Ensure no error is raised, see gh-21833
        b = a.__deepcopy__({})

        a[0] = 42
        with pytest.raises(AssertionError):
            assert_array_equal(a, b)

    def test__deepcopy___void_scalar(self):
        # see comments in gh-29643
        value = np.void('Rex', dtype=[('name', 'U10')])
        value_deepcopy = value.__deepcopy__(None)
        value[0] = None
        assert value_deepcopy[0] == 'Rex'

    @pytest.mark.parametrize("sctype", [np.int64, np.float32, np.float64])
    def test__deepcopy__scalar(self, sctype):
        # test optimization from gh-29656
        value = sctype(1.1)
        value_deepcopy = value.__deepcopy__(None)
        assert value is value_deepcopy

    def test__deepcopy__catches_failure(self):
        class MyObj:
            def __deepcopy__(self, *args, **kwargs):
                raise RuntimeError

        arr = np.array([1, MyObj(), 3], dtype='O')
        with pytest.raises(RuntimeError):
            arr.__deepcopy__({})

    def test_sort_order(self):
        # Test sorting an array with fields
        x1 = np.array([21, 32, 14])
        x2 = np.array(['my', 'first', 'name'])
        x3 = np.array([3.1, 4.5, 6.2])
        r = np.rec.fromarrays([x1, x2, x3], names='id,word,number')

        r.sort(order=['id'])
        assert_equal(r.id, np.array([14, 21, 32]))
        assert_equal(r.word, np.array(['name', 'my', 'first']))
        assert_equal(r.number, np.array([6.2, 3.1, 4.5]))

        r.sort(order=['word'])