|
| 1 | +import unittest |
| 2 | +import task |
| 3 | +from math import * |
| 4 | + |
| 5 | +class TestSmoothing(unittest.TestCase): |
| 6 | + |
| 7 | + def assertSmoothingValid(self, path, newpath_expected): |
| 8 | + path_persistent = [pair[:] for pair in path] |
| 9 | + |
| 10 | + newpath = task.smooth(path) |
| 11 | + |
| 12 | + self.assertTrue(type(newpath) == type(newpath_expected), |
| 13 | + "Function doesn't return a list") |
| 14 | + self.assertTrue(len(newpath) == len(newpath_expected), |
| 15 | + "Newpath has the wrong length") |
| 16 | + self.assertEqual(path, path_persistent, |
| 17 | + "Original path variable was modified by your code") |
| 18 | + for index, got, expected in zip(range(len(newpath_expected)), newpath, newpath_expected): |
| 19 | + self.assertTrue(type(got) == type(expected), |
| 20 | + "Returned list doesn't contain a point at position %d" % index) |
| 21 | + self.assertTrue(len(got) == len(expected), |
| 22 | + "Returned list doesn't contain a list of two coordinates " |
| 23 | + "at position %d" % index) |
| 24 | + self.assertAlmostEqual(got[0], expected[0], 3, |
| 25 | + "X coordinate differs for point %d: " |
| 26 | + "expected %.3f, got %.3f" % (index, expected[0], got[0])) |
| 27 | + self.assertAlmostEqual(got[1], expected[1], 3, |
| 28 | + "Y coordinate differs for point %d: " |
| 29 | + "expected %.3f, got %.3f" % (index, expected[1], got[1])) |
| 30 | + |
| 31 | + def test_videoPath(self): |
| 32 | + path = [[0, 0], |
| 33 | + [1, 0], |
| 34 | + [2, 0], |
| 35 | + [3, 0], |
| 36 | + [4, 0], |
| 37 | + [5, 0], |
| 38 | + [6, 0], |
| 39 | + [6, 1], |
| 40 | + [6, 2], |
| 41 | + [6, 3], |
| 42 | + [5, 3], |
| 43 | + [4, 3], |
| 44 | + [3, 3], |
| 45 | + [2, 3], |
| 46 | + [1, 3], |
| 47 | + [0, 3], |
| 48 | + [0, 2], |
| 49 | + [0, 1]] |
| 50 | + newpath_expected = [[0.5449300156668018, 0.47485226780102946], |
| 51 | + [1.2230705677535505, 0.2046277687200752], |
| 52 | + [2.079668890615267, 0.09810778721159963], |
| 53 | + [3.0000020176660755, 0.07007646364781912], |
| 54 | + [3.9203348821839112, 0.09810853832382399], |
| 55 | + [4.7769324511170455, 0.20462917195702085], |
| 56 | + [5.455071854686622, 0.4748541381544533], |
| 57 | + [5.697264197153936, 1.1249625336275617], |
| 58 | + [5.697263485026567, 1.8750401628534337], |
| 59 | + [5.455069810373743, 2.5251482916876378], |
| 60 | + [4.776929339068159, 2.795372759575895], |
| 61 | + [3.92033110541304, 2.9018927284871063], |
| 62 | + [2.999998066091118, 2.929924058932193], |
| 63 | + [2.0796652780381826, 2.90189200881968], |
| 64 | + [1.2230677654766597, 2.7953714133566603], |
| 65 | + [0.544928391271399, 2.5251464933327794], |
| 66 | + [0.3027360471605494, 1.875038145804603], |
| 67 | + [0.302736726373967, 1.1249605602741133]] |
| 68 | + self.assertSmoothingValid(path, newpath_expected) |
| 69 | + |
| 70 | + def test_secondPath(self): |
| 71 | + path = [[1, 0], # Move in the shape of a plus sign |
| 72 | + [2, 0], |
| 73 | + [2, 1], |
| 74 | + [3, 1], |
| 75 | + [3, 2], |
| 76 | + [2, 2], |
| 77 | + [2, 3], |
| 78 | + [1, 3], |
| 79 | + [1, 2], |
| 80 | + [0, 2], |
| 81 | + [0, 1], |
| 82 | + [1, 1]] |
| 83 | + |
| 84 | + answer = [[1.239080543767428, 0.5047204351187283], |
| 85 | + [1.7609243903912781, 0.5047216452560908], |
| 86 | + [2.0915039821562416, 0.9085017167753027], |
| 87 | + [2.495281862032503, 1.2390825203587184], |
| 88 | + [2.4952805300504783, 1.7609262468826048], |
| 89 | + [2.0915003641706296, 2.0915058211575475], |
| 90 | + [1.7609195135622062, 2.4952837841027695], |
| 91 | + [1.2390757942466555, 2.4952826072236918], |
| 92 | + [0.9084962737918979, 2.091502621431358], |
| 93 | + [0.5047183914625598, 1.7609219230352355], |
| 94 | + [0.504719649257698, 1.2390782835562297], |
| 95 | + [0.9084996902674257, 0.9084987462432871]] |
| 96 | + |
| 97 | + self.assertSmoothingValid(path, answer) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + unittest.main() |
| 102 | + |
| 103 | + |
| 104 | + |
0 commit comments