How to use biasedMaxLength method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

ArrayArbitrary.ts

Source:ArrayArbitrary.ts Github

copy

Full Screen

...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 arbitrary...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require("fast-check");2const { biasedMaxLength } = require("fast-check/lib/arbitrary/biasedMaxLength");3const { integer } = require("fast-check/lib/arbitrary/integer");4const { stringOf } = require("fast-check/lib/arbitrary/stringOf");5const biasedString = biasedMaxLength(6 stringOf(integer(0, 255).map((x) => String.fromCharCode(x)))7);8fc.assert(fc.property(biasedString, (s) => s.length <= 20));9const fc = require("fast-check");10const { biasedMaxLength } = require("fast-check/lib/arbitrary/biasedMaxLength");11const { integer } = require("fast-check/lib/arbitrary/integer");12const { stringOf } = require("fast-check/lib/arbitrary/stringOf");13const biasedString = biasedMaxLength(14 stringOf(integer(0, 255).map((x) => String.fromCharCode(x)))15);16fc.assert(fc.property(biasedString, (s) => s.length <= 20));17const fc = require("fast-check");18const { biasedMaxLength } = require("fast-check/lib/arbitrary/biasedMaxLength");19const { integer } = require("fast-check/lib/arbitrary/integer");20const { stringOf } = require("fast-check/lib/arbitrary/stringOf");21const biasedString = biasedMaxLength(22 stringOf(integer(0, 255).map((x) => String.fromCharCode(x)))23);24fc.assert(fc.property(biasedString, (s) => s.length <= 20));25const fc = require("fast-check");26const { biasedMaxLength } = require("fast-check/lib/arbitrary/biasedMaxLength");27const { integer } = require("fast-check/lib/arbitrary/integer");28const { stringOf } = require("fast-check/lib/arbitrary/stringOf");29const biasedString = biasedMaxLength(30 stringOf(integer(0, 255).map((x) => String.fromCharCode(x)))31);32fc.assert(fc.property(biasedString, (s) => s.length <= 20));

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const biasedMaxLength = require('fast-check/lib/arbitrary/_internals/BiasedArbitrary').biasedMaxLength;3const biasedArray = biasedMaxLength(fc.integer(), 5);4fc.assert(5 fc.property(biasedArray, (arr) => {6 return arr.length <= 5;7 })8);

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const biasedMaxLength = (min, max, bias) => {3 return fc.integer(min, max).map(v => Math.floor(Math.pow(v, bias)));4};5fc.assert(6 fc.property(7 fc.string(),8 fc.string(),9 biasedMaxLength(1, 100, 0.5),10 (a, b, len) => {11 return a.length + b.length <= len;12 }13);

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { biasedMaxLength } = require('fast-check/lib/arbitrary/_internals/BiasedArbitrary');3const biasedMaxLength = require('fast-check/lib/arbitrary/_internals/BiasedArbitrary').biasedMaxLength;4console.log(biasedMaxLength(5));5const biasedMaxLength = require('fast-check/lib/arbitrary/_internals/BiasedArbitrary').biasedMaxLength;6console.log(biasedMaxLength(5));7const biasedMaxLength = require('fast-check/lib/arbitrary/_internals/BiasedArbitrary').biasedMaxLength;8console.log(biasedMaxLength(5));9const biasedMaxLength = require('fast-check/lib/arbitrary/_internals/BiasedArbitrary').biasedMaxLength;10console.log(biasedMaxLength(5));11const biasedMaxLength = require('fast-check/lib/arbitrary/_internals/BiasedArbitrary').biasedMaxLength;12console.log(biasedMaxLength(5));13const biasedMaxLength = require('fast-check/lib/arbitrary/_internals/BiasedArbitrary').biasedMaxLength;14console.log(biasedMaxLength(5));15const biasedMaxLength = require('fast-check/lib/arbitrary/_internals/BiasedArbitrary').biasedMaxLength;16console.log(biasedMaxLength(5));17const biasedMaxLength = require('fast-check/lib/arbitrary/_internals/BiasedArbitrary').biasedMaxLength;18console.log(biasedMaxLength(5));19const biasedMaxLength = require('fast-check/lib/arbitrary/_internals/BiasedArbitrary').biasedMaxLength;20console.log(biasedMaxLength

Full Screen

Using AI Code Generation

copy

Full Screen

1const { biasedMaxLength } = require('@dubzzz/fast-check');2const biasFactor = 2.5;3const input = 4;4const output = biasedMaxLength(input, biasFactor);5console.log(output);6const { biasedMaxLength } = require('@dubzzz/fast-check');7const biasFactor = 2.5;8const input = 5;9const output = biasedMaxLength(input, biasFactor);10console.log(output);11const { biasedMaxLength } = require('@dubzzz/fast-check');12const biasFactor = 2.5;13const input = 6;14const output = biasedMaxLength(input, biasFactor);15console.log(output);16const { biasedMaxLength } = require('@dubzzz/fast-check');17const biasFactor = 2.5;18const input = 7;19const output = biasedMaxLength(input, biasFactor);20console.log(output);21const { biasedMaxLength } = require('@dubzzz/fast-check');22const biasFactor = 2.5;23const input = 8;24const output = biasedMaxLength(input, biasFactor);25console.log(output);26const { biasedMaxLength } = require('@dubzzz/fast-check');27const biasFactor = 2.5;28const input = 9;29const output = biasedMaxLength(input,

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