Based on clojure.test.check.generators.
コード例 #1
0
ファイル: Quick.php プロジェクト: steos/php-quickcheck
 public static function check($n, Gen $prop, array $opts = [])
 {
     $maxSize = @$opts['max_size'] ?: 200;
     $seed = @$opts['seed'] ?: intval(1000 * microtime(true));
     $rng = new Random($seed);
     $sizes = Gen::sizes($maxSize);
     for ($i = 0, $sizes->rewind(); $i < $n; ++$i, $sizes->next()) {
         $size = $sizes->current();
         $resultMapRose = $prop->call($rng, $size);
         $resultMap = $resultMapRose->getRoot();
         $result = $resultMap['result'];
         $args = $resultMap['args'];
         if (!$result || $result instanceof \Exception) {
             if (@$opts['echo']) {
                 echo 'F', PHP_EOL;
             }
             return self::failure($prop, $resultMapRose, $i, $size, $seed);
         }
         if (@$opts['echo']) {
             echo '.';
         }
         // TODO: trial reporting
     }
     return self::complete($prop, $n, $seed);
 }
コード例 #2
0
 function testQuickCheckShrink()
 {
     $prop = Gen::forAll([Gen::asciiStrings()], function ($s) {
         return !is_numeric($s);
     });
     $result = Quick::check(1000, $prop);
     $this->assertFalse($result['result']);
     $smallest = $result['shrunk']['smallest'][0];
     $this->assertEquals('0', $smallest, "expected smallest to be '0' but got '{$smallest}'");
 }
コード例 #3
0
ファイル: Generator.php プロジェクト: steos/php-quickcheck
 public static function containerTypes(self $innerType)
 {
     return self::oneOf($innerType->intoArrays(), $innerType->mapsFrom(self::oneOf(self::ints(), self::strings())));
 }
コード例 #4
0
ファイル: Annotation.php プロジェクト: steos/php-quickcheck
 /**
  * Determine the generators needed to test the function $f and then
  * use the predicate $p to assert correctness.
  *
  * If $p is omitted, will simply check that $f returns true for
  * each generated values.
  *
  * @param callable $f The function to test
  * @param callable $p The predicate
  * @param int $n number of iteration
  * @throws NoGeneratorAnnotationException
  * @return array
  */
 public static function check(callable $f, callable $p = null, $n = 10)
 {
     if (is_null($p)) {
         $p = function ($result) {
             return $result === true;
         };
     }
     $types = self::types($f);
     $args = array();
     foreach ($types as $t) {
         $array = false;
         if (substr($t, -2) == '[]') {
             $t = substr($t, 0, -2);
             $array = true;
         }
         if (array_key_exists($t, self::$generators)) {
             $generator = self::$generators[$t];
         } elseif (method_exists('QCheck\\Generator', $t . 's')) {
             $generator = $t . 's';
         } else {
             throw new NoGeneratorAnnotationException("Unable to find a generator for {$t}");
         }
         if (!$generator instanceof Generator) {
             $generator = call_user_func(array('QCheck\\Generator', $generator));
         }
         if ($array) {
             $generator = $generator->intoArrays();
         }
         $args[] = $generator;
     }
     $check = function () use($f, $p) {
         $result = call_user_func_array($f, func_get_args());
         return $p($result);
     };
     $prop = Generator::forAll($args, $check);
     return Quick::check($n, $prop);
 }
コード例 #5
0
 public function testHslToHsb()
 {
     $h = Gen::choose(0, 255);
     $s = Gen::choose(0, 100);
     $b = Gen::choose(0, 100);
     $gen = Gen::forAll([$h, $s, $b], function ($h, $s, $b) {
         (new \Colourist\HSL($h, $s, $b))->toHsb();
         return TRUE;
     });
     $check = Quick::check(self::RUN_COUNT, $gen);
     if ($check['result']) {
         $this->assertTrue($check['result']);
     } else {
         $failed = new \Colourist\HSL($check['fail'][0], $check['fail'][1], $check['fail'][2]);
         $this->assertTrue($check['result'], "Unable to generate HSB colour for " . $failed->inspect());
     }
 }