Permalink
Browse files

add unit tests

  • Loading branch information...
Firman Wandayandi
Firman Wandayandi committed Nov 10, 2008
1 parent 91db16e commit 8e791342c609f332019dbb2b08e83ef4ca62fb56
View
@@ -0,0 +1,54 @@
+<?php
+/**
+ * All test suite
+ *
+ * PHP Version 5
+ *
+ * @package Math_Numerical_RootFinding
+ * @subpackage UnitTests
+ * @link http://pear.php.net/package/Math_Numerical_RootFinding
+ * @version CVS: $Id$
+ * @since File available since Release 1.1.0a1
+ */
+if (!defined('PHPUnit_MAIN_METHOD')) {
+ define('PHPUnit_MAIN_METHOD', 'PEAR_Size_AllTests::main');
+}
+
+require_once 'PHPUnit/Framework/TestSuite.php';
+require_once 'PHPUnit/TextUI/TestRunner.php';
+
+require_once 'GeneralTest.php';
+require_once 'Method/BisectionTest.php';
+require_once 'Method/FalsePositionTest.php';
+require_once 'Method/FixedPointTest.php';
+require_once 'Method/NewtonRaphsonTest.php';
+require_once 'Method/NewtonRaphson2Test.php';
+require_once 'Method/RalstonRabinowitzTest.php';
+require_once 'Method/SecantTest.php';
+
+class Math_Numerical_RootFinding_AllTests
+{
+ public static function main()
+ {
+ PHPUnit_TextUI_TestRunner::run(self::suite());
+ }
+
+ public static function suite()
+ {
+ $suite = new PHPUnit_Framework_TestSuite('Math_Numerical_RootFinding All Tests');
+ $suite->addTestSuite('Math_Numerical_RootFinding_GeneralTest');
+ $suite->addTestSuite('Math_Numerical_RootFinding_BisectionTest');
+ $suite->addTestSuite('Math_Numerical_RootFinding_FalsePositionTest');
+ $suite->addTestSuite('Math_Numerical_RootFinding_FixedPointTest');
+ $suite->addTestSuite('Math_Numerical_RootFinding_NewtonRaphsonTest');
+ $suite->addTestSuite('Math_Numerical_RootFinding_NewtonRaphson2Test');
+ $suite->addTestSuite('Math_Numerical_RootFinding_RalstonRabinowitzTest');
+ $suite->addTestSuite('Math_Numerical_RootFinding_SecantTest');
+
+ return $suite;
+ }
+}
+
+if (PHPUnit_MAIN_METHOD == 'Math_Numerical_RootFinding_AllTests::main') {
+ Math_Numerical_RootFinding_AllTests::main();
+}
View
@@ -0,0 +1,75 @@
+<?php
+/**
+ * General test case
+ *
+ * PHP Version 5
+ *
+ * @package Math_Numerical_RootFinding
+ * @subpackage UnitTests
+ * @link http://pear.php.net/package/Math_Numerical_RootFinding
+ * @version CVS: $Id$
+ * @since File available since Release 1.1.0a1
+ */
+if (!defined('PHPUnit_MAIN_METHOD')) {
+ define('PHPUnit_MAIN_METHOD', 'Math_Numerical_RootFinding_GeneralTest::main');
+}
+
+require_once 'PHPUnit/Framework/TestCase.php';
+require_once 'PHPUnit/Framework/TestSuite.php';
+
+require_once 'Math/Numerical/RootFinding.php';
+
+class Math_Numerical_RootFinding_GeneralTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * Runs the test methods of this class.
+ *
+ * @return void
+ */
+ public static function main()
+ {
+ include_once "PHPUnit/TextUI/TestRunner.php";
+
+ $suite = new PHPUnit_Framework_TestSuite('Math_Numerical_RootFinding General Test');
+ PHPUnit_TextUI_TestRunner::run($suite);
+ }
+
+ public function testFactory()
+ {
+ $i = Math_Numerical_RootFinding::factory('Bisection');
+ $this->assertTrue($i instanceof Math_Numerical_RootFinding_Bisection);
+
+ $i = Math_Numerical_RootFinding::factory('FalsePosition');
+ $this->assertTrue($i instanceof Math_Numerical_RootFinding_FalsePosition);
+
+ $i = Math_Numerical_RootFinding::factory('FixedPoint');
+ $this->assertTrue($i instanceof Math_Numerical_RootFinding_FixedPoint);
+
+ $i = Math_Numerical_RootFinding::factory('NewtonRaphson');
+ $this->assertTrue($i instanceof Math_Numerical_RootFinding_NewtonRaphson);
+
+ $i = Math_Numerical_RootFinding::factory('NewtonRaphson2');
+ $this->assertTrue($i instanceof Math_Numerical_RootFinding_NewtonRaphson2);
+
+ $i = Math_Numerical_RootFinding::factory('RalstonRabinowitz');
+ $this->assertTrue($i instanceof Math_Numerical_RootFinding_RalstonRabinowitz);
+
+ $i = Math_Numerical_RootFinding::factory('Secant');
+ $this->assertTrue($i instanceof Math_Numerical_RootFinding_Secant);
+ }
+
+ public function testSetOptions()
+ {
+ $options = array(
+ 'max_iteration' => 20,
+ 'divergent_skip' => false
+ );
+
+ $o = Math_Numerical_RootFinding::factory('Bisection', $options);
+ $this->assertTrue($o instanceof Math_Numerical_RootFinding_Bisection);
+
+ $this->assertEquals($o->get('max_iteration'), $options['max_iteration']);
+ $this->assertFalse($o->get('divergent_skip'));
+ }
+}
+?>
@@ -0,0 +1,59 @@
+<?php
+/**
+ * Bisection test case
+ *
+ * PHP Version 5
+ *
+ * @package Math_Numerical_RootFinding
+ * @subpackage UnitTests
+ * @link http://pear.php.net/package/Math_Numerical_RootFinding
+ * @version CVS: $Id$
+ * @since File available since Release 1.1.0a1
+ */
+if (!defined('PHPUnit_MAIN_METHOD')) {
+ define('PHPUnit_MAIN_METHOD', 'Math_Numerical_RootFinding_BisectionTest::main');
+}
+
+require_once 'PHPUnit/Framework/TestCase.php';
+require_once 'PHPUnit/Framework/TestSuite.php';
+
+require_once 'Math/Numerical/RootFinding.php';
+
+class Math_Numerical_RootFinding_BisectionTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * Runs the test methods of this class.
+ *
+ * @return void
+ */
+ public static function main()
+ {
+ include_once 'PHPUnit/TextUI/TestRunner.php';
+
+ $suite = new PHPUnit_Framework_TestSuite('Math_Numerical_RootFinding Bisection Test');
+ PHPUnit_TextUI_TestRunner::run($suite);
+ }
+
+
+ public function testCompute()
+ {
+ $o = Math_Numerical_RootFinding::factory('Bisection');
+ $res = $o->compute(array(get_class($this), 'Fx'), 0, 1);
+ if (PEAR::isError($res)) {
+ $this->fail('Error has returned from compute(): ' . $res->getMessage());
+ }
+
+ $exact_root = 0.56714329;
+
+ $this->assertLessThanOrEqual($o->get('max_iteration'), $o->getIterationCount(), 'Invalid iteration count');
+ $this->assertLessThanOrEqual($o->get('err_tolerance'), $o->getEpsError());
+ $this->assertGreaterThanOrEqual($exact_root - $o->get('err_tolerance'), $o->getRoot());
+ $this->assertLessThanOrEqual($exact_root + $o->get('err_tolerance'), $o->getRoot());
+ }
+
+ public static function Fx($x)
+ {
+ return pow(M_E, -$x) - $x;
+ }
+}
+?>
@@ -0,0 +1,59 @@
+<?php
+/**
+ * False Position test case
+ *
+ * PHP Version 5
+ *
+ * @package Math_Numerical_RootFinding
+ * @subpackage UnitTests
+ * @link http://pear.php.net/package/Math_Numerical_RootFinding
+ * @version CVS: $Id$
+ * @since File available since Release 1.1.0a1
+ */
+if (!defined('PHPUnit_MAIN_METHOD')) {
+ define('PHPUnit_MAIN_METHOD', 'Math_Numerical_RootFinding_FalsePositionTest::main');
+}
+
+require_once 'PHPUnit/Framework/TestCase.php';
+require_once 'PHPUnit/Framework/TestSuite.php';
+
+require_once 'Math/Numerical/RootFinding.php';
+
+class Math_Numerical_RootFinding_FalsePositionTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * Runs the test methods of this class.
+ *
+ * @return void
+ */
+ public static function main()
+ {
+ include_once 'PHPUnit/TextUI/TestRunner.php';
+
+ $suite = new PHPUnit_Framework_TestSuite('Math_Numerical_RootFinding FalsePosition Test');
+ PHPUnit_TextUI_TestRunner::run($suite);
+ }
+
+
+ public function testCompute()
+ {
+ $o = Math_Numerical_RootFinding::factory('FalsePosition');
+ $res = $o->compute(array(get_class($this), 'Fx'), 0, 1);
+ if (PEAR::isError($res)) {
+ $this->fail('Error has returned from compute(): ' . $res->getMessage());
+ }
+
+ $exact_root = 0.56714329;
+
+ $this->assertLessThanOrEqual($o->get('max_iteration'), $o->getIterationCount(), 'Invalid iteration count');
+ $this->assertLessThanOrEqual($o->get('err_tolerance'), $o->getEpsError());
+ $this->assertGreaterThanOrEqual($exact_root - $o->get('err_tolerance'), $o->getRoot());
+ $this->assertLessThanOrEqual($exact_root + $o->get('err_tolerance'), $o->getRoot());
+ }
+
+ public static function Fx($x)
+ {
+ return pow(M_E, -$x) - $x;
+ }
+}
+?>
@@ -0,0 +1,60 @@
+<?php
+/**
+ * Fixed Point test case
+ *
+ * PHP Version 5
+ *
+ * @package Math_Numerical_RootFinding
+ * @subpackage UnitTests
+ * @link http://pear.php.net/package/Math_Numerical_RootFinding
+ * @version CVS: $Id$
+ * @since File available since Release 1.1.0a1
+ */
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+ define('PHPUnit_MAIN_METHOD', 'Math_Numerical_RootFinding_FixedPointTest::main');
+}
+
+require_once 'PHPUnit/Framework/TestCase.php';
+require_once 'PHPUnit/Framework/TestSuite.php';
+
+require_once 'Math/Numerical/RootFinding.php';
+
+class Math_Numerical_RootFinding_FixedPointTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * Runs the test methods of this class.
+ *
+ * @return void
+ */
+ public static function main()
+ {
+ include_once 'PHPUnit/TextUI/TestRunner.php';
+
+ $suite = new PHPUnit_Framework_TestSuite('Math_Numerical_RootFinding FixedPoint Test');
+ PHPUnit_TextUI_TestRunner::run($suite);
+ }
+
+
+ public function testCompute()
+ {
+ $o = Math_Numerical_RootFinding::factory('FixedPoint');
+ $res = $o->compute(array(get_class($this), 'Gx'), 0);
+ if (PEAR::isError($res)) {
+ $this->fail('Error has returned from compute(): ' . $res->getMessage());
+ }
+
+ $exact_root = 0.56714329;
+
+ $this->assertLessThanOrEqual($o->get('max_iteration'), $o->getIterationCount(), 'Invalid iteration count');
+ $this->assertLessThanOrEqual($o->get('err_tolerance'), $o->getEpsError());
+ $this->assertGreaterThanOrEqual($exact_root - $o->get('err_tolerance'), $o->getRoot());
+ $this->assertLessThanOrEqual($exact_root + $o->get('err_tolerance'), $o->getRoot());
+ }
+
+ public static function Gx($x)
+ {
+ return pow(M_E, -$x);
+ }
+}
+?>
@@ -0,0 +1,69 @@
+<?php
+/**
+ * Newton-Raphson 2 test case
+ *
+ * PHP Version 5
+ *
+ * @package Math_Numerical_RootFinding
+ * @subpackage UnitTests
+ * @link http://pear.php.net/package/Math_Numerical_RootFinding
+ * @version CVS: $Id$
+ * @since File available since Release 1.1.0a1
+ */
+if (!defined('PHPUnit_MAIN_METHOD')) {
+ define('PHPUnit_MAIN_METHOD', 'Math_Numerical_RootFinding_NewtonRaphson2Test::main');
+}
+
+require_once 'PHPUnit/Framework/TestCase.php';
+require_once 'PHPUnit/Framework/TestSuite.php';
+
+require_once 'Math/Numerical/RootFinding.php';
+
+class Math_Numerical_RootFinding_NewtonRaphson2Test extends PHPUnit_Framework_TestCase
+{
+ /**
+ * Runs the test methods of this class.
+ *
+ * @return void
+ */
+ public static function main()
+ {
+ include_once 'PHPUnit/TextUI/TestRunner.php';
+
+ $suite = new PHPUnit_Framework_TestSuite('Math_Numerical_RootFinding NewtonRaphson2 Test');
+ PHPUnit_TextUI_TestRunner::run($suite);
+ }
+
+
+ public function testCompute()
+ {
+ $o = Math_Numerical_RootFinding::factory('NewtonRaphson2');
+ $res = $o->compute(array(get_class($this), 'Fx'), array(get_class($this), 'D1x'), array(get_class($this), 'D2x'), 0);
+ if (PEAR::isError($res)) {
+ $this->fail('Error has returned from compute(): ' . $res->getMessage());
+ }
+
+ $exact_root = 1;
+
+ $this->assertLessThanOrEqual($o->get('max_iteration'), $o->getIterationCount(), 'Invalid iteration count');
+ $this->assertLessThanOrEqual($o->get('err_tolerance'), $o->getEpsError());
+ $this->assertGreaterThanOrEqual($exact_root - $o->get('err_tolerance'), $o->getRoot());
+ $this->assertLessThanOrEqual($exact_root + $o->get('err_tolerance'), $o->getRoot());
+ }
+
+ public static function Fx($x)
+ {
+ return pow($x, 3) - (5 * pow($x,2)) + (7 * $x) - 3;
+ }
+
+ public static function D1x($x)
+ {
+ return 3 * pow($x, 2) - (10 * $x) + 7;
+ }
+
+ public static function D2x($x)
+ {
+ return 6 * $x - 10;
+ }
+}
+?>
Oops, something went wrong.

0 comments on commit 8e79134

Please sign in to comment.