diff options
Diffstat (limited to 'pbt/ts/src/property.ts')
| -rw-r--r-- | pbt/ts/src/property.ts | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/pbt/ts/src/property.ts b/pbt/ts/src/property.ts index e8d3a21..40e32c5 100644 --- a/pbt/ts/src/property.ts +++ b/pbt/ts/src/property.ts @@ -13,7 +13,10 @@ type Predicate<A> = (a: A) => boolean; export type Gen<A> = (rng: Prando) => ((size: number) => A); -type Shrinker<A> = (a: A) => [A]; +type Shrinker<A> = (a: A) => A[]; + +type Property<A> = (rng: Prando, + size: number) => TestResult<A>; const MAX_SUCCESS = 100; @@ -26,6 +29,7 @@ function findMinimalCounterExample<A>(x: A, let shrinks = depth; for (let y of xs) { if (!predicate(y)) { + console.log("Shrinking with " + y); let shrink = findMinimalCounterExample(y, predicate, shrinker, depth + 1); if (shrink.shrinks > depth) { counterexample = shrink.counterexample; @@ -36,18 +40,28 @@ function findMinimalCounterExample<A>(x: A, return { counterexample, shrinks }; } -export function property<A>(rng: Prando, - size: number, + +export function generate<A>(gen: Gen<A>): A { + let rng = new Prando(Math.random() * 1000); + return gen(rng)(100); +} + +export function property<A>( predicate: Predicate<A>, generator: Gen<A>, - shrinker: Shrinker<A>): TestResult<A> { - let gen = generator(rng); - for (let i = 0; i < MAX_SUCCESS; i++) { - let x = gen(size); - if (!predicate(x)) { - let counterexample = findMinimalCounterExample(x, predicate, shrinker); - return { result: 'Falsified', counterexample }; - } - } - return { result: 'OK', counterexample: null }; + shrinker: Shrinker<A>): Property<A> { + return (rng: Prando, + size: number) => { + let gen = generator(rng); + let i = 0; + for (; i < MAX_SUCCESS; i++) { + let x = gen(size); + if (!predicate(x)) { + let counterexample = findMinimalCounterExample(x, predicate, shrinker); + return { result: 'Falsified', tests: i, counterexample }; + } + size++; + } + return { result: 'OK', tests: i, counterexample: null }; + }; } |
