From 2a30dbb095fdbfc82ba43cf381ec34e8a2343f50 Mon Sep 17 00:00:00 2001 From: Guilhem Saurel Date: Wed, 9 Oct 2019 16:01:52 +0200 Subject: [PATCH] [Python][Test] use unittest --- python/test/quick.py | 25 ++++--- python/test/test.py | 151 +++++++++++++++++++++++-------------------- 2 files changed, 97 insertions(+), 79 deletions(-) diff --git a/python/test/quick.py b/python/test/quick.py index 6e290b8..36d0413 100644 --- a/python/test/quick.py +++ b/python/test/quick.py @@ -1,13 +1,22 @@ -from libparametric_curves_pywrap import polynomial, curve_constraints, forcecurve, spline +import unittest from numpy import matrix -cc = curve_constraints() -cc.init_vel = matrix(range(3)).T +from libparametric_curves_pywrap import (curve_constraints, forcecurve, polynomial, spline) -fc = forcecurve() -sp = spline() -pn = polynomial(matrix('1 2 3;4 5 6;7 8 9')) -assert pn.min() == 0.0 -assert pn.max() == 1.0 +class ParametricCurvesQuickTests(unittest.TestCase): + def test_quick(self): + cc = curve_constraints() + cc.init_vel = matrix(range(3)).T + + forcecurve() + spline() + pn = polynomial(matrix('1 2 3;4 5 6;7 8 9')) + + self.assertEqual(pn.min(), 0.0) + self.assertEqual(pn.max(), 1.0) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/test/test.py b/python/test/test.py index 155f3af..4d7c115 100644 --- a/python/test/test.py +++ b/python/test/test.py @@ -1,76 +1,85 @@ +import unittest + from numpy import matrix from numpy.linalg import norm from spline import (bezier, bezier6, curve_constraints, exact_cubic, polynom, spline_deriv_constraint) -waypoints = matrix([[1., 2., 3.], [4., 5., 6.]]).transpose() -waypoints6 = matrix([[1., 2., 3., 7., 5., 5.], [4., 5., 6., 4., 5., 6.]]).transpose() -time_waypoints = matrix([0., 1.]) - -# testing bezier curve -a = bezier6(waypoints6) -a = bezier(waypoints, -1., 3.) - -assert (a.degree == a.nbWaypoints - 1) -a.min() -a.max() -a(0.4) -assert ((a.derivate(0.4, 0) == a(0.4)).all()) -a.derivate(0.4, 2) -a = a.compute_derivate(100) - -prim = a.compute_primitive(1) - -for i in range(10): - t = float(i) / 10. - assert (a(t) == prim.derivate(t, 1)).all() -assert (prim(0) == matrix([0., 0., 0.])).all() - -prim = a.compute_primitive(2) -for i in range(10): - t = float(i) / 10. - assert (a(t) == prim.derivate(t, 2)).all() -assert (prim(0) == matrix([0., 0., 0.])).all() - -# testing bezier with constraints -c = curve_constraints() -c.init_vel = matrix([0., 1., 1.]) -c.end_vel = matrix([0., 1., 1.]) -c.init_acc = matrix([0., 1., -1.]) -c.end_acc = matrix([0., 100., 1.]) - -a = bezier(waypoints, c) -assert norm(a.derivate(0, 1) - c.init_vel) < 1e-10 -assert norm(a.derivate(1, 2) - c.end_acc) < 1e-10 - -# testing polynom function -a = polynom(waypoints) -a = polynom(waypoints, -1., 3.) -a.min() -a.max() -a(0.4) -assert ((a.derivate(0.4, 0) == a(0.4)).all()) -a.derivate(0.4, 2) - -# testing exact_cubic function -a = exact_cubic(waypoints, time_waypoints) -a.min() -a.max() -a(0.4) -assert ((a.derivate(0.4, 0) == a(0.4)).all()) -a.derivate(0.4, 2) - -# testing spline_deriv_constraints -c = curve_constraints() -c.init_vel -c.end_vel -c.init_acc -c.end_acc - -c.init_vel = matrix([0., 1., 1.]) -c.end_vel = matrix([0., 1., 1.]) -c.init_acc = matrix([0., 1., 1.]) -c.end_acc = matrix([0., 1., 1.]) - -a = spline_deriv_constraint(waypoints, time_waypoints) -a = spline_deriv_constraint(waypoints, time_waypoints, c) + +class ParametricCurvesTests(unittest.TestCase): + def test_all(self): + waypoints = matrix([[1., 2., 3.], [4., 5., 6.]]).transpose() + waypoints6 = matrix([[1., 2., 3., 7., 5., 5.], [4., 5., 6., 4., 5., 6.]]).transpose() + time_waypoints = matrix([0., 1.]) + + # testing bezier curve + a = bezier6(waypoints6) + a = bezier(waypoints, -1., 3.) + + self.assertEqual(a.degree, a.nbWaypoints - 1) + a.min() + a.max() + a(0.4) + self.assertTrue((a.derivate(0.4, 0) == a(0.4)).all()) + a.derivate(0.4, 2) + a = a.compute_derivate(100) + + prim = a.compute_primitive(1) + + for i in range(10): + t = float(i) / 10. + self.assertTrue((a(t) == prim.derivate(t, 1)).all()) + self.assertTrue((prim(0) == matrix([0., 0., 0.])).all()) + + prim = a.compute_primitive(2) + for i in range(10): + t = float(i) / 10. + self.assertTrue((a(t) == prim.derivate(t, 2)).all()) + self.assertTrue((prim(0) == matrix([0., 0., 0.])).all()) + + # testing bezier with constraints + c = curve_constraints() + c.init_vel = matrix([0., 1., 1.]) + c.end_vel = matrix([0., 1., 1.]) + c.init_acc = matrix([0., 1., -1.]) + c.end_acc = matrix([0., 100., 1.]) + + a = bezier(waypoints, c) + self.assertLess(norm(a.derivate(0, 1) - c.init_vel), 1e-10) + self.assertLess(norm(a.derivate(1, 2) - c.end_acc), 1e-10) + + # testing polynom function + a = polynom(waypoints) + a = polynom(waypoints, -1., 3.) + a.min() + a.max() + a(0.4) + self.assertTrue(((a.derivate(0.4, 0) == a(0.4)).all())) + a.derivate(0.4, 2) + + # testing exact_cubic function + a = exact_cubic(waypoints, time_waypoints) + a.min() + a.max() + a(0.4) + self.assertTrue(((a.derivate(0.4, 0) == a(0.4)).all())) + a.derivate(0.4, 2) + + # testing spline_deriv_constraints + c = curve_constraints() + c.init_vel + c.end_vel + c.init_acc + c.end_acc + + c.init_vel = matrix([0., 1., 1.]) + c.end_vel = matrix([0., 1., 1.]) + c.init_acc = matrix([0., 1., 1.]) + c.end_acc = matrix([0., 1., 1.]) + + a = spline_deriv_constraint(waypoints, time_waypoints) + a = spline_deriv_constraint(waypoints, time_waypoints, c) + + +if __name__ == '__main__': + unittest.main()