fast-check
Version:
Property based testing framework for JavaScript (like QuickCheck)
57 lines (56 loc) • 2.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.bigUint = exports.bigInt = exports.bigUintN = exports.bigIntN = void 0;
const ArbitraryWithShrink_1 = require("./definition/ArbitraryWithShrink");
const BiasedArbitraryWrapper_1 = require("./definition/BiasedArbitraryWrapper");
const Shrinkable_1 = require("./definition/Shrinkable");
const BiasNumeric_1 = require("./helpers/BiasNumeric");
const ShrinkNumeric_1 = require("./helpers/ShrinkNumeric");
class BigIntArbitrary extends ArbitraryWithShrink_1.ArbitraryWithShrink {
constructor(min, max) {
super();
this.min = min;
this.max = max;
this.biasedBigIntArbitrary = null;
}
wrapper(value, shrunkOnce) {
return new Shrinkable_1.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 ShrinkNumeric_1.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_1.biasNumeric(this.min, this.max, BigIntArbitrary, logLike);
return this.biasedBigIntArbitrary;
}
withBias(freq) {
return BiasedArbitraryWrapper_1.biasWrapper(freq, this, (originalArbitrary) => originalArbitrary.pureBiasedArbitrary());
}
}
function bigIntN(n) {
return new BigIntArbitrary(BigInt(-1) << BigInt(n - 1), (BigInt(1) << BigInt(n - 1)) - BigInt(1));
}
exports.bigIntN = bigIntN;
function bigUintN(n) {
return new BigIntArbitrary(BigInt(0), (BigInt(1) << BigInt(n)) - BigInt(1));
}
exports.bigUintN = bigUintN;
function bigInt(min, max) {
return max === undefined ? bigIntN(256) : new BigIntArbitrary(min, max);
}
exports.bigInt = bigInt;
function bigUint(max) {
return max === undefined ? bigUintN(256) : new BigIntArbitrary(BigInt(0), max);
}
exports.bigUint = bigUint;