ListOpsTest.php 7.7 KB

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