Repository: numpy/numpy
File: numpy/lib/tests/test_function_base.py (Python)

Code before:

        a_trim1 = np.zeros((4, 2))
        a_trim1[1:3, :] = 1

        a_trim_both = np.ones((2, 2))

        res = trim_zeros(a, axis=())
        assert_array_equal(res, a)

        res = trim_zeros(a, axis=0)
        assert_array_equal(res, a_trim0)

        res = trim_zeros(a, axis=1)
        assert_array_equal(res, a_trim1)

        res = trim_zeros(a, axis=(-2,))
        assert_array_equal(res, a_trim0)

        res = trim_zeros(a, axis=(-1,))
        assert_array_equal(res, a_trim1)

        res = trim_zeros(a, axis=(0, -1))
        assert_array_equal(res, a_trim_both)

        res = trim_zeros(a, axis=None)
        assert_array_equal(res, a_trim_both)


class TestExtins:

    def test_basic(self):
        a = np.array([1, 3, 2, 1, 2, 3, 3])
        b = extract(a > 1, a)
        assert_array_equal(b, [3, 2, 2, 3, 3])

    def test_place(self):
        # Make sure that non-np.ndarray objects
        # raise an error instead of doing nothing
        assert_raises(TypeError, place, [1, 2, 3], [True, False], [0, 1])

        a = np.array([1, 4, 3, 2, 5, 8, 7])
        place(a, [0, 1, 0, 1, 0, 1, 0], [2, 4, 6])
        assert_array_equal(a, [1, 2, 3, 4, 5, 6, 7])

        place(a, np.zeros(7), [])
        assert_array_equal(a, np.arange(1, 8))

        place(a, [1, 0, 1, 0, 1, 0, 1], [8, 9])
        assert_array_equal(a, [8, 2, 9, 4, 8, 6, 9])
        assert_raises_regex(ValueError, "Cannot insert from an empty array",
                            lambda: place(a, [0, 0, 0, 0, 0, 1, 0], []))

Review comment (suggestion):
This is also a behavior change. I'm not so sure about it. I fear that there might be code relying on this behavior:

```
>>> res = trim_zeros(a, axis=None)
>>> a
array([[0., 0., 0., 0.],
       [0., 1., 1., 0.],
       [0., 1., 1., 0.],
       [0., 0., 0., 0.]])
```

Unless I'm misinterpreting or made a mistake somewhere...

Code after:
    def test_allzero(self, ndim):
        a = np.zeros((3,) * ndim)
        res = trim_zeros(a, axis=None)
        assert_array_equal(res, np.zeros((0,) * ndim))

    def test_trim_arg(self):
        a = np.array([0, 1, 2, 0])

        res = trim_zeros(a, trim='f')
        assert_array_equal(res, [1, 2, 0])

        res = trim_zeros(a, trim='b')
        assert_array_equal(res, [0, 1, 2])

    @pytest.mark.parametrize("trim", ("front", ""))
    def test_unexpected_trim_value(self, trim):
        arr = self.a
        with pytest.raises(ValueError, match=r"unexpected character\(s\) in `trim`"):
            trim_zeros(arr, trim=trim)

    @pytest.mark.parametrize("shape, axis", [
        [(5,), None],
        [(5,), ()],
        [(5,), 0],
        [(5, 6), None],
        [(5, 6), ()],
        [(5, 6), 0],
        [(5, 6), (-1,)],
        [(5, 6, 7), None],
        [(5, 6, 7), ()],
        [(5, 6, 7), 1],
        [(5, 6, 7), (0, 2)],
        [(5, 6, 7, 8), None],
        [(5, 6, 7, 8), ()],
        [(5, 6, 7, 8), -2],
        [(5, 6, 7, 8), (0, 1, 3)],
    ])
    @pytest.mark.parametrize("trim", ['fb', 'f', 'b'])
    def test_multiple_axes(self, shape, axis, trim):
        rng = np.random.default_rng(4321)
        data, expected = self.construct_input_output(rng, shape, axis, trim)
        assert_array_equal(trim_zeros(data, axis=axis, trim=trim), expected)


class TestExtins:

    def test_basic(self):
        a = np.array([1, 3, 2, 1, 2, 3, 3])
        b = extract(a > 1, a)
        assert_array_equal(b, [3, 2, 2, 3, 3])