1
0
pwr_project/test/test_vector.py

122 lines
3.1 KiB
Python
Raw Normal View History

2023-12-12 14:31:09 +01:00
from unittest import TestCase
from vector import Vector
class TestVector(TestCase):
def test_should_create_vector_dim_5(self):
2023-12-12 16:34:54 +01:00
vector = Vector(5)
2023-12-12 14:31:09 +01:00
actual = vector.get_dimension()
2023-12-12 16:34:54 +01:00
expected = 5
2023-12-12 14:31:09 +01:00
self.assertEqual(expected, actual)
def test_should_create_zero_vector(self):
2023-12-12 16:34:54 +01:00
vector = Vector(5)
2023-12-12 14:31:09 +01:00
actual = vector.get_data()
expected = [0, 0, 0, 0, 0]
self.assertEqual(expected, actual)
def test_should_create_vector(self):
data = list(range(5))
vector = Vector(data)
actual = vector.get_data()
expected = [0, 1, 2, 3, 4]
2023-12-12 16:34:54 +01:00
self.assertEqual(expected, actual)
2023-12-12 14:31:09 +01:00
2023-12-12 16:34:54 +01:00
def test_should_neg_vector(self):
v = Vector([1, 2])
expected = Vector([-1, -2])
actual = -v
2023-12-12 14:31:09 +01:00
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
2023-12-12 16:34:54 +01:00
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
2023-12-12 14:31:09 +01:00
2023-12-12 16:34:54 +01:00
expected = Vector([3, 4])
actual = s + v
2023-12-12 14:31:09 +01:00
self.assertEqual(expected, actual)
2023-12-12 16:34:54 +01:00
def test_should_raise_value_missmatch_error_while_adding_vectors(self):
2023-12-12 14:31:09 +01:00
v1 = Vector(1)
v2 = Vector(2)
2023-12-12 16:34:54 +01:00
self.assertRaises(ValueError, lambda: v1 + v2)
2023-12-12 14:31:09 +01:00
2023-12-12 16:34:54 +01:00
def test_should_raise_dimension_error_while_adding_vectors(self):
v1 = Vector(1)
v2 = '0'
2023-12-12 14:31:09 +01:00
self.assertRaises(ValueError, lambda: v1 + v2)
2023-12-12 16:34:54 +01:00
def test_should_subtract_vectors(self):
v1 = Vector([1, 2])
v2 = Vector([3, 4])
expected = Vector([-2, -2])
actual = v1 - v2
self.assertEqual(expected, actual)
def test_should_subtract_scalar_of_vector(self):
v = Vector([1, 2])
s = 2
expected = Vector([-1, 0])
actual = v - s
self.assertEqual(expected, actual)
def test_should_rsubtract_scalar_of_vector(self):
v = Vector([1, 2])
s = 2
expected = Vector([1, 0])
actual = s - v
self.assertEqual(expected, actual)
def test_should_multiply_vectors(self):
v1 = Vector([1, 2])
v2 = Vector([3, 4])
expected = 11
actual = v1 * v2
self.assertEqual(expected, actual)
def test_should_multiply_scalar_with_vector(self):
v = Vector([1, 2])
s = 2
expected = Vector([2, 4])
actual = v * s
self.assertEqual(expected, actual)
def test_should_rmultiply_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_multiplying_vectors(self):
v1 = Vector([1, 2])
v2 = '0'
self.assertRaises(ValueError, lambda: v1 * v2)