Best JavaScript code snippet using fast-check-monorepo
sparseArray.ts
Source:sparseArray.ts
1import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';2import { Array, safeMap, safeSlice } from '../utils/globals';3import { tuple } from './tuple';4import { uniqueArray } from './uniqueArray';5import { restrictedIntegerArbitraryBuilder } from './_internals/builders/RestrictedIntegerArbitraryBuilder';6import { DepthIdentifier } from './_internals/helpers/DepthContext';7import {8 maxGeneratedLengthFromSizeForArbitrary,9 MaxLengthUpperBound,10 SizeForArbitrary,11} from './_internals/helpers/MaxLengthFromMinLength';12const safeMathMin = Math.min;13const safeMathMax = Math.max;14const safeArrayIsArray = Array.isArray;15const safeObjectEntries = Object.entries;16/**17 * Constraints to be applied on {@link sparseArray}18 * @remarks Since 2.13.019 * @public20 */21export interface SparseArrayConstraints {22 /**23 * Upper bound of the generated array size (maximal size: 4294967295)24 * @remarks Since 2.13.025 */26 maxLength?: number;27 /**28 * Lower bound of the number of non-hole elements29 * @remarks Since 2.13.030 */31 minNumElements?: number;32 /**33 * Upper bound of the number of non-hole elements34 * @remarks Since 2.13.035 */36 maxNumElements?: number;37 /**38 * When enabled, all generated arrays will either be the empty array or end by a non-hole39 * @remarks Since 2.13.040 */41 noTrailingHole?: boolean;42 /**43 * Define how large the generated values should be (at max)44 * @remarks Since 2.22.045 */46 size?: SizeForArbitrary;47 /**48 * When receiving a depth identifier, the arbitrary will impact the depth49 * attached to it to avoid going too deep if it already generated lots of items.50 *51 * In other words, if the number of generated values within the collection is large52 * then the generated items will tend to be less deep to avoid creating structures a lot53 * larger than expected.54 *55 * For the moment, the depth is not taken into account to compute the number of items to56 * define for a precise generate call of the array. Just applied onto eligible items.57 *58 * @remarks Since 2.25.059 */60 depthIdentifier?: DepthIdentifier | string;61}62/** @internal */63function extractMaxIndex(indexesAndValues: [number, unknown][]) {64 let maxIndex = -1;65 for (let index = 0; index !== indexesAndValues.length; ++index) {66 maxIndex = safeMathMax(maxIndex, indexesAndValues[index][0]);67 }68 return maxIndex;69}70/** @internal */71function arrayFromItems<T>(length: number, indexesAndValues: [number, T][]) {72 const array = Array<T>(length);73 for (let index = 0; index !== indexesAndValues.length; ++index) {74 const it = indexesAndValues[index];75 if (it[0] < length) array[it[0]] = it[1];76 }77 return array;78}79/**80 * For sparse arrays of values coming from `arb`81 * @param arb - Arbitrary used to generate the values inside the sparse array82 * @param constraints - Constraints to apply when building instances83 * @remarks Since 2.13.084 * @public85 */86export function sparseArray<T>(arb: Arbitrary<T>, constraints: SparseArrayConstraints = {}): Arbitrary<T[]> {87 const {88 size,89 minNumElements = 0,90 maxLength = MaxLengthUpperBound,91 maxNumElements = maxLength, // cap maxNumElements to maxLength92 noTrailingHole,93 depthIdentifier,94 } = constraints;95 const maxGeneratedNumElements = maxGeneratedLengthFromSizeForArbitrary(96 size,97 minNumElements,98 maxNumElements,99 constraints.maxNumElements !== undefined100 );101 const maxGeneratedLength = maxGeneratedLengthFromSizeForArbitrary(102 size,103 maxGeneratedNumElements,104 maxLength,105 constraints.maxLength !== undefined106 );107 if (minNumElements > maxLength) {108 throw new Error(`The minimal number of non-hole elements cannot be higher than the maximal length of the array`);109 }110 if (minNumElements > maxNumElements) {111 throw new Error(`The minimal number of non-hole elements cannot be higher than the maximal number of non-holes`);112 }113 const resultedMaxNumElements = safeMathMin(maxNumElements, maxLength);114 const resultedSizeMaxNumElements = constraints.maxNumElements !== undefined || size !== undefined ? size : '=';115 const maxGeneratedIndexAuthorized = safeMathMax(maxGeneratedLength - 1, 0); // just preventing special case for maxGeneratedLength=0116 const maxIndexAuthorized = safeMathMax(maxLength - 1, 0); // just preventing special case for maxLength=0117 const sparseArrayNoTrailingHole = uniqueArray(118 tuple(restrictedIntegerArbitraryBuilder(0, maxGeneratedIndexAuthorized, maxIndexAuthorized), arb),119 {120 size: resultedSizeMaxNumElements,121 minLength: minNumElements,122 maxLength: resultedMaxNumElements,123 selector: (item) => item[0],124 depthIdentifier,125 }126 ).map(127 (items) => {128 // When maxLength=0 (implies resultedMaxNumElements=0) we will have items=[] leading to lastIndex=-1129 // resulting in an empty array130 const lastIndex = extractMaxIndex(items);131 return arrayFromItems(lastIndex + 1, items);132 },133 (value: unknown): [number, T][] => {134 if (!safeArrayIsArray(value)) {135 throw new Error('Not supported entry type');136 }137 if (noTrailingHole && value.length !== 0 && !(value.length - 1 in value)) {138 throw new Error('No trailing hole');139 }140 return safeMap(safeObjectEntries(value as T[]), (entry): [number, T] => [Number(entry[0]), entry[1]]);141 }142 );143 if (noTrailingHole || maxLength === minNumElements) {144 return sparseArrayNoTrailingHole;145 }146 return tuple(147 sparseArrayNoTrailingHole,148 restrictedIntegerArbitraryBuilder(minNumElements, maxGeneratedLength, maxLength)149 ).map(150 (data) => {151 const sparse = data[0];152 const targetLength = data[1];153 if (sparse.length >= targetLength) {154 return sparse;155 }156 const longerSparse = safeSlice(sparse);157 longerSparse.length = targetLength;158 return longerSparse;159 },160 (value: unknown): [T[], number] => {161 if (!safeArrayIsArray(value)) {162 throw new Error('Not supported entry type');163 }164 return [value, value.length];165 }166 );...
Using AI Code Generation
1const fc = require("fast-check");2console.log(fc.resultedMaxNumElements(10, 2, 2));3const fc = require("fast-check");4console.log(fc.resultedMaxNumElements(10, 2, 2));5const fc = require("fast-check");6console.log(fc.resultedMaxNumElements(10, 2, 2));7const fc = require("fast-check");8console.log(fc.resultedMaxNumElements(10, 2, 2));9const fc = require("fast-check");10console.log(fc.resultedMaxNumElements(10, 2, 2));11const fc = require("fast-check");12console.log(fc.resultedMaxNumElements(10, 2, 2));13const fc = require("fast-check");14console.log(fc.resultedMaxNumElements(10, 2, 2));15const fc = require("fast-check");16console.log(fc.resultedMaxNumElements(10, 2, 2));17const fc = require("fast-check");18console.log(fc.resultedMaxNumElements(10, 2, 2));19const fc = require("fast-check");20console.log(fc.resultedMaxNumElements(10, 2, 2));21const fc = require("fast-check");22console.log(fc.resultedMaxNumElements(10
Using AI Code Generation
1const fc = require('fast-check');2const assert = require('assert');3const { Runner } = require('fast-check/src/check/runner/Runner');4const runner = new Runner();5const maxNumElements = runner.resultedMaxNumElements(100, 100, 100, 100, 100);6assert.equal(maxNumElements, 100);7const fc = require('fast-check');8const assert = require('assert');9const { Runner } = require('fast-check/src/check/runner/Runner');10const runner = new Runner();11const maxNumElements = runner.resultedMaxNumElements(100, 100, 100, 100, 100, 100);12assert.equal(maxNumElements, 100);13const fc = require('fast-check');14const assert = require('assert');15const { Runner } = require('fast-check/src/check/runner/Runner');16const runner = new Runner();17const maxNumElements = runner.resultedMaxNumElements(100, 100, 100, 100, 100, 100, 100);18assert.equal(maxNumElements, 100);19const fc = require('fast-check');20const assert = require('assert');21const { Runner } = require('fast-check/src/check/runner/Runner');22const runner = new Runner();23const maxNumElements = runner.resultedMaxNumElements(100, 100, 100, 100, 100, 100, 100, 100);24assert.equal(maxNumElements, 100);25const fc = require('fast-check');26const assert = require('assert');27const { Runner } = require('fast-check/src/check/runner/Runner');28const runner = new Runner();29const maxNumElements = runner.resultedMaxNumElements(100
Using AI Code Generation
1const { resultedMaxNumElements } = require("fast-check");2const maxNumElements = resultedMaxNumElements();3console.log("maxNumElements = " + maxNumElements);4const { resultedMaxDepth } = require("fast-check");5const maxDepth = resultedMaxDepth();6console.log("maxDepth = " + maxDepth);7const { resultedMaxSkips } = require("fast-check");8const maxSkips = resultedMaxSkips();9console.log("maxSkips = " + maxSkips);10const { resultedMaxTime } = require("fast-check");11const maxTime = resultedMaxTime();12console.log("maxTime = " + maxTime);13const { resultedSeed } = require("fast-check");14const seed = resultedSeed();15console.log("seed = " + seed);16const { resultedPath } = require("fast-check");
Using AI Code Generation
1import { array } from 'fast-check';2import { property } from 'fast-check';3const maxNumElements = 10;4const arrayArbitrary = array(array(maxNumElements));5console.log(arrayArbitrary.resultedMaxNumElements);6import { array } from 'fast-check';7import { property } from 'fast-check';8const maxNumElements = 10;9const arrayArbitrary = array(array(maxNumElements));10console.log(arrayArbitrary.resultedMaxNumElements);11import { array } from 'fast-check';12import { property } from 'fast-check';13const maxNumElements = 10;14const arrayArbitrary = array(array(maxNumElements));15console.log(arrayArbitrary.resultedMaxNumElements);16import { array } from 'fast-check';17import { property } from 'fast-check';18const maxNumElements = 10;19const arrayArbitrary = array(array(maxNumElements));20console.log(arrayArbitrary.resultedMaxNumElements);21import { array } from 'fast-check';22import { property } from 'fast-check';23const maxNumElements = 10;24const arrayArbitrary = array(array(maxNumElements));25console.log(arrayArbitrary.resultedMaxNumElements);26import { array } from 'fast-check';27import { property } from 'fast-check';28const maxNumElements = 10;29const arrayArbitrary = array(array(maxNumElements));30console.log(arrayArbitrary.resultedMaxNumElements);
Using AI Code Generation
1const { array } = require('fast-check');2const maxNumElements = 1000;3const maxStringLength = 1000;4const stringArray = array(string(), maxNumElements, maxStringLength);5console.log(stringArray.resultedMaxNumElements());6const { array } = require('fast-check');7const maxNumElements = 1000;8const maxStringLength = 1000;9const stringArray = array(string(), maxNumElements, maxStringLength);10console.log(stringArray.resultedMaxNumElements());11const { array } = require('fast-check');12const maxNumElements = 1000;13const maxStringLength = 1000;14const stringArray = array(string(), maxNumElements, maxStringLength);15console.log(stringArray.resultedMaxNumElements());16const { array } = require('fast-check');17const maxNumElements = 1000;18const maxStringLength = 1000;19const stringArray = array(string(), maxNumElements, maxStringLength);20console.log(stringArray.resultedMaxNumElements());21const { array } = require('fast-check');22const maxNumElements = 1000;23const maxStringLength = 1000;24const stringArray = array(string(), maxNumElements, maxStringLength);25console.log(stringArray.resultedMaxNumElements());26const { array } = require('fast-check');27const maxNumElements = 1000;28const maxStringLength = 1000;29const stringArray = array(string(), maxNumElements, maxStringLength);30console.log(stringArray.resultedMaxNumElements());
Using AI Code Generation
1var fc = require('fast-check');2var randomArray = fc.sample(fc.array(fc.integer()), 1, {numRuns: 1, verbose: 0});3console.log(randomArray);4var fc = require('fast-check');5var randomArray = fc.sample(fc.array(fc.integer()), 1, {numRuns: 1, verbose: 0});6console.log(randomArray);7var fc = require('fast-check');8var randomArray = fc.sample(fc.array(fc.integer()), 1, {numRuns: 1, verbose: 0});9console.log(randomArray);10var fc = require('fast-check');11var randomArray = fc.sample(fc.array(fc.integer()), 1, {numRuns: 1, verbose: 0});12console.log(randomArray);13var fc = require('fast-check');14var randomArray = fc.sample(fc.array(fc.integer()), 1, {
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!