From f95ecca1e28a62bbdd27c3a6e32f9b979f354c31 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Mon, 7 Mar 2022 08:38:50 +0100 Subject: [PATCH] Extended unit tests for operations --- unitTests/classes/src/BaseTestAbstract.php | 10 +-- unitTests/classes/src/MatrixTest.php | 2 +- unitTests/classes/src/Operations/addTest.php | 57 +++++++++++++++-- .../classes/src/Operations/dividebyTest.php | 56 +++++++++++++++- .../classes/src/Operations/divideintoTest.php | 54 +++++++++++++++- .../classes/src/Operations/multiplyTest.php | 64 +++++++++++++++++-- .../classes/src/Operations/subtractTest.php | 56 ++++++++++++++-- 7 files changed, 273 insertions(+), 26 deletions(-) diff --git a/unitTests/classes/src/BaseTestAbstract.php b/unitTests/classes/src/BaseTestAbstract.php index f2bf572..688e199 100644 --- a/unitTests/classes/src/BaseTestAbstract.php +++ b/unitTests/classes/src/BaseTestAbstract.php @@ -16,15 +16,15 @@ protected function assertIsMatrixObject($object) protected function assertOriginalMatrixIsUnchanged(array $grid, Matrix $matrix, $message = '') { - self::assertEquals($grid, $matrix->toArray(), $message); - self::assertEquals(count($grid), $matrix->rows, $message); - self::assertEquals(count($grid[0]), $matrix->columns, $message); + self::assertSame($grid, $matrix->toArray(), $message); + self::assertSame(count($grid), $matrix->rows, $message); + self::assertSame(count($grid[0]), $matrix->columns, $message); } protected function assertMatrixValues(Matrix $matrix, $rows, $columns, array $grid) { - self::assertEquals($rows, $matrix->rows, 'Row mismatch'); - self::assertEquals($columns, $matrix->columns, 'Column mismatch'); + self::assertSame($rows, $matrix->rows, 'Row mismatch'); + self::assertSame($columns, $matrix->columns, 'Column mismatch'); $matrixGrid = $matrix->toArray(); foreach ($grid as $row => $vector) { diff --git a/unitTests/classes/src/MatrixTest.php b/unitTests/classes/src/MatrixTest.php index e885f3a..fd1adf4 100644 --- a/unitTests/classes/src/MatrixTest.php +++ b/unitTests/classes/src/MatrixTest.php @@ -16,7 +16,7 @@ public function testInstantiate() // Must return an object of the correct type... $this->assertIsMatrixObject($matrixObject); // ... containing the correct data - $this->assertMatrixValues($matrixObject, ceil($cells / $columns), $columns, $this->buildArray($cells, $columns)); + $this->assertMatrixValues($matrixObject, (int) ceil($cells / $columns), $columns, $this->buildArray($cells, $columns)); } public function testRowsDefault() diff --git a/unitTests/classes/src/Operations/addTest.php b/unitTests/classes/src/Operations/addTest.php index a20d9a6..deaf12a 100644 --- a/unitTests/classes/src/Operations/addTest.php +++ b/unitTests/classes/src/Operations/addTest.php @@ -2,10 +2,10 @@ namespace MatrixTest\Operations; +use Matrix\Exception; use Matrix\Matrix; use Matrix\Operations; use MatrixTest\BaseTestAbstract; -use function Matrix\add; class addTest extends BaseTestAbstract { @@ -21,7 +21,7 @@ public function testAdditionStatic($expected, $value1, $value2) // Must return an object of the correct type... $this->assertIsMatrixObject($result); // ... containing the correct data - $this->assertMatrixValues($result, count($expected), count($expected[0]), $expected); + $this->assertSame($expected, $result->toArray()); } /** @@ -30,12 +30,13 @@ public function testAdditionStatic($expected, $value1, $value2) public function testAdditionInvoker($expected, $value1, $value2) { $matrix = new Matrix($value1); + $result = $matrix->add($value2); // Must return an object of the correct type... $this->assertIsMatrixObject($matrix); // ... containing the correct data - $this->assertMatrixValues($result, count($expected), count($expected[0]), $expected); + $this->assertSame($expected, $result->toArray()); // Verify that the original matrix remains unchanged $this->assertOriginalMatrixIsUnchanged($value1, $matrix); } @@ -43,9 +44,55 @@ public function testAdditionInvoker($expected, $value1, $value2) public function dataProvider() { return [ - [ + 'square - 2x2 + 2x2' => [ [[-1, 6], [-3, 12]], - [[1, 2], [3, 4]], [[-2, 4], [-6, 8]], + [[1, 2], [3, 4]], + [[-2, 4], [-6, 8]], + ], + 'row vector/row vector' => [ + [[5, 7, 9]], + [[1, 2, 3]], + [[4, 5, 6]], + ], + 'column vector/column vector' => [ + [[5], [7], [9]], + [[1], [2], [3]], + [[4], [5], [6]], + ], + 'matrix 3x2 + 3x2' => [ + [[8, 10, 12], [14, 16, 18]], + [[1, 2, 3], [4, 5, 6]], + [[7, 8, 9], [10, 11, 12]], + ], + 'matrix 2x3 / 2x3' => [ + [[8, 14], [10, 16], [12, 18]], + [[1, 4], [2, 5], [3, 6]], + [[7, 10], [8, 11], [9, 12]], + ], + ]; + } + + /** + * @dataProvider dataProviderException + */ + public function testAdditionException($value1, $value2) + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Matrices have mismatched dimensions'); + + Operations::add($value1, $value2); + } + + public function dataProviderException() + { + return [ + 'row vector/column vector' => [ + [[1, 2, 3]], + [[4], [5], [6]], + ], + 'column vector/row vector' => [ + [[1], [2], [3]], + [[4, 5, 6]], ], ]; } diff --git a/unitTests/classes/src/Operations/dividebyTest.php b/unitTests/classes/src/Operations/dividebyTest.php index d22b0d6..f0f4ccd 100644 --- a/unitTests/classes/src/Operations/dividebyTest.php +++ b/unitTests/classes/src/Operations/dividebyTest.php @@ -2,10 +2,10 @@ namespace MatrixTest\Operations; +use Matrix\Exception; use Matrix\Matrix; use Matrix\Operations; use MatrixTest\BaseTestAbstract; -use function Matrix\divideby; class dividebyTest extends BaseTestAbstract { @@ -44,8 +44,58 @@ public function dataProvider() { return [ [ - [[2.5, -1.0], [6.0, -2.5]], - [[1, 2], [3, 4]], [[-2, 4], [-6, 8]], + 'square - 2x2 * 2x2' => [[2.5, -1.0], [6.0, -2.5]], + [[1, 2], [3, 4]], + [[-2, 4], [-6, 8]], + ], + ]; + } + + /** + * @dataProvider dataProviderException + */ + public function testDivideByException($value1, $value2) + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Division can only be calculated for a square matrix'); + + Operations::divideby($value1, $value2); + } + + public function dataProviderException() + { + return [ + 'row vector/column vector' => [ + [[1, 2, 3]], + [[4], [5], [6]], + ], + 'column vector/row vector' => [ + [[1], [2], [3]], + [[4, 5, 6]], + ], + 'matrix 3x2 + 2x3' => [ + [[1, 2, 3], [4, 5, 6]], + [[7, 10], [8, 11], [9, 12]], + ], + 'matrix 2x3 / 3x2' => [ + [[1, 4], [2, 5], [3, 6]], + [[7, 8, 9], [10, 11, 12]], + ], + 'row vector/row vector' => [ + [[1, 2, 3]], + [[4, 5, 6]], + ], + 'column vector/column vector' => [ + [[1], [2], [3]], + [[4], [5], [6]], + ], + 'matrix 3x2 + 3x2' => [ + [[1, 2, 3], [4, 5, 6]], + [[7, 8, 9], [10, 11, 12]], + ], + 'matrix 2x3 / 2x3' => [ + [[1, 4], [2, 5], [3, 6]], + [[7, 10], [8, 11], [9, 12]], ], ]; } diff --git a/unitTests/classes/src/Operations/divideintoTest.php b/unitTests/classes/src/Operations/divideintoTest.php index adb945a..9fea87c 100644 --- a/unitTests/classes/src/Operations/divideintoTest.php +++ b/unitTests/classes/src/Operations/divideintoTest.php @@ -2,10 +2,10 @@ namespace MatrixTest\Operations; +use Matrix\Exception; use Matrix\Matrix; use Matrix\Operations; use MatrixTest\BaseTestAbstract; -use function Matrix\divideinto; class divideintoTest extends BaseTestAbstract { @@ -45,7 +45,57 @@ public function dataProvider() return [ [ [[10, -4], [24, -10]], - [[1, 2], [3, 4]], [[-2, 4], [-6, 8]], + [[1, 2], [3, 4]], + [[-2, 4], [-6, 8]], + ], + ]; + } + + /** + * @dataProvider dataProviderException + */ + public function testDivideIntoException($value1, $value2) + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Division can only be calculated for a square matrix'); + + Operations::divideinto($value1, $value2); + } + + public function dataProviderException() + { + return [ + 'row vector/column vector' => [ + [[1, 2, 3]], + [[4], [5], [6]], + ], + 'column vector/row vector' => [ + [[1], [2], [3]], + [[4, 5, 6]], + ], + 'matrix 3x2 + 2x3' => [ + [[1, 2, 3], [4, 5, 6]], + [[7, 10], [8, 11], [9, 12]], + ], + 'matrix 2x3 / 3x2' => [ + [[1, 4], [2, 5], [3, 6]], + [[7, 8, 9], [10, 11, 12]], + ], + 'row vector/row vector' => [ + [[1, 2, 3]], + [[4, 5, 6]], + ], + 'column vector/column vector' => [ + [[1], [2], [3]], + [[4], [5], [6]], + ], + 'matrix 3x2 + 3x2' => [ + [[1, 2, 3], [4, 5, 6]], + [[7, 8, 9], [10, 11, 12]], + ], + 'matrix 2x3 / 2x3' => [ + [[1, 4], [2, 5], [3, 6]], + [[7, 10], [8, 11], [9, 12]], ], ]; } diff --git a/unitTests/classes/src/Operations/multiplyTest.php b/unitTests/classes/src/Operations/multiplyTest.php index 9ebe1c9..0dd06dd 100644 --- a/unitTests/classes/src/Operations/multiplyTest.php +++ b/unitTests/classes/src/Operations/multiplyTest.php @@ -2,10 +2,10 @@ namespace MatrixTest\Operations; +use Matrix\Exception; use Matrix\Matrix; use Matrix\Operations; use MatrixTest\BaseTestAbstract; -use function Matrix\multiply; class multiplyTest extends BaseTestAbstract { @@ -21,7 +21,7 @@ public function testMultiplicationFunctionStatic($expected, $value1, $value2) // Must return an object of the correct type... $this->assertIsMatrixObject($result); // ... containing the correct data - $this->assertMatrixValues($result, count($expected), count($expected[0]), $expected); + $this->assertSame($expected, $result->toArray()); } /** @@ -35,7 +35,7 @@ public function testMultiplicationInvoker($expected, $value1, $value2) // Must return an object of the correct type... $this->assertIsMatrixObject($matrix); // ... containing the correct data - $this->assertMatrixValues($result, count($expected), count($expected[0]), $expected); + $this->assertSame($expected, $result->toArray()); // Verify that the original matrix remains unchanged $this->assertOriginalMatrixIsUnchanged($value1, $matrix); } @@ -43,9 +43,63 @@ public function testMultiplicationInvoker($expected, $value1, $value2) public function dataProvider() { return [ - [ + 'square - 2x2 * 2x2' => [ [[-14, 20], [-30, 44]], - [[1, 2], [3, 4]], [[-2, 4], [-6, 8]], + [[1, 2], [3, 4]], + [[-2, 4], [-6, 8]], + ], + 'row vector/column vector' => [ + [[32]], + [[1, 2, 3]], + [[4], [5], [6]], + ], + 'column vector/row vector' => [ + [[4, 5, 6], [8, 10, 12], [12, 15, 18]], + [[1], [2], [3]], + [[4, 5, 6]], + ], + 'matrix 3x2 + 2x3' => [ + [[50, 68], [122,167]], + [[1, 2, 3], [4, 5, 6]], + [[7, 10], [8, 11], [9, 12]], + ], + 'matrix 2x3 / 3x2' => [ + [[47, 52, 57], [64, 71, 78], [81, 90, 99]], + [[1, 4], [2, 5], [3, 6]], + [[7, 8, 9], [10, 11, 12]], + ], + ]; + } + + /** + * @dataProvider dataProviderException + */ + public function testMultiplicationException($value1, $value2) + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Matrices have mismatched dimensions'); + + Operations::multiply($value1, $value2); + } + + public function dataProviderException() + { + return [ + 'row vector/row vector' => [ + [[1, 2, 3]], + [[4, 5, 6]], + ], + 'column vector/column vector' => [ + [[1], [2], [3]], + [[4], [5], [6]], + ], + 'matrix 3x2 + 3x2' => [ + [[1, 2, 3], [4, 5, 6]], + [[7, 8, 9], [10, 11, 12]], + ], + 'matrix 2x3 / 2x3' => [ + [[1, 4], [2, 5], [3, 6]], + [[7, 10], [8, 11], [9, 12]], ], ]; } diff --git a/unitTests/classes/src/Operations/subtractTest.php b/unitTests/classes/src/Operations/subtractTest.php index e4b441d..6079efa 100644 --- a/unitTests/classes/src/Operations/subtractTest.php +++ b/unitTests/classes/src/Operations/subtractTest.php @@ -2,10 +2,10 @@ namespace MatrixTest\Operations; +use Matrix\Exception; use Matrix\Matrix; use Matrix\Operations; use MatrixTest\BaseTestAbstract; -use function Matrix\subtract; class subtractTest extends BaseTestAbstract { @@ -21,7 +21,7 @@ public function testSubtractionFunctionStatic($expected, $value1, $value2) // Must return an object of the correct type... $this->assertIsMatrixObject($result); // ... containing the correct data - $this->assertMatrixValues($result, count($expected), count($expected[0]), $expected); + $this->assertSame($expected, $result->toArray()); } /** @@ -35,7 +35,7 @@ public function testSubtractionInvoker($expected, $value1, $value2) // Must return an object of the correct type... $this->assertIsMatrixObject($matrix); // ... containing the correct data - $this->assertMatrixValues($result, count($expected), count($expected[0]), $expected); + $this->assertSame($expected, $result->toArray()); // Verify that the original matrix remains unchanged $this->assertOriginalMatrixIsUnchanged($value1, $matrix); } @@ -43,9 +43,55 @@ public function testSubtractionInvoker($expected, $value1, $value2) public function dataProvider() { return [ - [ + 'square - 2x2 - 2x2' => [ [[3, -2], [9, -4]], - [[1, 2], [3, 4]], [[-2, 4], [-6, 8]], + [[1, 2], [3, 4]], + [[-2, 4], [-6, 8]], + ], + 'row vector/row vector' => [ + [[-3, -3, -3]], + [[1, 2, 3]], + [[4, 5, 6]], + ], + 'column vector/column vector' => [ + [[-3], [-3], [-3]], + [[1], [2], [3]], + [[4], [5], [6]], + ], + 'matrix 3x2 + 3x2' => [ + [[-6, -6, -6], [-6, -6, -6]], + [[1, 2, 3], [4, 5, 6]], + [[7, 8, 9], [10, 11, 12]], + ], + 'matrix 2x3 / 2x3' => [ + [[-6, -6], [-6, -6], [-6, -6]], + [[1, 4], [2, 5], [3, 6]], + [[7, 10], [8, 11], [9, 12]], + ], + ]; + } + + /** + * @dataProvider dataProviderException + */ + public function testSubtractionException($value1, $value2) + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Matrices have mismatched dimensions'); + + Operations::subtract($value1, $value2); + } + + public function dataProviderException() + { + return [ + 'row vector/column vector' => [ + [[1, 2, 3]], + [[4], [5], [6]], + ], + 'column vector/row vector' => [ + [[1], [2], [3]], + [[4, 5, 6]], ], ]; }