Skip to content

⚡️ Import less from pure-rand #5718

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions examples/003-misc/mazeGenerator/src/mazeGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Implementation inspired from https://github.com/dubzzz/various-algorithms/blob/main/algorithms/graph/maze-generator/implem.cpp
import prand from 'pure-rand';
import { xorshift128plus } from 'pure-rand/generator/XorShift';
import { Random } from 'fast-check';

export type Dimension = {
Expand Down Expand Up @@ -95,7 +95,7 @@ const mazeGeneratorInternal = (
};

export const mazeGenerator = (seed: number, dim: Dimension, startPt: Point, endPt: Point): CellType[][] => {
const mrng = new Random(prand.xorshift128plus(seed));
const mrng = new Random(xorshift128plus(seed));

while (true) {
const { maze, hasPathLeadingToTheEnd } = mazeGeneratorInternal(mrng, dim, startPt, endPt);
Expand Down
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"express": "^4.21.2",
"fast-check": "workspace:*",
"lodash": "^4.17.21",
"pure-rand": "^7.0.0",
"pure-rand": "^7.0.1",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"supertest": "^7.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/fast-check/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"node": ">=8.0.0"
},
"dependencies": {
"pure-rand": "^7.0.0"
"pure-rand": "^7.0.1"
},
"devDependencies": {
"@fast-check/expect-type": "workspace:*",
Expand Down
4 changes: 2 additions & 2 deletions packages/fast-check/src/check/runner/Tosser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RandomGenerator } from 'pure-rand';
import { skipN } from 'pure-rand';
import type { RandomGenerator } from 'pure-rand/types/RandomGenerator';
import { skipN } from 'pure-rand/distribution/SkipN';

import { Random } from '../../random/generator/Random';
import type { IRawProperty } from '../property/IRawProperty';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { RandomType } from './RandomType';
import type { VerbosityLevel } from './VerbosityLevel';
import type { RunDetails } from '../reporter/RunDetails';
import type { RandomGenerator } from 'pure-rand';
import type { RandomGenerator } from 'pure-rand/types/RandomGenerator';

/**
* Customization of the parameters used to run the properties
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import prand, { unsafeSkipN } from 'pure-rand';
import type { Parameters } from './Parameters';
import { VerbosityLevel } from './VerbosityLevel';
import type { RunDetails } from '../reporter/RunDetails';
import type { RandomGenerator } from 'pure-rand';
import type { RandomGenerator } from 'pure-rand/types/RandomGenerator';
import { unsafeSkipN } from 'pure-rand/distribution/UnsafeSkipN';
import { xorshift128plus } from 'pure-rand/generator/XorShift';
import { mersenne } from 'pure-rand/generator/MersenneTwister';
import { congruential32 } from 'pure-rand/generator/LinearCongruential';
import { xoroshiro128plus } from 'pure-rand/generator/XoroShiro';

const safeDateNow = Date.now;
const safeMathMin = Math.min;
Expand Down Expand Up @@ -118,18 +122,18 @@ export class QualifiedParameters<T> {
return seed32 ^ (gap * 0x100000000);
};
private static readRandomType = <T>(p: Parameters<T>): ((seed: number) => QualifiedRandomGenerator) => {
if (p.randomType === undefined) return prand.xorshift128plus as (seed: number) => QualifiedRandomGenerator;
if (p.randomType === undefined) return xorshift128plus as (seed: number) => QualifiedRandomGenerator;
if (typeof p.randomType === 'string') {
switch (p.randomType) {
case 'mersenne':
return QualifiedParameters.createQualifiedRandomGenerator(prand.mersenne);
return QualifiedParameters.createQualifiedRandomGenerator(mersenne);
case 'congruential':
case 'congruential32':
return QualifiedParameters.createQualifiedRandomGenerator(prand.congruential32);
return QualifiedParameters.createQualifiedRandomGenerator(congruential32);
case 'xorshift128plus':
return prand.xorshift128plus as (seed: number) => QualifiedRandomGenerator;
return xorshift128plus as (seed: number) => QualifiedRandomGenerator;
case 'xoroshiro128plus':
return prand.xoroshiro128plus as (seed: number) => QualifiedRandomGenerator;
return xoroshiro128plus as (seed: number) => QualifiedRandomGenerator;
default:
throw new Error(`Invalid random specified: '${p.randomType}'`);
}
Expand Down
5 changes: 3 additions & 2 deletions packages/fast-check/src/random/generator/Random.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { RandomGenerator } from 'pure-rand';
import { unsafeUniformBigIntDistribution, unsafeUniformIntDistribution } from 'pure-rand';
import { unsafeUniformBigIntDistribution } from 'pure-rand/distribution/UnsafeUniformBigIntDistribution';
import { unsafeUniformIntDistribution } from 'pure-rand/distribution/UnsafeUniformIntDistribution';
import type { RandomGenerator } from 'pure-rand/types/RandomGenerator';

/**
* Wrapper around an instance of a `pure-rand`'s random number generator
Expand Down
6 changes: 3 additions & 3 deletions packages/fast-check/test/e2e/GenerateAllValues.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import * as prand from 'pure-rand';
import { xorshift128plus } from 'pure-rand/generator/XorShift';
import * as fc from '../../src/fast-check';
import { seed } from './seed';

Expand All @@ -9,7 +9,7 @@ describe(`Generate all values (seed: ${seed})`, () => {
* of their type / range
*/
const lookForMissing = <T>(arb: fc.Arbitrary<T>, expectedSize: number): void => {
const mrng = new fc.Random(prand.xorshift128plus(seed));
const mrng = new fc.Random(xorshift128plus(seed));
const alreadySeen = new Set<T>();
while (alreadySeen.size < expectedSize) {
const value = arb.generate(mrng, undefined).value;
Expand Down Expand Up @@ -52,7 +52,7 @@ describe(`Generate all values (seed: ${seed})`, () => {
) => {
it(`should be able to generate ${label}`, () => {
let numTries = 0;
const mrng = new fc.Random(prand.xorshift128plus(seed));
const mrng = new fc.Random(xorshift128plus(seed));
const arb = fc.anything({
withBoxedValues: true,
withMap: true,
Expand Down
12 changes: 6 additions & 6 deletions packages/fast-check/test/e2e/NoStackOverflowOnShrink.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, expect } from 'vitest';
import * as fc from '../../src/fast-check';
import { seed } from './seed';
import * as prand from 'pure-rand';
import { xorshift128plus } from 'pure-rand/generator/XorShift';

const computeMaximalStackSize = () => {
// Compute the maximal call stack size
Expand Down Expand Up @@ -71,7 +71,7 @@ describe(`NoStackOverflowOnShrink (seed: ${seed})`, () => {
// the maximal depth we computed before reaching a stack overflow
expect(maxDepthForArrays).toBeGreaterThan(callStackSizeWithMargin);

const mrng = new fc.Random(prand.xorshift128plus(seed));
const mrng = new fc.Random(xorshift128plus(seed));
const arb = fc.array(fc.boolean(), { maxLength: maxDepthForArrays, size: 'max' });
let value: fc.Value<boolean[]> | null = null;
while (value === null) {
Expand All @@ -88,7 +88,7 @@ describe(`NoStackOverflowOnShrink (seed: ${seed})`, () => {
// the maximal depth we computed before reaching a stack overflow
expect(maxDepthForArrays).toBeGreaterThan(callStackSizeWithMargin);

const mrng = new fc.Random(prand.xorshift128plus(seed));
const mrng = new fc.Random(xorshift128plus(seed));
const arb = fc.array(fc.boolean(), { minLength: maxDepthForArrays, maxLength: maxDepthForArrays });
const value: fc.Value<boolean[]> = arb.generate(mrng, undefined);
expect(() => iterateOverShrunkValues(arb, value)).not.toThrow();
Expand All @@ -99,7 +99,7 @@ describe(`NoStackOverflowOnShrink (seed: ${seed})`, () => {
// the maximal depth we computed before reaching a stack overflow
expect(maxDepthForArrays).toBeGreaterThan(callStackSizeWithMargin);

const mrng = new fc.Random(prand.xorshift128plus(seed));
const mrng = new fc.Random(xorshift128plus(seed));
const arb = fc.tuple<boolean[]>(...[...Array(maxDepthForArrays)].fill(fc.boolean()));
const value: fc.Value<boolean[]> = arb.generate(mrng, undefined);
expect(() => iterateOverShrunkValues(arb, value)).not.toThrow();
Expand All @@ -110,7 +110,7 @@ describe(`NoStackOverflowOnShrink (seed: ${seed})`, () => {
// the maximal depth we computed before reaching a stack overflow
expect(maxDepthForArrays).toBeGreaterThan(callStackSizeWithMargin);

const mrng = new fc.Random(prand.xorshift128plus(seed));
const mrng = new fc.Random(xorshift128plus(seed));
const arb = fc.shuffledSubarray([...Array(maxDepthForArrays)].map((_, i) => i));
let value: fc.Value<number[]> | null = null;
while (value === null) {
Expand All @@ -133,7 +133,7 @@ describe(`NoStackOverflowOnShrink (seed: ${seed})`, () => {
run = () => {};
}

const mrng = new fc.Random(prand.xorshift128plus(seed));
const mrng = new fc.Random(xorshift128plus(seed));
const arb = fc.commands([fc.boolean().map((b) => new AnyCommand(b))], {
maxCommands: maxDepthForArrays,
size: 'max',
Expand Down
4 changes: 2 additions & 2 deletions packages/fast-check/test/e2e/ReplayCommands.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect } from 'vitest';
import * as fc from '../../src/fast-check';
import * as prand from 'pure-rand';
import { mersenne } from 'pure-rand/generator/MersenneTwister';
import { seed } from './seed';

// Fake commands
Expand Down Expand Up @@ -65,7 +65,7 @@ describe(`ReplayCommands (seed: ${seed})`, () => {
expect(outReplayed.numRuns).toEqual(1);
});
it('Should be able to resume a stopped run by specifying replayPath in fc.commands', () => {
const mrng = new fc.Random(prand.mersenne(seed));
const mrng = new fc.Random(mersenne(seed));
const out = fc.check(buildProp(undefined, mrng), { seed: seed });
expect(out.failed).toBe(true);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'vitest';
import * as prand from 'pure-rand';
import { xorshift128plus } from 'pure-rand/generator/XorShift';
import * as fc from 'fast-check';
import { assertNoPoisoning, restoreGlobals } from '@fast-check/poisoning';

Expand Down Expand Up @@ -273,7 +273,7 @@ export function assertGenerateIndependentOfSize<T, U = never>(
// Various helpers

function randomFromSeed(seed: number): Random {
return new Random(prand.xorshift128plus(seed));
return new Random(xorshift128plus(seed));
}

function biasFactorArbitrary() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect, vi } from 'vitest';
import fc from 'fast-check';
import prand from 'pure-rand';
import { mersenne } from 'pure-rand/generator/MersenneTwister';

import { ArrayArbitrary } from '../../../../src/arbitrary/_internals/ArrayArbitrary';
import { Value } from '../../../../src/check/arbitrary/definition/Value';
Expand Down Expand Up @@ -544,7 +544,7 @@ describe('ArrayArbitrary (integration)', () => {
it('should not re-use twice the same instance of cloneable', () => {
// Arrange
const alreadySeenCloneable = new Set<unknown>();
const mrng = new Random(prand.mersenne(0));
const mrng = new Random(mersenne(0));
const arb = new ArrayArbitrary(new CloneableArbitrary(), 0, 5, 100, undefined, undefined, []); // 0 to 5 generated items

// Act
Expand Down
16 changes: 8 additions & 8 deletions packages/fast-check/test/unit/arbitrary/commands.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it, expect } from 'vitest';
import * as fc from 'fast-check';
import { commands } from '../../../src/arbitrary/commands';

import prand from 'pure-rand';
import { xorshift128plus } from 'pure-rand/generator/XorShift';
import type { Command } from '../../../src/check/model/command/Command';
import { Random } from '../../../src/random/generator/Random';
import { Arbitrary } from '../../../src/check/arbitrary/definition/Arbitrary';
Expand Down Expand Up @@ -35,7 +35,7 @@ describe('commands (integration)', () => {
fc.assert(
fc.property(fc.integer(), fc.option(fc.integer({ min: 2 }), { nil: undefined }), (seed, biasFactor) => {
// Arrange
const mrng = new Random(prand.xorshift128plus(seed));
const mrng = new Random(xorshift128plus(seed));
const logOnCheck: { data: string[] } = { data: [] };

// Act
Expand All @@ -55,7 +55,7 @@ describe('commands (integration)', () => {
fc.assert(
fc.property(fc.integer(), fc.option(fc.integer({ min: 2 }), { nil: undefined }), (seed, biasFactor) => {
// Arrange
const mrng = new Random(prand.xorshift128plus(seed));
const mrng = new Random(xorshift128plus(seed));
const logOnCheck: { data: string[] } = { data: [] };

// Act
Expand All @@ -82,7 +82,7 @@ describe('commands (integration)', () => {
fc.assert(
fc.property(fc.integer(), fc.option(fc.integer({ min: 2 }), { nil: undefined }), (seed, biasFactor) => {
// Arrange
const mrng = new Random(prand.xorshift128plus(seed));
const mrng = new Random(xorshift128plus(seed));
const logOnCheck: { data: string[] } = { data: [] };

// Act
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('commands (integration)', () => {
fc.assert(
fc.property(fc.integer(), fc.option(fc.integer({ min: 2 }), { nil: undefined }), (seed, biasFactor) => {
// Arrange
const mrng = new Random(prand.xorshift128plus(seed));
const mrng = new Random(xorshift128plus(seed));
const logOnCheck: { data: string[] } = { data: [] };

// Act
Expand Down Expand Up @@ -168,7 +168,7 @@ describe('commands (integration)', () => {
(seed, shrinkPath, biasFactor) => {
// Generate the first Value
const it = shrinkPath[Symbol.iterator]();
const mrng = new Random(prand.xorshift128plus(seed));
const mrng = new Random(xorshift128plus(seed));
let currentValue: Value<[number, Iterable<Cmd>, number]> | null = manyArbsIncludingCommandsOne.generate(
mrng,
biasFactor,
Expand Down Expand Up @@ -206,7 +206,7 @@ describe('commands (integration)', () => {
(seed, shrinkPath, biasFactor) => {
// Generate the first Value
const it = shrinkPath[Symbol.iterator]();
const mrng = new Random(prand.xorshift128plus(seed));
const mrng = new Random(xorshift128plus(seed));
let currentValue: Value<Iterable<Cmd>> | null = commandsArb.generate(mrng, biasFactor);

// Run all commands of first Value
Expand Down Expand Up @@ -245,7 +245,7 @@ describe('commands (integration)', () => {
const logOnCheck: { data: string[] } = { data: [] };

// generate scenario and simulate execution
const rng = prand.xorshift128plus(seed);
const rng = xorshift128plus(seed);
const refArbitrary = commands([
new FakeConstant(new SuccessCommand(logOnCheck)),
new FakeConstant(new SkippedCommand(logOnCheck)),
Expand Down
4 changes: 2 additions & 2 deletions packages/fast-check/test/unit/check/runner/Tosser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import type { Random } from '../../../../src/random/generator/Random';
import { Value } from '../../../../src/check/arbitrary/definition/Value';

import * as stubArb from '../../stubs/arbitraries';
import prand from 'pure-rand';
import { xorshift128plus } from 'pure-rand/generator/XorShift';
import type { QualifiedRandomGenerator } from '../../../../src/check/runner/configuration/QualifiedParameters';

const rngProducer = prand.xorshift128plus as (seed: number) => QualifiedRandomGenerator;
const rngProducer = xorshift128plus as (seed: number) => QualifiedRandomGenerator;

const wrap = <T>(arb: Arbitrary<T>): IRawProperty<T> =>
new (class implements IRawProperty<T> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { describe, it, expect } from 'vitest';
import * as fc from 'fast-check';
import * as prand from 'pure-rand';
import { mersenne } from 'pure-rand/generator/MersenneTwister';
import { congruential32 } from 'pure-rand/generator/LinearCongruential';
import { xoroshiro128plus } from 'pure-rand/generator/XoroShiro';
import { xorshift128plus } from 'pure-rand/generator/XorShift';

import { QualifiedParameters } from '../../../../../src/check/runner/configuration/QualifiedParameters';
import type { RandomType } from '../../../../../src/check/runner/configuration/RandomType';
import { VerbosityLevel } from '../../../../../src/check/runner/configuration/VerbosityLevel';

const prand = { mersenne, congruential32, xorshift128plus, xoroshiro128plus };
const parametersArbitrary = fc.record(
{
seed: fc.integer(),
randomType: fc.constantFrom(prand.mersenne, prand.congruential32, prand.xorshift128plus, prand.xoroshiro128plus),
randomType: fc.constantFrom(mersenne, congruential32, xorshift128plus, xoroshiro128plus),
numRuns: fc.nat(),
maxSkipsPerRun: fc.nat(),
timeout: fc.nat(),
Expand Down Expand Up @@ -70,7 +74,7 @@ describe('QualifiedParameters', () => {
(params, randomType) => {
const qparams = QualifiedParameters.read({ ...params, randomType });
const resolvedRandomType = randomType === 'congruential' ? 'congruential32' : randomType;
const defaultRandomType = prand.xorshift128plus;
const defaultRandomType = xorshift128plus;
if (resolvedRandomType === undefined) {
expect(qparams.randomType).toBe(defaultRandomType);
} else if (resolvedRandomType === 'congruential32' || resolvedRandomType === 'mersenne') {
Expand Down
14 changes: 7 additions & 7 deletions packages/fast-check/test/unit/random/generator/Random.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it } from 'vitest';
import * as prand from 'pure-rand';
import { xorshift128plus } from 'pure-rand/generator/XorShift';
import * as fc from 'fast-check';

import { Random } from '../../../../src/random/generator/Random';
Expand All @@ -10,7 +10,7 @@ describe('Random', () => {
it('Should produce values within 0 and 2 ** n - 1', () =>
fc.assert(
fc.property(fc.integer(), fc.nat(31), fc.nat(MAX_SIZE), (seed, n, num) => {
const mrng = new Random(prand.xorshift128plus(seed));
const mrng = new Random(xorshift128plus(seed));
for (let idx = 0; idx !== num; ++idx) {
const v = mrng.next(n);
if (v < 0 || v > (((1 << n) - 1) | 0)) return false;
Expand All @@ -23,7 +23,7 @@ describe('Random', () => {
it('Should produce values within the range', () =>
fc.assert(
fc.property(fc.integer(), fc.integer(), fc.integer(), fc.nat(MAX_SIZE), (seed, a, b, num) => {
const mrng = new Random(prand.xorshift128plus(seed));
const mrng = new Random(xorshift128plus(seed));
const min = a < b ? a : b;
const max = a < b ? b : a;
for (let idx = 0; idx !== num; ++idx) {
Expand All @@ -36,8 +36,8 @@ describe('Random', () => {
it('Should produce the same sequences given same seeds', () =>
fc.assert(
fc.property(fc.integer(), fc.nat(MAX_SIZE), (seed, num) => {
const mrng1 = new Random(prand.xorshift128plus(seed));
const mrng2 = new Random(prand.xorshift128plus(seed));
const mrng1 = new Random(xorshift128plus(seed));
const mrng2 = new Random(xorshift128plus(seed));
for (let idx = 0; idx !== num; ++idx) if (mrng1.nextInt() !== mrng2.nextInt()) return false;
return true;
}),
Expand All @@ -47,7 +47,7 @@ describe('Random', () => {
it('Should produce values within 0 and 1', () =>
fc.assert(
fc.property(fc.integer(), fc.nat(MAX_SIZE), (seed, num) => {
const mrng = new Random(prand.xorshift128plus(seed));
const mrng = new Random(xorshift128plus(seed));
for (let idx = 0; idx !== num; ++idx) {
const v = mrng.nextDouble();
if (v < 0 || v >= 1) return false;
Expand All @@ -60,7 +60,7 @@ describe('Random', () => {
it('Should produce the same sequences', () =>
fc.assert(
fc.property(fc.integer(), fc.nat(MAX_SIZE), (seed, num) => {
const mrng1 = new Random(prand.xorshift128plus(seed));
const mrng1 = new Random(xorshift128plus(seed));
const mrng2 = mrng1.clone();
for (let idx = 0; idx !== num; ++idx) if (mrng1.nextInt() !== mrng2.nextInt()) return false;
return true;
Expand Down
Loading
Loading