Skip to content

Commit 1f1bc35

Browse files
committed
TST: Add tests for np.nonzero with different input types
1 parent 617fe1c commit 1f1bc35

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

numpy/_core/tests/test_numeric.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,30 @@ def test_sparse(self):
17101710
assert_equal(np.nonzero(c)[0],
17111711
np.concatenate((np.arange(10 + i, 20 + i), [20 + i*2])))
17121712

1713+
def test_nonzero_dtypes(self):
1714+
rng = np.random.default_rng(seed = 10)
1715+
zero_indices = np.arange(50)
1716+
sample = ((2**33)*rng.normal(size=100))
1717+
1718+
# test for different dtypes
1719+
types = [bool, np.int8, np.int16, np.int32, np.int64, np.float32, np.float64]
1720+
for dtype in types:
1721+
x = sample.astype(dtype)
1722+
rng.shuffle(zero_indices)
1723+
x[zero_indices] = 0
1724+
idxs = np.nonzero(x)[0]
1725+
assert_equal(np.array_equal(np.where(x != 0)[0], idxs), True)
1726+
1727+
unsigned_types = [np.uint8, np.uint16, np.uint32, np.uint64]
1728+
sample = rng.integers(0, 255, size=100)
1729+
for dtype in unsigned_types:
1730+
x = sample.astype(dtype)
1731+
rng.shuffle(zero_indices)
1732+
x[zero_indices] = 0
1733+
idxs = np.nonzero(x)[0]
1734+
assert_equal(np.array_equal(np.where(x != 0)[0], idxs), True)
1735+
1736+
17131737
def test_return_type(self):
17141738
class C(np.ndarray):
17151739
pass

0 commit comments

Comments
 (0)