How to use potentialSelectedIndex method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

FrequencyArbitrary.ts

Source:FrequencyArbitrary.ts Github

copy

Full Screen

1import { Random } from '../../random/generator/Random';2import { Stream } from '../../stream/Stream';3import { Arbitrary } from '../../check/arbitrary/definition/Arbitrary';4import { Value } from '../../check/arbitrary/definition/Value';5import { DepthContext, DepthIdentifier, getDepthContextFor } from './helpers/DepthContext';6import { depthBiasFromSizeForArbitrary, DepthSize } from './helpers/MaxLengthFromMinLength';7import { safePush } from '../../utils/globals';8const safePositiveInfinity = Number.POSITIVE_INFINITY;9const safeMaxSafeInteger = Number.MAX_SAFE_INTEGER;10const safeNumberIsInteger = Number.isInteger;11const safeMathFloor = Math.floor;12const safeMathPow = Math.pow;13const safeMathMin = Math.min;14/** @internal */15export class FrequencyArbitrary<T> extends Arbitrary<T> {16 readonly cumulatedWeights: number[];17 readonly totalWeight: number;18 static from<T>(warbs: _WeightedArbitrary<T>[], constraints: _Constraints, label: string): Arbitrary<T> {19 if (warbs.length === 0) {20 throw new Error(`${label} expects at least one weighted arbitrary`);21 }22 let totalWeight = 0;23 for (let idx = 0; idx !== warbs.length; ++idx) {24 const currentArbitrary = warbs[idx].arbitrary;25 if (currentArbitrary === undefined) {26 throw new Error(`${label} expects arbitraries to be specified`);27 }28 const currentWeight = warbs[idx].weight;29 totalWeight += currentWeight;30 if (!safeNumberIsInteger(currentWeight)) {31 throw new Error(`${label} expects weights to be integer values`);32 }33 if (currentWeight < 0) {34 throw new Error(`${label} expects weights to be superior or equal to 0`);35 }36 }37 if (totalWeight <= 0) {38 throw new Error(`${label} expects the sum of weights to be strictly superior to 0`);39 }40 const sanitizedConstraints: _SanitizedConstraints = {41 depthBias: depthBiasFromSizeForArbitrary(constraints.depthSize, constraints.maxDepth !== undefined),42 maxDepth: constraints.maxDepth != undefined ? constraints.maxDepth : safePositiveInfinity,43 withCrossShrink: !!constraints.withCrossShrink,44 };45 return new FrequencyArbitrary(warbs, sanitizedConstraints, getDepthContextFor(constraints.depthIdentifier));46 }47 private constructor(48 readonly warbs: _WeightedArbitrary<T>[],49 readonly constraints: _SanitizedConstraints,50 readonly context: DepthContext51 ) {52 super();53 let currentWeight = 0;54 this.cumulatedWeights = [];55 for (let idx = 0; idx !== warbs.length; ++idx) {56 currentWeight += warbs[idx].weight;57 safePush(this.cumulatedWeights, currentWeight);58 }59 this.totalWeight = currentWeight;60 }61 generate(mrng: Random, biasFactor: number | undefined): Value<T> {62 if (this.mustGenerateFirst()) {63 // index=0 can be selected even if it has a weight equal to zero64 return this.safeGenerateForIndex(mrng, 0, biasFactor);65 }66 const selected = mrng.nextInt(this.computeNegDepthBenefit(), this.totalWeight - 1);67 for (let idx = 0; idx !== this.cumulatedWeights.length; ++idx) {68 if (selected < this.cumulatedWeights[idx]) {69 return this.safeGenerateForIndex(mrng, idx, biasFactor);70 }71 }72 throw new Error(`Unable to generate from fc.frequency`);73 }74 canShrinkWithoutContext(value: unknown): value is T {75 return this.canShrinkWithoutContextIndex(value) !== -1;76 }77 shrink(value: T, context?: unknown): Stream<Value<T>> {78 if (context !== undefined) {79 const safeContext = context as _FrequencyArbitraryContext<T>;80 const selectedIndex = safeContext.selectedIndex;81 const originalBias = safeContext.originalBias;82 const originalArbitrary = this.warbs[selectedIndex].arbitrary;83 const originalShrinks = originalArbitrary84 .shrink(value, safeContext.originalContext)85 .map((v) => this.mapIntoValue(selectedIndex, v, null, originalBias));86 if (safeContext.clonedMrngForFallbackFirst !== null) {87 if (safeContext.cachedGeneratedForFirst === undefined) {88 safeContext.cachedGeneratedForFirst = this.safeGenerateForIndex(89 safeContext.clonedMrngForFallbackFirst,90 0,91 originalBias92 );93 }94 const valueFromFirst = safeContext.cachedGeneratedForFirst;95 return Stream.of(valueFromFirst).join(originalShrinks);96 }97 return originalShrinks;98 }99 const potentialSelectedIndex = this.canShrinkWithoutContextIndex(value);100 if (potentialSelectedIndex === -1) {101 return Stream.nil(); // No arbitrary found to accept this value102 }103 return this.defaultShrinkForFirst(potentialSelectedIndex).join(104 this.warbs[potentialSelectedIndex].arbitrary105 .shrink(value, undefined) // re-checked by canShrinkWithoutContextIndex106 .map((v) => this.mapIntoValue(potentialSelectedIndex, v, null, undefined))107 );108 }109 /** Generate shrink values for first arbitrary when no context and no value was provided */110 private defaultShrinkForFirst(selectedIndex: number): Stream<Value<T>> {111 ++this.context.depth; // increase depth112 try {113 if (!this.mustFallbackToFirstInShrink(selectedIndex) || this.warbs[0].fallbackValue === undefined) {114 // Not applicable: no fallback to first arbitrary on shrink OR no hint to shrink without an initial value and context115 return Stream.nil();116 }117 } finally {118 --this.context.depth; // decrease depth (reset depth)119 }120 // The arbitrary at [0] accepts to shrink fallbackValue.default without any context (context=undefined)121 const rawShrinkValue = new Value(this.warbs[0].fallbackValue.default, undefined);122 return Stream.of(this.mapIntoValue(0, rawShrinkValue, null, undefined));123 }124 /** Extract the index of the generator that would have been able to gennrate the value */125 private canShrinkWithoutContextIndex(value: unknown): number {126 if (this.mustGenerateFirst()) {127 return this.warbs[0].arbitrary.canShrinkWithoutContext(value) ? 0 : -1;128 }129 try {130 ++this.context.depth; // increase depth131 for (let idx = 0; idx !== this.warbs.length; ++idx) {132 const warb = this.warbs[idx];133 if (warb.weight !== 0 && warb.arbitrary.canShrinkWithoutContext(value)) {134 return idx;135 }136 }137 return -1;138 } finally {139 --this.context.depth; // decrease depth (reset depth)140 }141 }142 /** Map the output of one of the children with the context of frequency */143 private mapIntoValue(144 idx: number,145 value: Value<T>,146 clonedMrngForFallbackFirst: Random | null,147 biasFactor: number | undefined148 ): Value<T> {149 const context: _FrequencyArbitraryContext<T> = {150 selectedIndex: idx,151 originalBias: biasFactor,152 originalContext: value.context,153 clonedMrngForFallbackFirst,154 };155 return new Value(value.value, context);156 }157 /** Generate using Arbitrary at index idx and safely handle depth context */158 private safeGenerateForIndex(mrng: Random, idx: number, biasFactor: number | undefined): Value<T> {159 ++this.context.depth; // increase depth160 try {161 const value = this.warbs[idx].arbitrary.generate(mrng, biasFactor);162 const clonedMrngForFallbackFirst = this.mustFallbackToFirstInShrink(idx) ? mrng.clone() : null;163 return this.mapIntoValue(idx, value, clonedMrngForFallbackFirst, biasFactor);164 } finally {165 --this.context.depth; // decrease depth (reset depth)166 }167 }168 /** Check if generating a value based on the first arbitrary is compulsory */169 private mustGenerateFirst(): boolean {170 return this.constraints.maxDepth <= this.context.depth;171 }172 /** Check if fallback on first arbitrary during shrinking is required */173 private mustFallbackToFirstInShrink(idx: number): boolean {174 return idx !== 0 && this.constraints.withCrossShrink && this.warbs[0].weight !== 0;175 }176 /** Compute the benefit for the current depth */177 private computeNegDepthBenefit(): number {178 const depthBias = this.constraints.depthBias;179 if (depthBias <= 0 || this.warbs[0].weight === 0) {180 return 0;181 }182 // We use a pow-based biased benefit as the deeper we go the more chance we have183 // to encounter thousands of instances of the current arbitrary.184 const depthBenefit = safeMathFloor(safeMathPow(1 + depthBias, this.context.depth)) - 1;185 // -0 has to be converted into 0 thus we call ||0186 return -safeMathMin(this.totalWeight * depthBenefit, safeMaxSafeInteger) || 0;187 }188}189/** @internal */190export type _Constraints = {191 withCrossShrink?: boolean;192 depthSize?: DepthSize;193 maxDepth?: number;194 depthIdentifier?: DepthIdentifier | string;195};196/** @internal */197type _SanitizedConstraints = {198 withCrossShrink: boolean;199 depthBias: number;200 maxDepth: number;201};202/** @internal */203interface _WeightedArbitrary<T> {204 weight: number;205 arbitrary: Arbitrary<T>;206 // If specified, the arbitrary must accept to shrink fallbackValue.default without any context207 fallbackValue?: { default: T };208}209/** @internal */210type _FrequencyArbitraryContext<T> = {211 selectedIndex: number;212 originalBias: number | undefined;213 originalContext: unknown;214 clonedMrngForFallbackFirst: Random | null;215 cachedGeneratedForFirst?: Value<T>;...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

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

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const potentialSelectedIndex = fc.integer(0, 5).potentialSelectedIndex();3console.log(potentialSelectedIndex);4const fc = require('fast-check');5const potentialSelectedIndex = fc.integer(0, 5).potentialSelectedIndex();6console.log(potentialSelectedIndex);7const fc = require('fast-check');8const potentialSelectedIndex = fc.integer(0, 5).potentialSelectedIndex();9console.log(potentialSelectedIndex);10const fc = require('fast-check');11const potentialSelectedIndex = fc.integer(0, 5).potentialSelectedIndex();12console.log(potentialSelectedIndex);13const fc = require('fast-check');14const potentialSelectedIndex = fc.integer(0, 5).potentialSelectedIndex();15console.log(potentialSelectedIndex);16const fc = require('fast-check');17const potentialSelectedIndex = fc.integer(0, 5).potentialSelectedIndex();18console.log(potentialSelectedIndex);19const fc = require('fast-check');20const potentialSelectedIndex = fc.integer(0, 5).potentialSelectedIndex();21console.log(potentialSelectedIndex);22const fc = require('fast-check');23const potentialSelectedIndex = fc.integer(0

Full Screen

Using AI Code Generation

copy

Full Screen

1const { Runner } = require('fast-check');2const { modelRun } = require('fast-check/lib/check/model/Runner');3const { ModelRun } = require('fast-check/lib/check/model/ModelRun');4const { cloneMethod } = require('fast-check/lib/check/model/commands/CloneArbitrary');5const { cloneMethod2 } = require('fast-check/lib/check/model/commands/CloneArbitrary2');6const { cloneMethod3 } = require('fast-check/lib/check/model/commands/CloneArbitrary3');7const { cloneMethod4 } = require('fast-check/lib/check/model/commands/CloneArbitrary4');8const { cloneMethod5 } = require('fast-check/lib/check/model/commands/CloneArbitrary5');9const { cloneMethod6 } = require('fast-check/lib/check/model/commands/CloneArbitrary6');10const { cloneMethod7 } = require('fast-check/lib/check/model/commands/CloneArbitrary7');11const { cloneMethod8 } = require('fast-check/lib/check/model/commands/CloneArbitrary8');12const { cloneMethod9 } = require('fast-check/lib/check/model/commands/CloneArbitrary9');13const { cloneMethod10 } = require('fast-check/lib/check/model/commands/CloneArbitrary10');14const { cloneMethod11 } = require('fast-check/lib/check/model/commands/CloneArbitrary11');15const { cloneMethod12 } = require('fast-check/lib/check/model/commands/CloneArbitrary12');16const { cloneMethod13 } = require('fast-check/lib/check/model/commands/CloneArbitrary13');17const { cloneMethod14 } = require('fast-check/lib/check/model/commands/CloneArbitrary14');18const { cloneMethod15 } = require('fast-check/lib/check/model/commands/CloneArbitrary15');19const { cloneMethod16 } = require('fast-check/lib/check/model/commands/CloneArbitrary16');20const { cloneMethod17 } = require('fast-check/lib/check/model/commands/CloneArbitrary17');21const { cloneMethod18 } = require('fast-check/lib/check/model/commands/CloneArbitrary18');22const { cloneMethod19 } = require('fast-check/lib/check/model/commands/CloneArbitrary19');23const { cloneMethod20 } = require('fast-check/lib/check/model/commands/CloneArbitrary20');24const { cloneMethod21 } = require

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require("fast-check");2const { potentialSelectedIndex } = require("fast-check/lib/check/runner/Runner");3const arb = fc.integer();4const predicate = (x) => {5 return x === 1;6};7const settings = {8};9const run = async () => {10 const result = await fc.assert(fc.property(arb, predicate), settings);11 const potentialIndex = potentialSelectedIndex(result);12 console.log("potentialIndex", potentialIndex);13};14run();15const fc = require("fast-check");16const { potentialSelectedIndex } = require("fast-check/lib/check/runner/Runner");17const arb = fc.integer();18const predicate = (x) => {19 return x === 1;20};21const settings = {22};23const run = async () => {24 const result = await fc.assert(fc.property(arb, predicate), settings);25 const potentialIndex = potentialSelectedIndex(result);26 console.log("potentialIndex", potentialIndex);27};28run();29const fc = require("fast-check");30const { potentialSelectedIndex } = require("fast-check/lib/check/runner/Runner");31const arb = fc.integer();32const predicate = (x) => {33 return x === 1;34};35const settings = {36};37const run = async () => {38 const result = await fc.assert(fc.property(arb, predicate), settings);39 const potentialIndex = potentialSelectedIndex(result);40 console.log("potentialIndex", potentialIndex);41};42run();

Full Screen

Using AI Code Generation

copy

Full Screen

1import * as fc from 'fast-check';2const runner = fc.runner();3const res = runner.run(4 fc.property(fc.integer(), fc.integer(), (a, b) => a + b === b + a),5 { seed: 42, numRuns: 10000 }6);7console.log(res.counterexample);8console.log(res.counterexampleIndex);9console.log(res.potentialSelectedIndex());10import * as fc from 'fast-check';11const runner = fc.runner();12const res = runner.run(13 fc.property(fc.integer(), fc.integer(), (a, b) => a + b === b + a),14 { seed: 42, numRuns: 10000 }15);16console.log(res.counterexample);17console.log(res.counterexampleIndex);18console.log(res.potentialSelectedIndex());19import * as fc from 'fast-check';20const runner = fc.runner();21const res = runner.run(22 fc.property(fc.integer(), fc.integer(), (a, b) => a + b === b + a),23 { seed: 42, numRuns: 10000 }24);25console.log(res.counterexample);26console.log(res.counterexampleIndex);27console.log(res.potentialSelectedIndex());28import * as fc from 'fast-check';29const runner = fc.runner();30const res = runner.run(31 fc.property(fc.integer(), fc.integer(), (a, b) => a + b === b + a),32 { seed: 42, numRuns: 10000 }33);34console.log(res.counterexample);35console.log(res.counterexampleIndex);36console.log(res.potentialSelectedIndex());37import * as fc from 'fast-check';38const runner = fc.runner();39const res = runner.run(40 fc.property(fc.integer

Full Screen

Using AI Code Generation

copy

Full Screen

1const { check } = require('fast-check');2const { potentialSelectedIndex } = require('fast-check/src/check/runner/Runner.js');3const { getArbitrary } = require('fast-check/src/check/arbitrary/definition/ArbitraryWithShrink.js');4const { integer } = require('fast-check');5const arb = integer(0, 100);6const predicate = (n) => n % 2 === 0;7const run = check(arb, predicate, { seed: 1, numRuns: 100 });8console.log(run.counterexample);9console.log(potentialSelectedIndex(run));10console.log(getArbitrary(arb).shrinkableFor(run.counterexample).value);11const { check } = require('fast-check');12const { potentialSelectedIndex } = require('fast-check/src/check/runner/Runner.js');13const { getArbitrary } = require('fast-check/src/check/arbitrary/definition/ArbitraryWithShrink.js');14const { integer } = require('fast-check');15const arb = integer(0, 100);16const predicate = (n) => n % 2 === 0;17const run = check(arb, predicate, { seed: 1, numRuns: 100 });18console.log(run.counterexample);19console.log(potentialSelectedIndex(run));20console.log(getArbitrary(arb).shrinkableFor(run.counterexample).value);21const { check } = require('fast-check');22const { potentialSelectedIndex } = require('fast-check/src/check/runner/Runner.js');23const { getArbitrary } = require('fast-check/src/check/arbitrary/definition/ArbitraryWithShrink.js');24const { integer } = require('fast-check');25const arb = integer(0, 100);26const predicate = (n) => n % 2 === 0;27const run = check(arb, predicate, { seed: 1, numRuns:

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