Functional\cycle tests
authorwidmogrod <widmogrod@gmail.com>
Thu, 21 Dec 2017 18:11:00 +0000 (19:11 +0100)
committerwidmogrod <widmogrod@gmail.com>
Thu, 21 Dec 2017 18:11:00 +0000 (19:11 +0100)
test/Functional/CycleTest.php [new file with mode: 0644]
test/Functional/IterateTest.php
test/Primitive/ProductTest.php

diff --git a/test/Functional/CycleTest.php b/test/Functional/CycleTest.php
new file mode 100644 (file)
index 0000000..02389a1
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+
+namespace test\Functional;
+
+use Eris\Generator;
+use Eris\TestTrait;
+use function Widmogrod\Functional\eql;
+use function Widmogrod\Functional\filter;
+use function Widmogrod\Functional\foldr;
+use const Widmogrod\Functional\identity;
+use function Widmogrod\Functional\cycle;
+use function Widmogrod\Functional\iterate;
+use function Widmogrod\Functional\length;
+use function Widmogrod\Functional\take;
+
+class CycleTest extends \PHPUnit_Framework_TestCase
+{
+    use TestTrait;
+
+    public function test_it_should_generate_infinite_list()
+    {
+        $this->forAll(
+            Generator\choose(1, 100),
+            Generator\int()
+        )->then(function ($n, $value) {
+            $list = cycle(take($n, iterate(identity, $value)));
+            $list = take($n * 2, $list);
+
+            return length($list) === $n * 2;
+        });
+    }
+
+    public function test_it_should_generate_repetive_value()
+    {
+        $this->forAll(
+            Generator\choose(1, 100),
+            Generator\int()
+        )->then(function ($n, $value) {
+            $addOne = function (int $i): int {
+                return $i + 1;
+            };
+
+            $list = cycle(take($n, iterate($addOne, $value)));
+            $list = take($n * 2, $list);
+
+            return length(filter(eql($value), $list)) === $n
+                &&  ($value + $n) * 2 === foldr(identity, $list);
+        });
+    }
+}
index 464e0e5..eb1be22 100644 (file)
@@ -34,9 +34,11 @@ class IterateTest extends \PHPUnit_Framework_TestCase
             Generator\choose(1, 100),
             Generator\int()
         )->then(function ($n, $value) {
-            $list = take($n, iterate(function ($i) {
+            $addOne = function (int $i): int {
                 return $i + 1;
-            }, $value));
+            };
+
+            $list = take($n, iterate($addOne, $value));
 
             return length(filter(eql($value), $list)) === $n
                 && $value + $n === foldr(identity, $list);
index 2655571..6e24ea0 100644 (file)
@@ -45,7 +45,7 @@ class ProductTest extends \PHPUnit_Framework_TestCase
 
     private function randomize()
     {
-        usleep(10);
+        usleep(100);
 
         return Product::of(random_int(-1000, 1000));
     }