Created
May 19, 2012 18:38
-
-
Save lawlesst/2731890 to your computer and use it in GitHub Desktop.
PHPUnit tests for a prime number calculator.
This file contains hidden or 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
---- test.php ---- | |
/** | |
* Unit tests for the prime number calculator included | |
* | |
* | |
*/ | |
<?php | |
require_once 'PHPUnit.php'; | |
require_once 'calcprime.php'; | |
class PrimeTest extends PHPUnit_TestCase | |
{ | |
// constructor of the test suite | |
function PrimeTest($name) { | |
$this->PHPUnit_TestCase($name); | |
} | |
public function testSinglePrimeNumbers() | |
{ | |
// Check known prime. | |
$number = '1409'; | |
$this->assertTrue(isPrime($number)); | |
} | |
public function testSingleNonPrimeNumbers() | |
{ | |
// Check known non-prime. | |
$number = '4'; | |
$this->assertFalse(isPrime($number)); | |
} | |
public function testNextTen() | |
{ | |
//Check known list of ten primes. | |
$primes = array(547,557,563,569,571,577,587,593,599,601); | |
$foundprimes = nextPrimes(546); | |
$this->assertEquals($primes, $foundprimes); | |
//Fail the test to make sure we are doing as we expect | |
$foundprimes = nextPrimes(563); | |
$this->assertFalse($primes == $foundprimes); | |
//Check upper bound | |
//See http://primes.utm.edu/lists/small/10000.txt | |
$primes = array(99991); | |
$foundprimes = nextPrimes(99990); | |
$this->assertEquals($primes, $foundprimes); | |
} | |
public function testInRange() | |
{ | |
//strings should fail | |
$this->assertFalse(checkInRange('bob')); | |
//negative numbers should fail | |
$this->assertFalse(checkInRange(-5)); | |
//zero should fail | |
$this->assertFalse(checkInRange(0)); | |
//positive integer less than 100,000 should pass | |
$this->assertTrue(checkInRange(5)); | |
//numbers greater than 100,000 should fail | |
$this->assertFalse(checkInRange(500000)); | |
//string values with commas should pass | |
$this->assertTrue(checkInRange('1,200')); | |
} | |
} | |
$suite = new PHPUnit_TestSuite("PrimeTest"); | |
$result = PHPUnit::run($suite); | |
echo $result -> toString(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment