def test_CompositeDirection_eq_ne(self): self.assertEqual(CompositeDirection(N, N, W), CompositeDirection(W, N, N)) self.assertNotEqual(CompositeDirection(N, N, W), CompositeDirection(N, N, N, W)) self.assertNotEqual(CompositeDirection(N), N)
def test_CompositeDirection_value_two_elements(self): rt_two_over_two = sqrt(2) / 2 self.assertAlmostEqual(CompositeDirection(N, E).value[0], rt_two_over_two, places=7) self.assertAlmostEqual(CompositeDirection(N, E).value[1], rt_two_over_two, places=7) self.assertAlmostEqual(CompositeDirection(N, W).value[0], -rt_two_over_two, places=7) self.assertAlmostEqual(CompositeDirection(N, W).value[1], rt_two_over_two, places=7) self.assertAlmostEqual(CompositeDirection(S, E).value[0], rt_two_over_two, places=7) self.assertAlmostEqual(CompositeDirection(S, E).value[1], -rt_two_over_two, places=7) self.assertAlmostEqual(CompositeDirection(S, W).value[0], -rt_two_over_two, places=7) self.assertAlmostEqual(CompositeDirection(S, W).value[1], -rt_two_over_two, places=7)
def test_CompositeDirection_more_values(self): self.assertAlmostEqual(CompositeDirection(N, N, W).value[0], -1 / sqrt(5), places=7) self.assertAlmostEqual(CompositeDirection(N, N, W).value[1], 2 / sqrt(5), places=7) self.assertAlmostEqual(CompositeDirection(S, S, S, E).value[0], 1 / sqrt(10), places=7) self.assertAlmostEqual(CompositeDirection(S, S, S, E).value[1], -3 / sqrt(10), places=7)
def test_get_direction_to_combination(self): start = Point(1, 2) end = Point(5, 0) answer = start.get_direction_to(end) self.assertEqual(answer, CompositeDirection(E, E, E, E, S, S)) magnitude = sqrt(20) self.assertAlmostEqual(answer.value[0], 4/magnitude, places=7) self.assertAlmostEqual(answer.value[1], -2/magnitude, places=7)
def test_CompositeDirection_is_unit_vector(self): test = CompositeDirection(N, N, W) self.assertAlmostEqual(sum(val**2 for val in test.value), 1.0, places=7) test = CompositeDirection(S, S, E, S) self.assertAlmostEqual(sum(val**2 for val in test.value), 1.0, places=7) test = CompositeDirection(W, W, N) self.assertAlmostEqual(sum(val**2 for val in test.value), 1.0, places=7) test = CompositeDirection(S, S, W, W, W) self.assertAlmostEqual(sum(val**2 for val in test.value), 1.0, places=7)
def get_direction_to(self, point: 'Point') -> CompositeDirection: """ :param point: target `Point` :return: CompositeDirection - a hashable immutable object with `value` = (x, y) such that x**2 + y**2 = 1 :raises ValueError: if `self.get_direction_to(pt)` where `pt==self`. """ del_x = point.x - self.x del_y = point.y - self.y if del_x > 0: x_dir = [E] * del_x else: x_dir = [W] * -del_x if del_y > 0: y_dir = [N] * del_y else: y_dir = [S] * -del_y total = x_dir + y_dir return CompositeDirection(*total)
def test_CompositeDirection_opposite_directions(self): self.assertEqual(CompositeDirection(N, N, S), CompositeDirection(N, N, N))
def test_CompositeDirection_right(self): test = CompositeDirection(N, N, E) self.assertEqual(test.right(), CompositeDirection(E, E, S))
def test_CompositeDirection_left(self): test = CompositeDirection(N, N, E) self.assertEqual(test.left(), CompositeDirection(W, W, N)) self.assertEqual(test.left(), CompositeDirection(N, W, W))
def test_CompositeDirection_opposite(self): test = CompositeDirection(N, N, E) self.assertEqual(test.opposite(), CompositeDirection(S, S, W))
def test_CompositeDirection_hash(self): self.assertEqual(hash(CompositeDirection(N, W)), hash(CompositeDirection(W, N))) test = CompositeDirection(N, N, W, N) self.assertEqual(hash(test), hash(test.value))
def test_get_direction_to_west(self): start = Point(1, 2) end = Point(-1, 2) answer = start.get_direction_to(end) self.assertEqual(answer, CompositeDirection(W)) self.assertEqual(answer.value, (-1.0, 0))
def test_CompositeDirection_value_one_element(self): for direction in Direction: float_vals = tuple(float(val) for val in direction.value) self.assertEqual(float_vals, CompositeDirection(direction).value)
def test_CompositeDirection_repr(self): test = CompositeDirection(N, N, W) self.assertEqual( repr(test), 'CompositeDirection(Direction.N, Direction.N, Direction.W)')
def test_CompositeDirection_name(self): test = CompositeDirection(N, N, W, S, E) self.assertEqual(test.name, 'NNWSE')
def test_CompositeDirection_string(self): test = CompositeDirection(N, N, W, S, E) self.assertEqual(str(test), 'NNWSE')
def test_from_dir_and_mag_composite_direction(self): v = Vector.from_dir_and_mag(CompositeDirection(N, E), 5) expected_x_y = 5 / sqrt(2) self.assertAlmostEqual(v.x, expected_x_y, places=7) self.assertAlmostEqual(v.y, expected_x_y, places=7)
def test_get_direction_to_south(self): start = Point(1, 2) end = Point(1, 0) answer = start.get_direction_to(end) self.assertEqual(answer, CompositeDirection(S)) self.assertEqual(answer.value, (0, -1.0))