Skip to content

Commit f1f6f1c

Browse files
committed
Now with tests™ And better code.
1 parent 57b4310 commit f1f6f1c

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

pygpu/gpuarray.pyx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,14 +1658,12 @@ cdef class GpuArray:
16581658
return pygpu_as_ndarray(self)
16591659

16601660
def __bool__(self):
1661-
if self.ga.nd == 0:
1662-
return True
1663-
if self.ga.nd == 1:
1664-
if self.ga.dimensions[0] == 0:
1665-
return False
1666-
if self.ga.dimensions[0] == 1:
1667-
return bool(numpy.asarray(self))
1668-
raise ValueError('The truth value of a multi-element array is ambiguous')
1661+
if self.size == 0:
1662+
return False
1663+
elif self.size == 1:
1664+
return bool(numpy.asarray(self))
1665+
else:
1666+
raise ValueError('The truth value of a multi-element array is ambiguous')
16691667

16701668
def _empty_like_me(self, dtype=None, order='C'):
16711669
"""

pygpu/tests/test_gpu_ndarray.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ def test_hash():
4747
assert exc is not None
4848

4949

50+
def test_bool():
51+
for data in [numpy.empty((0, 33)), [[1]], [[0]], [], [0], [1], 0, 1]:
52+
assert bool(pygpu.asarray(data)) == bool(numpy.asarray(data))
53+
54+
5055
def test_transfer():
5156
for shp in [(), (5,), (6, 7), (4, 8, 9), (1, 8, 9)]:
5257
for dtype in dtypes_all:

0 commit comments

Comments
 (0)