Created
January 5, 2015 18:16
-
-
Save nelsonsar/e1a8c7e6b5f10ea74fe8 to your computer and use it in GitHub Desktop.
Is Fibonacci?
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
<?php | |
class FibonacciValidator | |
{ | |
public function isFibonacci($number) | |
{ | |
$positiveVerification = (5 * pow($number, 2) + 4); | |
$negativeVerification = (5 * pow($number, 2) - 4); | |
return ($positiveVerification % 4 == 0 || $negativeVerification % 4 == 0); | |
} | |
} |
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
<?php | |
class FibonacciValidatorTest extends \PHPUnit_Framework_TestCase | |
{ | |
private $validator = null; | |
protected function setUp() | |
{ | |
$this->validator = new FibonacciValidator; | |
} | |
protected function tearDown() | |
{ | |
$this->validator = null; | |
} | |
/** | |
* @dataProvider validNumbersDataProvider | |
*/ | |
public function testShouldReturnTrueWhenNumberIsInSequence($validNumber) | |
{ | |
$this->assertTrue($this->validator->isFibonacciNumber($validNumber)); | |
} | |
/** | |
* @dataProvider invalidNumbersDataProvider | |
*/ | |
public function testShouldReturnFalseWhenNumberIsNotInSequence() | |
{ | |
$this->assertFalse($this->validator->isFibonacciNumber($validNumber)); | |
} | |
public function validNumbersDataProvider() | |
{ | |
return array(1, 1, 2, 3, 5, 8, 13); | |
} | |
public function invalidNumbersDataProvider() | |
{ | |
return array(4, 7, 12); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment