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

Code before:
        a = np.array([ThrowsAfter(5)] * 10)
        assert_raises(ValueError, np.nonzero, a)

        # raise exception in second pass for 1-dimensional loop
        a = np.array([ThrowsAfter(15)] * 10)
        assert_raises(ValueError, np.nonzero, a)

        # raise exception in second pass for n-dimensional loop
        a = np.array([[ThrowsAfter(15)]] * 10)
        assert_raises(ValueError, np.nonzero, a)

    def test_nonzero_byteorder(self):
        values = [0., -0., 1, float('nan'), 0, 1,
                  np.float16(0), np.float16(12.3)]
        expected = [0, 0, 1, 1, 0, 1, 0, 1]

        for value, expected in zip(values, expected):
            A = np.array([value])
            A_byteswapped = (A.view(A.dtype.newbyteorder()).byteswap()).copy()

            assert np.count_nonzero(A) == expected
            assert np.count_nonzero(A_byteswapped) == expected

    def test_count_nonzero_non_aligned_array(self):
        sz = 64
        b = np.zeros(64 + 1).view(np.int8)[1:-(np.intp(0).itemsize - 1)]
        b = b.view(np.float64)[:sz]
        b[...] = .1 + np.arange(sz)
        b[::2] = 0

        assert b.flags.aligned is False
        assert np.count_nonzero(b) == sz / 2
        print( b.flags.aligned, b.dtype)


class TestIndex:
    def test_boolean(self):
        a = rand(3, 5, 8)
        V = rand(5, 8)
        g1 = randint(0, 5, size=15)
        g2 = randint(0, 8, size=15)
        V[g1, g2] = -V[g1, g2]
        assert_((np.array([a[0][V > 0], a[1][V > 0], a[2][V > 0]]) == a[:, V > 0]).all())

    def test_boolean_edgecase(self):
        a = np.array([], dtype='int32')
        b = np.array([], dtype='bool')
        c = a[b]
        assert_equal(c, [])
        assert_equal(c.dtype, np.dtype('int32'))


Review comment (suggestion):
`intp` itemsize != float64 itemsize.  Just hardcode the 8/7 bytes, seems easier to read.  Can also create the `int8/uint8` array initially directly.

Code after:
        a = np.array([ThrowsAfter(5)] * 10)
        assert_raises(ValueError, np.nonzero, a)

        # raise exception in second pass for 1-dimensional loop
        a = np.array([ThrowsAfter(15)] * 10)
        assert_raises(ValueError, np.nonzero, a)

        # raise exception in second pass for n-dimensional loop
        a = np.array([[ThrowsAfter(15)]] * 10)
        assert_raises(ValueError, np.nonzero, a)

    def test_nonzero_byteorder(self):
        values = [0., -0., 1, float('nan'), 0, 1,
                  np.float16(0), np.float16(12.3)]
        expected = [0, 0, 1, 1, 0, 1, 0, 1]

        for value, expected in zip(values, expected):
            A = np.array([value])
            A_byteswapped = (A.view(A.dtype.newbyteorder()).byteswap()).copy()

            assert np.count_nonzero(A) == expected
            assert np.count_nonzero(A_byteswapped) == expected

    def test_count_nonzero_non_aligned_array(self):
        # gh-27523
        b = np.zeros(64 + 1, dtype=np.int8)[1:]
        b = b.view(int)
        b[:] = np.arange(b.size)
        b[::2] = 0
        assert b.flags.aligned is False
        assert np.count_nonzero(b) == b.size / 2

        b = np.zeros(64 + 1, dtype=np.float16)[1:]
        b = b.view(float)
        b[:] = np.arange(b.size)
        b[::2] = 0
        assert b.flags.aligned is False
        assert np.count_nonzero(b) == b.size / 2


class TestIndex:
    def test_boolean(self):
        a = rand(3, 5, 8)
        V = rand(5, 8)
        g1 = randint(0, 5, size=15)
        g2 = randint(0, 8, size=15)
        V[g1, g2] = -V[g1, g2]
        assert_((np.array([a[0][V > 0], a[1][V > 0], a[2][V > 0]]) == a[:, V > 0]).all())

    def test_boolean_edgecase(self):
        a = np.array([], dtype='int32')