Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/phpcs.xml.dist export-ignore
37 changes: 37 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: CI

on:
pull_request:
push:
branches:
- "master"
workflow_dispatch:

jobs:
tests:
name: PHP ${{ matrix.php }}
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
php:
- "8.3"
- "8.4"

steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
env:
phpts: ${{ matrix.phpts }}

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v3"
with:
composer-options: "--prefer-dist"

- name: Run PHPUnit tests
run: composer test
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
vendor
.phpunit.cache
composer.lock
phpunit.xml
vendor
18 changes: 18 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
cacheDirectory=".phpunit.cache"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerNotices="true"
failOnEmptyTestSuite="true"
>
<testsuites>
<testsuite name="DS Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
25 changes: 15 additions & 10 deletions tests/CollectionTest.php → tests/CollectionTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@
use function strpos;
use function var_dump;

abstract class CollectionTest extends TestCase
abstract class CollectionTestCase extends TestCase
{
/**
* Sample sizes.
*/
const MANY = 65;
const SOME = 17;

/**
* Create an instance of the data structure being tested.
*/
abstract public static function getInstance(array $values = []);

/**
* Generic mixed value sample array.
*/
public function sample()
public static function sample()
{
return array_merge(
[[]], // empty
Expand All @@ -38,23 +43,23 @@ public function sample()
/**
* @return array provides two equal values for each test.
*/
public function basicDataProvider()
public static function basicDataProvider()
{
$values = [
[],
['a'],
['a', 'b'],
['a', 'b', 'c'],
$this->sample(),
self::sample(),
];

return array_map(function($a) { return [$a, $a]; }, $values);
return array_map(fn($a) => [$a, $a], $values);
}

/**
* @return array a data provider for Sequence and Set to test out of range.
*/
public function outOfRangeDataProvider()
public static function outOfRangeDataProvider()
{
return [
[[ ], -1],
Expand All @@ -64,7 +69,7 @@ public function outOfRangeDataProvider()
];
}

public function badIndexDataProvider()
public static function badIndexDataProvider()
{
return [
[[], 'a'],
Expand Down Expand Up @@ -250,7 +255,7 @@ protected function cleanVarDump($expression)
*/
public function testConvertingToBoolean()
{
$instance = $this->getInstance();
$instance = static::getInstance();
$this->assertTrue((bool) $instance);
}
}
Expand All @@ -268,9 +273,9 @@ public function __construct($test) {
$this->test = $test;
}

public function getInstance(array $values = null)
public function getInstance(array|null $values = null)
{
return $this->test->getInstance($values);
return $this->test::getInstance($values);
}
}

14 changes: 6 additions & 8 deletions tests/Deque/__construct.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
<?php
namespace Ds\Tests\Deque;

use PHPUnit\Framework\Attributes\DataProvider;

use Ds\Deque;

trait __construct
{
public function constructDataProvider()
public static function constructDataProvider()
{
return array_map(function($a) { return [$a, $a]; }, [
[],
['a'],
['a', 'b'],
['a', 'b', 'c'],
$this->sample(),
self::sample(),
range(1, self::MANY),
]);
}

/**
* @dataProvider constructDataProvider
*/
#[DataProvider('constructDataProvider')]
public function testConstruct($values, array $expected)
{
$this->assertToArray($expected, new Deque($values));
}

/**
* @dataProvider constructDataProvider
*/
#[DataProvider('constructDataProvider')]
public function testConstructUsingNonArrayIterable(array $values, array $expected)
{
$this->assertToArray($expected, new Deque(new \ArrayIterator($values)));
Expand Down
10 changes: 5 additions & 5 deletions tests/Deque/allocate.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
namespace Ds\Tests\Deque;

use PHPUnit\Framework\Attributes\DataProvider;

trait allocate
{
public function allocateDataProvider()
public static function allocateDataProvider()
{
$m = \Ds\Deque::MIN_CAPACITY;

Expand All @@ -22,12 +24,10 @@ public function allocateDataProvider()
];
}

/**
* @dataProvider allocateDataProvider
*/
#[DataProvider('allocateDataProvider')]
public function testAllocate(int $initial, int $allocate, int $expected)
{
$instance = $this->getInstance();
$instance = static::getInstance();

$instance->allocate($initial);
$instance->allocate($allocate);
Expand Down
6 changes: 3 additions & 3 deletions tests/Deque/capacity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public function testCapacity()
{
$min = \Ds\Deque::MIN_CAPACITY;

$instance = $this->getInstance();
$instance = static::getInstance();
$this->assertEquals($min, $instance->capacity());

for ($i = 0; $i < $min; $i++) {
Expand Down Expand Up @@ -42,7 +42,7 @@ public function testAutoTruncate()
0 => 8,
];

$instance = $this->getInstance(range(1, array_keys($boundaries)[0]));
$instance = static::getInstance(range(1, array_keys($boundaries)[0]));

for (;;) {
if ( ! is_null(($expected = $boundaries[$instance->count()] ?? null))) {
Expand All @@ -61,7 +61,7 @@ public function testClearResetsCapacity()
{
$min = \Ds\Deque::MIN_CAPACITY;

$instance = $this->getInstance(range(1, self::MANY));
$instance = static::getInstance(range(1, self::MANY));
$instance->clear();
$this->assertEquals($min, $instance->capacity());
}
Expand Down
12 changes: 6 additions & 6 deletions tests/Deque/insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait insert
*/
public function testInsertExtended()
{
$instance = $this->getInstance();
$instance = static::getInstance();

// The head of the deque will wrap around if all items are unshifted.

Expand All @@ -32,7 +32,7 @@ public function testInsertExtended()

public function testInsertingIntoAnIsland()
{
$instance = $this->getInstance();
$instance = static::getInstance();

// It's possible that the head of the deque comes before the tail, but
// is not at zero. This could overflow the buffer.
Expand All @@ -59,7 +59,7 @@ public function testInsertingIntoAnIsland()

public function testInsertAtBoundaryWithMoreOnTheLeft()
{
$instance = $this->getInstance();
$instance = static::getInstance();
$instance->push(3, 4, 5, 6);
$instance->unshift(1, 2);

Expand All @@ -74,7 +74,7 @@ public function testInsertAtBoundaryWithMoreOnTheLeft()

public function testInsertAtBoundaryWithMoreOnTheRight()
{
$instance = $this->getInstance();
$instance = static::getInstance();
$instance->push(5, 6);
$instance->unshift(1, 2, 3, 4);

Expand All @@ -89,7 +89,7 @@ public function testInsertAtBoundaryWithMoreOnTheRight()

public function testInsertAtBoundaryWithEqualOnBothSides()
{
$instance = $this->getInstance();
$instance = static::getInstance();
$instance->push(4, 5, 6);
$instance->unshift(1, 2, 3);

Expand All @@ -103,7 +103,7 @@ public function testInsertAtBoundaryWithEqualOnBothSides()

public function testAlmostFullInsertAtZero()
{
$instance = $this->getInstance();
$instance = static::getInstance();
$instance->push(...range(1, 6));
$instance->insert(0, 0);

Expand Down
6 changes: 3 additions & 3 deletions tests/Deque/remove.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait remove
*/
public function testRemoveExtended()
{
$instance = $this->getInstance();
$instance = static::getInstance();

/* HEAD WRAPPED AROUND, TAIL = 0 */
// The head of the deque will wrap around if all items are unshifted.
Expand All @@ -25,7 +25,7 @@ public function testRemoveExtended()
$this->assertTrue($instance->isEmpty());

/* HEAD WRAPPED AROUND, TAIL > 0 */
$instance = $this->getInstance();
$instance = static::getInstance();

$instance->unshift('b'); // [_, _, ..., _, b] tail = 0, head = 3
$instance->unshift('a'); // [_, _, ..., a, b] tail = 0, head = 2
Expand All @@ -38,7 +38,7 @@ public function testRemoveExtended()
$this->assertTrue($instance->isEmpty());

/* HEAD NOT WRAPPED, TAIL > 0 */
$instance = $this->getInstance();
$instance = static::getInstance();

$instance->push('a'); // [a, _, _, ..., _] tail = 1, head = 0
$instance->push('b'); // [a, b, _, ..., _] tail = 2, head = 0
Expand Down
4 changes: 2 additions & 2 deletions tests/Deque/slice.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait slice
*/
public function testSliceExtended()
{
$instance = $this->getInstance();
$instance = static::getInstance();

$instance->unshift('c'); // [_, _, _, _, _, _, _, c] tail = 0, head = 7
$instance->unshift('b'); // [_, _, _, _, _, _, b, c] tail = 0, head = 6
Expand All @@ -30,7 +30,7 @@ public function testSliceExtended()
/* If only some values have wrapped around, slice would have to copy
from both the wrapped and not-wrapped values */

$instance = $this->getInstance();
$instance = static::getInstance();

$instance->push('b'); // [b, _, _, _, _, _, _, _] tail = 1, head = 0
$instance->push('c'); // [b, c, _, _, _, _, _, _] tail = 2, head = 1
Expand Down
Loading