Add class matrix.py (wip)
This commit is contained in:
55
test/test_matrix.py
Normal file
55
test/test_matrix.py
Normal file
@ -0,0 +1,55 @@
|
||||
from unittest import TestCase
|
||||
|
||||
import numpy
|
||||
|
||||
from matrix import Matrix
|
||||
|
||||
|
||||
class TestMatrix(TestCase):
|
||||
def test_should_create_matrix_from_numpy_array_with_shape_3_2(self):
|
||||
data = numpy.array([[0, 1], [2, 3], [4, 5]])
|
||||
m = Matrix(data)
|
||||
|
||||
actual_shape = m.shape()
|
||||
expected_shape = (3, 2)
|
||||
self.assertEqual(expected_shape, actual_shape)
|
||||
|
||||
actual_data = m.get_data()
|
||||
expected_data = [[0, 1], [2, 3], [4, 5]]
|
||||
self.assertEqual(expected_data, actual_data)
|
||||
|
||||
def test_should_create_matrix_from_numpy_array_with_shape_1_3(self):
|
||||
data = numpy.array([[0, 1, 2]])
|
||||
m = Matrix(data)
|
||||
|
||||
actual_shape = m.shape()
|
||||
expected_shape = (1, 3)
|
||||
self.assertEqual(expected_shape, actual_shape)
|
||||
|
||||
actual_data = m.get_data()
|
||||
expected_data = [[0, 1, 2]]
|
||||
self.assertEqual(expected_data, actual_data)
|
||||
|
||||
def test_should_create_vectorlike_matrix_from_numpy_array_with_shape_3_1(self):
|
||||
data = numpy.array([0, 1, 2])
|
||||
m = Matrix(data)
|
||||
|
||||
actual_shape = m.shape()
|
||||
expected_shape = (3, 1)
|
||||
self.assertEqual(expected_shape, actual_shape)
|
||||
|
||||
actual_data = m.get_data()
|
||||
expected_data = [0, 1, 2]
|
||||
self.assertEqual(expected_data, actual_data)
|
||||
|
||||
def test_should_create_matrix_from_list_with_shape_2_2(self):
|
||||
data = [0, 1, 2, 3]
|
||||
m = Matrix(data, shape=(2, 2))
|
||||
|
||||
actual_shape = m.shape()
|
||||
expected_shape = (2, 2)
|
||||
self.assertEqual(expected_shape, actual_shape)
|
||||
|
||||
actual_data = m.get_data()
|
||||
expected_data = [[0, 1], [2, 3]]
|
||||
self.assertEqual(expected_data, actual_data)
|
@ -5,21 +5,17 @@ from vector import Vector
|
||||
|
||||
class TestVector(TestCase):
|
||||
def test_should_create_vector_dim_5(self):
|
||||
dim = 5
|
||||
vector = Vector(dim)
|
||||
vector = Vector(5)
|
||||
|
||||
actual = vector.get_dimension()
|
||||
expected = dim
|
||||
|
||||
expected = 5
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_create_zero_vector(self):
|
||||
dim = 5
|
||||
vector = Vector(dim)
|
||||
vector = Vector(5)
|
||||
|
||||
actual = vector.get_data()
|
||||
expected = [0, 0, 0, 0, 0]
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_create_vector(self):
|
||||
@ -28,7 +24,13 @@ class TestVector(TestCase):
|
||||
|
||||
actual = vector.get_data()
|
||||
expected = [0, 1, 2, 3, 4]
|
||||
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):
|
||||
@ -37,11 +39,83 @@ class TestVector(TestCase):
|
||||
|
||||
expected = Vector([4, 6])
|
||||
actual = v1 + v2
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_raise_error_while_adding_vectors(self):
|
||||
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_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)
|
||||
|
Reference in New Issue
Block a user