Skip to content

Commit c8d7188

Browse files
committed
Reject unsupported ndarray ranks in isvector
1 parent 2de7801 commit c8d7188

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

‎spatialmath/base/argcheck.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,8 @@ def isvector(v: Any, dim: Optional[int] = None) -> bool:
510510
if dim is None:
511511
return (
512512
(len(s) == 1 and s[0] > 0)
513-
or (s[0] == 1 and s[1] > 0)
514-
or (s[0] > 0 and s[1] == 1)
513+
or (len(s) == 2 and s[0] == 1 and s[1] > 0)
514+
or (len(s) == 2 and s[0] > 0 and s[1] == 1)
515515
)
516516
else:
517517
return s == (dim,) or s == (1, dim) or s == (dim, 1)

‎tests/base/test_argcheck.py‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import numpy.testing as nt
1212

1313
from spatialmath.base.argcheck import *
14+
from spatialmath.base.transformsNd import e2h, h2e
1415

1516

1617
class Test_check(unittest.TestCase):
@@ -212,10 +213,30 @@ def test_isvector(self):
212213
self.assertFalse(isvector(np.array([[1, 2, 3]]), 4))
213214
self.assertFalse(isvector(np.array([[1], [2], [3]]), 4))
214215

215-
def test_isvector(self):
216+
def test_assertvector(self):
216217
l = [1, 2, 3]
217218
nt.assert_raises(ValueError, assertvector, l, 4)
218219

220+
def test_isvector_unsupported_array_shapes(self):
221+
for shape in [(), (2, 3), (1, 3, 2), (3, 1, 2), (1, 1, 1), (1, 3, 0)]:
222+
with self.subTest(shape=shape):
223+
value = np.ones(shape)
224+
self.assertFalse(isvector(value))
225+
with self.assertRaises(ValueError):
226+
assertvector(value)
227+
228+
def test_isvector_empty_arrays(self):
229+
for shape in [(0,), (1, 0), (0, 1), (0, 0)]:
230+
with self.subTest(shape=shape):
231+
self.assertFalse(isvector(np.empty(shape)))
232+
233+
def test_homogeneous_conversion_unsupported_array_shapes(self):
234+
for convert in [e2h, h2e]:
235+
for shape in [(), (1, 3, 2), (3, 1, 2), (1, 1, 1)]:
236+
with self.subTest(convert=convert.__name__, shape=shape):
237+
with self.assertRaises(ValueError):
238+
convert(np.ones(shape))
239+
219240
def test_getvector(self):
220241
l = [1, 2, 3]
221242
t = (1, 2, 3)

0 commit comments

Comments
 (0)