UNPKG

fast-check

Version:

Property based testing framework for JavaScript (like QuickCheck)

41 lines (40 loc) 1.58 kB
import { stream } from '../../../stream/Stream.js'; 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(shrinkDecr()) : 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); } export function shrinkNumber(min, max, current, shrunkOnce) { return shrinkNumeric(0, min, max, current, shrunkOnce, halvePosNumber, halveNegNumber); } export function shrinkBigInt(min, max, current, shrunkOnce) { return shrinkNumeric(BigInt(0), min, max, current, shrunkOnce, halveBigInt, halveBigInt); }