How to use clonedMrngForFallbackFirst 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 { clonedMrngForFallbackFirst } = require('fast-check-monorepo');2const mrng = clonedMrngForFallbackFirst();3console.log(mrng.nextInt());4const { clonedMrngForFallbackFirst } = require('fast-check-monorepo');5const mrng = clonedMrngForFallbackFirst();6console.log(mrng.nextInt());7const { clonedMrngForFallbackFirst } = require('fast-check-monorepo');8const mrng = clonedMrngForFallbackFirst();9const randomNumbers = [];10for (let i = 0; i < 100; i++) {11 randomNumbers.push(mrng.nextInt());12}13module.exports = randomNumbers;14const randomNumbers = require('./​test.js');15console.log(randomNumbers[0]);16const randomNumbers = require('./​test.js');17console.log(randomNumbers[1]);

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { clonedMrngForFallbackFirst } = require('fast-check-monorepo');3console.log('clonedMrngForFallbackFirst', clonedMrngForFallbackFirst);4const clonedMrng = clonedMrngForFallbackFirst();5console.log('clonedMrng', clonedMrng);6console.log('clonedMrng.nextInt', clonedMrng.nextInt);7clonedMrng { nextInt: [Function: nextInt], nextInt: [Function: nextInt] }

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require("fast-check");2const { cloneMethod } = require("fast-check-monorepo");3const { clonedMrngForFallbackFirst } = cloneMethod();4const { mrng } = clonedMrngForFallbackFirst();5const { cloneMethod: cloneMethod2 } = require("fast-check-monorepo");6const { clonedMrngForFallbackFirst: clonedMrngForFallbackFirst2 } =7 cloneMethod2();8const { mrng: mrng2 } = clonedMrngForFallbackFirst2();9const { cloneMethod: cloneMethod3 } = require("fast-check-monorepo");10const { clonedMrngForFallbackFirst: clonedMrngForFallbackFirst3 } =11 cloneMethod3();12const { mrng: mrng3 } = clonedMrngForFallbackFirst3();13const fc = require("fast-check");14const { cloneMethod } = require("fast-check-monorepo");15const { clonedMrngForFallbackFirst } = cloneMethod();16const { mrng } = clonedMrngForFallbackFirst();17const { cloneMethod: cloneMethod2 } = require("fast-check-monorepo");18const { clonedMrngForFallbackFirst: clonedMrngForFallbackFirst2 } =19 cloneMethod2();20const { mrng: mrng2 } = clonedMrngForFallbackFirst2();21const { cloneMethod: cloneMethod3 } = require("fast-check-monorepo");22const { clonedMrngForFallbackFirst: clonedMrngForFallbackFirst3 } =23 cloneMethod3();24const { mrng: mrng3 } = clonedMrngForFallbackFirst3();25const fc = require("fast-check");26const { cloneMethod } = require("fast-check-monorepo");27const { clonedMrngForFallbackFirst } = cloneMethod();28const { mrng } = clonedMrngForFallbackFirst();29const { cloneMethod: cloneMethod2 } = require("fast-check-monorepo");30const { clonedMrngForFallbackFirst: clonedMrngForFallbackFirst2 } =31 cloneMethod2();32const { mrng: mrng2 } = clonedMrngForFallbackFirst2();33const { cloneMethod: cloneMethod3 }

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { clonedMrngForFallbackFirst } = require('fast-check-monorepo');3const { mrng } = clonedMrngForFallbackFirst();4const fc = require('fast-check');5const { clonedMrngForFallbackFirst } = require('fast-check-monorepo');6const { mrng } = clonedMrngForFallbackFirst();7const fc = require('fast-check');8const { clonedMrngForFallbackFirst } = require('fast-check-monorepo');9const { mrng } = clonedMrngForFallbackFirst();

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { mrng } = require('fast-check/​lib/​check/​arbitrary/​helpers/​Random');3const { fallbackFirst } = require('fast-check/​lib/​check/​arbitrary/​helpers/​FallbackValue');4const clonedMrngForFallbackFirst = mrng.clone();5console.log("clonedMrngForFallbackFirst: ", clonedMrngForFallbackFirst);6console.log("fallbackFirst: ", fallbackFirst);7console.log("fallbackFirst(clonedMrngForFallbackFirst, 1, 2): ", fallbackFirst(clonedMrngForFallbackFirst, 1, 2));

Full Screen

Blogs

Check out the latest blogs from LambdaTest on this topic:

Pair testing strategy in an Agile environment

Pair testing can help you complete your testing tasks faster and with higher quality. But who can do pair testing, and when should it be done? And what form of pair testing is best for your circumstance? Check out this blog for more information on how to conduct pair testing to optimize its benefits.

Continuous Integration explained with jenkins deployment

Continuous integration is a coding philosophy and set of practices that encourage development teams to make small code changes and check them into a version control repository regularly. Most modern applications necessitate the development of code across multiple platforms and tools, so teams require a consistent mechanism for integrating and validating changes. Continuous integration creates an automated way for developers to build, package, and test their applications. A consistent integration process encourages developers to commit code changes more frequently, resulting in improved collaboration and code quality.

Acquiring Employee Support for Change Management Implementation

Enterprise resource planning (ERP) is a form of business process management software—typically a suite of integrated applications—that assists a company in managing its operations, interpreting data, and automating various back-office processes. The introduction of a new ERP system is analogous to the introduction of a new product into the market. If the product is not handled appropriately, it will fail, resulting in significant losses for the business. Most significantly, the employees’ time, effort, and morale would suffer as a result of the procedure.

Best 13 Tools To Test JavaScript Code

Unit and functional testing are the prime ways of verifying the JavaScript code quality. However, a host of tools are available that can also check code before or during its execution in order to test its quality and adherence to coding standards. With each tool having its unique features and advantages contributing to its testing capabilities, you can use the tool that best suits your need for performing JavaScript testing.

Keeping Quality Transparency Throughout the organization

In general, software testers have a challenging job. Software testing is frequently the final significant activity undertaken prior to actually delivering a product. Since the terms “software” and “late” are nearly synonymous, it is the testers that frequently catch the ire of the whole business as they try to test the software at the end. It is the testers who are under pressure to finish faster and deem the product “release candidate” before they have had enough opportunity to be comfortable. To make matters worse, if bugs are discovered in the product after it has been released, everyone looks to the testers and says, “Why didn’t you spot those bugs?” The testers did not cause the bugs, but they must bear some of the guilt for the bugs that were disclosed.

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