Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import polydrivetrain |
| 4 | import numpy |
| 5 | from numpy.testing import * |
| 6 | import polytope |
| 7 | import unittest |
| 8 | |
| 9 | __author__ = 'Austin Schuh (austin.linux@gmail.com)' |
| 10 | |
| 11 | |
| 12 | class TestVelocityDrivetrain(unittest.TestCase): |
| 13 | def MakeBox(self, x1_min, x1_max, x2_min, x2_max): |
| 14 | H = numpy.matrix([[1, 0], |
| 15 | [-1, 0], |
| 16 | [0, 1], |
| 17 | [0, -1]]) |
| 18 | K = numpy.matrix([[x1_max], |
| 19 | [-x1_min], |
| 20 | [x2_max], |
| 21 | [-x2_min]]) |
| 22 | return polytope.HPolytope(H, K) |
| 23 | |
| 24 | def test_coerce_inside(self): |
| 25 | """Tests coercion when the point is inside the box.""" |
| 26 | box = self.MakeBox(1, 2, 1, 2) |
| 27 | |
| 28 | # x1 = x2 |
| 29 | K = numpy.matrix([[1, -1]]) |
| 30 | w = 0 |
| 31 | |
| 32 | assert_array_equal(polydrivetrain.CoerceGoal(box, K, w, |
| 33 | numpy.matrix([[1.5], [1.5]])), |
| 34 | numpy.matrix([[1.5], [1.5]])) |
| 35 | |
| 36 | def test_coerce_outside_intersect(self): |
| 37 | """Tests coercion when the line intersects the box.""" |
| 38 | box = self.MakeBox(1, 2, 1, 2) |
| 39 | |
| 40 | # x1 = x2 |
| 41 | K = numpy.matrix([[1, -1]]) |
| 42 | w = 0 |
| 43 | |
| 44 | assert_array_equal(polydrivetrain.CoerceGoal(box, K, w, numpy.matrix([[5], [5]])), |
| 45 | numpy.matrix([[2.0], [2.0]])) |
| 46 | |
| 47 | def test_coerce_outside_no_intersect(self): |
| 48 | """Tests coercion when the line does not intersect the box.""" |
| 49 | box = self.MakeBox(3, 4, 1, 2) |
| 50 | |
| 51 | # x1 = x2 |
| 52 | K = numpy.matrix([[1, -1]]) |
| 53 | w = 0 |
| 54 | |
| 55 | assert_array_equal(polydrivetrain.CoerceGoal(box, K, w, numpy.matrix([[5], [5]])), |
| 56 | numpy.matrix([[3.0], [2.0]])) |
| 57 | |
| 58 | def test_coerce_middle_of_edge(self): |
| 59 | """Tests coercion when the line intersects the middle of an edge.""" |
| 60 | box = self.MakeBox(0, 4, 1, 2) |
| 61 | |
| 62 | # x1 = x2 |
| 63 | K = numpy.matrix([[-1, 1]]) |
| 64 | w = 0 |
| 65 | |
| 66 | assert_array_equal(polydrivetrain.CoerceGoal(box, K, w, numpy.matrix([[5], [5]])), |
| 67 | numpy.matrix([[2.0], [2.0]])) |
| 68 | |
| 69 | def test_coerce_perpendicular_line(self): |
| 70 | """Tests coercion when the line does not intersect and is in quadrant 2.""" |
| 71 | box = self.MakeBox(1, 2, 1, 2) |
| 72 | |
| 73 | # x1 = -x2 |
| 74 | K = numpy.matrix([[1, 1]]) |
| 75 | w = 0 |
| 76 | |
| 77 | assert_array_equal(polydrivetrain.CoerceGoal(box, K, w, numpy.matrix([[5], [5]])), |
| 78 | numpy.matrix([[1.0], [1.0]])) |
| 79 | |
| 80 | |
| 81 | if __name__ == '__main__': |
| 82 | unittest.main() |