Ejemplo n.º 1
0
 public function testConcatenationMaintainsLength()
 {
     $this->forAll(Generator\vector(10, Generator\nat(1000)), Generator\vector(10, Generator\nat(1000)))->then(function ($first, $second) {
         $concatenated = array_merge($first, $second);
         $this->assertEquals(count($concatenated), count($first) + count($second), var_export($first, true) . " and " . var_export($second, true) . " do not maintain their length when concatenated.");
     });
 }
Ejemplo n.º 2
0
 public function testAlwaysFails()
 {
     $this->forAll([Generator\frequency([[8, Generator\nat(1, 100)], [4, Generator\nat(100, 200)], [4, Generator\nat(200, 300)]])])->then(function ($element) {
         // Expected to shrunk always to 1
         $this->assertEquals(0, $element);
     });
 }
Ejemplo n.º 3
0
 public function testShrinkingMappedValuesInsideOtherGenerators()
 {
     $this->forAll(Generator\vector(3, Generator\map(function ($n) {
         return $n * 2;
     }, Generator\nat())))->then(function ($tripleOfEvenNumbers) {
         $this->assertLessThanOrEqual(100, array_sum($tripleOfEvenNumbers), "The triple sum " . var_export($tripleOfEvenNumbers, true) . " is not less than 100");
     });
 }
Ejemplo n.º 4
0
 public function testGeneratedDataCollectionWithCustomMapper()
 {
     $this->forAll(Generator\seq(Generator\nat()))->withMaxSize(10)->hook(Listener\collectFrequencies(function ($array) {
         return count($array);
     }))->then(function ($array) {
         $this->assertEquals(count($array), count(array_reverse($array)));
     });
 }
Ejemplo n.º 5
0
 /**
  * Shrinking may be avoided when then() is slow or non-deterministic.
  */
 public function testThenIsNotCalledMultipleTime()
 {
     $this->calls = 0;
     $this->forAll(Generator\nat())->disableShrinking()->then(function ($number) {
         $this->calls++;
         $this->assertTrue(false, "Total calls: {$this->calls}");
     });
 }
Ejemplo n.º 6
0
 public function testSetsOfAnotherGeneratorsDomain()
 {
     $this->forAll(Generator\set(Generator\nat()))->then(function ($set) {
         $this->assertInternalType('array', $set);
         foreach ($set as $element) {
             $this->assertGreaterThanOrEqual(0, $element);
         }
     });
 }
Ejemplo n.º 7
0
 public function testArraySorting()
 {
     $this->forAll(Generator\seq(Generator\nat()))->then(function ($array) {
         sort($array);
         for ($i = 0; $i < count($array) - 1; $i++) {
             $this->assertTrue($array[$i] <= $array[$i + 1], "Array is not sorted: " . var_export($array, true));
         }
     });
 }
Ejemplo n.º 8
0
 public function testArraySortingIsIdempotent()
 {
     $this->forAll(Generator\seq(Generator\nat()))->then(function ($array) {
         sort($array);
         $expected = $array;
         sort($array);
         $this->assertEquals($expected, $array);
     });
 }
Ejemplo n.º 9
0
 public function testCreatingABrandNewGeneratorFromAGeneratedValue()
 {
     $this->forAll(Generator\bind(Generator\vector(4, Generator\nat()), function ($vector) {
         return Generator\tuple(Generator\elements($vector), Generator\constant($vector));
     }))->then(function ($tuple) {
         list($element, $vector) = $tuple;
         $this->assertContains($element, $vector);
     });
 }
Ejemplo n.º 10
0
 public function testNotWithstandingCrashesJobsAreEventuallyPerformed()
 {
     $this->limitTo(100)->forAll(Generator\bind(Generator\choose(1, 4), function ($workers) {
         return Generator\tuple(Generator\constant($workers), Generator\seq(Generator\oneOf(Generator\map(function ($durationAndTag) {
             list($duration, $tag) = $durationAndTag;
             return ['enqueueJob', $duration, $tag];
         }, Generator\tuple(Generator\nat(), Generator\elements(['generic', 'fast-lane']))), Generator\map(function ($workerIndex) {
             return ['restartWorkerGracefully', $workerIndex];
         }, Generator\choose(0, $workers - 1)), Generator\map(function ($workerIndex) {
             return ['restartWorkerByKilling', $workerIndex];
         }, Generator\choose(0, $workers - 1)), Generator\constant('restartRecruiterGracefully'), Generator\constant('restartRecruiterByKilling'), Generator\map(function ($milliseconds) {
             return ['sleep', $milliseconds];
         }, Generator\choose(1, 1000)))));
     }))->hook(Listener\log('/tmp/recruiter-test-iterations.log'))->hook(Listener\collectFrequencies())->disableShrinking()->then(function ($tuple) {
         list($workers, $actions) = $tuple;
         $this->clean();
         $this->start($workers);
         foreach ($actions as $action) {
             $this->logAction($action);
             if (is_array($action)) {
                 $arguments = $action;
                 $method = array_shift($arguments);
                 call_user_func_array([$this, $method], $arguments);
             } else {
                 $this->{$action}();
             }
         }
         $estimatedTime = max(count($actions) * 4, 60);
         Timeout::inSeconds($estimatedTime, function () {
             return "all {$this->jobs} jobs to be performed. Now is " . date('c') . " Logs: " . $this->files();
         })->until(function () {
             return $this->jobRepository->countArchived() === $this->jobs;
         });
         $at = T\now();
         $statistics = $this->recruiter->statistics($tag = null, $at);
         $this->assertInvariantsOnStatistics($statistics);
         // TODO: remove duplication
         $statisticsByTag = [];
         $cumulativeThroughput = 0;
         foreach (['generic', 'fast-lane'] as $tag) {
             $statisticsByTag[$tag] = $this->recruiter->statistics($tag, $at);
             $this->assertInvariantsOnStatistics($statisticsByTag[$tag]);
             $cumulativeThroughput += $statisticsByTag[$tag]['throughput']['value'];
         }
         var_dump($statistics, $statisticsByTag);
         // TODO: add tolerance
         $this->assertEquals($statistics['throughput']['value'], $cumulativeThroughput);
     });
 }
Ejemplo n.º 11
0
 public function testUseConstantGeneratorImplicitly()
 {
     $this->forAll(Generator\nat(), 2)->then(function ($number, $alwaysTwo) {
         $this->assertTrue($number * $alwaysTwo % 2 === 0);
     });
 }
Ejemplo n.º 12
0
 public function testNaturalNumbersMagnitude()
 {
     $this->forAll(Generator\nat(1000))->then(function ($number) {
         $this->assertTrue($number < 42, "{$number} is not less than 42 apparently");
     });
 }
Ejemplo n.º 13
0
 public function testArrayReverse()
 {
     $this->forAll(Generator\seq(Generator\nat()))->then(function ($array) {
         $this->assertEquals($array, array_reverse(array_reverse($array)));
     });
 }
Ejemplo n.º 14
0
 public function testDiff()
 {
     $this->iterations = 1000;
     $this->forAll(Generator\nat(), Generator\date(new DateTime('1980-01-01'), new DateTime('2020-12-31')))->then(function ($days, $datetime) {
         $date = UTCDateTime::box($datetime);
         $addDiff = $date->addDays($days)->diff($date)->days;
         $subDiff = $date->subtractDays($days)->diff($date)->days;
         $this->assertSame($days, $addDiff, "adding and diffing {$days} days(s) from {$date->toIso8601()} returned {$addDiff}");
         $this->assertEquals($days, $subDiff, "subtracting and diffing {$days} month(s) from {$date->toIso8601()} returned {$subDiff}");
     });
 }
Ejemplo n.º 15
0
 public function testSamplingShrinking()
 {
     $generator = Generator\nat(1000);
     var_dump($this->sampleShrink($generator));
 }
Ejemplo n.º 16
0
 public function testPropertyNeverSatisfied()
 {
     $this->forAll(Generator\nat(1000), Generator\nat(1000))->then(function ($first, $second) {
         $this->assertEquals(-1, my_sum($first, $second), "Summing {$first} and {$second}");
     });
 }