from unittest import TestCase from vector import Vector class TestVector(TestCase): def test_should_create_vector_dim_5(self): actual = Vector(5).get_dimension() expected = 5 self.assertEqual(expected, actual) def test_should_create_zero_vector(self): actual = Vector(5) expected = Vector([0, 0, 0, 0, 0]) self.assertEqual(expected, actual) def test_should_create_vector(self): actual = Vector(list(range(5))) expected = Vector([0, 1, 2, 3, 4]) self.assertEqual(expected, actual) def test_should_transpose_vector(self): data = [1, 2, 3, 4, 5, 6] actual = Vector(data) actual = actual.transpose() expected = [[1, 2, 3, 4, 5, 6]] self.assertEqual(expected, actual) def test_should_neg_vector(self): v = Vector([1, 2]) expected = Vector([-1, -2]) actual = -v self.assertEqual(expected, actual) def test_should_add_vectors(self): v1 = Vector([1, 2]) v2 = Vector([3, 4]) expected = Vector([4, 6]) actual = v1 + v2 self.assertEqual(expected, actual) def test_should_add_scalar_to_vector(self): v = Vector([1, 2]) s = 2 expected = Vector([3, 4]) actual = v + s self.assertEqual(expected, actual) def test_should_radd_scalar_to_vector(self): v = Vector([1, 2]) s = 2 expected = Vector([3, 4]) actual = s + v self.assertEqual(expected, actual) def test_should_raise_value_missmatch_error_while_adding_vectors(self): v1 = Vector(1) v2 = Vector(2) self.assertRaises(ValueError, lambda: v1 + v2) def test_should_raise_dimension_error_while_adding_vectors(self): v1 = Vector(1) v2 = '0' self.assertRaises(ValueError, lambda: v1 + v2) def test_should_sub_vectors(self): v1 = Vector([1, 2]) v2 = Vector([3, 4]) expected = Vector([-2, -2]) actual = v1 - v2 self.assertEqual(expected, actual) def test_should_sub_scalar_of_vector(self): v = Vector([1, 2]) s = 2 expected = Vector([-1, 0]) actual = v - s self.assertEqual(expected, actual) def test_should_rsub_scalar_of_vector(self): v = Vector([1, 2]) s = 2 expected = Vector([1, 0]) actual = s - v self.assertEqual(expected, actual) def test_should_mul_same_shape_vectors(self): v1 = Vector([1, 2]) v2 = Vector([3, 4]) expected = Vector([3, 8]) actual = v1 * v2 self.assertEqual(expected, actual) def test_should_mul_vectors_dot(self): v1 = Vector([1, 2]) v2 = Vector([3, 4]) expected = 11 actual = v1.T() * v2 self.assertEqual(expected, actual) def test_should_mul_scalar_with_vector(self): v = Vector([1, 2]) s = 2 expected = Vector([2, 4]) actual = v * s self.assertEqual(expected, actual) def test_should_rmul_scalar_with_vector(self): v = Vector([1, 2]) s = 2 expected = Vector([2, 4]) actual = s * v self.assertEqual(expected, actual) def test_should_raise_value_missmatch_error_while_mul_vectors(self): v1 = Vector([1, 2]) v2 = '0' self.assertRaises(ValueError, lambda: v1 * v2) def test_should_return_vector_norm(self): v = Vector([1, 2]) actual = v.norm() expected = 2.236 self.assertAlmostEqual(expected, actual, 3) def test_should_return_normalized_vector(self): v = Vector([1, 2]) actual = v.normalize() expected = [1 / 2.236, 2 / 2.236] self.assertAlmostEqual(expected[0], actual[0][0], 3) self.assertAlmostEqual(expected[1], actual[1][0], 3)