def test_cross_product_requires_length_3(self): vector1 = Vector(-6, 8) vector2 = Mock(Vector) vector2._values = [4, 9, 2] with self.assertRaises(ValueError): vector1.cross(vector2) vector1 = Vector(3, -3, 1) vector2._values = [4, 9] with self.assertRaises(ValueError): vector1.cross(vector2)
def test_can_get_angle_between_vectors(self, mock_dot, mock_mag): mock_dot.return_value = 1 mock_mag.return_value = 4 vector1 = Vector(7, 1) vector2 = Mock(Vector) vector2.magnitude.return_value = 0.5 vector2.length.return_value = 2 self.assertAlmostEqual(vector1.angle_with(vector2), 1.0471, delta=0.0005)
def test_can_check_linear_dependence(self, mock_span): vector = Vector(3, 4) v2, v3 = Mock(Vector), Mock(Vector) mock_span.return_value = [vector] self.assertTrue(vector.linearly_dependent_on(v2, v3)) mock_span.assert_called_with(v2, v3) mock_span.return_value = [] self.assertFalse(vector.linearly_dependent_on(v2, v3))
def test_can_check_linear_independence(self, mock_dep): mock_dep.return_value = True vector = Vector(3, 4) v2, v3 = Mock(Vector), Mock(Vector) self.assertFalse(vector.linearly_independent_of(v2, v3)) mock_dep.assert_called_with(v2, v3) mock_dep.return_value = False self.assertTrue(vector.linearly_independent_of(v2, v3)) mock_dep.assert_called_with(v2, v3)
def test_can_get_distance_between_vectors(self, mock_sub): vector1 = Vector(3, 4) vector2 = Mock(Vector) resultant_vector = Mock(Vector) mock_sub.return_value = resultant_vector resultant_vector.magnitude.return_value = 100 distance = vector1.distance_to(vector2) mock_sub.assert_called_with(vector2) self.assertEqual(distance, 100)
def test_can_get_angle_between_vectors_when_zero(self, mock_dot, mock_mag): mock_dot.return_value = 1 mock_mag.return_value = 0 vector1 = Vector(7, 1) vector2 = Mock(Vector) vector2.magnitude.return_value = 0.5 vector2.length.return_value = 2 self.assertAlmostEqual(vector1.angle_with(vector2), math.pi / 4, delta=0.0005) vector2.magnitude.return_value = 0 mock_mag.return_value = 9 self.assertAlmostEqual(vector1.angle_with(vector2), math.pi / 4, delta=0.0005)
def test_can_insert_value(self): vector = Vector(2, 5, 1) vector.insert(1, 12) self.assertEqual(vector._values, [2, 12, 5, 1])
def test_can_get_vector_len(self): vector = Vector(2, 5, 1) self.assertEqual(len(vector), 3)
def test_can_add_value(self): vector = Vector(2, 5, 1) vector.append(12) self.assertEqual(vector._values, [2, 5, 1, 12])
def test_can_get_vector_index(self): vector = Vector(2, 5, 1) self.assertEqual(vector[0], 2) self.assertEqual(vector[1], 5) self.assertEqual(vector[2], 1)
def test_can_set_vector_index(self): vector = Vector(2, 5, 1) vector[0] = 4 vector[1] = 6 vector[2] = 8 self.assertEqual(vector._values, [4, 6, 8])
def test_long_vector_str(self, mock_repr): vector = Vector(range(1, 11)) self.assertEqual( str(vector), "<Vector [1, 2, (...6 items omitted...), 9, 10]>" ) self.assertFalse(mock_repr.called)
def test_angle_requires_equal_length(self): vector1 = Vector(-6, 8) vector2 = Mock(Vector) vector2.length.return_value = 3 with self.assertRaises(ValueError): vector1.angle_with(vector2)
def test_can_remove_value(self): vector = Vector(2, 5, 1) vector.remove(5) self.assertEqual(vector._values, [2, 1])
def test_can_pop_value(self): vector = Vector(2, 5, 1) val = vector.pop() self.assertEqual(val, 1) self.assertEqual(vector._values, [2, 5])
def test_can_get_vector_components(self): vector = Vector(3, 4) components = vector.components() self.assertEqual(len(components), 2) self.assertEqual(components[0]._values, [3, 0]) self.assertEqual(components[1]._values, [0, 4])
def test_vector_repr(self): vector = Vector(2, 5, 1) self.assertEqual(repr(vector), "<Vector [2, 5, 1]>")
def test_can_pop_any_position(self): vector = Vector(2, 5, 1) val = vector.pop(1) self.assertEqual(val, 5) self.assertEqual(vector._values, [2, 1])
def test_can_get_span(self, mock_span): mock_span.return_value = "SPAN" vector = Vector(3, 4) v2, v3 = Mock(Vector), Mock(Vector) self.assertEqual(vector.span_with(v2, v3), "SPAN") mock_span.assert_called_with(vector, v2, v3)
def test_can_get_dot_product(self): vector1 = Vector(-6, 8) vector2 = Mock(Vector) vector2._values = [5, 12] vector2.length.return_value = 2 self.assertEqual(vector1.dot(vector2), 66)
def test_angle_requires_vector(self): vector1 = Vector(-6, -8) with self.assertRaises(TypeError): vector1.angle_with("vector")
def test_dot_product_requires_equal_length(self): vector1 = Vector(-6, 8) vector2 = Mock(Vector) vector2.length.return_value = 3 with self.assertRaises(ValueError): vector1.dot(vector2)
def test_vector_is_container(self): vector = Vector(2, 5, 1) self.assertIn(5, vector) self.assertNotIn(7, vector)
def test_can_get_cross_product(self): vector1 = Vector(3, -3, 1) vector2 = Mock(Vector) vector2._values = [4, 9, 2] self.assertEqual(vector1.cross(vector2)._values, [-15, -2, 39])
def test_can_make_vector(self): vector = Vector(2, 5, 1) self.assertEqual(vector._values, [2, 5, 1])
def test_vector_str(self, mock_repr): mock_repr.return_value = "xxxxx" vector = Vector(2, 5, 1) self.assertEqual(str(vector), "xxxxx") self.assertTrue(mock_repr.called)
def test_cannot_set_vector_out_of_index(self): vector = Vector(2, 5, 1) with self.assertRaises(IndexError): vector[3] = 9
def test_cross_product_requires_vector(self): vector1 = Vector(-6, -8) with self.assertRaises(TypeError): vector1.cross("vector")
def test_vector_is_iterable(self): vector = Vector(2, 5, 1) self.assertEqual(list(iter(vector)), vector._values)
def test_can_get_vector_magnitude(self): vector = Vector(3, 4) self.assertEqual(vector.magnitude(), 5)