-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d3e2d2
commit cbe7d14
Showing
14 changed files
with
535 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"image": "mcr.microsoft.com/devcontainers/universal:2", | ||
"features": { | ||
"ghcr.io/devcontainers/features/php:1": { | ||
"version": "8.3", | ||
"installComposer": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
<?php | ||
/* | ||
* By adding type hints and enabling strict type checking, code can become | ||
* easier to read, self-documenting and reduce the number of potential bugs. | ||
* By default, type declarations are non-strict, which means they will attempt | ||
* to change the original type to match the type specified by the | ||
* type-declaration. | ||
* | ||
* In other words, if you pass a string to a function requiring a float, | ||
* it will attempt to convert the string value to a float. | ||
* | ||
* To enable strict mode, a single declare directive must be placed at the top | ||
* of the file. | ||
* This means that the strictness of typing is configured on a per-file basis. | ||
* This directive not only affects the type declarations of parameters, but also | ||
* a function's return type. | ||
* | ||
* For more info review the Concept on strict type checking in the PHP track | ||
* <link>. | ||
* | ||
* 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'; | ||
} | ||
{% set case0 = cases[0] -%} | ||
{% for case in case0.cases -%} | ||
/** | ||
* @testdox {{ case0.description }} -> {{ case.description }} | ||
*/ | ||
public function {{ testfn(case0.description ~ ' ' ~ case.description) }}() | ||
{ | ||
$listOps = new ListOps(); | ||
$this->assertEquals({{ export(case.expected) }}, $listOps->{{ case.property }}({{ export(case.input.list1) }}, {{ export(case.input.list2) }})); | ||
} | ||
{% endfor -%} | ||
{% set case1 = cases[1] -%} | ||
{% for case in case1.cases -%} | ||
/** | ||
* @testdox {{ case1.description }} -> {{ case.description }} | ||
*/ | ||
public function {{ testfn(case1.description ~ ' ' ~ case.description) }}() | ||
{ | ||
$listOps = new ListOps(); | ||
$this->assertEquals({{ export(case.expected) }}, $listOps->{{ case.property }}({{ case.input.lists | map(l => export(l)) | join(', ') }})); | ||
} | ||
{% endfor -%} | ||
/** | ||
* @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]])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vendor/ |
Oops, something went wrong.