From 5de1d66253b9d3797e6a8b23461b1ceae72bb432 Mon Sep 17 00:00:00 2001 From: Arkadiusz Kondas Date: Wed, 10 Apr 2024 06:24:49 +0200 Subject: [PATCH] Implement containsAll method --- src/Value.php | 16 ++++++++++++++++ tests/Collection/GenericListTest.php | 12 ++++++++++++ tests/Collection/StreamTest.php | 12 ++++++++++++ 3 files changed, 40 insertions(+) diff --git a/src/Value.php b/src/Value.php index 1889bf3..b5eb7b8 100644 --- a/src/Value.php +++ b/src/Value.php @@ -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; @@ -118,6 +119,21 @@ public function contains($element): bool return false; } + /** + * @param Traversable $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 */ diff --git a/tests/Collection/GenericListTest.php b/tests/Collection/GenericListTest.php index 453446a..8aa9935 100644 --- a/tests/Collection/GenericListTest.php +++ b/tests/Collection/GenericListTest.php @@ -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']))); + } } diff --git a/tests/Collection/StreamTest.php b/tests/Collection/StreamTest.php index 5519866..da60b69 100644 --- a/tests/Collection/StreamTest.php +++ b/tests/Collection/StreamTest.php @@ -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(