UNPKG

fast-check

Version:

Property based testing framework for JavaScript (like QuickCheck)

68 lines (67 loc) 2.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.compareBooleanFunc = exports.compareFunc = exports.func = void 0; const hash_1 = require("../../utils/hash"); const stringify_1 = require("../../utils/stringify"); const symbols_1 = require("../symbols"); const ArrayArbitrary_1 = require("./ArrayArbitrary"); const IntegerArbitrary_1 = require("./IntegerArbitrary"); const TupleArbitrary_1 = require("./TupleArbitrary"); function func(arb) { return TupleArbitrary_1.tuple(ArrayArbitrary_1.array(arb, 1, 10), IntegerArbitrary_1.integer().noShrink()).map(([outs, seed]) => { const producer = () => { const recorded = {}; const f = (...args) => { const repr = stringify_1.stringify(args); const val = outs[hash_1.hash(`${seed}${repr}`) % outs.length]; recorded[repr] = val; return symbols_1.hasCloneMethod(val) ? val[symbols_1.cloneMethod]() : val; }; return Object.assign(f, { toString: () => '<function :: ' + Object.keys(recorded) .sort() .map((k) => `${k} => ${stringify_1.stringify(recorded[k])}`) .join(', ') + '>', [symbols_1.cloneMethod]: producer, }); }; return producer(); }); } exports.func = func; function compareFuncImplem(cmp) { return TupleArbitrary_1.tuple(IntegerArbitrary_1.integer().noShrink(), IntegerArbitrary_1.integer(1, 0xffffffff).noShrink()).map(([seed, hashEnvSize]) => { const producer = () => { const recorded = {}; const f = (a, b) => { const reprA = stringify_1.stringify(a); const reprB = stringify_1.stringify(b); const hA = hash_1.hash(`${seed}${reprA}`) % hashEnvSize; const hB = hash_1.hash(`${seed}${reprB}`) % hashEnvSize; const val = cmp(hA, hB); recorded[`[${reprA},${reprB}]`] = val; return val; }; return Object.assign(f, { toString: () => '<function :: ' + Object.keys(recorded) .sort() .map((k) => `${k} => ${recorded[k]}`) .join(', ') + '>', [symbols_1.cloneMethod]: producer, }); }; return producer(); }); } function compareFunc() { return compareFuncImplem((hA, hB) => hA - hB); } exports.compareFunc = compareFunc; function compareBooleanFunc() { return compareFuncImplem((hA, hB) => hA < hB); } exports.compareBooleanFunc = compareBooleanFunc;