Skip to content

Instantly share code, notes, and snippets.

@nelsonsar
Created January 5, 2015 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nelsonsar/e1a8c7e6b5f10ea74fe8 to your computer and use it in GitHub Desktop.
Save nelsonsar/e1a8c7e6b5f10ea74fe8 to your computer and use it in GitHub Desktop.
Is Fibonacci?
<?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);
}
}
<?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