How to use maxInitialIterations method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

initial.spec.ts

Source:initial.spec.ts Github

copy

Full Screen

1import {2 RandomGenerator,3 unsafeUniformIntDistribution,4 xorshift128plus,5} from 'pure-rand'6export const decompPrime = (n: number): number[] => {7 // Quick implementation: the maximal number supported is 2**31-18 let done = false9 const factors: number[] = []10 while (!done) {11 done = true12 const stop = Math.sqrt(n)13 for (let i = 2; i <= stop; ++i) {14 if (n % i === 0) {15 factors.push(i)16 n = Math.floor(n / i)17 done = false18 break19 }20 }21 }22 return [...factors, n]23}24const MAX_INPUT = 6553625class Random {26 private readonly internalRandomGenerator: RandomGenerator27 constructor(randomGenerator: RandomGenerator) {28 this.internalRandomGenerator = randomGenerator29 }30 nextInt(min: number, max: number): number {31 return unsafeUniformIntDistribution(32 min,33 max,34 this.internalRandomGenerator35 )36 }37}38class NextValue<T> {39 readonly value: T40 constructor(value: T) {41 this.value = value42 }43}44abstract class NextArbitrary<T> {45 abstract generate(random: Random): NextValue<T>46 map<U>(mapper: (t: T) => U): NextArbitrary<U> {47 return new MapArbitrary(this, mapper)48 }49}50class MapArbitrary<T, U> extends NextArbitrary<U> {51 constructor(readonly arb: NextArbitrary<T>, readonly mapper: (t: T) => U) {52 super()53 }54 generate(random: Random): NextValue<U> {55 const g = this.arb.generate(random)56 return this.valueMapper(g)57 }58 private valueMapper(v: NextValue<T>): NextValue<U> {59 const value = this.mapper(v.value)60 return new NextValue(value)61 }62}63class IntegerArbitrary extends NextArbitrary<number> {64 constructor(private readonly min: number, private readonly max: number) {65 super()66 }67 generate(random: Random): NextValue<number> {68 return new NextValue(random.nextInt(this.min, this.max))69 }70}71type ArbsArray<T extends unknown[]> = { [K in keyof T]: NextArbitrary<T[K]> }72class TupleArbitrary<T extends unknown[]> extends NextArbitrary<T> {73 constructor(readonly arbs: ArbsArray<T>) {74 super()75 }76 generate(random: Random): NextValue<T> {77 const vs = [] as unknown as T & unknown[]78 for (const arb of this.arbs) {79 vs.push(arb.generate(random).value)80 }81 return new NextValue(vs)82 }83}84function nat(max: number) {85 return new IntegerArbitrary(0, max)86}87function integer(min: number, max: number) {88 return new IntegerArbitrary(min, max)89}90function char() {91 return new IntegerArbitrary(0x20, 0x7e).map((n) => String.fromCharCode(n))92}93interface INextProperty<T> {94 generate(random: Random): NextValue<T>95 run(v: T): Error | string | null96}97class Property<T> implements INextProperty<T> {98 constructor(99 readonly arbitrary: NextArbitrary<T>,100 readonly predicate: (v: T) => void | boolean101 ) {}102 generate(random: Random): NextValue<T> {103 return this.arbitrary.generate(random)104 }105 run(v: T): string | null {106 const out = this.predicate(v)107 // TODO: add PreconditionFailure108 return out == null || out === true109 ? null110 : 'Property failed by returning false'111 }112}113interface Parameters {114 numRuns?: number115}116interface RunDetails {117 failed: boolean118}119function reportRunDetails(out: RunDetails) {120 if (!out.failed) return121 throw new Error('Property failed')122}123function* toss<T>(124 property: INextProperty<T>,125 seed: number,126 random: (seed: number) => RandomGenerator127) {128 const randomGenerator = random(seed)129 while (true) {130 yield () => property.generate(new Random(randomGenerator))131 }132}133class Stream<T> {134 constructor(private readonly g: IterableIterator<T>) {}135 [Symbol.iterator](): IterableIterator<T> {136 return this.g137 }138 next(): IteratorResult<T> {139 return this.g.next()140 }141}142function stream<T>(g: IterableIterator<T>) {143 return new Stream(g)144}145function buildInitialValues<T>(146 valueProducers: IterableIterator<() => NextValue<T>>147) {148 return stream(valueProducers)149}150class SourceValuesIterator<T> implements IterableIterator<T> {151 constructor(152 readonly initialValues: IterableIterator<() => T>,153 private maxInitialIterations: number154 ) {}155 [Symbol.iterator](): IterableIterator<T> {156 return this157 }158 next(): IteratorResult<T> {159 if (--this.maxInitialIterations !== -1) {160 const n = this.initialValues.next()161 if (!n.done) {162 return { done: false, value: n.value() }163 }164 }165 return { done: true, value: undefined }166 }167}168class RunExecution<T> {169 private failed: boolean = false170 fail() {171 this.failed = true172 }173 toRunDetails(): RunDetails {174 return { failed: this.failed }175 }176}177class RunnerIterator<T> implements IterableIterator<T> {178 runExecution: RunExecution<T>179 constructor(readonly sourceValues: SourceValuesIterator<NextValue<T>>) {180 this.runExecution = new RunExecution()181 }182 [Symbol.iterator](): IterableIterator<T> {183 return this184 }185 next(): IteratorResult<T> {186 const nextValue = this.sourceValues.next()187 const something = nextValue.value188 if (nextValue.done) {189 return { done: true, value: undefined }190 }191 return { done: false, value: nextValue.value.value }192 }193 handleResult(result: string | null) {194 if (result != null) {195 this.runExecution.fail()196 }197 }198}199function runIt<T>(200 property: INextProperty<T>,201 sourceValues: SourceValuesIterator<NextValue<T>>202) {203 // RunnerIterator will just generate the next value from the SourceValuesIterator204 const runner = new RunnerIterator(sourceValues)205 for (const v of runner) {206 const out = property.run(v) as string | null207 runner.handleResult(out)208 }209 return runner.runExecution210}211function check<T>(property: INextProperty<T>, params: Parameters) {212 // calls `generate` on the property by passing the random generator.213 // this `generate` property will also call generate on the arbitrary by passing the same random generator.214 // this will return a generated NextValue depending on the type of the arbitrary215 const generator = toss(216 property,217 Date.now() ^ (Math.random() * 0x100000000),218 xorshift128plus219 )220 const initialValues = buildInitialValues(generator)221 // this SourceValuesIterator is controlling when to stop the generation of values222 const sourceValues = new SourceValuesIterator(223 initialValues,224 params.numRuns || 100225 )226 return runIt(property, sourceValues).toRunDetails()227}228function property<T0>(229 arb0: NextArbitrary<T0>,230 predicate: (t0: T0) => void | boolean231): INextProperty<[T0]>232function property<T0, T1>(233 arb0: NextArbitrary<T0>,234 arb1: NextArbitrary<T1>,235 predicate: (t0: T0, t1: T1) => void | boolean236): INextProperty<[T0, T1]>237function property<T>(...args: any): any {238 const arbs = args.slice(0, args.length - 1)239 const p = args[args.length - 1]240 const tuple = new TupleArbitrary(arbs)241 return new Property(tuple, (t) => p(...t))242}243function assert<T>(property: INextProperty<T>, params: Parameters) {244 const out = check(property, params)245 reportRunDetails(out)246}247describe('decampPrime', () => {248 it('should produce an array such that the product equals the input', () => {249 assert(250 property(nat(1000), (n) => {251 console.log({ n })252 const factors = decompPrime(n)253 const productOfFactors = factors.reduce((a, b) => a * b, 1)254 return productOfFactors === n255 }),256 {257 numRuns: 10,258 }259 )260 })261 it('should be able to decompose a product of two numbers', () => {262 assert(263 property(integer(2, MAX_INPUT), integer(2, MAX_INPUT), (a, b) => {264 console.log({ a, b })265 const n = a * b266 const factors = decompPrime(n)267 return factors.length >= 2268 }),269 {270 numRuns: 4,271 }272 )273 })274 it('should return one single char', () => {275 assert(276 property(char(), (s) => {277 console.log(s)278 return typeof s === 'string'279 }),280 {281 numRuns: 4,282 }283 )284 })...

Full Screen

Full Screen

derivables.ts

Source:derivables.ts Github

copy

Full Screen

1/* Jovian (c) 2020, License: MIT */2import { settingsInitialize, TypeToolsBase, TypeToolsExtension, TypeToolsExtensionData } from './type-tools';3import { DataImportable } from './data-importable';4import { PropertiesController, PropertiesControllerSettings } from './properties-controller';5import { Class, PartialCustom } from './type-transform';6import { ClassLineage } from './class-lineage';7import { Context } from './context';8import { typeFullName } from './upstream/common.iface';9export class DerivablesSettings extends PropertiesControllerSettings {10 static extensionDerivables = 'Derivables';11 extensionDerivables = DerivablesSettings.extensionDerivables;12 constructor(init?: Partial<DerivablesSettings>) {13 super(init);14 if (init) { Object.assign(this, init); }15 }16}17export interface DerivablesMetadata<T> {18 from?: PartialCustom<T, any>;19 derive: () => any;20}21export interface DerivablesOptions {22 derive?: boolean23}24export class DerivablesExtensionData implements TypeToolsExtensionData {25 rubric: { [propName: string]: { from: string[], longHand: boolean, derive:() => any; } };26 triggers: { [propName: string]: { list: string[], guard: {[propName:string]: boolean}} };27}28export class Derivables implements TypeToolsExtension {29 static maxInitialIterations = 5;30 static getExtensionData(target: any, settings = DerivablesSettings): DerivablesExtensionData {31 return TypeToolsBase.getExtension(target, settings.extensionDerivables, settings);32 }33 static typeCheck(target: any, settings = DerivablesSettings): boolean {34 return target && !!Derivables.getExtensionData(target, settings);35 }36 static implementOn(target: any, settings = DerivablesSettings): boolean {37 if (!TypeToolsBase.checkContext(Derivables)) { return false; }38 if (!Derivables.getExtensionData(target, settings)) {39 DataImportable.implementOn(target);40 PropertiesController.implementOn(target, settings);41 const pcExtension = PropertiesController.getExtensionData(target, settings);42 const extension: DerivablesExtensionData = { rubric: {}, triggers: {} };43 pcExtension.onpropertychanges.push((propName, oldValue, newValue, immediate) => {44 const trigger = extension.triggers[propName];45 if (!trigger) { return; }46 for (const targetDerivedPropName of trigger.list) {47 const rubric = extension.rubric[targetDerivedPropName];48 let result;49 if (rubric.longHand) { // long-hand doesn't require props;50 result = rubric.derive.apply(target);51 } else {52 result = rubric.derive.apply(target, rubric.from.map(a => target[a]));53 }54 if (result !== undefined) {55 target[targetDerivedPropName] = result;56 }57 }58 });59 TypeToolsBase.addExtension(target, settings.extensionDerivables, extension);60 }61 return true;62 }63 static of<T = any>(target: T, options: DerivablesOptions,64 deriveRubric: PartialCustom<T, ((...props:any[]) => any) | DerivablesMetadata<T>>,65 settings = DerivablesSettings) {66 if (!Derivables.implementOn(target, settings)) { return; }67 if (!options) { options = {}; }68 const extension = Derivables.getExtensionData(target, settings);69 const type = ClassLineage.typeOf(target);70 const cacheKeyPrefix = DerivablesSettings.extensionDerivables + '::' +71 (Context.current ? typeFullName(Context.current) + '::' : '');72 const derivablesKeys = Object.keys(deriveRubric);73 for (const propName of derivablesKeys) {74 const rubric = deriveRubric[propName];75 let cached = TypeToolsBase.typeCacheGet(type, cacheKeyPrefix + propName);76 let from;77 if (cached) {78 from = cached.from;79 } else {80 const longHand = (rubric as any).derive ? true : false;81 if (!longHand) {82 from = (rubric + '').split('\n')[0].split('(')[1].split(')')[0].split(',').map(a => a.trim());83 } else {84 from = Object.keys((rubric as DerivablesMetadata<T>).from);85 }86 cached = { from, longHand };87 const skel = TypeToolsBase.getSkeleton(type);88 for (const prop of from) {89 let msg = null;90 if (skel[prop] === undefined) {91 msg = `Derivable property '${propName}' cannot derive a non-member property '${prop}'`92 }93 if (prop === propName) {94 msg = `Derivable property '${propName}' cannot be derived from itself.'`95 }96 if (msg) {97 const e = new Error(msg);98 if (Context.throwErrors) { throw e; }99 from.length = 0;100 break;101 }102 }103 TypeToolsBase.typeCacheSet(type, cacheKeyPrefix + propName, cached); 104 }105 if (from.length > 0) {106 extension.rubric[propName] = {107 from: cached.from,108 longHand: cached.longHand,109 derive: cached.longHand ? rubric.derive : rubric110 };111 for (const sourcePropName of from) {112 let trigger = extension.triggers[sourcePropName];113 if (!trigger) { trigger = extension.triggers[sourcePropName] = { list:[], guard: {} }; }114 if (!trigger.guard[propName]) {115 trigger.list.push(propName);116 trigger.guard[propName] = true;117 }118 }119 }120 }121 // const descriptorsRubric: PartialCustom<T, Partial<PropertyControlLayer>> = {};122 // for (const propName of derivablesKeys) {123 // descriptorsRubric[propName] = { set(newValue, e) { e.stopPropagation(); } };124 // }125 // // EXTENSION_ORDER_DEF126 // const manageOptions: PropertiesManagementOptions = { alwaysFront: true, order: 1 };127 // PropertiesController.manage(target, manageOptions, descriptorsRubric, settings);128 // const managedProps = PropertiesController.getExtensionData(target, settings).managed;129 // for (const propName of derivablesKeys) {130 // if (managedProps[propName]) {131 // managedProps[propName].extension.derivables = true;132 // }133 // }134 // Initialize the derived ones135 let iteration = 0;136 let changed: any = { __first: true };137 while (Object.keys(changed).length > 0 && iteration < Derivables.maxInitialIterations) {138 changed = {};139 ++iteration;140 for (const targetDerivedPropName of derivablesKeys) {141 const rubric = extension.rubric[targetDerivedPropName];142 if (!rubric) { continue; }143 let result;144 if (rubric.longHand) { // long-hand doesn't require props;145 result = rubric.derive.apply(target);146 } else {147 result = rubric.derive.apply(target, rubric.from.map(a => target[a]));148 }149 if (target[targetDerivedPropName] !== result) {150 target[targetDerivedPropName] = result;151 changed[targetDerivedPropName] = true;152 }153 }154 }155 if (iteration >= Derivables.maxInitialIterations) { // something circular is going on.156 if (Context.throwErrors) {157 throw new Error(`Derivable keeps changing after ${iteration} iterations [keys=${Object.keys(changed).join(', ')}]. `);158 }159 }160 }161 settings: DerivablesSettings;162 constructor(settings?: Partial<DerivablesSettings>) {163 this.settings = settingsInitialize(DerivablesSettings, settings);164 }165 getExtensionData(target: any) { return Derivables.getExtensionData(target, this.settings as any); }166 typeCheck(target: any) { return Derivables.typeCheck(target, this.settings as any); }167 implementOn(target: any) { return Derivables.implementOn(target, this.settings as any); }...

Full Screen

Full Screen

SourceValuesIterator.ts

Source:SourceValuesIterator.ts Github

copy

Full Screen

1/**2 * Try to extract maxInitialIterations non-skipped values3 * with a maximal number of remainingSkips skipped values4 * from initialValues source5 * @internal6 */7export class SourceValuesIterator<Ts> implements IterableIterator<Ts> {8 constructor(9 readonly initialValues: IterableIterator<() => Ts>,10 private maxInitialIterations: number,11 private remainingSkips: number12 ) {}13 [Symbol.iterator](): IterableIterator<Ts> {14 return this;15 }16 next(): IteratorResult<Ts> {17 if (--this.maxInitialIterations !== -1 && this.remainingSkips >= 0) {18 const n = this.initialValues.next();19 if (!n.done) return { value: n.value(), done: false };20 }21 return { value: undefined, done: true };22 }23 skippedOne(): void {24 --this.remainingSkips;25 ++this.maxInitialIterations;26 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const {maxInitialIterations} = require('fast-check/lib/check/arbitrary/definition/Configuration');3const {maxInitialIterations: maxInitialIterations2} = require('fast-check/lib/check/arbitrary/definition/Configuration');4console.log(maxInitialIterations);5console.log(maxInitialIterations2);6const fc = require('fast-check');7const {maxInitialIterations} = require('fast-check/lib/check/arbitrary/definition/Configuration');8console.log(maxInitialIterations);9const fc = require('fast-check');10const {maxInitialIterations} = require('fast-check/lib/check/arbitrary/definition/Configuration');11console.log(maxInitialIterations);12const fc = require('fast-check');13const {maxInitialIterations} = require('fast-check/lib/check/arbitrary/definition/Configuration');14console.log(maxInitialIterations);15const fc = require('fast-check');16const {maxInitialIterations} = require('fast-check/lib/check/arbitrary/definition/Configuration');17console.log(maxInitialIterations);18const fc = require('fast-check');19const {maxInitialIterations} = require('fast-check/lib/check/arbitrary/definition/Configuration');20console.log(maxInitialIterations);21const fc = require('fast-check');22const {maxInitialIterations} = require('fast-check/lib/check/arbitrary/definition/Configuration');23console.log(maxInitialIterations);24const fc = require('fast-check');25const {maxInitialIterations} = require('fast-check/lib/check/arbitrary/definition/Configuration');26console.log(maxInitialIterations);

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const {maxInitialIterations} = require('fast-check/lib/check/arbitrary/definition/Configuration');3const {maxInitialIterations: maxInitialIterations2} = require('fast-check/lib/check/arbitrary/definition/ConfigurationArbitrary');4const max = maxInitialIterations();5const max2 = maxInitialIterations2();6console.log(max);7console.log(max2);8const fc = require('fast-check');9const {maxInitialIterations} = require('fast-check/lib/check/arbitrary/definition/Configuration');10const {maxInitialIterations: maxInitialIterations2} = require('fast-check/lib/check/arbitrary/definition/ConfigurationArbitrary');11const max = fc.property(fc.integer(), fc.integer(), (a, b) => {12 const max = maxInitialIterations();13 const max2 = maxInitialIterations2();14 console.log(max);15 console.log(max2);16 return true;17});18fc.assert(max);

Full Screen

Using AI Code Generation

copy

Full Screen

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

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require("fast-check");2const maxInitialIterations = fc.maxInitialIterations;3const arb = fc.integer();4const [result, numIterations] = maxInitialIterations(arb, 1000, 10);5console.log("result: " + result);6console.log("numIterations: " + numIterations);7const fc = require("fast-check");8const maxInitialIterations = fc.maxInitialIterations;9const arb = fc.integer();10const [result, numIterations] = maxInitialIterations(arb, 1000, 10);11console.log("result: " + result);12console.log("numIterations: " + numIterations);13const fc = require("fast-check");14const maxInitialIterations = fc.maxInitialIterations;15const arb = fc.integer();16const [result, numIterations] = maxInitialIterations(arb, 1000, 10);17console.log("result: " + result);18console.log("numIterations: " + numIterations);19const fc = require("fast-check");20const maxInitialIterations = fc.maxInitialIterations;21const arb = fc.integer();22const [result, numIterations] = maxInitialIterations(arb, 1000, 10);23console.log("result: " + result);24console.log("numIterations: " + numIterations);25const fc = require("fast-check");26const maxInitialIterations = fc.maxInitialIterations;27const arb = fc.integer();28const [result, numIterations] = maxInitialIterations(arb, 1000, 10);29console.log("result: " + result);30console.log("numIterations: " + numIterations);31const fc = require("fast-check");32const maxInitialIterations = fc.maxInitialIterations;33const arb = fc.integer();34const [result, numIterations] = maxInitialIterations(arb, 1000, 10);35console.log("result: " + result);36console.log("numIterations: " + num

Full Screen

Using AI Code Generation

copy

Full Screen

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

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require("fast-check");2const { maxInitialIterations } = require("fast-check");3const maxIterations = maxInitialIterations(10);4describe("test3", () => {5 it("test3", () => {6 fc.assert(7 fc.property(fc.integer(), fc.integer(), (a, b) => {8 console.log("maxIterations: ", maxIterations);9 console.log("a: ", a);10 console.log("b: ", b);11 return a + b === b + a;12 }),13 { numRuns: maxIterations }14 );15 });16});17const fc = require("fast-check");18const { maxInitialIterations } = require("fast-check");19const maxIterations = maxInitialIterations(10);20describe("test4", () => {21 it("test4", () => {22 fc.assert(23 fc.property(fc.integer(), fc.integer(), (a, b) => {24 console.log("maxIterations: ", maxIterations);25 console.log("a: ", a);26 console.log("b: ", b);27 return a + b === b + a;28 }),29 { numRuns: maxIterations }30 );31 });32});33const fc = require("fast-check");34const { maxInitialIterations } = require("fast-check");35const maxIterations = maxInitialIterations(10);36describe("test5", () => {37 it("test5", () => {38 fc.assert(39 fc.property(fc.integer(), fc.integer(), (a, b) => {40 console.log("maxIterations:

Full Screen

Using AI Code Generation

copy

Full Screen

1const { maxInitialIterations } = require("fast-check");2const test1 = require("./test1");3const test2 = require("./test2");4const maxIterations = maxInitialIterations(1000);5const sample = test1.sample(test1.arb, { numRuns: 1000, seed: 1 });6const sample2 = test2.sample(test2.arb, { numRuns: 100, seed: 1 });7console.log("sample1: ", sample);8console.log("sample2: ", sample2);9console.log("maxIterations: ", maxIterations);

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