diff options
| author | Arnaud Bailly <arnaud.bailly@iohk.io> | 2025-01-25 22:36:19 +0100 |
|---|---|---|
| committer | Arnaud Bailly <arnaud.bailly@iohk.io> | 2025-01-25 22:36:19 +0100 |
| commit | 064e80689c7d7126a504400d8c4e962b6b334bac (patch) | |
| tree | f7c3aca995ac06ef534c4a213a70875e8f338eef /pbt/ts/src/property.ts | |
| parent | 8bcaac40ee1afb8c36f98beabe0348ae3713d44d (diff) | |
| download | lambda-nantes-064e80689c7d7126a504400d8c4e962b6b334bac.tar.gz | |
Added some 'interesting' properties about lists
These are the classical properties from Claessen and Hughes' paper.
Shrinking is too expensive as it is so need to prune recursion tree.
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 }; + }; } |
