summaryrefslogtreecommitdiff
path: root/pbt/ts/src/gen.test.ts
diff options
context:
space:
mode:
authorArnaud Bailly <arnaud@pankzsoft.com>2026-09-02 22:27:04 +0200
committerArnaud Bailly <arnaud@pankzsoft.com>2026-09-02 22:27:04 +0200
commitd68fdea59aaf05877438bfba669c96d716523765 (patch)
tree17913de3ed05cbc494b44b4ffabece46608c89f0 /pbt/ts/src/gen.test.ts
parent7f69074b259f479b05777ba915a6ef3481a749af (diff)
downloadlambda-nantes-main.tar.gz
Refactor generators to use a factory for passing bytesHEADmain
Diffstat (limited to 'pbt/ts/src/gen.test.ts')
-rw-r--r--pbt/ts/src/gen.test.ts33
1 files changed, 13 insertions, 20 deletions
diff --git a/pbt/ts/src/gen.test.ts b/pbt/ts/src/gen.test.ts
index 6e891e4..8d73beb 100644
--- a/pbt/ts/src/gen.test.ts
+++ b/pbt/ts/src/gen.test.ts
@@ -1,18 +1,18 @@
import Prando from 'prando';
import { none } from 'fp-ts/lib/Option.js';
-import * as G from './gen.js';
+import G from './gen.js';
import { test, expect } from 'vitest';
import { chiSquare, computeChiSquaredPValue, computeCramersV } from './stats.js';
test('can generate and shrink bytes', async () => {
- const prng = new Prando(Math.random() * 1000);
- const gen = new G.BytesGen(prng);
+ const prng = G();
+ const gen = prng.genBytes();
const bytes: number[][] = [];
for (let i = 1; i < 10; i++) {
bytes.push(gen.generate(i));
}
console.log('Generated bytes:', bytes);
- const replay = gen.replay();
+ const replay = prng.replay().genBytes();
for (let i = 1; i < 10; i++) {
const replayed = replay.generate(i);
expect(replayed, `Replaying at ${i}`).toEqual(bytes[i - 1]);
@@ -20,22 +20,19 @@ test('can generate and shrink bytes', async () => {
});
test('can generate an arbitrary integer', () => {
- const prng = new Prando(Math.random() * 1000);
- const gen = G.genInt();
+ const gen = G().genInt();
const value = gen.generate();
expect(typeof value).toBe('number');
});
test('can generate N arbitrary integers', () => {
- const prng = new Prando(Math.random() * 1000);
- const gen = G.genInt();
+ const gen = G().genInt();
const value = gen.generateN(5);
expect(value.length).toBe(5);
});
test('can generate arbitrary integer within bounds', () => {
- const seed = Math.random() * 1000;
- const gen = G.genIntWithin(1000, 200000, seed);
+ const gen = G().genIntWithin(1000, 200000);
for (let i = 0; i < 100; i++) {
const value = gen.generate();
expect(value).toBeGreaterThanOrEqual(1000);
@@ -44,10 +41,9 @@ test('can generate arbitrary integer within bounds', () => {
});
test('generated integers are uniformly distributed', () => {
- const seed = Math.random() * 1000;
const lb = 0;
const ub = 100000;
- const gen = G.genIntWithin(lb, ub, seed);
+ const gen = G().genIntWithin(lb, ub);
const range = ub - lb;
const buckets = 100;
const numSamples = 100000;
@@ -75,14 +71,12 @@ test('generated integers are uniformly distributed', () => {
// compute p-value and Cramér's V.
const cramerv = computeCramersV(chiSquared, numSamples, buckets);
const pValue = computeChiSquaredPValue(chiSquared, buckets);
- console.log(`Chi-squared: ${chiSquared}, p-value: ${pValue}, Cramer V: ${cramerv}`);
- expect(cramerv).toBeLessThan(0.015);
- expect(pValue).toBeGreaterThan(0.05);
+ expect(cramerv, `Chi-squared: ${chiSquared}, Cramer V: ${cramerv}`).toBeLessThan(0.015);
+ expect(pValue, `Chi-squared: ${chiSquared}, p-value: ${pValue}`).toBeGreaterThan(0.05);
});
test('can generate uniformly distributed arbitrary strings shorter than 256 characters', () => {
- const seed = Math.random() * (1 << 31);
- const gen = G.genASCIIString(seed);
+ const gen = G().genString();
const buckets = 16;
const numSamples = (1 << 16);
const lengths = new Array(buckets).fill(0);
@@ -92,10 +86,9 @@ test('can generate uniformly distributed arbitrary strings shorter than 256 char
expect(value.length).toBeLessThan(256);
}
- console.log(lengths);
const chiSquared = chiSquare(lengths, new Array(buckets).fill(numSamples / buckets));
const cramerv = computeCramersV(chiSquared, numSamples, buckets);
const pValue = computeChiSquaredPValue(chiSquared, buckets);
- console.log(`Chi-squared: ${chiSquared}, p-value: ${pValue}, Cramer V: ${cramerv}`);
- expect(cramerv).toBeLessThan(0.015);
+ expect(cramerv, `Chi-squared: ${chiSquared}, Cramer V: ${cramerv}`).toBeLessThan(0.015);
+ expect(pValue, `Chi-squared: ${chiSquared}, p-value: ${pValue}`).toBeGreaterThan(0.05);
});