123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- declare(strict_types=1);
- use PHPUnit\Framework\ExpectationFailedException;
- class ListOpsTest extends PHPUnit\Framework\TestCase
- {
- public static function setUpBeforeClass(): void
- {
- require_once 'ListOps.php';
- }
-
- public function testAppendEmptyLists()
- {
- $listOps = new ListOps();
- $this->assertEquals([], $listOps->append([], []));
- }
-
- public function testAppendNonEmptyListToEmptyList()
- {
- $listOps = new ListOps();
- $this->assertEquals([1, 2, 3, 4], $listOps->append([1, 2, 3, 4], []));
- }
-
- public function testAppendEmptyListToNonEmptyList()
- {
- $listOps = new ListOps();
- $this->assertEquals([1, 2, 3, 4], $listOps->append([], [1, 2, 3, 4]));
- }
-
- public function testAppendNonEmptyLists()
- {
- $listOps = new ListOps();
- $this->assertEquals([1, 2, 2, 3, 4, 5], $listOps->append([1, 2], [2, 3, 4, 5]));
- }
-
- public function testConcatEmptyLists()
- {
- $listOps = new ListOps();
- $this->assertEquals([], $listOps->concat([], []));
- }
-
- public function testConcatLists()
- {
- $listOps = new ListOps();
- $this->assertEquals([1, 2, 3, 4, 5, 6], $listOps->concat([1, 2], [3], [], [4, 5, 6]));
- }
-
- public function testConcatNestedLists()
- {
- $listOps = new ListOps();
- $this->assertEquals([[1], [2], [3], [], [4, 5, 6]], $listOps->concat([[1], [2]], [[3]], [[]], [[4, 5, 6]]));
- }
-
- public function testFilterEmptyList()
- {
- $listOps = new ListOps();
- $this->assertEquals(
- [],
- $listOps->filter(static fn ($el) => $el % 2 === 1, [])
- );
- }
-
- public function testFilterNonEmptyList()
- {
- $listOps = new ListOps();
- $this->assertEquals(
- [1, 3, 5],
- $listOps->filter(static fn ($el) => $el % 2 === 1, [1, 2, 3, 5])
- );
- }
-
- public function testLengthEmptyList()
- {
- $listOps = new ListOps();
- $this->assertEquals(0, $listOps->length([]));
- }
-
- public function testLengthNonEmptyList()
- {
- $listOps = new ListOps();
- $this->assertEquals(4, $listOps->length([1, 2, 3, 4]));
- }
-
- public function testMapEmptyList()
- {
- $listOps = new ListOps();
- $this->assertEquals(
- [],
- $listOps->map(static fn ($el) => $el + 1, [])
- );
- }
-
- public function testMapNonEmptyList()
- {
- $listOps = new ListOps();
- $this->assertEquals(
- [2, 4, 6, 8],
- $listOps->map(static fn ($el) => $el + 1, [1, 3, 5, 7])
- );
- }
-
- public function testFoldlEmptyList()
- {
- $listOps = new ListOps();
- $this->assertEquals(
- 2,
- $listOps->foldl(static fn ($acc, $el) => $el * $acc, [], 2)
- );
- }
-
- public function testFoldlDirectionIndependentNonEmptyList()
- {
- $listOps = new ListOps();
- $this->assertEquals(
- 15,
- $listOps->foldl(static fn ($acc, $el) => $acc + $el, [1, 2, 3, 4], 5)
- );
- }
-
- public function testFoldlDirectionDependentNonEmptyList()
- {
- $listOps = new ListOps();
- $this->assertEquals(
- 64,
- $listOps->foldl(static fn ($acc, $el) => $el / $acc, [1, 2, 3, 4], 24)
- );
- }
-
- public function testFoldrEmptyList()
- {
- $listOps = new ListOps();
- $this->assertEquals(
- 2,
- $listOps->foldr(static fn ($acc, $el) => $el * $acc, [], 2)
- );
- }
-
- public function testFoldrDirectionIndependentNonEmptyList()
- {
- $listOps = new ListOps();
- $this->assertEquals(
- 15,
- $listOps->foldr(static fn ($acc, $el) => $acc + $el, [1, 2, 3, 4], 5)
- );
- }
-
- public function testFoldrDirectionDependentNonEmptyList()
- {
- $listOps = new ListOps();
- $this->assertEquals(
- 9,
- $listOps->foldr(static fn ($acc, $el) => $el / $acc, [1, 2, 3, 4], 24)
- );
- }
-
- public function testReverseEmptyList()
- {
- $listOps = new ListOps();
- $this->assertEquals([], $listOps->reverse([]));
- }
-
- public function testReverseNonEmptyList()
- {
- $listOps = new ListOps();
- $this->assertEquals([7, 5, 3, 1], $listOps->reverse([1, 3, 5, 7]));
- }
-
- public function testReverseNonEmptyListIsNotFlattened()
- {
- $listOps = new ListOps();
- $this->assertEquals([[4, 5, 6], [], [3], [1, 2]], $listOps->reverse([[1, 2], [3], [], [4, 5, 6]]));
- }
- }
|