Created
June 24, 2015 14:15
-
-
Save HarishChaudhari/8aa081455bfb405d53dc to your computer and use it in GitHub Desktop.
Polish Calculator Unit tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* First, download phpunit.phar from www.phpunit.de | |
* Navigate to PHP bin folder and enter following command to execute tests: | |
* | |
* php.exe phpunit.phar --bootstrap c:\wamp\www\unit\src\polishcal\polishcalculator.php c:\wamp\www\unit\tests\polishcal_test\polishcalculator_test.php | |
* | |
*/ | |
class polishcalculatorTest extends PHPunit_Framework_Testcase { | |
/* | |
* Testing the addition function | |
*/ | |
private $expected ; | |
private $postfixstring ; | |
public function testAdd() { | |
//include('../my_functions.php'); // must include if tests are for non OOP code | |
$this->expected = 7 ; | |
$this->postfixstring = "3 4 +"; | |
$polishtestObj = new polishcalculator($this->postfixstring); | |
$result = $polishtestObj->evaluate(); | |
$this->assertEquals($this->expected, $result); | |
} | |
public function testSubstract() { | |
//include('../my_functions.php'); // must include if tests are for non OOP code | |
$this->expected = -7 ; | |
$this->postfixstring = "3 10 -"; | |
$polishtestObj = new polishcalculator($this->postfixstring); | |
$result = $polishtestObj->evaluate(); | |
$this->assertEquals($this->expected, $result); | |
} | |
public function testMultiply() { | |
//include('../my_functions.php'); // must include if tests are for non OOP code | |
$this->expected = 14; | |
$this->postfixstring = "7 2 *"; | |
$polishtestObj = new polishcalculator($this->postfixstring); | |
$result = $polishtestObj->evaluate(); | |
$this->assertEquals($this->expected, $result); | |
} | |
public function testDivide() { | |
//include('../my_functions.php'); // must include if tests are for non OOP code | |
$this->expected = 5 ; | |
$this->postfixstring = "10 2 /"; | |
$polishtestObj = new polishcalculator($this->postfixstring); | |
$result = $polishtestObj->evaluate(); | |
$this->assertEquals($this->expected, $result); | |
} | |
public function testPercent() { | |
//include('../my_functions.php'); // must include if tests are for non OOP code | |
$this->expected = 1; | |
$this->postfixstring = "50 % 2 *"; | |
$polishtestObj = new polishcalculator($this->postfixstring); | |
$result = $polishtestObj->evaluate(); | |
$this->assertEquals($this->expected, $result); | |
} | |
public function testLongExpr1() { | |
//include('../my_functions.php'); // must include if tests are for non OOP code | |
$this->expected = 4; | |
$this->postfixstring = "6 2 * 3 /"; | |
$polishtestObj = new polishcalculator($this->postfixstring); | |
$result = $polishtestObj->evaluate(); | |
$this->assertEquals($this->expected, $result); | |
} | |
public function testLongExpr2() { | |
//include('../my_functions.php'); // must include if tests are for non OOP code | |
$this->expected = 17 ; | |
$this->postfixstring = "2 3 ^ 4 5 + +"; | |
$polishtestObj = new polishcalculator($this->postfixstring); | |
$result = $polishtestObj->evaluate(); | |
$this->assertEquals($this->expected, $result); | |
} | |
public function testLongExpr3() { | |
//include('../my_functions.php'); // must include if tests are for non OOP code | |
$this->expected = 14 ; | |
$this->postfixstring = "5 1 2 + 4 * + 3 -"; | |
$polishtestObj = new polishcalculator($this->postfixstring); | |
$result = $polishtestObj->evaluate(); | |
$this->assertEquals($this->expected, $result); | |
} | |
public function testEmptyStringShouldMatchToZero() { | |
//include('../my_functions.php'); // must include if tests are for non OOP code | |
$this->expected = 0 ; | |
$this->postfixstring = ""; | |
$polishtestObj = new polishcalculator($this->postfixstring); | |
$result = $polishtestObj->evaluate(); | |
$this->assertEquals($this->expected, $result); | |
} | |
// Failings tests | |
public function testFailing1() { | |
//include('../my_functions.php'); // must include if tests are for non OOP code | |
$this->expected = 6 ; | |
$this->postfixstring = "10 2 /"; | |
$polishtestObj = new polishcalculator($this->postfixstring); | |
$result = $polishtestObj->evaluate(); | |
$this->assertEquals($this->expected, $result); | |
} | |
public function testFailing2() { | |
//include('../my_functions.php'); // must include if tests are for non OOP code | |
$this->expected = 7; | |
$this->postfixstring = "50 % 2 *"; | |
$polishtestObj = new polishcalculator($this->postfixstring); | |
$result = $polishtestObj->evaluate(); | |
$this->assertEquals($this->expected, $result); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment