Skip to content

Commit

Permalink
Extended unit tests for operations
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkBaker committed Mar 7, 2022
1 parent 6e698b7 commit f95ecca
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 26 deletions.
10 changes: 5 additions & 5 deletions unitTests/classes/src/BaseTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion unitTests/classes/src/MatrixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
57 changes: 52 additions & 5 deletions unitTests/classes/src/Operations/addTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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());
}

/**
Expand All @@ -30,22 +30,69 @@ 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);
}

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]],
],
];
}
Expand Down
56 changes: 53 additions & 3 deletions unitTests/classes/src/Operations/dividebyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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]],
],
];
}
Expand Down
54 changes: 52 additions & 2 deletions unitTests/classes/src/Operations/divideintoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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]],
],
];
}
Expand Down
64 changes: 59 additions & 5 deletions unitTests/classes/src/Operations/multiplyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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());
}

/**
Expand All @@ -35,17 +35,71 @@ 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);
}

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]],
],
];
}
Expand Down
Loading

0 comments on commit f95ecca

Please sign in to comment.