Skip to content

Commit

Permalink
Fix mutations + math float tests
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Oct 22, 2023
1 parent 4affb62 commit 66dee5d
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 14 deletions.
46 changes: 44 additions & 2 deletions config/infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,48 @@
}
},
"timeout": 10,
"minMsi": 98,
"minCoveredMsi": 100
"minMsi": 100,
"minCoveredMsi": 100,
"mutators": {
"@default": true,
"Break_": {
"ignore": [
"Psl\\Collection\\*Map::zip"
]
},
"DecrementInteger": {
"ignore": [
"Psl\\DataStructure\\PriorityQueue::peek"
]
},
"FunctionCallRemoval": {
"ignore": [
"Psl\\Result\\Success::getThrowable"
]
},
"IncrementInteger": {
"ignore": [
"Psl\\DataStructure\\PriorityQueue::peek"
]
},
"LogicalNot": {
"ignore": [
"Psl\\Hash\\Context::update"
]
},
"MethodCallRemoval": {
"ignore": [
"Psl\\Iter\\Iterator::seek",
"Psl\\Iter\\Iterator::count"
]
},
"Throw_": {
"ignore": [
"Psl\\File\\ReadHandle::__construct",
"Psl\\File\\WriteHandle::__construct",
"Psl\\File\\ReadWriteHandle::__construct",
"Psl\\Hash\\Context::update"
]
}
}
}
1 change: 1 addition & 0 deletions config/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
</coverage>
<php>
<ini name="error_reporting" value="-1" />
<ini name="precision" value="16" />
</php>
<testsuites>
<testsuite name="PHP Standard Library">
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Collection/MutableMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public function get(string|int $k): mixed
*/
public function values(): MutableVector
{
return MutableVector::fromArray(array_values($this->elements));
return MutableVector::fromArray($this->elements);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/Collection/AbstractMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,13 @@ public function testZip(): void
static::assertCount(2, $zipped);
static::assertSame(['hello', 'foo'], $zipped->at(1));
static::assertSame(['world', 'bar'], $zipped->at(2));

$map = $this->create([1 => 'hello', 2 => 'world']);
$zipped = $map->zip(['foo' => 'foo', 'bar' => 'bar']);
static::assertInstanceOf($this->mapClass, $zipped);
static::assertCount(2, $zipped);
static::assertSame(['hello', 'foo'], $zipped->at(1));
static::assertSame(['world', 'bar'], $zipped->at(2));
}

public function testTake(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Collection/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ final class MapTest extends AbstractMapTest
*/
protected function create(iterable $items): Map
{
return new Map($items);
return Map::fromArray($items);
}
}
2 changes: 1 addition & 1 deletion tests/unit/Collection/MutableMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,6 @@ public function testRemove(): void
*/
protected function create(iterable $items): MutableMap
{
return new MutableMap($items);
return MutableMap::fromArray($items);
}
}
18 changes: 18 additions & 0 deletions tests/unit/DataStructure/PriorityQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ public function testEnqueueAndDequeue(): void
static::assertSame('hi', $queue->dequeue());
}

public function testDefaultEnqueueSettings(): void
{
$queue = new DataStructure\PriorityQueue();
$queue->enqueue('hey');
$queue->enqueue('ho', 0);
$queue->enqueue('hi', -1);
$queue->enqueue('hello', 1);

static::assertCount(4, $queue);
static::assertSame('hello', $queue->dequeue());
static::assertCount(3, $queue);
static::assertSame('hey', $queue->dequeue());
static::assertCount(2, $queue);
static::assertSame('ho', $queue->dequeue());
static::assertCount(1, $queue);
static::assertSame('hi', $queue->dequeue());
}

public function testMultipleNodesWithSamePriority(): void
{
$queue = new DataStructure\PriorityQueue();
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/File/ReadHandleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ public function testNonExisting(): void
$this->expectException(File\Exception\NotFoundException::class);
File\open_read_only($temporary_file);
}

public function testNonFile(): void
{
$temporary_file = Filesystem\create_temporary_file();
Filesystem\delete_file($temporary_file);

$this->expectException(File\Exception\NotFileException::class);
File\open_read_only(dirname($temporary_file));
}
}
4 changes: 3 additions & 1 deletion tests/unit/Math/AcosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

final class AcosTest extends TestCase
{
use FloatAsserts;

/**
* @dataProvider provideData
*/
public function testAcos(float $expected, float $number): void
{
static::assertSame($expected, Math\acos($number));
static::assertFloatEquals($expected, Math\acos($number));
}

public function provideData(): array
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/Math/AsinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

final class AsinTest extends TestCase
{
use FloatAsserts;

/**
* @dataProvider provideData
*/
public function testAsin(float $expected, float $number): void
{
static::assertSame($expected, Math\asin($number));
static::assertFloatEquals($expected, Math\asin($number));
}

public function provideData(): array
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/Math/Atan2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

final class Atan2Test extends TestCase
{
use FloatAsserts;

/**
* @dataProvider provideData
*/
public function testAtan2(float $expected, float $y, float $x): void
{
static::assertSame($expected, Math\atan2($y, $x));
static::assertFloatEquals($expected, Math\atan2($y, $x));
}

public function provideData(): array
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/Math/AtanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

final class AtanTest extends TestCase
{
use FloatAsserts;

/**
* @dataProvider provideData
*/
public function testAtan(float $expected, float $number): void
{
static::assertSame($expected, Math\atan($number));
static::assertFloatEquals($expected, Math\atan($number));
}

public function provideData(): array
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/Math/CosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

final class CosTest extends TestCase
{
use FloatAsserts;

/**
* @dataProvider provideData
*/
public function testCos(float $expected, float $number): void
{
static::assertSame($expected, Math\cos($number));
static::assertFloatEquals($expected, Math\cos($number));
}

public function provideData(): array
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/Math/FloatAsserts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Psl\Tests\Unit\Math;

use PHPUnit\Framework\TestCase;
use Psl\Math;

trait FloatAsserts
{
/**
* Because not all systems have the same rounding rules and precisions,
* This method provides an assertion to compare floats based on epsilon:.
*
* @see https://www.php.net/manual/en/language.types.float.php#language.types.float.comparison
*/
public static function assertFloatEquals(float $a, float $b, float $epsilon = PHP_FLOAT_EPSILON): void
{
TestCase::assertTrue(
Math\abs($a - $b) <= $epsilon,
'Failed asserting that float ' . $a . ' is equal to ' . $b . '.'
);
}
}
4 changes: 3 additions & 1 deletion tests/unit/Math/SinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

final class SinTest extends TestCase
{
use FloatAsserts;

/**
* @dataProvider provideData
*/
public function testSin(float $expected, float $number): void
{
static::assertSame($expected, Math\sin($number));
static::assertFloatEquals($expected, Math\sin($number));
}

public function provideData(): array
Expand Down
9 changes: 6 additions & 3 deletions tests/unit/Math/TanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@

final class TanTest extends TestCase
{
use FloatAsserts;

/**
* @dataProvider provideData
*/
public function testTan(float $expected, float $number): void
public function testTan(float $expected, float $number, float $epsilon = PHP_FLOAT_EPSILON): void
{
static::assertSame($expected, Math\tan($number));
static::assertFloatEquals($expected, Math\tan($number), $epsilon);
}

public function provideData(): array
{
return [
[
-3.380515006246586,
5.0
5.0,
0.00000000000001
],

[
Expand Down

0 comments on commit 66dee5d

Please sign in to comment.