Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Expose internal state of the PRNG from Random #4817

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .yarn/versions/a6ab7de6.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
releases:
fast-check: minor

declined:
- "@fast-check/ava"
- "@fast-check/jest"
- "@fast-check/vitest"
- "@fast-check/worker"
2 changes: 1 addition & 1 deletion packages/fast-check/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"node": ">=8.0.0"
},
"dependencies": {
"pure-rand": "^6.0.0"
"pure-rand": "^6.1.0"
},
"devDependencies": {
"@babel/core": "^7.24.0",
Expand Down
10 changes: 10 additions & 0 deletions packages/fast-check/src/random/generator/Random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,14 @@ export class Random {
const b = this.next(27);
return (a * Random.DBL_FACTOR + b) * Random.DBL_DIVISOR;
}

/**
* Extract the internal state of the internal RandomGenerator backing the current instance of Random
*/
getState(): readonly number[] | undefined {
if ('getState' in this.internalRng && typeof this.internalRng.getState === 'function') {
return this.internalRng.getState();
}
return undefined;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ export function fakeRandom(): { instance: Random } & Omit<MaybeMocked<Random>, '
const nextBigInt = jest.fn();
const nextArrayInt = jest.fn();
const nextDouble = jest.fn();
const getState = jest.fn();
class MyRandom extends Random {
clone = clone;
next = next;
nextBoolean = nextBoolean;
nextInt = nextInt;
nextBigInt = nextBigInt;
nextArrayInt = nextArrayInt;
nextDouble = nextDouble;
getState = getState;
}

// Calling `new MyRandom` triggers a call to the default ctor of `Random`.
// As we don't use anything from this base class, we just pass the ctor with a value that looks ok for it.
const instance = new MyRandom({ clone: () => null } as any);
return { instance, clone, next, nextBoolean, nextInt, nextBigInt, nextArrayInt, nextDouble };
return { instance, clone, next, nextBoolean, nextInt, nextBigInt, nextArrayInt, nextDouble, getState };
}
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9381,7 +9381,7 @@ __metadata:
glob: "npm:^10.3.10"
jest: "npm:^29.7.0"
not-node-buffer: "npm:buffer@^6.0.3"
pure-rand: "npm:^6.0.0"
pure-rand: "npm:^6.1.0"
regexp-tree: "npm:^0.1.27"
replace-in-file: "npm:^7.1.0"
typedoc: "npm:^0.25.10"
Expand Down Expand Up @@ -15886,10 +15886,10 @@ __metadata:
languageName: node
linkType: hard

"pure-rand@npm:^6.0.0, pure-rand@npm:^6.0.4":
version: 6.0.4
resolution: "pure-rand@npm:6.0.4"
checksum: 10c0/0fe7b12f25b10ea5b804598a6f37e4bcf645d2be6d44fe963741f014bf0095bdb6ff525106d6da6e76addc8142358fd380f1a9b8c62ea4d5516bf26a96a37c95
"pure-rand@npm:^6.0.0, pure-rand@npm:^6.0.4, pure-rand@npm:^6.1.0":
version: 6.1.0
resolution: "pure-rand@npm:6.1.0"
checksum: 10c0/1abe217897bf74dcb3a0c9aba3555fe975023147b48db540aa2faf507aee91c03bf54f6aef0eb2bf59cc259a16d06b28eca37f0dc426d94f4692aeff02fb0e65
languageName: node
linkType: hard

Expand Down
Loading