UNPKG

fast-check

Version:

Property based testing framework for JavaScript (like QuickCheck)

51 lines (50 loc) 1.94 kB
import { ArbitraryWithShrink } from './definition/ArbitraryWithShrink.js'; import { biasWrapper } from './definition/BiasedArbitraryWrapper.js'; import { Shrinkable } from './definition/Shrinkable.js'; import { biasNumeric } from './helpers/BiasNumeric.js'; import { shrinkBigInt } from './helpers/ShrinkNumeric.js'; class BigIntArbitrary extends ArbitraryWithShrink { constructor(min, max) { super(); this.min = min; this.max = max; this.biasedBigIntArbitrary = null; } wrapper(value, shrunkOnce) { return new Shrinkable(value, () => this.shrink(value, shrunkOnce).map((v) => this.wrapper(v, true))); } generate(mrng) { return this.wrapper(mrng.nextBigInt(this.min, this.max), false); } shrink(value, shrunkOnce) { return shrinkBigInt(this.min, this.max, value, shrunkOnce === true); } pureBiasedArbitrary() { if (this.biasedBigIntArbitrary != null) { return this.biasedBigIntArbitrary; } const logLike = (v) => { if (v === BigInt(0)) return BigInt(0); return BigInt(v.toString().length); }; this.biasedBigIntArbitrary = biasNumeric(this.min, this.max, BigIntArbitrary, logLike); return this.biasedBigIntArbitrary; } withBias(freq) { return biasWrapper(freq, this, (originalArbitrary) => originalArbitrary.pureBiasedArbitrary()); } } function bigIntN(n) { return new BigIntArbitrary(BigInt(-1) << BigInt(n - 1), (BigInt(1) << BigInt(n - 1)) - BigInt(1)); } function bigUintN(n) { return new BigIntArbitrary(BigInt(0), (BigInt(1) << BigInt(n)) - BigInt(1)); } function bigInt(min, max) { return max === undefined ? bigIntN(256) : new BigIntArbitrary(min, max); } function bigUint(max) { return max === undefined ? bigUintN(256) : new BigIntArbitrary(BigInt(0), max); } export { bigIntN, bigUintN, bigInt, bigUint };