|
| 1 | +<?php |
| 2 | +
|
| 3 | +/* |
| 4 | + * By adding type hints and enabling strict type checking, code can become |
| 5 | + * easier to read, self-documenting and reduce the number of potential bugs. |
| 6 | + * By default, type declarations are non-strict, which means they will attempt |
| 7 | + * to change the original type to match the type specified by the |
| 8 | + * type-declaration. |
| 9 | + * |
| 10 | + * In other words, if you pass a string to a function requiring a float, |
| 11 | + * it will attempt to convert the string value to a float. |
| 12 | + * |
| 13 | + * To enable strict mode, a single declare directive must be placed at the top |
| 14 | + * of the file. |
| 15 | + * This means that the strictness of typing is configured on a per-file basis. |
| 16 | + * This directive not only affects the type declarations of parameters, but also |
| 17 | + * a function's return type. |
| 18 | + * |
| 19 | + * For more info review the Concept on strict type checking in the PHP track |
| 20 | + * <link>. |
| 21 | + * |
| 22 | + * To disable strict typing, comment out the directive below. |
| 23 | + */ |
| 24 | +
|
| 25 | +declare(strict_types=1); |
| 26 | +
|
| 27 | +use PHPUnit\Framework\ExpectationFailedException; |
| 28 | +
|
| 29 | +class ListOpsTest extends PHPUnit\Framework\TestCase |
| 30 | +{ |
| 31 | + public static function setUpBeforeClass(): void |
| 32 | + { |
| 33 | + require_once 'ListOps.php'; |
| 34 | + } |
| 35 | +
|
| 36 | + {% set case0 = cases[0] -%} |
| 37 | + {% for case in case0.cases -%} |
| 38 | + /** |
| 39 | + * @testdox {{ case0.description }} -> {{ case.description }} |
| 40 | + */ |
| 41 | + public function {{ testfn(case0.description ~ ' ' ~ case.description) }}() |
| 42 | + { |
| 43 | + $listOps = new ListOps(); |
| 44 | + $this->assertEquals({{ export(case.expected) }}, $listOps->{{ case.property }}({{ export(case.input.list1) }}, {{ export(case.input.list2) }})); |
| 45 | + } |
| 46 | +
|
| 47 | + {% endfor -%} |
| 48 | +
|
| 49 | + {% set case1 = cases[1] -%} |
| 50 | + {% for case in case1.cases -%} |
| 51 | + /** |
| 52 | + * @testdox {{ case1.description }} -> {{ case.description }} |
| 53 | + */ |
| 54 | + public function {{ testfn(case1.description ~ ' ' ~ case.description) }}() |
| 55 | + { |
| 56 | + $listOps = new ListOps(); |
| 57 | + $this->assertEquals({{ export(case.expected) }}, $listOps->{{ case.property }}({{ case.input.lists | map(l => export(l)) | join(', ') }})); |
| 58 | + } |
| 59 | +
|
| 60 | + {% endfor -%} |
| 61 | +
|
| 62 | + /** |
| 63 | + * @testdox filter list returning only values that satisfy the filter function -> empty list |
| 64 | + */ |
| 65 | + public function testFilterEmptyList() |
| 66 | + { |
| 67 | + $listOps = new ListOps(); |
| 68 | + $this->assertEquals( |
| 69 | + [], |
| 70 | + $listOps->filter(static fn ($el) => $el % 2 === 1, []) |
| 71 | + ); |
| 72 | + } |
| 73 | +
|
| 74 | + /** |
| 75 | + * @testdox filter list returning only values that satisfy the filter function -> non empty list |
| 76 | + */ |
| 77 | + public function testFilterNonEmptyList() |
| 78 | + { |
| 79 | + $listOps = new ListOps(); |
| 80 | + $this->assertEquals( |
| 81 | + [1, 3, 5], |
| 82 | + $listOps->filter(static fn ($el) => $el % 2 === 1, [1, 2, 3, 5]) |
| 83 | + ); |
| 84 | + } |
| 85 | +
|
| 86 | + /** |
| 87 | + * @testdox returns the length of a list -> empty list |
| 88 | + */ |
| 89 | + public function testLengthEmptyList() |
| 90 | + { |
| 91 | + $listOps = new ListOps(); |
| 92 | + $this->assertEquals(0, $listOps->length([])); |
| 93 | + } |
| 94 | +
|
| 95 | + /** |
| 96 | + * @testdox returns the length of a list -> non-empty list |
| 97 | + */ |
| 98 | + public function testLengthNonEmptyList() |
| 99 | + { |
| 100 | + $listOps = new ListOps(); |
| 101 | + $this->assertEquals(4, $listOps->length([1, 2, 3, 4])); |
| 102 | + } |
| 103 | +
|
| 104 | + /** |
| 105 | + * @testdox returns a list of elements whose values equal the list value transformed by the mapping function -> empty list |
| 106 | + */ |
| 107 | + public function testMapEmptyList() |
| 108 | + { |
| 109 | + $listOps = new ListOps(); |
| 110 | + $this->assertEquals( |
| 111 | + [], |
| 112 | + $listOps->map(static fn ($el) => $el + 1, []) |
| 113 | + ); |
| 114 | + } |
| 115 | +
|
| 116 | + /** |
| 117 | + * @testdox returns a list of elements whose values equal the list value transformed by the mapping function -> non-empty list |
| 118 | + */ |
| 119 | + public function testMapNonEmptyList() |
| 120 | + { |
| 121 | + $listOps = new ListOps(); |
| 122 | + $this->assertEquals( |
| 123 | + [2, 4, 6, 8], |
| 124 | + $listOps->map(static fn ($el) => $el + 1, [1, 3, 5, 7]) |
| 125 | + ); |
| 126 | + } |
| 127 | +
|
| 128 | + /** |
| 129 | + * @testdox folds (reduces) the given list from the left with a function -> empty list |
| 130 | + */ |
| 131 | + public function testFoldlEmptyList() |
| 132 | + { |
| 133 | + $listOps = new ListOps(); |
| 134 | + $this->assertEquals( |
| 135 | + 2, |
| 136 | + $listOps->foldl(static fn ($acc, $el) => $el * $acc, [], 2) |
| 137 | + ); |
| 138 | + } |
| 139 | +
|
| 140 | + /** |
| 141 | + * @testdox folds (reduces) the given list from the left with a function -> direction independent function applied to non-empty list |
| 142 | + */ |
| 143 | + public function testFoldlDirectionIndependentNonEmptyList() |
| 144 | + { |
| 145 | + $listOps = new ListOps(); |
| 146 | + $this->assertEquals( |
| 147 | + 15, |
| 148 | + $listOps->foldl(static fn ($acc, $el) => $acc + $el, [1, 2, 3, 4], 5) |
| 149 | + ); |
| 150 | + } |
| 151 | +
|
| 152 | + /** |
| 153 | + * @testdox folds (reduces) the given list from the left with a function -> direction dependent function applied to non-empty list |
| 154 | + */ |
| 155 | + public function testFoldlDirectionDependentNonEmptyList() |
| 156 | + { |
| 157 | + $listOps = new ListOps(); |
| 158 | + $this->assertEquals( |
| 159 | + 64, |
| 160 | + $listOps->foldl(static fn ($acc, $el) => $el / $acc, [1, 2, 3, 4], 24) |
| 161 | + ); |
| 162 | + } |
| 163 | +
|
| 164 | + /** |
| 165 | + * @testdox folds (reduces) the given list from the right with a function -> empty list |
| 166 | + */ |
| 167 | + public function testFoldrEmptyList() |
| 168 | + { |
| 169 | + $listOps = new ListOps(); |
| 170 | + $this->assertEquals( |
| 171 | + 2, |
| 172 | + $listOps->foldr(static fn ($acc, $el) => $el * $acc, [], 2) |
| 173 | + ); |
| 174 | + } |
| 175 | +
|
| 176 | + /** |
| 177 | + * @testdox folds (reduces) the given list from the right with a function -> direction independent function applied to non-empty list |
| 178 | + */ |
| 179 | + public function testFoldrDirectionIndependentNonEmptyList() |
| 180 | + { |
| 181 | + $listOps = new ListOps(); |
| 182 | + $this->assertEquals( |
| 183 | + 15, |
| 184 | + $listOps->foldr(static fn ($acc, $el) => $acc + $el, [1, 2, 3, 4], 5) |
| 185 | + ); |
| 186 | + } |
| 187 | +
|
| 188 | + /** |
| 189 | + * @testdox folds (reduces) the given list from the right with a function -> direction dependent function applied to non-empty list |
| 190 | + */ |
| 191 | + public function testFoldrDirectionDependentNonEmptyList() |
| 192 | + { |
| 193 | + $listOps = new ListOps(); |
| 194 | + $this->assertEquals( |
| 195 | + 9, |
| 196 | + $listOps->foldr(static fn ($acc, $el) => $el / $acc, [1, 2, 3, 4], 24) |
| 197 | + ); |
| 198 | + } |
| 199 | +
|
| 200 | + /** |
| 201 | + * @testdox reverse the elements of a list -> empty list |
| 202 | + */ |
| 203 | + public function testReverseEmptyList() |
| 204 | + { |
| 205 | + $listOps = new ListOps(); |
| 206 | + $this->assertEquals([], $listOps->reverse([])); |
| 207 | + } |
| 208 | +
|
| 209 | + /** |
| 210 | + * @testdox reverse the elements of a list -> non-empty list |
| 211 | + */ |
| 212 | + public function testReverseNonEmptyList() |
| 213 | + { |
| 214 | + $listOps = new ListOps(); |
| 215 | + $this->assertEquals([7, 5, 3, 1], $listOps->reverse([1, 3, 5, 7])); |
| 216 | + } |
| 217 | +
|
| 218 | + /** |
| 219 | + * @testdox reverse the elements of a list -> list of lists is not flattened |
| 220 | + */ |
| 221 | + public function testReverseNonEmptyListIsNotFlattened() |
| 222 | + { |
| 223 | + $listOps = new ListOps(); |
| 224 | + $this->assertEquals([[4, 5, 6], [], [3], [1, 2]], $listOps->reverse([[1, 2], [3], [], [4, 5, 6]])); |
| 225 | + } |
| 226 | +} |
0 commit comments