diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2006-08-16 12:59:26 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2006-08-16 12:59:26 +0000 |
commit | bb028918891c1840d200530a0cb458db9a07cf95 (patch) | |
tree | 7f98afbf839f41a2e4ad27bb1fb9179f4ce96ee2 /tests | |
parent | ae311d59bf94eb135dfaa1c09dd93e1ebea4ee05 (diff) | |
download | zetacomponents-graph-bb028918891c1840d200530a0cb458db9a07cf95.zip zetacomponents-graph-bb028918891c1840d200530a0cb458db9a07cf95.tar.gz |
- Added matrix and polynom classes for future use in statistical datasets
Diffstat (limited to 'tests')
-rw-r--r-- | tests/matrix_test.php | 263 | ||||
-rw-r--r-- | tests/polynom_test.php | 122 | ||||
-rw-r--r-- | tests/suite.php | 4 |
3 files changed, 389 insertions, 0 deletions
diff --git a/tests/matrix_test.php b/tests/matrix_test.php new file mode 100644 index 0000000..0afd631 --- /dev/null +++ b/tests/matrix_test.php @@ -0,0 +1,263 @@ +<?php +/** + * ezcGraphMatrixTest + * + * @package Graph + * @version //autogen// + * @subpackage Tests + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Tests for ezcGraph class. + * + * @package ImageAnalysis + * @subpackage Tests + */ +class ezcGraphMatrixTest extends ezcTestCase +{ + + public static function suite() + { + return new ezcTestSuite( "ezcGraphMatrixTest" ); + } + + /** + * setUp + * + * @access public + */ + public function setUp() + { + } + + /** + * tearDown + * + * @access public + */ + public function tearDown() + { + } + + public function testCreateIdentityMatrix() + { + $matrix = new ezcGraphMatrix(); + + $this->assertEquals( + array( + array( 1, 0, 0 ), + array( 0, 1, 0 ), + array( 0, 0, 1 ), + ), + $this->getAttribute( $matrix, 'matrix' ) + ); + } + + public function testCreateCustomIdentityMatrix() + { + $matrix = new ezcGraphMatrix( 2, 4 ); + + $this->assertEquals( + array( + array( 1, 0, 0, 0 ), + array( 0, 1, 0, 0 ), + ), + $this->getAttribute( $matrix, 'matrix' ) + ); + } + + public function testCreateCustomMatrix() + { + $matrix = new ezcGraphMatrix( 2, 4, array( array( 1, 0, 5 ), array( 6, -1, -3, .5 ) ) ); + + $this->assertEquals( + array( + array( 1, 0, 5, 0 ), + array( 6, -1, -3, .5 ), + ), + $this->getAttribute( $matrix, 'matrix' ) + ); + } + + public function testGetMatrixValue() + { + $matrix = new ezcGraphMatrix( ); + + $this->assertEquals( + 1, + $matrix->get( 1, 1 ) + ); + } + + public function testSetMatrixValue() + { + $matrix = new ezcGraphMatrix( ); + + $matrix->set( 1, 2, .45 ); + + $this->assertEquals( + .45, + $matrix->get( 1, 2 ) + ); + } + + public function testSetInvalidMatrixValue() + { + $matrix = new ezcGraphMatrix( ); + + try + { + $matrix->set( 1, 32, .45 ); + } + catch ( ezcGraphMatrixOutOfBoundingsException $e ) + { + return true; + } + + $this->fail( 'Expected ezcGraphMatrixOutOfBoundingsException.' ); + } + + public function testGetInvalidMatrixValue() + { + $matrix = new ezcGraphMatrix( ); + + try + { + $matrix->get( 1, 32 ); + } + catch ( ezcGraphMatrixOutOfBoundingsException $e ) + { + return true; + } + + $this->fail( 'Expected ezcGraphMatrixOutOfBoundingsException.' ); + } + + public function testAddMatrices() + { + $matrix = new ezcGraphMatrix(); + $matrix->add( new ezcGraphMatrix() ); + + $this->assertEquals( + array( + array( 2, 0, 0 ), + array( 0, 2, 0 ), + array( 0, 0, 2 ), + ), + $this->getAttribute( $matrix, 'matrix' ) + ); + } + + public function testDiffMatrices() + { + $matrix = new ezcGraphMatrix(); + $matrix->diff( new ezcGraphMatrix() ); + + $this->assertEquals( + array( + array( 0, 0, 0 ), + array( 0, 0, 0 ), + array( 0, 0, 0 ), + ), + $this->getAttribute( $matrix, 'matrix' ) + ); + } + + public function testAddIncompatibleMatrices() + { + $matrix = new ezcGraphMatrix(); + + try + { + $matrix->add( new ezcGraphMatrix( 4, 4 ) ); + } + catch ( ezcGraphMatrixInvalidDimensionsException $e ) + { + return true; + } + + $this->fail( 'Expected ezcGraphMatrixInvalidDimensionsException.' ); + } + + public function testDiffIncompatibleMatrices() + { + $matrix = new ezcGraphMatrix(); + + try + { + $matrix->add( new ezcGraphMatrix( 4, 4 ) ); + } + catch ( ezcGraphMatrixInvalidDimensionsException $e ) + { + return true; + } + + $this->fail( 'Expected ezcGraphMatrixInvalidDimensionsException.' ); + } + + public function testScalarMultiplication() + { + $matrix = new ezcGraphMatrix(); + $matrix->scalar( -.5 ); + + $this->assertEquals( + array( + array( -.5, 0, 0 ), + array( 0, -.5, 0 ), + array( 0, 0, -.5 ), + ), + $this->getAttribute( $matrix, 'matrix' ) + ); + } + + public function testMatrixMultiplication() + { + $a = new ezcGraphMatrix( 2, 3, array( + array( 1, 2, 3 ), + array( 4, 5, 6 ), + ) ); + $b = new ezcGraphMatrix( 3, 2, array( + array( 6, -1 ), + array( 3, 2 ), + array( 0, -3 ), + ) ); + + $c = $a->multiply( $b ); + + $this->assertEquals( + array( + array( 12, -6 ), + array( 39, -12 ), + ), + $this->getAttribute( $c, 'matrix' ) + ); + } + + public function testMatrixMultiplication2() + { + $a = new ezcGraphMatrix( 2, 3, array( + array( 1, 2, 3 ), + array( 4, 5, 6 ), + ) ); + $b = new ezcGraphMatrix( 3, 3, array( + array( 6, -1 ), + array( 3, 2 ), + array( 0, -3 ), + ) ); + + try + { + $a->multiply( $b ); + } + catch( ezcGraphMatrixInvalidDimensionsException $e ) + { + return true; + } + + $this->fail( 'Expected ezcGraphMatrixInvalidDimensionsException.' ); + } +} + +?> diff --git a/tests/polynom_test.php b/tests/polynom_test.php new file mode 100644 index 0000000..c46d8c5 --- /dev/null +++ b/tests/polynom_test.php @@ -0,0 +1,122 @@ +<?php +/** + * ezcGraphPolynomTest + * + * @package Graph + * @version //autogen// + * @subpackage Tests + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Tests for ezcGraph class. + * + * @package ImageAnalysis + * @subpackage Tests + */ +class ezcGraphPolynomTest extends ezcTestCase +{ + + public static function suite() + { + return new ezcTestSuite( "ezcGraphPolynomTest" ); + } + + /** + * setUp + * + * @access public + */ + public function setUp() + { + } + + /** + * tearDown + * + * @access public + */ + public function tearDown() + { + } + + public function testCreatePolynom() + { + $polynom = new ezcGraphPolynom( array( 2 => 1 ) ); + + $this->assertEquals( + 'x^2', + $polynom->__to_string() + ); + } + + public function testCreatePolynom2() + { + $polynom = new ezcGraphPolynom( array( 2 => .5, 1 => 3, 0 => -4.5 ) ); + + $this->assertEquals( + '0.50 * x^2 + 3.00 * x + -4.50', + $polynom->__to_string() + ); + } + + public function testPolynomGetOrder() + { + $polynom = new ezcGraphPolynom( array( 2 => .5, 1 => 3, 0 => -4.5 ) ); + + $this->assertEquals( + 2, + $polynom->getOrder() + ); + } + + public function testAddPolynom() + { + $polynom = new ezcGraphPolynom( array( 2 => .5, 1 => 3, 0 => -4.5 ) ); + $polynom->add( new ezcGraphPolynom( array( 2 => 1 ) ) ); + + $this->assertEquals( + '1.50 * x^2 + 3.00 * x + -4.50', + $polynom->__to_string() + ); + } + + public function testEvaluatePolynom() + { + $polynom = new ezcGraphPolynom( array( 2 => 1 ) ); + + $this->assertEquals( + 4., + $polynom->evaluate( 2 ), + 'Calculated wrong value', + .1 + ); + } + + public function testEvaluatePolynomNegativeValue() + { + $polynom = new ezcGraphPolynom( array( 2 => 1 ) ); + + $this->assertEquals( + 4., + $polynom->evaluate( -2 ), + 'Calculated wrong value', + .1 + ); + } + + public function testEvaluateComplexPolynom() + { + $polynom = new ezcGraphPolynom( array( 2 => .5, 1 => 3, 0 => -4.5 ) ); + + $this->assertEquals( + 9., + $polynom->evaluate( 3 ), + 'Calculated wrong value', + .1 + ); + } +} + +?> diff --git a/tests/suite.php b/tests/suite.php index 71c4aca..2237fdd 100644 --- a/tests/suite.php +++ b/tests/suite.php @@ -30,6 +30,8 @@ require_once 'driver_gd_test.php'; require_once 'driver_svg_test.php'; require_once 'font_test.php'; require_once 'palette_test.php'; +require_once 'matrix_test.php'; +require_once 'polynom_test.php'; /** * Test suite for ImageAnalysis package. @@ -62,6 +64,8 @@ class ezcGraphSuite extends ezcTestSuite $this->addTest( ezcGraphFontTest::suite() ); $this->addTest( ezcGraphTextTest::suite() ); $this->addTest( ezcGraphPaletteTest::suite() ); + $this->addTest( ezcGraphMatrixTest::suite() ); + $this->addTest( ezcGraphPolynomTest::suite() ); } public static function suite() |