8000 add string tests · flyinger/spatialmath-python@6af193e · GitHub
[go: up one dir, main page]

Skip to content

Commit 6af193e

Browse files
committed
add string tests
add SO3, SE3 tests
1 parent 851569b commit 6af193e

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

spatialmath/test_quaternion.py

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
def qcompare(x, y):
1212
if isinstance(x, Quaternion):
1313
x = x.vec
14+
elif isinstance(x, SMPose):
15+
x = x.A
1416
if isinstance(y, Quaternion):
1517
y = y.vec
18+
elif isinstance(y, SMPose):
19+
y = y.A
1620
nt.assert_array_almost_equal(x, y)
1721

1822
# straight port of the MATLAB unit tests
@@ -120,13 +124,23 @@ def test_concat(self):
120124
self.assertIsInstance(uu, UnitQuaternion)
121125
self.assertEqual(len(uu), 4)
122126

123-
def test_convert_string(self):
124-
# char
127+
def test_string(self):
125128

126129
u = UnitQuaternion()
127130

128131
s = str(u)
129132
self.assertIsInstance(s, str)
133+
self.assertTrue(s.endswith(' >>'))
134+
self.assertEqual(s.count('\n'), 0)
135+
136+
q = UnitQuaternion.Rx([0.3, 0.4, 0.5])
137+
s = str(q)
138+
self.assertIsInstance(s, str)
139+
self.assertEqual(s.count('\n'), 2)
140+
141+
def test_properties(self):
142+
143+
u = UnitQuaternion()
130144

131145
# s,v
132146
nt.assert_array_almost_equal(UnitQuaternion([1, 0, 0, 0]).s, 1)
@@ -142,17 +156,20 @@ def test_convert_string(self):
142156
nt.assert_array_almost_equal(UnitQuaternion([0, 0, 0, 1]).v, [0, 0, 1])
143157

144158
# R,T
145-
nt.assert_array_almost_equal(u.R, eye(3))
159+
nt.assert_array_almost_equal(u.R, np.eye(3))
146160

147161
nt.assert_array_almost_equal(UnitQuaternion(rotx(pi / 2)).R, rotx(pi / 2))
148162
nt.assert_array_almost_equal(UnitQuaternion(roty(-pi / 2)).R, roty(-pi / 2))
149163
nt.assert_array_almost_equal(UnitQuaternion(rotz(pi)).R, rotz(pi))
150164

151-
nt.assert_array_almost_equal(UnitQuaternion(rotx(pi / 2)).T, trotx(pi / 2))
152-
nt.assert_array_almost_equal(UnitQuaternion(roty(-pi / 2)).T, troty(-pi / 2))
153-
nt.assert_array_almost_equal(UnitQuaternion(rotz(pi)).T, trotz(pi))
165+
qcompare(UnitQuaternion(rotx(pi / 2)).SO3(), SO3.Rx(pi / 2))
166+
qcompare(UnitQuaternion(roty(-pi / 2)).SO3(), SO3.Ry(-pi / 2))
167+
qcompare(UnitQuaternion(rotz(pi)).SO3(), SO3.Rz(pi))
168+
169+
qcompare(UnitQuaternion(rotx(pi / 2)).SE3(), SE3.Rx(pi / 2))
170+
qcompare(UnitQuaternion(roty(-pi / 2)).SE3(), SE3.Ry(-pi / 2))
171+
qcompare(UnitQuaternion(rotz(pi)).SE3(), SE3.Rz(pi))
154172

155-
nt.assert_array_almost_equal(UnitQuaternion.q2r(u.vec), eye(3, 3))
156173

157174
def test_staticconstructors(self):
158175
# rotation primitives
@@ -609,6 +626,21 @@ def test_constructor(self):
609626
# tc.verifyError( @() Quaternion(2), 'SMTB:Quaternion:badarg')
610627
# tc.verifyError( @() Quaternion([1, 2, 3]), 'SMTB:Quaternion:badarg')
611628

629+
def test_string(self):
630+
631+
u = Quaternion()
632+
633+
s = str(u)
634+
self.assertIsInstance(s, str)
635+
self.assertTrue(s.endswith(' >'))
636+
self.assertEqual(s.count('\n'), 0)
637+
self.assertEqual(len(s), 41)
638+
639+
q = Quaternion([u, u, u])
640+
s = str(q)
641+
self.assertIsInstance(s, str)
642+
self.assertEqual(s.count('\n'), 2)
643+
612644
def test_properties(self):
613645

614646
q = Quaternion([1, 2, 3, 4])
@@ -627,29 +659,7 @@ def log_test_exp(self):
627659
#nt.assert_array_almost_equal(log(exp(q1)), q1)
628660
#nt.assert_array_almost_equal(log(exp(q2)), q2)
629661

630-
def test_char(self):
631662

632-
# char
633-
q = Quaternion()
634-
635-
s = str(q)
636-
self.assertTrue(isinstance(s, str))
637-
self.assertEqual(len(s), 42)
638-
639-
s = str(Quaternion([q, q, q]))
640-
self.assertEqual(len(s), 126)
641-
self.assertEqual(s.count('\n'), 3)
642-
643-
# symbolic display
644-
# syms s x y z real
645-
# q = Quaternion([s x y z])
646-
# s = char( q )
647-
# tc.verifyTrue( ischar(s))
648-
# nt.assert_array_almost_equal(size(s,1), 1)
649-
650-
# s = char( [q q q] )
651-
# tc.verifyTrue( ischar(s))
652-
# nt.assert_array_almost_equal(size(s,1), 3)
653663

654664
def test_concat(self):
655665
u = Quaternion()

0 commit comments

Comments
 (0)
0