From 064e80689c7d7126a504400d8c4e962b6b334bac Mon Sep 17 00:00:00 2001 From: Arnaud Bailly Date: Sat, 25 Jan 2025 22:36:19 +0100 Subject: 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. --- pbt/ts/src/property.ts | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) (limited to 'pbt/ts/src/property.ts') 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) => boolean; export type Gen = (rng: Prando) => ((size: number) => A); -type Shrinker = (a: A) => [A]; +type Shrinker = (a: A) => A[]; + +type Property = (rng: Prando, + size: number) => TestResult; const MAX_SUCCESS = 100; @@ -26,6 +29,7 @@ function findMinimalCounterExample(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(x: A, return { counterexample, shrinks }; } -export function property(rng: Prando, - size: number, + +export function generate(gen: Gen): A { + let rng = new Prando(Math.random() * 1000); + return gen(rng)(100); +} + +export function property( predicate: Predicate, generator: Gen, - shrinker: Shrinker): TestResult { - 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): Property { + 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 }; + }; } -- cgit v1.2.3