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}'"); }
/** * 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); }
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()); } }