How to use depthImpact method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

ArrayArbitrary.ts

Source:ArrayArbitrary.ts Github

copy

Full Screen

1import { Random } from '../../random/generator/Random';2import { Stream } from '../../stream/Stream';3import { cloneIfNeeded, cloneMethod } from '../../check/symbols';4import { integer } from '../integer';5import { makeLazy } from '../../stream/LazyIterableIterator';6import { Arbitrary } from '../../check/arbitrary/definition/Arbitrary';7import { Value } from '../../check/arbitrary/definition/Value';8import { CustomSetBuilder } from './interfaces/CustomSet';9import { DepthContext, DepthIdentifier, getDepthContextFor } from './helpers/DepthContext';10import { buildSlicedGenerator } from './helpers/BuildSlicedGenerator';11import { safeMap, safePush, safeSlice } from '../../utils/globals';12const safeMathFloor = Math.floor;13const safeMathLog = Math.log;14const safeMathMax = Math.max;15const safeArrayIsArray = Array.isArray;16/** @internal */17type ArrayArbitraryContext = {18 shrunkOnce: boolean;19 lengthContext: unknown;20 itemsContexts: unknown[];21 startIndex: number;22};23/** @internal */24function biasedMaxLength(minLength: number, maxLength: number): number {25 if (minLength === maxLength) {26 return minLength;27 }28 return minLength + safeMathFloor(safeMathLog(maxLength - minLength) / safeMathLog(2));29}30/** @internal */31export class ArrayArbitrary<T> extends Arbitrary<T[]> {32 readonly lengthArb: Arbitrary<number>;33 readonly depthContext: DepthContext;34 constructor(35 readonly arb: Arbitrary<T>,36 readonly minLength: number,37 readonly maxGeneratedLength: number,38 readonly maxLength: number,39 depthIdentifier: DepthIdentifier | string | undefined,40 // Whenever passing a isEqual to ArrayArbitrary, you also have to filter41 // it's output just in case produced values are too small (below minLength)42 readonly setBuilder: CustomSetBuilder<Value<T>> | undefined,43 readonly customSlices: T[][]44 ) {45 super();46 this.lengthArb = integer({ min: minLength, max: maxGeneratedLength });47 this.depthContext = getDepthContextFor(depthIdentifier);48 }49 private preFilter(tab: Value<T>[]): Value<T>[] {50 if (this.setBuilder === undefined) {51 return tab;52 }53 const s = this.setBuilder();54 for (let index = 0; index !== tab.length; ++index) {55 s.tryAdd(tab[index]);56 }57 return s.getData();58 }59 private static makeItCloneable<T>(vs: T[], shrinkables: Value<T>[]) {60 (vs as any)[cloneMethod] = () => {61 const cloned: T[] = [];62 for (let idx = 0; idx !== shrinkables.length; ++idx) {63 safePush(cloned, shrinkables[idx].value); // push potentially cloned values64 }65 this.makeItCloneable(cloned, shrinkables);66 return cloned;67 };68 return vs;69 }70 private generateNItemsNoDuplicates(71 setBuilder: CustomSetBuilder<Value<T>>,72 N: number,73 mrng: Random,74 biasFactorItems: number | undefined75 ): Value<T>[] {76 let numSkippedInRow = 0;77 const s = setBuilder();78 const slicedGenerator = buildSlicedGenerator(this.arb, mrng, this.customSlices, biasFactorItems);79 // Try to append into items up to the target size80 // We may reject some items as they are already part of the set81 // so we need to retry and generate other ones. In order to prevent infinite loop,82 // we accept a max of maxGeneratedLength consecutive failures. This circuit breaker may cause83 // generated to be smaller than the minimal accepted one.84 while (s.size() < N && numSkippedInRow < this.maxGeneratedLength) {85 const current = slicedGenerator.next();86 if (s.tryAdd(current)) {87 numSkippedInRow = 0;88 } else {89 numSkippedInRow += 1;90 }91 }92 return s.getData();93 }94 private safeGenerateNItemsNoDuplicates(95 setBuilder: CustomSetBuilder<Value<T>>,96 N: number,97 mrng: Random,98 biasFactorItems: number | undefined99 ): Value<T>[] {100 const depthImpact = safeMathMax(0, N - biasedMaxLength(this.minLength, this.maxGeneratedLength)); // no depth impact for biased lengths101 this.depthContext.depth += depthImpact; // increase depth102 try {103 return this.generateNItemsNoDuplicates(setBuilder, N, mrng, biasFactorItems);104 } finally {105 this.depthContext.depth -= depthImpact; // decrease depth (reset depth)106 }107 }108 private generateNItems(N: number, mrng: Random, biasFactorItems: number | undefined): Value<T>[] {109 const items: Value<T>[] = [];110 const slicedGenerator = buildSlicedGenerator(this.arb, mrng, this.customSlices, biasFactorItems);111 slicedGenerator.attemptExact(N);112 for (let index = 0; index !== N; ++index) {113 const current = slicedGenerator.next();114 safePush(items, current);115 }116 return items;117 }118 private safeGenerateNItems(N: number, mrng: Random, biasFactorItems: number | undefined): Value<T>[] {119 const depthImpact = safeMathMax(0, N - biasedMaxLength(this.minLength, this.maxGeneratedLength)); // no depth impact for biased lengths120 this.depthContext.depth += depthImpact; // increase depth121 try {122 return this.generateNItems(N, mrng, biasFactorItems);123 } finally {124 this.depthContext.depth -= depthImpact; // decrease depth (reset depth)125 }126 }127 private wrapper(128 itemsRaw: Value<T>[],129 shrunkOnce: boolean,130 itemsRawLengthContext: unknown,131 startIndex: number132 ): Value<T[]> {133 // We need to explicitly apply filtering on shrink items134 // has they might have duplicates (on non shrunk it is not the case by construct)135 const items = shrunkOnce ? this.preFilter(itemsRaw) : itemsRaw;136 let cloneable = false;137 const vs: T[] = [];138 const itemsContexts: unknown[] = [];139 for (let idx = 0; idx !== items.length; ++idx) {140 const s = items[idx];141 cloneable = cloneable || s.hasToBeCloned;142 safePush(vs, s.value);143 safePush(itemsContexts, s.context);144 }145 if (cloneable) {146 ArrayArbitrary.makeItCloneable(vs, items);147 }148 const context: ArrayArbitraryContext = {149 shrunkOnce,150 lengthContext:151 itemsRaw.length === items.length && itemsRawLengthContext !== undefined152 ? itemsRawLengthContext // items and itemsRaw have the same length context is applicable153 : undefined,154 itemsContexts,155 startIndex,156 };157 return new Value(vs, context);158 }159 generate(mrng: Random, biasFactor: number | undefined): Value<T[]> {160 const biasMeta = this.applyBias(mrng, biasFactor);161 const targetSize = biasMeta.size;162 const items =163 this.setBuilder !== undefined164 ? this.safeGenerateNItemsNoDuplicates(this.setBuilder, targetSize, mrng, biasMeta.biasFactorItems)165 : this.safeGenerateNItems(targetSize, mrng, biasMeta.biasFactorItems);166 return this.wrapper(items, false, undefined, 0);167 }168 private applyBias(mrng: Random, biasFactor: number | undefined): { size: number; biasFactorItems?: number } {169 if (biasFactor === undefined) {170 // We don't bias anything171 return { size: this.lengthArb.generate(mrng, undefined).value };172 }173 // We directly forward bias to items whenever no bias applicable onto length174 if (this.minLength === this.maxGeneratedLength) {175 // We only apply bias on items176 return { size: this.lengthArb.generate(mrng, undefined).value, biasFactorItems: biasFactor };177 }178 if (mrng.nextInt(1, biasFactor) !== 1) {179 // We don't bias anything180 return { size: this.lengthArb.generate(mrng, undefined).value };181 }182 // We apply bias (1 chance over biasFactor)183 if (mrng.nextInt(1, biasFactor) !== 1 || this.minLength === this.maxGeneratedLength) {184 // We only apply bias on items ((biasFactor-1) chances over biasFactor²)185 return { size: this.lengthArb.generate(mrng, undefined).value, biasFactorItems: biasFactor };186 }187 // We apply bias for both items and length (1 chance over biasFactor²)188 const maxBiasedLength = biasedMaxLength(this.minLength, this.maxGeneratedLength);189 const targetSizeValue = integer({ min: this.minLength, max: maxBiasedLength }).generate(mrng, undefined);190 return { size: targetSizeValue.value, biasFactorItems: biasFactor };191 }192 canShrinkWithoutContext(value: unknown): value is T[] {193 if (!safeArrayIsArray(value) || this.minLength > value.length || value.length > this.maxLength) {194 return false;195 }196 for (let index = 0; index !== value.length; ++index) {197 if (!(index in value)) {198 // sparse array cannot be produced by this instance199 return false;200 }201 if (!this.arb.canShrinkWithoutContext(value[index])) {202 // item at index cannot be produced by our arbitrary203 return false;204 }205 }206 // `preFilter` only drops items, it does not reorder them or add some more207 // if calling it with `value` results into a smaller array it means that the value was not generated by this instance208 const filtered = this.preFilter(safeMap(value, (item) => new Value(item, undefined)));209 return filtered.length === value.length;210 }211 private shrinkItemByItem(212 value: T[],213 safeContext: ArrayArbitraryContext,214 endIndex: number215 ): IterableIterator<[Value<T>[], unknown, number]> {216 let shrinks = Stream.nil<[Value<T>[], unknown, number]>();217 for (let index = safeContext.startIndex; index < endIndex; ++index) {218 shrinks = shrinks.join(219 makeLazy(() =>220 this.arb.shrink(value[index], safeContext.itemsContexts[index]).map((v): [Value<T>[], unknown, number] => {221 const beforeCurrent = safeMap(222 safeSlice(value, 0, index),223 (v, i) => new Value(cloneIfNeeded(v), safeContext.itemsContexts[i])224 );225 const afterCurrent = safeMap(226 safeSlice(value, index + 1),227 (v, i) => new Value(cloneIfNeeded(v), safeContext.itemsContexts[i + index + 1])228 );229 return [230 [...beforeCurrent, v, ...afterCurrent],231 undefined, // no length context232 index, // avoid shrinking entries before index in sub-shrinks233 ];234 })235 )236 );237 }238 return shrinks;239 }240 private shrinkImpl(value: T[], context?: unknown): Stream<[Value<T>[], unknown, number]> {241 if (value.length === 0) {242 return Stream.nil();243 }244 const safeContext: ArrayArbitraryContext =245 context !== undefined246 ? (context as ArrayArbitraryContext)247 : { shrunkOnce: false, lengthContext: undefined, itemsContexts: [], startIndex: 0 };248 return (249 this.lengthArb250 .shrink(251 value.length,252 // lengthContext is a context returned by a previous call to the integer253 // arbitrary and the integer value items.length.254 safeContext.lengthContext255 )256 // in case we already shrunk once but don't have any dedicated context to help the shrinker, we drop the first item257 // except if reached we have the minimal size +1, in that case we apply a last chance try policy258 .drop(259 safeContext.shrunkOnce && safeContext.lengthContext === undefined && value.length > this.minLength + 1 ? 1 : 0260 )261 .map((lengthValue): [Value<T>[], unknown, number] => {262 const sliceStart = value.length - lengthValue.value;263 return [264 safeMap(265 safeSlice(value, sliceStart),266 (v, index) => new Value(cloneIfNeeded(v), safeContext.itemsContexts[index + sliceStart])267 ), // array of length lengthValue.value268 lengthValue.context, // integer context for value lengthValue.value (the length)269 0,270 ];271 })272 // Length context value will be set to undefined for remaining shrinking values273 // as they are outside of our shrinking process focused on items.length.274 // None of our computed contexts will apply for them.275 .join(276 makeLazy(() =>277 value.length > this.minLength278 ? this.shrinkItemByItem(value, safeContext, 1)279 : this.shrinkItemByItem(value, safeContext, value.length)280 )281 )282 .join(283 value.length > this.minLength284 ? makeLazy(() => {285 // We pass itemsLengthContext=undefined to next shrinker to start shrinking286 // without any assumptions on the current state (we never explored that one)287 const subContext: ArrayArbitraryContext = {288 shrunkOnce: false,289 lengthContext: undefined,290 itemsContexts: safeSlice(safeContext.itemsContexts, 1),291 startIndex: 0,292 };293 return this.shrinkImpl(safeSlice(value, 1), subContext)294 .filter((v) => this.minLength <= v[0].length + 1)295 .map((v): [Value<T>[], unknown, number] => {296 return [[new Value(cloneIfNeeded(value[0]), safeContext.itemsContexts[0]), ...v[0]], undefined, 0];297 });298 })299 : Stream.nil()300 )301 );302 }303 shrink(value: T[], context?: unknown): Stream<Value<T[]>> {304 return this.shrinkImpl(value, context).map((contextualValue) =>305 this.wrapper(contextualValue[0], true, contextualValue[1], contextualValue[2])306 );307 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { check, property } = require('fast-check');2const { depthImpact } = require('fast-check-monorepo');3const isPrime = (n) => {4 if (n <= 1) {5 return false;6 }7 for (let i = 2; i < n; i++) {8 if (n % i === 0) {9 return false;10 }11 }12 return true;13}14const isPrimeArb = (min, max) => {15 return property(depthImpact((n) => {16 return n >= min && n <= max;17 }, 1), isPrime);18}19const isPrimeArb2 = (min, max) => {20 return property(depthImpact((n) => {21 return n >= min && n <= max;22 }, 2), isPrime);23}24const isPrimeArb3 = (min, max) => {25 return property(depthImpact((n) => {26 return n >= min && n <= max;27 }, 3), isPrime);28}29describe('isPrime', () => {30 it('should work', () => {31 check(isPrimeArb(2, 1000), { numRuns: 10000 });32 });33 it('should work with depth impact 2', () => {34 check(isPrimeArb2(2, 1000), { numRuns: 10000 });35 });36 it('should work with depth impact 3', () => {37 check(isPrimeArb3(2, 1000), { numRuns: 10000 });38 });39});40const { depthImpact } = require('fast-check-monorepo');41const isPrime = (n) => {42 if (n <= 1) {43 return false;44 }45 for (let i = 2; i < n; i++) {46 if (n % i === 0) {47 return false;48 }49 }50 return true;51}52const isPrimeArb = (min, max) => {53 return property(depthImpact((n) => {

Full Screen

Using AI Code Generation

copy

Full Screen

1const { depthImpact } = require('fast-check-monorepo');2const impact = depthImpact(3);3console.log(impact);4const { depthImpact } = require('fast-check-monorepo');5const impact = depthImpact(4);6console.log(impact);7const { depthImpact } = require('fast-check-monorepo');8const impact = depthImpact(5);9console.log(impact);10const { depthImpact } = require('fast-check-monorepo');11const impact = depthImpact(6);12console.log(impact);13const { depthImpact } = require('fast-check-monorepo');14const impact = depthImpact(7);15console.log(impact);16const { depthImpact } = require('fast-check-monorepo');17const impact = depthImpact(8);18console.log(impact);19const { depthImpact } = require('fast-check-monorepo');20const impact = depthImpact(9);21console.log(impact);22const { depthImpact } = require('fast-check-monorepo');23const impact = depthImpact(10);24console.log(impact);25const { depthImpact } = require('fast-check-monorepo');26const impact = depthImpact(11);27console.log(impact);28const { depthImpact } = require('fast-check-monorepo');29const impact = depthImpact(12);30console.log(impact);31const { depthImpact } = require('fast-check-monorepo');

Full Screen

Using AI Code Generation

copy

Full Screen

1const { depthImpact } = require("fast-check-monorepo");2const test3 = () => {3 const res = depthImpact(3);4 console.log(res);5};6test3();7const { depthImpact } = require("fast-check-monorepo");8const test4 = () => {9 const res = depthImpact(4);10 console.log(res);11};12test4();13const { depthImpact } = require("fast-check-monorepo");14const test5 = () => {15 const res = depthImpact(5);16 console.log(res);17};18test5();19const { depthImpact } = require("fast-check-monorepo");20const test6 = () => {21 const res = depthImpact(6);22 console.log(res);23};24test6();25const { depthImpact } = require("fast-check-monorepo");26const test7 = () => {27 const res = depthImpact(7);28 console.log(res);29};30test7();31const { depthImpact } = require("fast-check-monorepo");32const test8 = () => {33 const res = depthImpact(8);34 console.log(res);35};36test8();37const { depthImpact } = require("fast-check-monorepo");38const test9 = () => {39 const res = depthImpact(9);40 console.log(res);41};42test9();43const { depthImpact } = require("fast-check-monorepo");44const test10 = () => {45 const res = depthImpact(10);46 console.log(res);47};48test10();49const { depthImpact } = require("fast-check-monorepo");

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { depthImpact } = require('fast-check-monorepo');3const m = 100;4const n = 100;5fc.assert(6 fc.property(fc.integer(0, m), fc.integer(0, n), (a, b) => {7 const result = a + b;8 return result >= 0 && result <= m + n;9 }),10 { depthImpact: depthImpact(1, m) }11);12const fc = require('fast-check');13const { depthImpact } = require('fast-check-monorepo');14const m = 100;15const n = 100;16fc.assert(17 fc.property(fc.integer(0, m), fc.integer(0, n), (a, b) => {18 const result = a + b;19 return result >= 0 && result <= m + n;20 }),21 { depthImpact: depthImpact(1, n) }22);23const fc = require('fast-check');24const { depthImpact } = require('fast-check-monorepo');25const m = 100;26const n = 100;27fc.assert(28 fc.property(fc.integer(0, m), fc.integer(0, n), (a, b) => {29 const result = a + b;30 return result >= 0 && result <= m + n;31 }),32 { depthImpact: depthImpact(1, m + n) }33);34const fc = require('fast-check');35const { depthImpact } = require('fast-check-monorepo');36const m = 100;37const n = 100;38fc.assert(39 fc.property(fc.integer(0, m), fc.integer(0, n), (a, b) => {40 const result = a + b;41 return result >= 0 && result <= m + n;42 }),43 { depthImpact: depthImpact(1, m + n + 1) }44);45const fc = require('fast-check');46const { depthImpact } = require

Full Screen

Using AI Code Generation

copy

Full Screen

1const { depthImpact } = require("fast-check");2const impact = depthImpact({3});4console.log(impact);5const { depthImpact } = require("fast-check");6const impact = depthImpact({7});8console.log(impact);9const { depthImpact } = require("fast-check");10const impact = depthImpact({11});12console.log(impact);13const { depthImpact } = require("fast-check");14const impact = depthImpact({15});16console.log(impact);17const { depthImpact } = require("fast-check");18const impact = depthImpact({19});20console.log(impact);21const { depthImpact } = require("fast-check");22const impact = depthImpact({23});24console.log(impact);25const { depthImpact } = require("fast-check");26const impact = depthImpact({27});28console.log(impact);29const { depthImpact } = require("fast-check");30const impact = depthImpact({31});32console.log(impact);

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require("fast-check");2const { depthImpact } = require("fast-check-monorepo");3const myArbitrary = fc.string();4const myArbitrary2 = fc.string();5 .tuple(myArbitrary, myArbitrary2)6 .map(([a, b]) => a + b);7const impact = depthImpact(myArbitrary3);8const impact2 = depthImpact(myArbitrary);9const impact3 = depthImpact(myArbitrary2);

Full Screen

Using AI Code Generation

copy

Full Screen

1const { depthImpact } = require("fast-check-monorepo");2const impact = depthImpact("test2.js", "test2.js");3console.log("impact", impact);4const { depthImpact } = require("fast-check-monorepo");5const impact = depthImpact("test3.js", "test3.js");6console.log("impact", impact);7const { depthImpact } = require("fast-check-monorepo");8const impact = depthImpact("test4.js", "test4.js");9console.log("impact", impact);10const { depthImpact } = require("fast-check-monorepo");11const impact = depthImpact("test5.js", "test5.js");12console.log("impact", impact);13const { depthImpact } = require("fast-check-monorepo");14const impact = depthImpact("test6.js", "test6.js");15console.log("impact", impact);

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