UNPKG

fast-check

Version:

Property based testing framework for JavaScript (like QuickCheck)

46 lines (45 loc) 1.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.shrinkBigInt = exports.shrinkNumber = void 0; const Stream_1 = require("../../../stream/Stream"); function shrinkNumericInternal(current, target, shrunkOnce, halvePos, halveNeg) { const realGap = (current - target); function* shrinkDecr() { const gap = shrunkOnce ? halvePos(realGap) : realGap; for (let toremove = gap; toremove > 0; toremove = halvePos(toremove)) { yield (current - toremove); } } function* shrinkIncr() { const gap = shrunkOnce ? halveNeg(realGap) : realGap; for (let toremove = gap; toremove < 0; toremove = halveNeg(toremove)) { yield (current - toremove); } } return realGap > 0 ? Stream_1.stream(shrinkDecr()) : Stream_1.stream(shrinkIncr()); } function halveBigInt(n) { return n / BigInt(2); } function halvePosNumber(n) { return Math.floor(n / 2); } function halveNegNumber(n) { return Math.ceil(n / 2); } function shrinkNumeric(zero, min, max, current, shrunkOnce, halvePos, halveNeg) { if (min <= zero && max >= zero) { return shrinkNumericInternal(current, zero, shrunkOnce, halvePos, halveNeg); } return current < zero ? shrinkNumericInternal(current, max, shrunkOnce, halvePos, halveNeg) : shrinkNumericInternal(current, min, shrunkOnce, halvePos, halveNeg); } function shrinkNumber(min, max, current, shrunkOnce) { return shrinkNumeric(0, min, max, current, shrunkOnce, halvePosNumber, halveNegNumber); } exports.shrinkNumber = shrinkNumber; function shrinkBigInt(min, max, current, shrunkOnce) { return shrinkNumeric(BigInt(0), min, max, current, shrunkOnce, halveBigInt, halveBigInt); } exports.shrinkBigInt = shrinkBigInt;