Skip to content

Commit

Permalink
Implement containsAll method
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas committed Apr 10, 2024
1 parent 74e66b4 commit 5de1d66
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Munus\Collection\Iterator;
use Munus\Collection\Stream;
use Munus\Collection\Stream\Collector;
use Munus\Collection\Traversable;
use Munus\Control\Option;
use Munus\Control\TryTo;
use Munus\Value\Comparator;
Expand Down Expand Up @@ -118,6 +119,21 @@ public function contains($element): bool
return false;
}

/**
* @param Traversable<T> $elements
*/
public function containsAll(Traversable $elements): bool
{
$iterator = $elements->iterator();
while ($iterator->hasNext()) {
if (!$this->contains($iterator->next())) {
return false;
}
}

return true;
}

/**
* @param callable(T):bool $predicate
*/
Expand Down
12 changes: 12 additions & 0 deletions tests/Collection/GenericListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,16 @@ public function testListSorted(): void
{
self::assertTrue(GenericList::of('a', 'b', 'c', 'd', 'e')->equals(GenericList::of('e', 'd', 'c', 'b', 'a')->sorted()));
}

public function testListContainsAll()
{
self::assertTrue(GenericList::ofAll([1, 2, 3])->containsAll(GenericList::ofAll([3, 2, 1])));
self::assertTrue(GenericList::ofAll([1, 2, 3])->containsAll(GenericList::ofAll([2, 1])));
self::assertTrue(GenericList::ofAll([1, 2, 3])->containsAll(GenericList::ofAll([1])));

self::assertFalse(GenericList::ofAll([1, 2, 3])->containsAll(GenericList::ofAll([1, 2, 3, 4])));
self::assertFalse(GenericList::ofAll([1, 2, 3])->containsAll(GenericList::ofAll([1, 2, 4])));
self::assertFalse(GenericList::ofAll([1, 2, 3])->containsAll(GenericList::ofAll([1, 4])));
self::assertFalse(GenericList::ofAll([1, 2, 3])->containsAll(GenericList::ofAll(['a'])));
}
}
12 changes: 12 additions & 0 deletions tests/Collection/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ public function testStreamContains(): void
self::assertTrue(Stream::ofAll([1, 2, 3])->contains(2));
}

public function testStreamContainsAll(): void
{
self::assertTrue(Stream::ofAll([1, 2, 3])->containsAll(Stream::ofAll([3, 2, 1])));
self::assertTrue(Stream::ofAll([1, 2, 3])->containsAll(Stream::ofAll([2, 1])));
self::assertTrue(Stream::ofAll([1, 2, 3])->containsAll(Stream::ofAll([1])));

self::assertFalse(Stream::ofAll([1, 2, 3])->containsAll(Stream::ofAll([1, 2, 3, 4])));
self::assertFalse(Stream::ofAll([1, 2, 3])->containsAll(Stream::ofAll([1, 2, 4])));
self::assertFalse(Stream::ofAll([1, 2, 3])->containsAll(Stream::ofAll([1, 4])));
self::assertFalse(Stream::ofAll([1, 2, 3])->containsAll(Stream::ofAll(['a'])));
}

public function testStreamMapSingle(): void
{
self::assertTrue(
Expand Down

0 comments on commit 5de1d66

Please sign in to comment.