Skip to content

Commit 19ab4b1

Browse files
author
MarkBaker
committed
Extended unit tests for operations
1 parent 6e698b7 commit 19ab4b1

File tree

7 files changed

+275
-27
lines changed

7 files changed

+275
-27
lines changed

unitTests/classes/src/BaseTestAbstract.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,23 @@ protected function assertIsMatrixObject($object)
1616

1717
protected function assertOriginalMatrixIsUnchanged(array $grid, Matrix $matrix, $message = '')
1818
{
19-
self::assertEquals($grid, $matrix->toArray(), $message);
20-
self::assertEquals(count($grid), $matrix->rows, $message);
21-
self::assertEquals(count($grid[0]), $matrix->columns, $message);
19+
self::assertSame($grid, $matrix->toArray(), $message);
20+
self::assertSame(count($grid), $matrix->rows, $message);
21+
self::assertSame(count($grid[0]), $matrix->columns, $message);
2222
}
2323

2424
protected function assertMatrixValues(Matrix $matrix, $rows, $columns, array $grid)
2525
{
26-
self::assertEquals($rows, $matrix->rows, 'Row mismatch');
27-
self::assertEquals($columns, $matrix->columns, 'Column mismatch');
26+
self::assertSame($rows, $matrix->rows, 'Row mismatch');
27+
self::assertSame($columns, $matrix->columns, 'Column mismatch');
2828

2929
$matrixGrid = $matrix->toArray();
3030
foreach ($grid as $row => $vector) {
3131
foreach ($vector as $column => $expectedValue) {
32-
self::assertSame(
32+
self::assertEqualsWithDelta(
3333
(float) $expectedValue,
3434
(float) $matrixGrid[$row][$column],
35+
1.0e-12,
3536
"Invalid result at row {$row} and column {$column}"
3637
);
3738
}

unitTests/classes/src/MatrixTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function testInstantiate()
1616
// Must return an object of the correct type...
1717
$this->assertIsMatrixObject($matrixObject);
1818
// ... containing the correct data
19-
$this->assertMatrixValues($matrixObject, ceil($cells / $columns), $columns, $this->buildArray($cells, $columns));
19+
$this->assertMatrixValues($matrixObject, (int) ceil($cells / $columns), $columns, $this->buildArray($cells, $columns));
2020
}
2121

2222
public function testRowsDefault()

unitTests/classes/src/Operations/addTest.php

+52-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace MatrixTest\Operations;
44

5+
use Matrix\Exception;
56
use Matrix\Matrix;
67
use Matrix\Operations;
78
use MatrixTest\BaseTestAbstract;
8-
use function Matrix\add;
99

1010
class addTest extends BaseTestAbstract
1111
{
@@ -21,7 +21,7 @@ public function testAdditionStatic($expected, $value1, $value2)
2121
// Must return an object of the correct type...
2222
$this->assertIsMatrixObject($result);
2323
// ... containing the correct data
24-
$this->assertMatrixValues($result, count($expected), count($expected[0]), $expected);
24+
$this->assertSame($expected, $result->toArray());
2525
}
2626

2727
/**
@@ -30,22 +30,69 @@ public function testAdditionStatic($expected, $value1, $value2)
3030
public function testAdditionInvoker($expected, $value1, $value2)
3131
{
3232
$matrix = new Matrix($value1);
33+
3334
$result = $matrix->add($value2);
3435

3536
// Must return an object of the correct type...
3637
$this->assertIsMatrixObject($matrix);
3738
// ... containing the correct data
38-
$this->assertMatrixValues($result, count($expected), count($expected[0]), $expected);
39+
$this->assertSame($expected, $result->toArray());
3940
// Verify that the original matrix remains unchanged
4041
$this->assertOriginalMatrixIsUnchanged($value1, $matrix);
4142
}
4243

4344
public function dataProvider()
4445
{
4546
return [
46-
[
47+
'square - 2x2 + 2x2' => [
4748
[[-1, 6], [-3, 12]],
48-
[[1, 2], [3, 4]], [[-2, 4], [-6, 8]],
49+
[[1, 2], [3, 4]],
50+
[[-2, 4], [-6, 8]],
51+
],
52+
'row vector/row vector' => [
53+
[[5, 7, 9]],
54+
[[1, 2, 3]],
55+
[[4, 5, 6]],
56+
],
57+
'column vector/column vector' => [
58+
[[5], [7], [9]],
59+
[[1], [2], [3]],
60+
[[4], [5], [6]],
61+
],
62+
'matrix 3x2 + 3x2' => [
63+
[[8, 10, 12], [14, 16, 18]],
64+
[[1, 2, 3], [4, 5, 6]],
65+
[[7, 8, 9], [10, 11, 12]],
66+
],
67+
'matrix 2x3 / 2x3' => [
68+
[[8, 14], [10, 16], [12, 18]],
69+
[[1, 4], [2, 5], [3, 6]],
70+
[[7, 10], [8, 11], [9, 12]],
71+
],
72+
];
73+
}
74+
75+
/**
76+
* @dataProvider dataProviderException
77+
*/
78+
public function testAdditionException($value1, $value2)
79+
{
80+
$this->expectException(Exception::class);
81+
$this->expectExceptionMessage('Matrices have mismatched dimensions');
82+
83+
Operations::add($value1, $value2);
84+
}
85+
86+
public function dataProviderException()
87+
{
88+
return [
89+
'row vector/column vector' => [
90+
[[1, 2, 3]],
91+
[[4], [5], [6]],
92+
],
93+
'column vector/row vector' => [
94+
[[1], [2], [3]],
95+
[[4, 5, 6]],
4996
],
5097
];
5198
}

unitTests/classes/src/Operations/dividebyTest.php

+53-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace MatrixTest\Operations;
44

5+
use Matrix\Exception;
56
use Matrix\Matrix;
67
use Matrix\Operations;
78
use MatrixTest\BaseTestAbstract;
8-
use function Matrix\divideby;
99

1010
class dividebyTest extends BaseTestAbstract
1111
{
@@ -44,8 +44,58 @@ public function dataProvider()
4444
{
4545
return [
4646
[
47-
[[2.5, -1.0], [6.0, -2.5]],
48-
[[1, 2], [3, 4]], [[-2, 4], [-6, 8]],
47+
'square - 2x2 * 2x2' => [[2.5, -1.0], [6.0, -2.5]],
48+
[[1, 2], [3, 4]],
49+
[[-2, 4], [-6, 8]],
50+
],
51+
];
52+
}
53+
54+
/**
55+
* @dataProvider dataProviderException
56+
*/
57+
public function testDivideByException($value1, $value2)
58+
{
59+
$this->expectException(Exception::class);
60+
$this->expectExceptionMessage('Division can only be calculated for a square matrix');
61+
62+
Operations::divideby($value1, $value2);
63+
}
64+
65+
public function dataProviderException()
66+
{
67+
return [
68+
'row vector/column vector' => [
69+
[[1, 2, 3]],
70+
[[4], [5], [6]],
71+
],
72+
'column vector/row vector' => [
73+
[[1], [2], [3]],
74+
[[4, 5, 6]],
75+
],
76+
'matrix 3x2 + 2x3' => [
77+
[[1, 2, 3], [4, 5, 6]],
78+
[[7, 10], [8, 11], [9, 12]],
79+
],
80+
'matrix 2x3 / 3x2' => [
81+
[[1, 4], [2, 5], [3, 6]],
82+
[[7, 8, 9], [10, 11, 12]],
83+
],
84+
'row vector/row vector' => [
85+
[[1, 2, 3]],
86+
[[4, 5, 6]],
87+
],
88+
'column vector/column vector' => [
89+
[[1], [2], [3]],
90+
[[4], [5], [6]],
91+
],
92+
'matrix 3x2 + 3x2' => [
93+
[[1, 2, 3], [4, 5, 6]],
94+
[[7, 8, 9], [10, 11, 12]],
95+
],
96+
'matrix 2x3 / 2x3' => [
97+
[[1, 4], [2, 5], [3, 6]],
98+
[[7, 10], [8, 11], [9, 12]],
4999
],
50100
];
51101
}

unitTests/classes/src/Operations/divideintoTest.php

+52-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace MatrixTest\Operations;
44

5+
use Matrix\Exception;
56
use Matrix\Matrix;
67
use Matrix\Operations;
78
use MatrixTest\BaseTestAbstract;
8-
use function Matrix\divideinto;
99

1010
class divideintoTest extends BaseTestAbstract
1111
{
@@ -45,7 +45,57 @@ public function dataProvider()
4545
return [
4646
[
4747
[[10, -4], [24, -10]],
48-
[[1, 2], [3, 4]], [[-2, 4], [-6, 8]],
48+
[[1, 2], [3, 4]],
49+
[[-2, 4], [-6, 8]],
50+
],
51+
];
52+
}
53+
54+
/**
55+
* @dataProvider dataProviderException
56+
*/
57+
public function testDivideIntoException($value1, $value2)
58+
{
59+
$this->expectException(Exception::class);
60+
$this->expectExceptionMessage('Division can only be calculated for a square matrix');
61+
62+
Operations::divideinto($value1, $value2);
63+
}
64+
65+
public function dataProviderException()
66+
{
67+
return [
68+
'row vector/column vector' => [
69+
[[1, 2, 3]],
70+
[[4], [5], [6]],
71+
],
72+
'column vector/row vector' => [
73+
[[1], [2], [3]],
74+
[[4, 5, 6]],
75+
],
76+
'matrix 3x2 + 2x3' => [
77+
[[1, 2, 3], [4, 5, 6]],
78+
[[7, 10], [8, 11], [9, 12]],
79+
],
80+
'matrix 2x3 / 3x2' => [
81+
[[1, 4], [2, 5], [3, 6]],
82+
[[7, 8, 9], [10, 11, 12]],
83+
],
84+
'row vector/row vector' => [
85+
[[1, 2, 3]],
86+
[[4, 5, 6]],
87+
],
88+
'column vector/column vector' => [
89+
[[1], [2], [3]],
90+
[[4], [5], [6]],
91+
],
92+
'matrix 3x2 + 3x2' => [
93+
[[1, 2, 3], [4, 5, 6]],
94+
[[7, 8, 9], [10, 11, 12]],
95+
],
96+
'matrix 2x3 / 2x3' => [
97+
[[1, 4], [2, 5], [3, 6]],
98+
[[7, 10], [8, 11], [9, 12]],
4999
],
50100
];
51101
}

unitTests/classes/src/Operations/multiplyTest.php

+59-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace MatrixTest\Operations;
44

5+
use Matrix\Exception;
56
use Matrix\Matrix;
67
use Matrix\Operations;
78
use MatrixTest\BaseTestAbstract;
8-
use function Matrix\multiply;
99

1010
class multiplyTest extends BaseTestAbstract
1111
{
@@ -21,7 +21,7 @@ public function testMultiplicationFunctionStatic($expected, $value1, $value2)
2121
// Must return an object of the correct type...
2222
$this->assertIsMatrixObject($result);
2323
// ... containing the correct data
24-
$this->assertMatrixValues($result, count($expected), count($expected[0]), $expected);
24+
$this->assertSame($expected, $result->toArray());
2525
}
2626

2727
/**
@@ -35,17 +35,71 @@ public function testMultiplicationInvoker($expected, $value1, $value2)
3535
// Must return an object of the correct type...
3636
$this->assertIsMatrixObject($matrix);
3737
// ... containing the correct data
38-
$this->assertMatrixValues($result, count($expected), count($expected[0]), $expected);
38+
$this->assertSame($expected, $result->toArray());
3939
// Verify that the original matrix remains unchanged
4040
$this->assertOriginalMatrixIsUnchanged($value1, $matrix);
4141
}
4242

4343
public function dataProvider()
4444
{
4545
return [
46-
[
46+
'square - 2x2 * 2x2' => [
4747
[[-14, 20], [-30, 44]],
48-
[[1, 2], [3, 4]], [[-2, 4], [-6, 8]],
48+
[[1, 2], [3, 4]],
49+
[[-2, 4], [-6, 8]],
50+
],
51+
'row vector/column vector' => [
52+
[[32]],
53+
[[1, 2, 3]],
54+
[[4], [5], [6]],
55+
],
56+
'column vector/row vector' => [
57+
[[4, 5, 6], [8, 10, 12], [12, 15, 18]],
58+
[[1], [2], [3]],
59+
[[4, 5, 6]],
60+
],
61+
'matrix 3x2 + 2x3' => [
62+
[[50, 68], [122,167]],
63+
[[1, 2, 3], [4, 5, 6]],
64+
[[7, 10], [8, 11], [9, 12]],
65+
],
66+
'matrix 2x3 / 3x2' => [
67+
[[47, 52, 57], [64, 71, 78], [81, 90, 99]],
68+
[[1, 4], [2, 5], [3, 6]],
69+
[[7, 8, 9], [10, 11, 12]],
70+
],
71+
];
72+
}
73+
74+
/**
75+
* @dataProvider dataProviderException
76+
*/
77+
public function testMultiplicationException($value1, $value2)
78+
{
79+
$this->expectException(Exception::class);
80+
$this->expectExceptionMessage('Matrices have mismatched dimensions');
81+
82+
Operations::multiply($value1, $value2);
83+
}
84+
85+
public function dataProviderException()
86+
{
87+
return [
88+
'row vector/row vector' => [
89+
[[1, 2, 3]],
90+
[[4, 5, 6]],
91+
],
92+
'column vector/column vector' => [
93+
[[1], [2], [3]],
94+
[[4], [5], [6]],
95+
],
96+
'matrix 3x2 + 3x2' => [
97+
[[1, 2, 3], [4, 5, 6]],
98+
[[7, 8, 9], [10, 11, 12]],
99+
],
100+
'matrix 2x3 / 2x3' => [
101+
[[1, 4], [2, 5], [3, 6]],
102+
[[7, 10], [8, 11], [9, 12]],
49103
],
50104
];
51105
}

0 commit comments

Comments
 (0)