Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Summary::isEmpty() method added #43

Merged
merged 2 commits into from
Mar 8, 2023
Merged
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
16 changes: 16 additions & 0 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,8 @@ public function anyMatch(callable $predicate): bool
* @param callable|null $predicate
*
* @return bool
*
* @see Summary::exactlyN()
*/
public function exactlyN(int $n, callable $predicate = null): bool
{
Expand All @@ -1127,12 +1129,26 @@ public function exactlyN(int $n, callable $predicate = null): bool
* @param callable|null $predicate
*
* @return bool
*
* @see Summary::isPartitioned()
*/
public function isPartitioned(callable $predicate = null): bool
{
return Summary::isPartitioned($this->iterable, $predicate);
}

/**
* Returns true if stream is empty.
*
* @return bool
*
* @see Summary::isEmpty()
*/
public function isEmpty(): bool
{
return Summary::isEmpty($this->iterable);
}

/**
* Returns true if no element matches the predicate function.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,22 @@ public static function isPartitioned(iterable $data, callable $predicate = null)

return true;
}

/**
* Returns true if given iterable is empty.
*
* @param iterable<mixed> $data
*
* @return bool
*/
public static function isEmpty(iterable $data): bool
{
$iterator = Transform::toIterator($data);

if ($data instanceof \IteratorAggregate) {
$iterator->rewind();
}

return !$iterator->valid();
}
}
40 changes: 40 additions & 0 deletions tests/Stream/SummaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ public function dataProviderForArrayTrue(): array
->sort()
->isPartitioned(fn ($item) => $item < 0),
],
[
[],
fn (iterable $iterable) => Stream::of($iterable)
->isEmpty(),
],
];
}

Expand Down Expand Up @@ -376,6 +381,11 @@ public function dataProviderForArrayFalse(): array
fn (iterable $iterable) => Stream::of($iterable)
->isPartitioned(fn ($item) => $item % 2 === 0),
],
[
[1, 2, 3],
fn (iterable $iterable) => Stream::of($iterable)
->isEmpty(),
],
];
}

Expand Down Expand Up @@ -569,6 +579,11 @@ public function dataProviderForGeneratorTrue(): array
->sort()
->isPartitioned(fn ($item) => $item < 0),
],
[
$gen([]),
fn (iterable $iterable) => Stream::of($iterable)
->isEmpty(),
],
];
}

Expand Down Expand Up @@ -722,6 +737,11 @@ public function dataProviderForGeneratorFalse(): array
fn (iterable $iterable) => Stream::of($iterable)
->isPartitioned(fn ($item) => $item % 2 === 0),
],
[
$gen([1, 2, 3]),
fn (iterable $iterable) => Stream::of($iterable)
->isEmpty(),
],
];
}

Expand Down Expand Up @@ -915,6 +935,11 @@ public function dataProviderForIteratorTrue(): array
->sort()
->isPartitioned(fn ($item) => $item < 0),
],
[
$iter([]),
fn (iterable $iterable) => Stream::of($iterable)
->isEmpty(),
],
];
}

Expand Down Expand Up @@ -1068,6 +1093,11 @@ public function dataProviderForIteratorFalse(): array
fn (iterable $iterable) => Stream::of($iterable)
->isPartitioned(fn ($item) => $item % 2 === 0),
],
[
$iter([1, 2, 3]),
fn (iterable $iterable) => Stream::of($iterable)
->isEmpty(),
],
];
}

Expand Down Expand Up @@ -1261,6 +1291,11 @@ public function dataProviderForTraversableTrue(): array
->sort()
->isPartitioned(fn ($item) => $item < 0),
],
[
$trav([]),
fn (iterable $iterable) => Stream::of($iterable)
->isEmpty(),
],
];
}

Expand Down Expand Up @@ -1414,6 +1449,11 @@ public function dataProviderForTraversableFalse(): array
fn (iterable $iterable) => Stream::of($iterable)
->isPartitioned(fn ($item) => $item % 2 === 0),
],
[
$trav([1, 2, 3]),
fn (iterable $iterable) => Stream::of($iterable)
->isEmpty(),
],
];
}
}
146 changes: 146 additions & 0 deletions tests/Summary/IsEmptyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

declare(strict_types=1);

namespace IterTools\Tests\Summary;

use IterTools\Summary;
use IterTools\Tests\Fixture\DataProvider;
use IterTools\Tests\Fixture\GeneratorFixture;
use IterTools\Tests\Fixture\IteratorAggregateFixture;

class IsEmptyTest extends \PHPUnit\Framework\TestCase
{
use DataProvider;

/**
* @dataProvider dataProviderForEmptyIterable
* @param iterable $iterable
*/
public function testTrue(iterable $iterable)
{
// When
$result = Summary::isEmpty($iterable);

// Then
$this->assertTrue($result);
}

/**
* @dataProvider dataProviderForArrayFalse
* @param array $iterable
*/
public function testArrayFalse(array $iterable)
{
// When
$result = Summary::isEmpty($iterable);

// Then
$this->assertFalse($result);
}

public function dataProviderForArrayFalse(): array
{
return [
[[0]],
[[[]]],
[[null]],
[[1]],
Smoren marked this conversation as resolved.
Show resolved Hide resolved
[[1, 2]],
[[1, 2, 3]],
[[1 => '1']],
[['a' => 1]],
[['a' => 1, 'b' => 2, 'c' => 3]],
];
}

/**
* @dataProvider dataProviderForGeneratorsFalse
* @param \Generator $iterable
*/
public function testGeneratorsFalse(\Generator $iterable)
{
// When
$result = Summary::isEmpty($iterable);

// Then
$this->assertFalse($result);
}

public function dataProviderForGeneratorsFalse(): array
{
$gen = fn ($data) => GeneratorFixture::getKeyValueGenerator($data);

return [
[$gen([0])],
[$gen([[]])],
[$gen([null])],
[$gen([1])],
[$gen([1, 2])],
[$gen([1, 2, 3])],
[$gen([1 => '1'])],
[$gen(['a' => 1])],
[$gen(['a' => 1, 'b' => 2, 'c' => 3])],
];
}

/**
* @dataProvider dataProviderForIteratorsFalse
* @param \Iterator $iterable
*/
public function testIteratorsFalse(\Iterator $iterable)
{
// When
$result = Summary::isEmpty($iterable);

// Then
$this->assertFalse($result);
}

public function dataProviderForIteratorsFalse(): array
{
$iter = fn ($data) => new \ArrayIterator($data);

return [
[$iter([0])],
[$iter([[]])],
[$iter([null])],
[$iter([1])],
[$iter([1, 2])],
[$iter([1, 2, 3])],
[$iter([1 => '1'])],
[$iter(['a' => 1])],
[$iter(['a' => 1, 'b' => 2, 'c' => 3])],
];
}

/**
* @dataProvider dataProviderForTraversablesFalse
* @param \Traversable $iterable
*/
public function testTraversablesFalse(\Traversable $iterable)
{
// When
$result = Summary::isEmpty($iterable);

// Then
$this->assertFalse($result);
}

public function dataProviderForTraversablesFalse(): array
{
$trav = fn ($data) => new IteratorAggregateFixture($data);

return [
[$trav([0])],
[$trav([[]])],
[$trav([null])],
[$trav([1])],
[$trav([1, 2])],
[$trav([1, 2, 3])],
[$trav([1 => '1'])],
[$trav(['a' => 1])],
[$trav(['a' => 1, 'b' => 2, 'c' => 3])],
];
}
}