. * * To disable strict typing, comment out the directive below. */ declare(strict_types=1); use PHPUnit\Framework\ExpectationFailedException; class ListOpsTest extends PHPUnit\Framework\TestCase { public static function setUpBeforeClass(): void { require_once 'ListOps.php'; } /** * @testdox append entries to a list and return the new list -> empty lists */ public function testAppendEmptyLists() { $listOps = new ListOps(); $this->assertEquals([], $listOps->append([], [])); } /** * @testdox append entries to a list and return the new list -> list to empty list */ public function testAppendNonEmptyListToEmptyList() { $listOps = new ListOps(); $this->assertEquals([1, 2, 3, 4], $listOps->append([1, 2, 3, 4], [])); } /** * @testdox append entries to a list and return the new list -> empty list to list */ public function testAppendEmptyListToNonEmptyList() { $listOps = new ListOps(); $this->assertEquals([1, 2, 3, 4], $listOps->append([], [1, 2, 3, 4])); } /** * @testdox append entries to a list and return the new list -> non-empty lists */ public function testAppendNonEmptyLists() { $listOps = new ListOps(); $this->assertEquals([1, 2, 2, 3, 4, 5], $listOps->append([1, 2], [2, 3, 4, 5])); } /** * @testdox concatenate a list of lists -> empty list */ public function testConcatEmptyLists() { $listOps = new ListOps(); $this->assertEquals([], $listOps->concat([], [])); } /** * @testdox concatenate a list of lists -> list of lists */ public function testConcatLists() { $listOps = new ListOps(); $this->assertEquals([1, 2, 3, 4, 5, 6], $listOps->concat([1, 2], [3], [], [4, 5, 6])); } /** * @testdox concatenate a list of lists -> list of nested lists */ public function testConcatNestedLists() { $listOps = new ListOps(); $this->assertEquals([[1], [2], [3], [], [4, 5, 6]], $listOps->concat([[1], [2]], [[3]], [[]], [[4, 5, 6]])); } /** * @testdox filter list returning only values that satisfy the filter function -> empty list */ public function testFilterEmptyList() { $listOps = new ListOps(); $this->assertEquals( [], $listOps->filter(static fn ($el) => $el % 2 === 1, []) ); } /** * @testdox filter list returning only values that satisfy the filter function -> non empty list */ public function testFilterNonEmptyList() { $listOps = new ListOps(); $this->assertEquals( [1, 3, 5], $listOps->filter(static fn ($el) => $el % 2 === 1, [1, 2, 3, 5]) ); } /** * @testdox returns the length of a list -> empty list */ public function testLengthEmptyList() { $listOps = new ListOps(); $this->assertEquals(0, $listOps->length([])); } /** * @testdox returns the length of a list -> non-empty list */ public function testLengthNonEmptyList() { $listOps = new ListOps(); $this->assertEquals(4, $listOps->length([1, 2, 3, 4])); } /** * @testdox returns a list of elements whose values equal the list value transformed by the mapping function -> empty list */ public function testMapEmptyList() { $listOps = new ListOps(); $this->assertEquals( [], $listOps->map(static fn ($el) => $el + 1, []) ); } /** * @testdox returns a list of elements whose values equal the list value transformed by the mapping function -> non-empty list */ public function testMapNonEmptyList() { $listOps = new ListOps(); $this->assertEquals( [2, 4, 6, 8], $listOps->map(static fn ($el) => $el + 1, [1, 3, 5, 7]) ); } /** * @testdox folds (reduces) the given list from the left with a function -> empty list */ public function testFoldlEmptyList() { $listOps = new ListOps(); $this->assertEquals( 2, $listOps->foldl(static fn ($acc, $el) => $el * $acc, [], 2) ); } /** * @testdox folds (reduces) the given list from the left with a function -> direction independent function applied to non-empty list */ public function testFoldlDirectionIndependentNonEmptyList() { $listOps = new ListOps(); $this->assertEquals( 15, $listOps->foldl(static fn ($acc, $el) => $acc + $el, [1, 2, 3, 4], 5) ); } /** * @testdox folds (reduces) the given list from the left with a function -> direction dependent function applied to non-empty list */ public function testFoldlDirectionDependentNonEmptyList() { $listOps = new ListOps(); $this->assertEquals( 64, $listOps->foldl(static fn ($acc, $el) => $el / $acc, [1, 2, 3, 4], 24) ); } /** * @testdox folds (reduces) the given list from the right with a function -> empty list */ public function testFoldrEmptyList() { $listOps = new ListOps(); $this->assertEquals( 2, $listOps->foldr(static fn ($acc, $el) => $el * $acc, [], 2) ); } /** * @testdox folds (reduces) the given list from the right with a function -> direction independent function applied to non-empty list */ public function testFoldrDirectionIndependentNonEmptyList() { $listOps = new ListOps(); $this->assertEquals( 15, $listOps->foldr(static fn ($acc, $el) => $acc + $el, [1, 2, 3, 4], 5) ); } /** * @testdox folds (reduces) the given list from the right with a function -> direction dependent function applied to non-empty list */ public function testFoldrDirectionDependentNonEmptyList() { $listOps = new ListOps(); $this->assertEquals( 9, $listOps->foldr(static fn ($acc, $el) => $el / $acc, [1, 2, 3, 4], 24) ); } /** * @testdox reverse the elements of a list -> empty list */ public function testReverseEmptyList() { $listOps = new ListOps(); $this->assertEquals([], $listOps->reverse([])); } /** * @testdox reverse the elements of a list -> non-empty list */ public function testReverseNonEmptyList() { $listOps = new ListOps(); $this->assertEquals([7, 5, 3, 1], $listOps->reverse([1, 3, 5, 7])); } /** * @testdox reverse the elements of a list -> list of lists is not flattened */ public function testReverseNonEmptyListIsNotFlattened() { $listOps = new ListOps(); $this->assertEquals([[4, 5, 6], [], [3], [1, 2]], $listOps->reverse([[1, 2], [3], [], [4, 5, 6]])); } }