How to use maxBiasedLength 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 fc = require('fast-check');2const { maxBiasedLength } = require('fast-check/lib/check/arbitrary/definition/BiasedArbitrary');3const { convertFromNext } = require('fast-check/lib/check/arbitrary/definition/Converters');4const { array } = require('fast-check/lib/check/arbitrary/ArrayArbitrary');5const { bias } = require('fast-check/lib/check/arbitrary/BiasedArbitraryWrapper');6const { integer } = require('fast-check/lib/check/arbitrary/IntegerArbitrary');7const { tuple } = require('fast-check/lib/check/arbitrary/TupleArbitrary');8const { record } = require('fast-check/lib/check/arbitrary/RecordArbitrary');9const { string } = require('fast-check/lib/check/arbitrary/StringArbitrary');10const { oneof } = require('fast-check/lib/check/arbitrary/OneOfArbitrary');11const { constantFrom } = require('fast-check/lib/check/arbitrary/ConstantArbitrary');12const { option } = require('fast-check/lib/check/arbitrary/OptionArbitrary');13const { double } = require('fast-check/lib/check/arbitrary/DoubleArbitrary');14const { bigInt } = require('fast-check/lib/check/arbitrary/BigIntArbitrary');15const { boolean } = require('fast-check/lib/check/arbitrary/BooleanArbitrary');16const { date } = require('fast-check/lib/check/arbitrary/DateArbitrary');17const { unicodeJson } = require('fast-check/lib/check/arbitrary/UnicodeJsonArbitrary');18const { unicodeJsonObject } = require('fast-check/lib/check/arbitrary/UnicodeJsonObjectArbitrary');19const { unicodeJsonString } = require('fast-check/lib/check/arbitrary/UnicodeJsonStringArbitrary');20const { unicodeJsonNumber } = require('fast-check/lib/check/arbitrary/UnicodeJsonNumberArbitrary');21const { unicodeJsonArray } = require('fast-check/lib/check/arbitrary/UnicodeJsonArrayArbitrary');22const { unicodeJsonConstant } = require('fast-check/lib/check/arbitrary/UnicodeJsonConstantArbitrary');23const { unicodeJsonKey } = require('fast-check/lib/check/arbitrary/UnicodeJsonKeyArbitrary');24const { unicodeJsonNull } = require('fast-check/lib/check/arbitrary/UnicodeJsonNullArbitrary');25const { unicodeJsonBoolean } = require('fast-check/lib/check/arbitrary/UnicodeJsonBooleanArbitrary');26const { unicodeJsonRecord } = require('fast-check/lib

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check-monorepo');2const { maxBiasedLength } = require('fast-check-monorepo/lib/check/arbitrary/definition/ArbitraryWithShrink');3const arb = fc.array(fc.integer());4const maxBiasLength = maxBiasedLength(arb);5console.log(maxBiasLength);6const fc = require('fast-check-monorepo');7const { maxBiasedLength } = require('fast-check-monorepo/lib/check/arbitrary/definition/ArbitraryWithShrink');8const arb = fc.array(fc.integer());9const maxBiasLength = maxBiasedLength(arb);10console.log(maxBiasLength);

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { maxBiasedLength } = require('@jscad/fast-check-monorepo');3const arb = fc.array(fc.integer(), { maxLength: 10 });4const arb2 = maxBiasedLength(arb, 3);5console.log(fc.sample(arb2));6console.log(fc.sample(arb2));7console.log(fc.sample(arb2));8console.log(fc.sample(arb2));9const fc = require('fast-check');10const { maxBiasedLength } = require('@jscad/fast-check-monorepo');11const arb = fc.array(fc.integer(), { maxLength: 10 });12const arb2 = maxBiasedLength(arb, 3);13console.log(fc.sample(arb2));14console.log(fc.sample(arb2));15console.log(fc.sample(arb2));16console.log(fc.sample(arb2));17const fc = require('fast-check');18const { maxBiasedLength } = require('@jscad/fast-check-monorepo');19const arb = fc.array(fc.integer(), { maxLength: 10 });20const arb2 = maxBiasedLength(arb, 3);21console.log(fc.sample(arb

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require("fast-check");2const maxBiasedLength = require("fast-check/lib/arbitrary/_internals/MaxBiasedLength.js");3const { MaxBiasedLength } = maxBiasedLength;4const biasedArb = fc.integer(0, 100);5const biasedArb2 = fc.integer(0, 100);6const biasedArb3 = fc.integer(0, 100);7const biasedArb4 = fc.integer(0, 100);8const biasedArb5 = fc.integer(0, 100);9const biasedArb6 = fc.integer(0, 100);10const biasedArb7 = fc.integer(0, 100);11const biasedArb8 = fc.integer(0, 100);12const biasedArb9 = fc.integer(0, 100);13const biasedArb10 = fc.integer(0, 100);14const biasedArb11 = fc.integer(0, 100);15const biasedArb12 = fc.integer(0, 100);16const biasedArb13 = fc.integer(0, 100);17const biasedArb14 = fc.integer(0, 100);18const biasedArb15 = fc.integer(0, 100);19const biasedArb16 = fc.integer(0, 100);20const biasedArb17 = fc.integer(0, 100);21const biasedArb18 = fc.integer(0, 100);22const biasedArb19 = fc.integer(0, 100);23const biasedArb20 = fc.integer(0, 100);24const biasedArb21 = fc.integer(0, 100);25const biasedArb22 = fc.integer(0, 100);26const biasedArb23 = fc.integer(0, 100);27const biasedArb24 = fc.integer(0, 100);28const biasedArb25 = fc.integer(0, 100);29const biasedArb26 = fc.integer(0, 100);30const biasedArb27 = fc.integer(0, 100);31const biasedArb28 = fc.integer(0, 100);32const biasedArb29 = fc.integer(0, 100);33const biasedArb30 = fc.integer(0, 100);34const biasedArb31 = fc.integer(0, 100);35const biasedArb32 = fc.integer(0, 100);

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require("fast-check");2const { maxBiasedLength } = require("fast-check/lib/check/arbitrary/definition/BiasedArbitraryWrapper");3console.log(maxBiasedLength(100, 100));4const { maxBiasedLength } = require("fast-check/lib/check/arbitrary/definition/BiasedArbitraryWrapper");5console.log(maxBiasedLength(100, 100));6const { maxBiasedLength } = require("fast-check/lib/check/arbitrary/definition/BiasedArbitraryWrapper");7console.log(maxBiasedLength(100, 100));8const { maxBiasedLength } = require("fast-check/lib/check/arbitrary/definition/BiasedArbitraryWrapper");9console.log(maxBiasedLength(100, 100));10const { maxBiasedLength } = require("fast-check/lib/check/ar

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check')2const assert = require('assert')3const test1 = () => {4 const test2 = () => {5 const test3 = () => {6 const test4 = () => {7 const test5 = () => {8 const test6 = () => {9 const test7 = () => {10 const test8 = () => {11 const test9 = () => {12 const test10 = () => {13 const test11 = () => {14 const test12 = () => {15 const test13 = () => {16 const test14 = () => {17 const test15 = () => {18 const test16 = () => {19 const test17 = () => {20 const test18 = () => {21 const test19 = () => {22 const test20 = () => {23 const test21 = () => {24 const test22 = () => {25 const test23 = () => {26 const test24 = () => {27 const test25 = () => {28 const test26 = () => {29 const test27 = () => {30 const test28 = () => {31 const test29 = () => {32 const test30 = () => {33 const test31 = () => {34 const test32 = () => {35 const test33 = () => {36 const test34 = () => {37 const test35 = () => {38 const test36 = () => {39 const test37 = () => {40 const test38 = () => {41 const test39 = () => {42 const test40 = () => {43 const test41 = () => {44 const test42 = () => {45 const test43 = () => {46 const test44 = () => {47 const test45 = () => {48 const test46 = () => {49 const test47 = () => {50 const test48 = () => {51 const test49 = () => {52 const test50 = () => {53 const test51 = () => {54 const test52 = () => {55 const test53 = () => {56 const test54 = () => {57 const test55 = () => {58 const test56 = () => {59 const test57 = () => {

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