Skip to content

Commit

Permalink
Improving provider tests in Core
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanRL committed Oct 27, 2022
1 parent 6efbbc6 commit 45a86e4
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 48 deletions.
1 change: 1 addition & 0 deletions phpunit.xml
Expand Up @@ -28,6 +28,7 @@
</coverage>
<logging>
<junit outputFile="build/logs/junit.xml"/>
<testdoxHtml outputFile="build/logs/testdox.html"/>
</logging>
<testsuites>
<testsuite name="Core">
Expand Down
17 changes: 7 additions & 10 deletions src/Samsara/Fermat/Core/Provider/SequenceProvider.php
Expand Up @@ -126,7 +126,7 @@ public static function nthEvenNumber(int $n, int $scale = null, bool $asCollecti

return $sequence;
}
if ($n > (PHP_INT_MAX/2)) {
if ($n >= (PHP_INT_MAX/2)) {
$n = new ImmutableDecimal($n, $scale);

return $n->multiply(2);
Expand Down Expand Up @@ -310,17 +310,15 @@ public static function nthPrimeNumbers(int $n): NumberCollection
{
$collection = new NumberCollection();

/** @noinspection PhpUnhandledExceptionInspection */
$collection->push(new ImmutableDecimal(2));

/** @noinspection PhpUnhandledExceptionInspection */
$currentPrime = new ImmutableDecimal(3);

for ($i = 1;$i < $n;$i++) {
while (!$currentPrime->isPrime()) {
$currentPrime = $currentPrime->add(2);
}

$collection->push($currentPrime);
$currentPrime = $currentPrime->add(2);
$currentPrime = self::_nextprime($currentPrime);
}

return $collection;
Expand Down Expand Up @@ -415,8 +413,8 @@ public static function nthLucasNumber(int $n): ImmutableDecimal
);
}

[$F1, $fib] = self::_fib(new ImmutableDecimal($n-1));
[, $F2] = self::_fib($fib);
[$F1,] = self::_fib(new ImmutableDecimal($n-1));
[,$F2] = self::_fib(new ImmutableDecimal($n));

return $F1->add($F2);

Expand All @@ -432,7 +430,7 @@ public static function nthLucasNumber(int $n): ImmutableDecimal
public static function nthTriangularNumber(int $n): ImmutableDecimal
{

if ($n < 1) {
if ($n < 0) {
throw new IntegrityConstraint(
'Negative term numbers not valid for Triangular Numbers',
'Provide a positive term number',
Expand All @@ -449,7 +447,6 @@ public static function nthTriangularNumber(int $n): ImmutableDecimal
/**
* @param ImmutableDecimal $number
* @return ImmutableDecimal[]
* @throws IntegrityConstraint
*/
private static function _fib(ImmutableDecimal $number): array
{
Expand Down
23 changes: 23 additions & 0 deletions tests/Samsara/Fermat/Core/Provider/CalculationModeProviderTest.php
@@ -0,0 +1,23 @@
<?php

namespace Provider;

use Samsara\Fermat\Core\Enums\CalcMode;
use Samsara\Fermat\Core\Provider\CalculationModeProvider;
use PHPUnit\Framework\TestCase;

class CalculationModeProviderTest extends TestCase
{

public function testGetCurrentMode()
{
$this->assertEquals(CalcMode::Auto, CalculationModeProvider::getCurrentMode());
}

public function testSetCurrentMode()
{
CalculationModeProvider::setCurrentMode(CalcMode::Precision);
$this->assertEquals(CalcMode::Precision, CalculationModeProvider::getCurrentMode());
CalculationModeProvider::setCurrentMode(CalcMode::Auto);
}
}
Expand Up @@ -467,7 +467,7 @@ public function testRoundHalfStochastic()

$this->assertEqualsWithDelta(30, $sum, 10);

$num1 = new ImmutableDecimal('-1.5');
$num1 = new ImmutableDecimal('-1.53674');

$sum = 0;
for ($i=0;$i<20;$i++) {
Expand Down
120 changes: 83 additions & 37 deletions tests/Samsara/Fermat/Core/Provider/SequenceProviderTest.php
Expand Up @@ -11,22 +11,24 @@
*/
class SequenceProviderTest extends TestCase
{

/**
* @small
*/
public function testOddNumber()
{

$five = SequenceProvider::nthOddNumber(2);

$this->assertEquals('5', $five->getValue());

$firstFiveOdds = SequenceProvider::nthOddNumber(0, null, true, 5);

$this->assertEquals(5, $firstFiveOdds->count());

$this->assertEquals('5', $firstFiveOdds->get(2)->getValue());

$phpIntMax = SequenceProvider::nthOddNumber(PHP_INT_MAX/2);
$expected = (new ImmutableDecimal(PHP_INT_MAX))->add(2);
$this->assertEquals($expected->getValue(), $phpIntMax->getValue());

}

/**
Expand All @@ -36,15 +38,16 @@ public function testEvenNumber()
{

$four = SequenceProvider::nthEvenNumber(2);

$this->assertEquals('4', $four->getValue());

$firstFiveEvens = SequenceProvider::nthEvenNumber(0, null, true, 5);

$this->assertEquals(5, $firstFiveEvens->count());

$this->assertEquals('4', $firstFiveEvens->get(2)->getValue());

$phpIntMax = SequenceProvider::nthEvenNumber(PHP_INT_MAX/2);
$expected = (new ImmutableDecimal(PHP_INT_MAX))->add(1);
$this->assertEquals($expected->getValue(), $phpIntMax->getValue());

}

/**
Expand All @@ -54,131 +57,174 @@ public function testPowNegOne()
{

$one = SequenceProvider::nthPowerNegativeOne(2);

$this->assertEquals('1', $one->getValue());

$firstFivePows = SequenceProvider::nthPowerNegativeOne(0, true, 5);

$this->assertEquals(5, $firstFivePows->count());

$this->assertEquals('-1', $firstFivePows->get(1)->getValue());
$this->assertEquals('1', $firstFivePows->get(2)->getValue());
$this->assertEquals('-1', $firstFivePows->get(3)->getValue());

}

/**
* @medium
* @small
*/
public function testNthEulerZigzag()
{

$fifth = SequenceProvider::nthEulerZigzag(5);

$this->assertEquals('16', $fifth->getValue());

$eulerZigzagCollection = SequenceProvider::nthEulerZigzag(2, true, 3);

$this->assertEquals(3, $eulerZigzagCollection->count());
$this->assertEquals('5', $eulerZigzagCollection->get(2)->getValue());

$this->expectException(IntegrityConstraint::class);
$this->expectExceptionMessage('This library does not support the Euler Zigzag Sequence (OEIS: A000111) beyond E(50)');

SequenceProvider::nthEulerZigzag(51);
}

/**
* @medium
*/
public function testNthBernoulliNumber()
{

$zero = SequenceProvider::nthBernoulliNumber(0);

$this->assertEquals('1', $zero->getValue());

$one = SequenceProvider::nthBernoulliNumber(1);

$this->assertEquals('-0.5', $one->getValue());

// $two = SequenceProvider::nthBernoulliNumber(2);

// $this->assertEquals('0.16666', $two->truncateToScale(5)->getValue());
$two = SequenceProvider::nthBernoulliNumber(2);
$this->assertEquals('0.16667', $two->getValue());

$three = SequenceProvider::nthBernoulliNumber(3);

$this->assertEquals('0', $three->getValue());

$four = SequenceProvider::nthBernoulliNumber(4);

$this->assertEquals('-0.03333', $four->getValue());

$six = SequenceProvider::nthBernoulliNumber(6);

$this->assertEquals('0.02381', $six->getValue());

$eighteen = SequenceProvider::nthBernoulliNumber(18);

$this->assertEquals('54.97118', $eighteen->getValue());

$eighteen = SequenceProvider::nthBernoulliNumber(18, 20);

$this->assertEquals('54.97117794486215538847', $eighteen->getValue());

$twenty = SequenceProvider::nthBernoulliNumber(20);

$this->assertEquals('-529.12424', $twenty->getValue());

$twentyTwo = SequenceProvider::nthBernoulliNumber(22);

$this->assertEquals('6192.12319', $twentyTwo->getValue());

}

/**
* @medium
* @small
*/
public function testNthPrimeNumbers()
{
$five = SequenceProvider::nthPrimeNumbers(5);
$this->assertEquals(5, $five->count());
$this->assertEquals('11', $five->get(4)->getValue());
}

/**
* @small
*/
public function testNthFibonacciNumber()
{

$zero = SequenceProvider::nthFibonacciNumber(0);

$this->assertEquals('0', $zero->getValue());

$one = SequenceProvider::nthFibonacciNumber(1);

$this->assertEquals('1', $one->getValue());

$two = SequenceProvider::nthFibonacciNumber(2);

$this->assertEquals('1', $two->getValue());

$eight = SequenceProvider::nthFibonacciNumber(8);

$this->assertEquals('21', $eight->getValue());


$firstTenFibs = SequenceProvider::nthFibonacciNumber(0, true, 10);

$this->assertEquals(10, $firstTenFibs->count());
$this->assertEquals('21', $firstTenFibs->get(8)->getValue());

$collectionFibs = SequenceProvider::nthFibonacciNumber(7, true, 3);

$this->assertEquals(3, $collectionFibs->count());
$this->assertEquals('34', $collectionFibs->get(2)->getValue());

}

/**
* @medium
* @small
*/
public function testNegativeFibonacci()
{

$this->expectException(IntegrityConstraint::class);
$this->expectExceptionMessage('A negative term number for the Fibonacci sequence was requested; provide a positive term number');

SequenceProvider::nthFibonacciNumber(-1);

}

/**
* @small
*/
public function testNegativeFibonacciPair()
{

$this->expectException(IntegrityConstraint::class);
SequenceProvider::nthFibonacciPair(-1);

}

/**
* @small
*/
public function testNthLucasNumber()
{

$lucas = SequenceProvider::nthLucasNumber(0);
$this->assertEquals('2', $lucas->getValue());

$lucas = SequenceProvider::nthLucasNumber(1);
$this->assertEquals('1', $lucas->getValue());

$lucas = SequenceProvider::nthLucasNumber(8);
$this->assertEquals('47', $lucas->getValue());

}

/**
* @small
*/
public function testNegativeLucasNumber()
{

$this->expectException(IntegrityConstraint::class);
SequenceProvider::nthLucasNumber(-1);

}

/**
* @small
*/
public function testNthTriangularNumber()
{

$triangularNumber = SequenceProvider::nthTriangularNumber(0);
$this->assertEquals('0', $triangularNumber->getValue());

$triangularNumber = SequenceProvider::nthTriangularNumber(1);
$this->assertEquals('1', $triangularNumber->getValue());

$triangularNumber = SequenceProvider::nthTriangularNumber(8);
$this->assertEquals('36', $triangularNumber->getValue());

}

}

0 comments on commit 45a86e4

Please sign in to comment.