How to use arbNegate method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

BigIntArbitrary.spec.ts

Source:BigIntArbitrary.spec.ts Github

copy

Full Screen

1import * as fc from 'fast-check';2import { BigIntArbitrary } from '../../../../src/arbitrary/_internals/BigIntArbitrary';3import { Value } from '../../../../src/check/arbitrary/definition/Value';4import { fakeRandom } from '../__test-helpers__/RandomHelpers';5import {6 assertProduceValuesShrinkableWithoutContext,7 assertProduceCorrectValues,8 assertShrinkProducesSameValueWithoutInitialContext,9 assertShrinkProducesStrictlySmallerValue,10 assertProduceSameValueGivenSameSeed,11} from '../__test-helpers__/ArbitraryAssertions';12import { buildShrinkTree, renderTree, walkTree } from '../__test-helpers__/ShrinkTree';13import { Stream } from '../../../../src/stream/Stream';14import * as BiasNumericRangeMock from '../../../../src/arbitrary/_internals/helpers/BiasNumericRange';15import * as ShrinkBigIntMock from '../../../../src/arbitrary/_internals/helpers/ShrinkBigInt';16function beforeEachHook() {17 jest.resetModules();18 jest.restoreAllMocks();19 fc.configureGlobal({ beforeEach: beforeEachHook });20}21beforeEach(beforeEachHook);22describe('BigIntArbitrary', () => {23 if (typeof BigInt === 'undefined') {24 it('no test', () => {25 expect(true).toBe(true);26 });27 return;28 }29 describe('generate', () => {30 it('should never bias and generate the full range when biasFactor is not specified', () =>31 fc.assert(32 fc.property(fc.bigInt(), fc.bigInt(), fc.bigInt(), (a, b, c) => {33 // Arrange34 const [min, mid, max] = [a, b, c].sort((v1, v2) => Number(v1 - v2));35 const { instance: mrng, nextBigInt } = fakeRandom();36 nextBigInt.mockReturnValueOnce(mid);37 // Act38 const arb = new BigIntArbitrary(min, max);39 const out = arb.generate(mrng, undefined);40 // Assert41 expect(out.value).toBe(mid);42 expect(nextBigInt).toHaveBeenCalledTimes(1);43 expect(nextBigInt).toHaveBeenCalledWith(min, max);44 })45 ));46 it('should not always bias values (expect 1 times over biasFreq) and still generate full range when unbiased', () =>47 fc.assert(48 fc.property(fc.bigInt(), fc.bigInt(), fc.bigInt(), fc.maxSafeInteger(), (a, b, c, biasFactor) => {49 // Arrange50 const [min, mid, max] = [a, b, c].sort((v1, v2) => Number(v1 - v2));51 const { instance: mrng, nextInt, nextBigInt } = fakeRandom();52 nextInt.mockReturnValueOnce(2); // 1 means bias, all others are unbiased53 nextBigInt.mockReturnValueOnce(mid);54 // Act55 const arb = new BigIntArbitrary(min, max);56 const out = arb.generate(mrng, biasFactor);57 // Assert58 expect(out.value).toBe(mid);59 expect(nextInt).toHaveBeenCalledTimes(1);60 expect(nextInt).toHaveBeenCalledWith(1, biasFactor);61 expect(nextBigInt).toHaveBeenCalledTimes(1);62 expect(nextBigInt).toHaveBeenCalledWith(min, max);63 })64 ));65 it('should bias values (1 times over biasFreq) by using one of the ranges from biasNumericRange', () =>66 fc.assert(67 fc.property(68 fc.bigInt(),69 fc.bigInt(),70 fc.bigInt(),71 fc.maxSafeInteger(),72 fc.maxSafeInteger(),73 // Remark:74 // Following biasNumericRange is not fully identical to the onces that would be provided.75 // Range (in this stub) can be larger than the requested one. Not impacting from a unit-test point of view.76 fc.array(77 fc.tuple(fc.bigInt(), fc.bigInt()).map(([a, b]) => (a < b ? { min: a, max: b } : { min: b, max: a })),78 { minLength: 1 }79 ),80 (a, b, c, biasFactor, mod, ranges) => {81 // Arrange82 const [min, mid, max] = [a, b, c].sort((v1, v2) => Number(v1 - v2));83 const { instance: mrng, nextInt, nextBigInt } = fakeRandom();84 nextInt.mockReturnValueOnce(1); // 1 means bias85 if (ranges.length !== 1) {86 nextInt.mockImplementationOnce((min, max) => min + (mod % (max - min + 1)));87 }88 nextBigInt.mockReturnValueOnce(mid); // Remark: this value will most of the time be outside of requested range89 const biasNumericRange = jest.spyOn(90 BiasNumericRangeMock,91 'biasNumericRange'92 ) as unknown as jest.SpyInstance<{ min: bigint; max: bigint }[], [bigint, bigint, () => bigint]>;93 biasNumericRange.mockReturnValueOnce(ranges);94 // Act95 const arb = new BigIntArbitrary(min, max);96 const out = arb.generate(mrng, biasFactor);97 // Assert98 expect(out.value).toBe(mid);99 expect(biasNumericRange).toHaveBeenCalledTimes(1);100 expect(biasNumericRange).toHaveBeenCalledWith(min, max, expect.any(Function));101 if (ranges.length === 1) {102 expect(nextInt).toHaveBeenCalledTimes(1);103 expect(nextInt).toHaveBeenCalledWith(1, biasFactor);104 expect(nextBigInt).toHaveBeenCalledTimes(1);105 expect(nextBigInt).toHaveBeenCalledWith(ranges[0].min, ranges[0].max);106 } else {107 expect(nextInt).toHaveBeenCalledTimes(2);108 expect(nextInt).toHaveBeenCalledWith(1, biasFactor);109 const secondNextIntParams = nextInt.mock.calls[1];110 expect(secondNextIntParams[0]).toBeLessThan(0); // arbitrary is supposed to prefer the first entry111 expect(secondNextIntParams[1]).toBe(ranges.length - 2); // other entries do not have any special treatments112 // negative values for [0], positive value n means ranges[n+1]113 const secondNextIntResult = nextInt.mock.results[1].value;114 const selectedRange = secondNextIntResult < 0 ? 0 : secondNextIntResult + 1;115 expect(nextBigInt).toHaveBeenCalledWith(ranges[selectedRange].min, ranges[selectedRange].max);116 }117 }118 )119 ));120 });121 describe('canShrinkWithoutContext', () => {122 it('should always tells it can generate values included in the requested range', () =>123 fc.assert(124 fc.property(fc.bigInt(), fc.bigInt(), fc.bigInt(), (a, b, c) => {125 // Arrange126 const [min, mid, max] = [a, b, c].sort((v1, v2) => Number(v1 - v2));127 // Act128 const arb = new BigIntArbitrary(min, max);129 const out = arb.canShrinkWithoutContext(mid);130 // Assert131 expect(out).toBe(true);132 })133 ));134 it('should always reject values outside of the requested range', () =>135 fc.assert(136 fc.property(137 fc.bigInt(),138 fc.bigInt(),139 fc.bigInt(),140 fc.constantFrom(...(['lower', 'higher'] as const)),141 (a, b, c, position) => {142 // Arrange143 const s = [a, b, c].sort((v1, v2) => Number(v1 - v2));144 const [min, max, requested] = position === 'lower' ? [s[1], s[2], s[0]] : [s[0], s[1], s[2]];145 fc.pre(requested !== min && requested !== max);146 // Act147 const arb = new BigIntArbitrary(min, max);148 const out = arb.canShrinkWithoutContext(requested);149 // Assert150 expect(out).toBe(false);151 }152 )153 ));154 it.each`155 requested156 ${'1'}157 ${1}158 ${'1n'}159 ${1.5}160 ${-0}161 ${Number.NaN}162 ${''}163 ${{}}164 `('should always reject non bigint values like $requested', ({ requested }) => {165 // Arrange166 const min = BigInt(0);167 const max = BigInt(100);168 // Act169 const arb = new BigIntArbitrary(min, max);170 const out = arb.canShrinkWithoutContext(requested);171 // Assert172 expect(out).toBe(false);173 });174 });175 describe('shrink', () => {176 it('should always call shrink helper when no context provided', () =>177 fc.assert(178 fc.property(fc.bigInt(), fc.bigInt(), fc.bigInt(), (a, b, c) => {179 // Arrange180 const [min, mid, max] = [a, b, c].sort((v1, v2) => Number(v1 - v2));181 const expectedShrinks = Stream.nil<Value<bigint>>();182 const shrinkBigInt = jest.spyOn(ShrinkBigIntMock, 'shrinkBigInt');183 shrinkBigInt.mockReturnValueOnce(expectedShrinks);184 // Act185 const arb = new BigIntArbitrary(min, max);186 const shrinks = arb.shrink(mid, undefined);187 // Assert188 expect(shrinks).toBe(expectedShrinks);189 expect(shrinkBigInt).toHaveBeenCalledTimes(1);190 expect(shrinkBigInt).toHaveBeenCalledWith(mid, expect.any(BigInt), true);191 })192 ));193 });194});195describe('BigIntArbitrary (integration)', () => {196 if (typeof BigInt === 'undefined') {197 it('no test', () => {198 expect(true).toBe(true);199 });200 return;201 }202 type Extra = { min: bigint; max: bigint };203 const extraParameters: fc.Arbitrary<Extra> = fc204 .tuple(fc.bigInt(), fc.bigInt())205 .map(([a, b]) => (a < b ? { min: a, max: b } : { min: b, max: a }));206 const isCorrect = (value: bigint, extra: Extra) =>207 typeof value === 'bigint' && extra.min <= value && value <= extra.max;208 const isStrictlySmaller = (v1: bigint, v2: bigint) => {209 const absV1 = v1 < BigInt(0) ? -v1 : v1;210 const absV2 = v2 < BigInt(0) ? -v2 : v2;211 return absV1 < absV2;212 };213 const bigIntBuilder = (extra: Extra) => new BigIntArbitrary(extra.min, extra.max);214 it('should produce the same values given the same seed', () => {215 assertProduceSameValueGivenSameSeed(bigIntBuilder, { extraParameters });216 });217 it('should only produce correct values', () => {218 assertProduceCorrectValues(bigIntBuilder, isCorrect, { extraParameters });219 });220 it('should produce values seen as shrinkable without any context', () => {221 assertProduceValuesShrinkableWithoutContext(bigIntBuilder, { extraParameters });222 });223 it('should be able to shrink to the same values without initial context', () => {224 assertShrinkProducesSameValueWithoutInitialContext(bigIntBuilder, { extraParameters });225 });226 it('should shrink towards strictly smaller values', () => {227 assertShrinkProducesStrictlySmallerValue(bigIntBuilder, isStrictlySmaller, { extraParameters });228 });229 describe('shrink', () => {230 it('should build a mirrored version of the shrinking tree if we negate all the values', () =>231 fc.assert(232 fc.property(233 fc.bigInt(),234 fc.bigUint({ max: BigInt(20) }), // larger trees might be too wide235 fc.bigUint({ max: BigInt(20) }),236 (start, o1, o2) => {237 // Arrange238 const min = start;239 const [mid, max] = o1 < o2 ? [min + o1, min + o2] : [min + o2, min + o1];240 const arb = new BigIntArbitrary(min, max);241 const arbNegate = new BigIntArbitrary(-max, -min);242 // Act243 const source = new Value(mid, undefined);244 const sourceNegate = new Value(-mid, undefined);245 const tree = buildShrinkTree(arb, source);246 const treeNegate = buildShrinkTree(arbNegate, sourceNegate);247 const flat: bigint[] = [];248 const flatNegate: bigint[] = [];249 walkTree(tree, (v) => flat.push(v));250 walkTree(treeNegate, (v) => flatNegate.push(-v));251 // Assert252 expect(arb.canShrinkWithoutContext(source.value)).toBe(true);253 expect(arbNegate.canShrinkWithoutContext(sourceNegate.value)).toBe(true);254 expect(flatNegate).toEqual(flat);255 }256 )257 ));258 it('should build an offset version of the shrinking tree if we offset all the values (keep every value >=0)', () =>259 fc.assert(260 fc.property(261 fc.bigUint(),262 fc.bigUint({ max: BigInt(20) }), // larger trees might be too wide263 fc.bigUint({ max: BigInt(20) }),264 fc.bigUint(),265 (start, o1, o2, offset) => {266 // Arrange267 fc.pre(start + o1 + offset <= Number.MAX_SAFE_INTEGER);268 fc.pre(start + o2 + offset <= Number.MAX_SAFE_INTEGER);269 const min = start;270 const [mid, max] = o1 < o2 ? [min + o1, min + o2] : [min + o2, min + o1];271 const arb = new BigIntArbitrary(min, max);272 const arbOffset = new BigIntArbitrary(min + offset, max + offset);273 // Act274 const source = new Value(mid, undefined);275 const sourceOffset = new Value(mid + offset, undefined);276 const tree = buildShrinkTree(arb, source);277 const treeOffset = buildShrinkTree(arbOffset, sourceOffset);278 const flat: bigint[] = [];279 const flatOffset: bigint[] = [];280 walkTree(tree, (v) => flat.push(v));281 walkTree(treeOffset, (v) => flatOffset.push(v - offset));282 // Assert283 expect(arb.canShrinkWithoutContext(source.value)).toBe(true);284 expect(arbOffset.canShrinkWithoutContext(sourceOffset.value)).toBe(true);285 expect(flatOffset).toEqual(flat);286 }287 )288 ));289 it('should shrink strictly positive value for positive range including zero', () => {290 // Arrange291 const arb = new BigIntArbitrary(BigInt(0), BigInt(10));292 const source = new Value(BigInt(8), undefined);293 // Act294 const tree = buildShrinkTree(arb, source);295 const renderedTree = renderTree(tree).join('\n');296 // Assert297 expect(arb.canShrinkWithoutContext(source.value)).toBe(true);298 // When there is no more option, the shrinker retry one time with the value299 // current-1 to check if something that changed outside (another value not itself)300 // may have changed the situation.301 expect(renderedTree).toMatchInlineSnapshot(`302 "8n303 ├> 0n304 ├> 4n305 | ├> 2n306 | | └> 1n307 | | └> 0n308 | └> 3n309 | └> 2n310 | ├> 0n311 | └> 1n312 | └> 0n313 ├> 6n314 | └> 5n315 | └> 4n316 | ├> 0n317 | ├> 2n318 | | └> 1n319 | | └> 0n320 | └> 3n321 | └> 2n322 | ├> 0n323 | └> 1n324 | └> 0n325 └> 7n326 └> 6n327 ├> 0n328 ├> 3n329 | └> 2n330 | └> 1n331 | └> 0n332 └> 5n333 └> 4n334 └> 3n335 ├> 0n336 └> 2n337 └> 1n338 └> 0n"339 `);340 // Remarks:341 // * When we shrink 5 in path 8 > 6 > 5342 // we already now that 4 passed so we now that the smallest failing case343 // to look for is >= 5344 // * Same thing when we shrink 6 in path 8 > 6345 // * When we shrink 7 in path 8 > 7346 // we already now that 6 passed so we now that the smallest failing case347 // to look for is >= 7348 });349 it('should shrink strictly positive value for range not including zero', () => {350 // Arrange351 const arb = new BigIntArbitrary(BigInt(10), BigInt(20));352 const source = new Value(BigInt(18), undefined);353 // Act354 const tree = buildShrinkTree(arb, source);355 const renderedTree = renderTree(tree).join('\n');356 // Assert357 expect(arb.canShrinkWithoutContext(source.value)).toBe(true);358 // As the range [10, 20] and the value 18359 // are just offset by +10 compared to the first case,360 // the rendered tree will be offset by 10 too361 expect(renderedTree).toMatchInlineSnapshot(`362 "18n363 ├> 10n364 ├> 14n365 | ├> 12n366 | | └> 11n367 | | └> 10n368 | └> 13n369 | └> 12n370 | ├> 10n371 | └> 11n372 | └> 10n373 ├> 16n374 | └> 15n375 | └> 14n376 | ├> 10n377 | ├> 12n378 | | └> 11n379 | | └> 10n380 | └> 13n381 | └> 12n382 | ├> 10n383 | └> 11n384 | └> 10n385 └> 17n386 └> 16n387 ├> 10n388 ├> 13n389 | └> 12n390 | └> 11n391 | └> 10n392 └> 15n393 └> 14n394 └> 13n395 ├> 10n396 └> 12n397 └> 11n398 └> 10n"399 `);400 });401 it('should shrink strictly negative value for negative range including zero', () => {402 // Arrange403 const arb = new BigIntArbitrary(BigInt(-10), BigInt(0));404 const source = new Value(BigInt(-8), undefined);405 // Act406 const tree = buildShrinkTree(arb, source);407 const renderedTree = renderTree(tree).join('\n');408 // Assert409 expect(arb.canShrinkWithoutContext(source.value)).toBe(true);410 // As the range [-10, 0] and the value -8411 // are the opposite of first case, the rendered tree will be the same except412 // it contains opposite values413 expect(renderedTree).toMatchInlineSnapshot(`414 "-8n415 ├> 0n416 ├> -4n417 | ├> -2n418 | | └> -1n419 | | └> 0n420 | └> -3n421 | └> -2n422 | ├> 0n423 | └> -1n424 | └> 0n425 ├> -6n426 | └> -5n427 | └> -4n428 | ├> 0n429 | ├> -2n430 | | └> -1n431 | | └> 0n432 | └> -3n433 | └> -2n434 | ├> 0n435 | └> -1n436 | └> 0n437 └> -7n438 └> -6n439 ├> 0n440 ├> -3n441 | └> -2n442 | └> -1n443 | └> 0n444 └> -5n445 └> -4n446 └> -3n447 ├> 0n448 └> -2n449 └> -1n450 └> 0n"451 `);452 });453 });...

Full Screen

Full Screen

IntegerArbitrary.spec.ts

Source:IntegerArbitrary.spec.ts Github

copy

Full Screen

1import * as fc from 'fast-check';2import { IntegerArbitrary } from '../../../../src/arbitrary/_internals/IntegerArbitrary';3import { Value } from '../../../../src/check/arbitrary/definition/Value';4import { fakeRandom } from '../__test-helpers__/RandomHelpers';5import {6 assertProduceValuesShrinkableWithoutContext,7 assertProduceCorrectValues,8 assertShrinkProducesSameValueWithoutInitialContext,9 assertShrinkProducesStrictlySmallerValue,10 assertProduceSameValueGivenSameSeed,11} from '../__test-helpers__/ArbitraryAssertions';12import { buildShrinkTree, renderTree, walkTree } from '../__test-helpers__/ShrinkTree';13import * as BiasNumericRangeMock from '../../../../src/arbitrary/_internals/helpers/BiasNumericRange';14import * as ShrinkIntegerMock from '../../../../src/arbitrary/_internals/helpers/ShrinkInteger';15import { Stream } from '../../../../src/stream/Stream';16function beforeEachHook() {17 jest.resetModules();18 jest.restoreAllMocks();19 fc.configureGlobal({ beforeEach: beforeEachHook });20}21beforeEach(beforeEachHook);22describe('IntegerArbitrary', () => {23 describe('generate', () => {24 it('should never bias and generate the full range when biasFactor is not specified', () =>25 fc.assert(26 fc.property(fc.maxSafeInteger(), fc.maxSafeInteger(), fc.maxSafeInteger(), (a, b, c) => {27 // Arrange28 const [min, mid, max] = [a, b, c].sort((v1, v2) => v1 - v2);29 const { instance: mrng, nextInt } = fakeRandom();30 nextInt.mockReturnValueOnce(mid);31 // Act32 const arb = new IntegerArbitrary(min, max);33 const out = arb.generate(mrng, undefined);34 // Assert35 expect(out.value).toBe(mid);36 expect(nextInt).toHaveBeenCalledTimes(1);37 expect(nextInt).toHaveBeenCalledWith(min, max);38 })39 ));40 it('should not always bias values (expect 1 times over biasFreq) and still generate full range when unbiased', () =>41 fc.assert(42 fc.property(43 fc.maxSafeInteger(),44 fc.maxSafeInteger(),45 fc.maxSafeInteger(),46 fc.maxSafeInteger(),47 (a, b, c, biasFactor) => {48 // Arrange49 const [min, mid, max] = [a, b, c].sort((v1, v2) => v1 - v2);50 const { instance: mrng, nextInt } = fakeRandom();51 nextInt.mockReturnValueOnce(2); // 1 means bias, all others are unbiased52 nextInt.mockReturnValueOnce(mid);53 // Act54 const arb = new IntegerArbitrary(min, max);55 const out = arb.generate(mrng, biasFactor);56 // Assert57 expect(out.value).toBe(mid);58 expect(nextInt).toHaveBeenCalledTimes(2);59 expect(nextInt).toHaveBeenCalledWith(1, biasFactor);60 expect(nextInt).toHaveBeenCalledWith(min, max);61 }62 )63 ));64 it('should bias values (1 times over biasFreq) by using one of the ranges from biasNumericRange', () =>65 fc.assert(66 fc.property(67 fc.maxSafeInteger(),68 fc.maxSafeInteger(),69 fc.maxSafeInteger(),70 fc.maxSafeInteger(),71 fc.maxSafeInteger(),72 // Remark:73 // Following biasNumericRange is not fully identical to the onces that would be provided.74 // Range (in this stub) can be larger than the requested one. Not impacting from a unit-test point of view.75 fc.array(76 fc77 .tuple(fc.maxSafeInteger(), fc.maxSafeInteger())78 .map(([a, b]) => (a < b ? { min: a, max: b } : { min: b, max: a })),79 { minLength: 1 }80 ),81 (a, b, c, biasFactor, mod, ranges) => {82 // Arrange83 const [min, mid, max] = [a, b, c].sort((v1, v2) => v1 - v2);84 const { instance: mrng, nextInt } = fakeRandom();85 nextInt.mockReturnValueOnce(1); // 1 means bias86 if (ranges.length !== 1) {87 nextInt.mockImplementationOnce((min, max) => min + (mod % (max - min + 1)));88 }89 nextInt.mockReturnValueOnce(mid); // Remark: this value will most of the time be outside of requested range90 const biasNumericRange = jest.spyOn(91 BiasNumericRangeMock,92 'biasNumericRange'93 ) as unknown as jest.SpyInstance<{ min: number; max: number }[], [number, number, () => number]>;94 biasNumericRange.mockReturnValueOnce(ranges);95 // Act96 const arb = new IntegerArbitrary(min, max);97 const out = arb.generate(mrng, biasFactor);98 // Assert99 expect(out.value).toBe(mid);100 expect(biasNumericRange).toHaveBeenCalledTimes(1);101 expect(biasNumericRange).toHaveBeenCalledWith(min, max, expect.any(Function));102 if (ranges.length === 1) {103 expect(nextInt).toHaveBeenCalledTimes(2);104 expect(nextInt).toHaveBeenCalledWith(1, biasFactor);105 expect(nextInt).toHaveBeenCalledWith(ranges[0].min, ranges[0].max);106 } else {107 expect(nextInt).toHaveBeenCalledTimes(3);108 expect(nextInt).toHaveBeenCalledWith(1, biasFactor);109 const secondNextIntParams = nextInt.mock.calls[1];110 expect(secondNextIntParams[0]).toBeLessThan(0); // arbitrary is supposed to prefer the first entry111 expect(secondNextIntParams[1]).toBe(ranges.length - 2); // other entries do not have any special treatments112 // negative values for [0], positive value n means ranges[n+1]113 const secondNextIntResult = nextInt.mock.results[1].value;114 const selectedRange = secondNextIntResult < 0 ? 0 : secondNextIntResult + 1;115 expect(nextInt).toHaveBeenCalledWith(ranges[selectedRange].min, ranges[selectedRange].max);116 }117 }118 )119 ));120 });121 describe('canShrinkWithoutContext', () => {122 it('should always tells it can generate values included in the requested range', () =>123 fc.assert(124 fc.property(fc.maxSafeInteger(), fc.maxSafeInteger(), fc.maxSafeInteger(), (a, b, c) => {125 // Arrange126 const [min, mid, max] = [a, b, c].sort((v1, v2) => v1 - v2);127 // Act128 const arb = new IntegerArbitrary(min, max);129 const out = arb.canShrinkWithoutContext(mid);130 // Assert131 expect(out).toBe(true);132 })133 ));134 it('should always reject values outside of the requested range', () =>135 fc.assert(136 fc.property(137 fc.maxSafeInteger(),138 fc.maxSafeInteger(),139 fc.maxSafeInteger(),140 fc.constantFrom(...(['lower', 'higher'] as const)),141 (a, b, c, position) => {142 // Arrange143 const s = [a, b, c].sort((v1, v2) => v1 - v2);144 const [min, max, requested] = position === 'lower' ? [s[1], s[2], s[0]] : [s[0], s[1], s[2]];145 fc.pre(requested !== min && requested !== max);146 // Act147 const arb = new IntegerArbitrary(min, max);148 const out = arb.canShrinkWithoutContext(requested);149 // Assert150 expect(out).toBe(false);151 }152 )153 ));154 it.each`155 requested156 ${'1'}157 ${1.5}158 ${-0}159 ${Number.NaN}160 ${''}161 ${{}}162 `('should always reject non integral values like $requested', ({ requested }) => {163 // Arrange164 const min = 0;165 const max = 100;166 // Act167 const arb = new IntegerArbitrary(min, max);168 const out = arb.canShrinkWithoutContext(requested);169 // Assert170 expect(out).toBe(false);171 });172 });173 describe('shrink', () => {174 it('should always call shrink helper when no context provided', () =>175 fc.assert(176 fc.property(fc.maxSafeNat(), fc.maxSafeNat(), fc.maxSafeNat(), (a, b, c) => {177 // Arrange178 const [min, mid, max] = [a, b, c].sort((v1, v2) => v1 - v2);179 const expectedShrinks = Stream.nil<Value<number>>();180 const shrinkInteger = jest.spyOn(ShrinkIntegerMock, 'shrinkInteger');181 shrinkInteger.mockReturnValueOnce(expectedShrinks);182 // Act183 const arb = new IntegerArbitrary(min, max);184 const shrinks = arb.shrink(mid, undefined);185 // Assert186 expect(shrinks).toBe(expectedShrinks);187 expect(shrinkInteger).toHaveBeenCalledTimes(1);188 expect(shrinkInteger).toHaveBeenCalledWith(mid, expect.any(Number), true);189 })190 ));191 });192});193describe('IntegerArbitrary (integration)', () => {194 type Extra = { min: number; max: number };195 const extraParameters: fc.Arbitrary<Extra> = fc196 .tuple(fc.maxSafeInteger(), fc.maxSafeInteger())197 .map(([a, b]) => (a < b ? { min: a, max: b } : { min: b, max: a }));198 const isCorrect = (value: number, extra: Extra) =>199 typeof value === 'number' &&200 Number.isInteger(value) &&201 !Object.is(value, -0) &&202 extra.min <= value &&203 value <= extra.max;204 const isStrictlySmaller = (v1: number, v2: number) => Math.abs(v1) < Math.abs(v2);205 const integerBuilder = (extra: Extra) => new IntegerArbitrary(extra.min, extra.max);206 it('should produce the same values given the same seed', () => {207 assertProduceSameValueGivenSameSeed(integerBuilder, { extraParameters });208 });209 it('should only produce correct values', () => {210 assertProduceCorrectValues(integerBuilder, isCorrect, { extraParameters });211 });212 it('should produce values seen as shrinkable without any context', () => {213 assertProduceValuesShrinkableWithoutContext(integerBuilder, { extraParameters });214 });215 it('should be able to shrink to the same values without initial context', () => {216 assertShrinkProducesSameValueWithoutInitialContext(integerBuilder, { extraParameters });217 });218 it('should shrink towards strictly smaller values', () => {219 assertShrinkProducesStrictlySmallerValue(integerBuilder, isStrictlySmaller, { extraParameters });220 });221 describe('shrink', () => {222 it('should build a mirrored version of the shrinking tree if we negate all the values', () =>223 fc.assert(224 fc.property(225 fc.maxSafeInteger(),226 fc.integer({ min: 0, max: 20 }), // larger trees might be too wide227 fc.integer({ min: 0, max: 20 }),228 (start, o1, o2) => {229 // Arrange230 fc.pre(start + o1 <= Number.MAX_SAFE_INTEGER);231 fc.pre(start + o2 <= Number.MAX_SAFE_INTEGER);232 const min = start;233 const [mid, max] = o1 < o2 ? [min + o1, min + o2] : [min + o2, min + o1];234 const arb = new IntegerArbitrary(min, max);235 const arbNegate = new IntegerArbitrary(max !== 0 ? -max : 0, min !== 0 ? -min : min); // !==0 to avoid -0236 // Act237 const source = new Value(mid, undefined);238 const sourceNegate = new Value(mid !== 0 ? -mid : 0, undefined); // !==0 to avoid -0239 const tree = buildShrinkTree(arb, source);240 const treeNegate = buildShrinkTree(arbNegate, sourceNegate);241 const flat: number[] = [];242 const flatNegate: number[] = [];243 walkTree(tree, (v) => flat.push(v));244 walkTree(treeNegate, (v) => flatNegate.push(v !== 0 ? -v : 0));245 // Assert246 expect(arb.canShrinkWithoutContext(source.value)).toBe(true);247 expect(arbNegate.canShrinkWithoutContext(sourceNegate.value)).toBe(true);248 expect(flatNegate).toEqual(flat);249 }250 )251 ));252 it('should build an offset version of the shrinking tree if we offset all the values (keep every value >=0)', () =>253 fc.assert(254 fc.property(255 fc.integer({ min: 0, max: Number.MAX_SAFE_INTEGER }),256 fc.integer({ min: 0, max: 20 }), // larger trees might be too wide257 fc.integer({ min: 0, max: 20 }),258 fc.integer({ min: 0, max: Number.MAX_SAFE_INTEGER }),259 (start, o1, o2, offset) => {260 // Arrange261 fc.pre(start + o1 + offset <= Number.MAX_SAFE_INTEGER);262 fc.pre(start + o2 + offset <= Number.MAX_SAFE_INTEGER);263 const min = start;264 const [mid, max] = o1 < o2 ? [min + o1, min + o2] : [min + o2, min + o1];265 const arb = new IntegerArbitrary(min, max);266 const arbOffset = new IntegerArbitrary(min + offset, max + offset);267 // Act268 const source = new Value(mid, undefined);269 const sourceOffset = new Value(mid + offset, undefined);270 const tree = buildShrinkTree(arb, source);271 const treeOffset = buildShrinkTree(arbOffset, sourceOffset);272 const flat: number[] = [];273 const flatOffset: number[] = [];274 walkTree(tree, (v) => flat.push(v));275 walkTree(treeOffset, (v) => flatOffset.push(v - offset));276 // Assert277 expect(arb.canShrinkWithoutContext(source.value)).toBe(true);278 expect(arbOffset.canShrinkWithoutContext(sourceOffset.value)).toBe(true);279 expect(flatOffset).toEqual(flat);280 }281 )282 ));283 it('should shrink strictly positive value for positive range including zero', () => {284 // Arrange285 const arb = new IntegerArbitrary(0, 10);286 const source = new Value(8, undefined);287 // Act288 const tree = buildShrinkTree(arb, source);289 const renderedTree = renderTree(tree).join('\n');290 // Assert291 expect(arb.canShrinkWithoutContext(source.value)).toBe(true);292 // When there is no more option, the shrinker retry one time with the value293 // current-1 to check if something that changed outside (another value not itself)294 // may have changed the situation.295 expect(renderedTree).toMatchInlineSnapshot(`296 "8297 ├> 0298 ├> 4299 | ├> 2300 | | └> 1301 | | └> 0302 | └> 3303 | └> 2304 | ├> 0305 | └> 1306 | └> 0307 ├> 6308 | └> 5309 | └> 4310 | ├> 0311 | ├> 2312 | | └> 1313 | | └> 0314 | └> 3315 | └> 2316 | ├> 0317 | └> 1318 | └> 0319 └> 7320 └> 6321 ├> 0322 ├> 3323 | └> 2324 | └> 1325 | └> 0326 └> 5327 └> 4328 └> 3329 ├> 0330 └> 2331 └> 1332 └> 0"333 `);334 // Remarks:335 // * When we shrink 5 in path 8 > 6 > 5336 // we already now that 4 passed so we now that the smallest failing case337 // to look for is >= 5338 // * Same thing when we shrink 6 in path 8 > 6339 // * When we shrink 7 in path 8 > 7340 // we already now that 6 passed so we now that the smallest failing case341 // to look for is >= 7342 });343 it('should shrink strictly positive value for range not including zero', () => {344 // Arrange345 const arb = new IntegerArbitrary(10, 20);346 const source = new Value(18, undefined);347 // Act348 const tree = buildShrinkTree(arb, source);349 const renderedTree = renderTree(tree).join('\n');350 // Assert351 expect(arb.canShrinkWithoutContext(source.value)).toBe(true);352 // As the range [10, 20] and the value 18353 // are just offset by +10 compared to the first case,354 // the rendered tree will be offset by 10 too355 expect(renderedTree).toMatchInlineSnapshot(`356 "18357 ├> 10358 ├> 14359 | ├> 12360 | | └> 11361 | | └> 10362 | └> 13363 | └> 12364 | ├> 10365 | └> 11366 | └> 10367 ├> 16368 | └> 15369 | └> 14370 | ├> 10371 | ├> 12372 | | └> 11373 | | └> 10374 | └> 13375 | └> 12376 | ├> 10377 | └> 11378 | └> 10379 └> 17380 └> 16381 ├> 10382 ├> 13383 | └> 12384 | └> 11385 | └> 10386 └> 15387 └> 14388 └> 13389 ├> 10390 └> 12391 └> 11392 └> 10"393 `);394 });395 it('should shrink strictly negative value for negative range including zero', () => {396 // Arrange397 const arb = new IntegerArbitrary(-10, 0);398 const source = new Value(-8, undefined);399 // Act400 const tree = buildShrinkTree(arb, source);401 const renderedTree = renderTree(tree).join('\n');402 // Assert403 expect(arb.canShrinkWithoutContext(source.value)).toBe(true);404 // As the range [-10, 0] and the value -8405 // are the opposite of first case, the rendered tree will be the same except406 // it contains opposite values407 expect(renderedTree).toMatchInlineSnapshot(`408 "-8409 ├> 0410 ├> -4411 | ├> -2412 | | └> -1413 | | └> 0414 | └> -3415 | └> -2416 | ├> 0417 | └> -1418 | └> 0419 ├> -6420 | └> -5421 | └> -4422 | ├> 0423 | ├> -2424 | | └> -1425 | | └> 0426 | └> -3427 | └> -2428 | ├> 0429 | └> -1430 | └> 0431 └> -7432 └> -6433 ├> 0434 ├> -3435 | └> -2436 | └> -1437 | └> 0438 └> -5439 └> -4440 └> -3441 ├> 0442 └> -2443 └> -1444 └> 0"445 `);446 });447 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require("fast-check");2const arbNegate = require("fast-check-monorepo").arbNegate;3const arb1 = fc.integer();4const arb2 = arbNegate(arb1);5fc.assert(6 fc.property(arb1, (x) => {7 return x === -arb2().value;8 })9);

Full Screen

Using AI Code Generation

copy

Full Screen

1import { arbNegate } from 'fast-check-monorepo';2import * as fc from 'fast-check';3const negated = arbNegate(fc.integer());4console.log(negated);5import { arbNegate } from 'fast-check-monorepo';6import * as fc from 'fast-check';7const negated = arbNegate(fc.integer());8console.log(negated);

Full Screen

Using AI Code Generation

copy

Full Screen

1import { arbNegate } from 'fast-check-monorepo';2import { integer } from 'fast-check';3const arb = integer();4const arbNeg = arbNegate(arb);5const neg = arbNeg.generate();6console.log(neg);7import { arbNegate } from 'fast-check-monorepo';8import { integer } from 'fast-check';9const arb = integer();10const arbNeg = arbNegate(arb);11const neg = arbNeg.generate();12console.log(neg);13import { arbNegate } from 'fast-check-monorepo';14import { integer } from 'fast-check';15const arb = integer();16const arbNeg = arbNegate(arb);17const neg = arbNeg.generate();18console.log(neg);19import { arbNegate } from 'fast-check-monorepo';20import { integer } from 'fast-check';21const arb = integer();22const arbNeg = arbNegate(arb);23const neg = arbNeg.generate();24console.log(neg);25import { arbNegate } from 'fast-check-monorepo';26import { integer } from 'fast-check';27const arb = integer();28const arbNeg = arbNegate(arb);29const neg = arbNeg.generate();30console.log(neg);31import { arbNegate } from 'fast-check-monorepo';32import { integer } from 'fast-check';33const arb = integer();34const arbNeg = arbNegate(arb);35const neg = arbNeg.generate();36console.log(neg);37import { arbNegate } from 'fast-check-monorepo';38import { integer } from 'fast-check';39const arb = integer();40const arbNeg = arbNegate(arb);41const neg = arbNeg.generate();42console.log(neg);

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require("fast-check");2const arbNegate = require("fast-check-monorepo");3fc.assert(4 fc.property(arb, x => {5 })6);7const fc = require("fast-check");8const arbNegate = require("fast-check-monorepo");9fc.assert(10 fc.property(arb, x => {11 })12);13const fc = require("fast-check");14const arbNegate = require("fast-check-monorepo");15fc.assert(16 fc.property(arb,

Full Screen

Using AI Code Generation

copy

Full Screen

1const arb = fc.integer();2const negated = arbNegate(arb);3negated.generate();4const arb = fc.integer();5const negated = arbNegate(arb);6negated.generate();7const arb = fc.integer();8const negated = arbNegate(arb);9negated.generate();10const arb = fc.integer();11const negated = arbNegate(arb);12negated.generate();13const arb = fc.integer();14const negated = arbNegate(arb);15negated.generate();16const arb = fc.integer();17const negated = arbNegate(arb);18negated.generate();19const arb = fc.integer();20const negated = arbNegate(arb);21negated.generate();22const arb = fc.integer();23const negated = arbNegate(arb);24negated.generate();25const arb = fc.integer();26const negated = arbNegate(arb);27negated.generate();28const arb = fc.integer();29const negated = arbNegate(arb);30negated.generate();

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require("fast-check")2const arb = fc.integer().map(x => -x)3fc.assert(4 fc.property(arb, x => x > 0)5const fc = require("fast-check")6function myFunction(f) {7 return f(5)8}9fc.assert(10 fc.property(fc.integer(), x => myFunction(x => x + 1) > 0)11const fc = require("fast-check")12function myFunction() {13}14fc.assert(15 fc.property(fc.integer(), x => myFunction()(x) > 0)16const fc = require("fast-check")17function myFunction(f) {18 return f(5)19}20fc.assert(21 fc.property(fc.integer(), x => myFunction(x => x + 1) > 0)22const fc = require("fast-check")23function myFunction() {24}25fc.assert(

Full Screen

Using AI Code Generation

copy

Full Screen

1const { arbNegate } = require("@fast-check/arbitrary-integer");2const result = arbNegate(0, 1000);3console.log(result);4const { arbNegate } = require("@fast-check/arbitrary-integer");5const result = arbNegate(-1000, 1000);6console.log(result);7const { arbNegate } = require("@fast-check/arbitrary-integer");8const result = arbNegate(-1000, 0);9console.log(result);10const { arbNegate } = require("@fast-check/arbitrary-integer");11const result = arbNegate(0, 0);12console.log(result);13const { arbNegate } = require("@fast-check/arbitrary-integer");14const result = arbNegate(1, 0);15console.log(result);

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fast-check-monorepo automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful