From 74a9d3a14de8524e326188a831381cd5327851da Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Wed, 20 Mar 2024 22:57:15 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Expose=20internal=20state=20of=20th?= =?UTF-8?q?e=20PRNG=20from=20`Random`=20(#4817)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the context of https://github.com/dubzzz/fast-check/pull/4063, we need to have an access onto the internal state of the PRNG backing the instance of `Random` in order to be able to serialize it and recreate the "exact" same instance of `Random` on destination (another worker thread). The current PR adds a `getState` method on `Random`. Adding it should be enough to fulfill the needs of https://github.com/dubzzz/fast-check/pull/4063. But we will wait for it to be confirmed before releasing any minor version including that change. --- .yarn/versions/a6ab7de6.yml | 8 ++++++++ packages/fast-check/package.json | 2 +- packages/fast-check/src/random/generator/Random.ts | 10 ++++++++++ .../unit/arbitrary/__test-helpers__/RandomHelpers.ts | 5 +++-- yarn.lock | 10 +++++----- 5 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 .yarn/versions/a6ab7de6.yml diff --git a/.yarn/versions/a6ab7de6.yml b/.yarn/versions/a6ab7de6.yml new file mode 100644 index 00000000000..a9650bcd747 --- /dev/null +++ b/.yarn/versions/a6ab7de6.yml @@ -0,0 +1,8 @@ +releases: + fast-check: minor + +declined: + - "@fast-check/ava" + - "@fast-check/jest" + - "@fast-check/vitest" + - "@fast-check/worker" diff --git a/packages/fast-check/package.json b/packages/fast-check/package.json index 9f4201b6bf8..8b3c22e8f84 100644 --- a/packages/fast-check/package.json +++ b/packages/fast-check/package.json @@ -60,7 +60,7 @@ "node": ">=8.0.0" }, "dependencies": { - "pure-rand": "^6.0.0" + "pure-rand": "^6.1.0" }, "devDependencies": { "@babel/core": "^7.24.3", diff --git a/packages/fast-check/src/random/generator/Random.ts b/packages/fast-check/src/random/generator/Random.ts index 4bf853d468b..669ee75dc4a 100644 --- a/packages/fast-check/src/random/generator/Random.ts +++ b/packages/fast-check/src/random/generator/Random.ts @@ -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; + } } diff --git a/packages/fast-check/test/unit/arbitrary/__test-helpers__/RandomHelpers.ts b/packages/fast-check/test/unit/arbitrary/__test-helpers__/RandomHelpers.ts index 80aab14beb3..db5bf0142f3 100644 --- a/packages/fast-check/test/unit/arbitrary/__test-helpers__/RandomHelpers.ts +++ b/packages/fast-check/test/unit/arbitrary/__test-helpers__/RandomHelpers.ts @@ -9,6 +9,7 @@ export function fakeRandom(): { instance: Random } & Omit, ' const nextBigInt = jest.fn(); const nextArrayInt = jest.fn(); const nextDouble = jest.fn(); + const getState = jest.fn(); class MyRandom extends Random { clone = clone; next = next; @@ -16,11 +17,11 @@ export function fakeRandom(): { instance: Random } & Omit, ' 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 }; } diff --git a/yarn.lock b/yarn.lock index 6f90165337e..a7eee021f0a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9466,7 +9466,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.12" @@ -15980,10 +15980,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