From 01660b4c6ac5d451102d73a7df22483f9fcc2ea1 Mon Sep 17 00:00:00 2001 From: Arnaud Bailly Date: Mon, 1 Dec 2025 17:16:38 +0100 Subject: complete operations sequence tests --- pbt/ts/src/property.ts | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'pbt/ts/src/property.ts') diff --git a/pbt/ts/src/property.ts b/pbt/ts/src/property.ts index 04f2a62..da05328 100644 --- a/pbt/ts/src/property.ts +++ b/pbt/ts/src/property.ts @@ -131,21 +131,26 @@ export function frequency(distribution: [number, Gen][]): Gen { const total: number = distribution.reduce((acc, [freq, _]) => acc + freq, 0); return (rng: Prando) => (size: number) => { - const idx = rng.next(0, total); - let selectedGen: Option> = none; - selectedGen = distribution.reduce((found, [freq, gen]) => { - if (isSome(found)) { - return found; - } else if (idx <= freq) { - return some(gen); - } else { - return none; - } - }, selectedGen); + const idx = Math.floor(rng.next(0, total)); + const [selectedGen, _] = distribution.reduce( + ([found, sum], [freq, gen]) => { + const current = sum + freq; + if (isSome(found)) { + return [found, current]; + } else if (idx <= current) { + return [some(gen), current]; + } else { + return [none, current]; + } + }, + [none as Option>, 0], + ); const generator: Gen = pipe( selectedGen, getOrElse((() => { - throw new Error(`invalid frequency distribution ${distribution}`); + throw new Error( + `invalid frequency distribution ${distribution}, idx=${idx}`, + ); }) as LazyArg>), ); return generator(rng)(size); -- cgit v1.3.1