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 { maxIndexAuthorized } = require('fast-check-monorepo');2console.log(maxIndexAuthorized(3, 3));3const { maxIndexAuthorized } = require('fast-check-monorepo');4console.log(maxIndexAuthorized(3, 3));5const { maxIndexAuthorized } = require('./fast-check-monorepo');6const { maxIndexAuthorized } = require('./fast-check-monorepo/dist');7const { maxIndexAuthorized } = require('./fast-check-monorepo/dist/src');8const { maxIndexAuthorized } = require('./fast-check-monorepo/dist/src/arbitrary');9const { maxIndexAuthorized } = require('./fast-check-monorepo/dist/src/arbitrary/_internals');10const { maxIndexAuthorized } = require('./fast-check-monorepo/dist/src/arbitrary/_internals/helpers');11const { maxIndexAuthorized } = require('./fast-check-monorepo/dist/src/arbitrary/_internals/helpers/ArrayInt64');
Using AI Code Generation
1const { maxIndexAuthorized } = require('fast-check-monorepo');2const maxIndex = maxIndexAuthorized();3console.log(maxIndex);4const { maxIndexAuthorized } = require('fast-check-monorepo');5const maxIndex = maxIndexAuthorized();6console.log(maxIndex);7const { maxIndexAuthorized } = require('fast-check-monorepo');8const maxIndex = maxIndexAuthorized();9console.log(maxIndex);10const { maxIndexAuthorized } = require('fast-check-monorepo');11const maxIndex = maxIndexAuthorized();12console.log(maxIndex);13const { maxIndexAuthorized } = require('fast-check-monorepo');14const maxIndex = maxIndexAuthorized();15console.log(maxIndex);16const { maxIndexAuthorized } = require('fast-check-monorepo');17const maxIndex = maxIndexAuthorized();18console.log(maxIndex);19const { maxIndexAuthorized } = require('fast-check-monorepo');20const maxIndex = maxIndexAuthorized();21console.log(maxIndex);22const { maxIndexAuthorized } = require('fast-check-monorepo');23const maxIndex = maxIndexAuthorized();24console.log(maxIndex);25const { maxIndexAuthorized } = require('fast-check-monorepo');26const maxIndex = maxIndexAuthorized();27console.log(maxIndex);28const { maxIndexAuthorized } = require('fast-check-monorepo');29const maxIndex = maxIndexAuthorized();30console.log(maxIndex);
Using AI Code Generation
1const { maxIndexAuthorized } = require('fast-check-monorepo');2console.log(maxIndexAuthorized);3console.log(maxIndexAuthorized([1, 2, 3, 4, 5, 6], 5));4const { maxIndexAuthorized } = require('fast-check-monorepo');5console.log(maxIndexAuthorized);6console.log(maxIndexAuthorized([1, 2, 3, 4, 5, 6], 5));7const { maxIndexAuthorized } = require('fast-check-monorepo');8console.log(maxIndexAuthorized);9console.log(maxIndexAuthorized([1, 2, 3, 4, 5, 6], 5));10const { maxIndexAuthorized } = require('fast-check-monorepo');11console.log(maxIndexAuthorized);12console.log(maxIndexAuthorized([1, 2, 3, 4, 5, 6], 5));13const { maxIndexAuthorized } = require('fast-check-monorepo');14console.log(maxIndexAuthorized);15console.log(maxIndexAuthorized([1, 2, 3, 4, 5, 6], 5));16const { maxIndexAuthorized } = require('fast-check-monorepo');17console.log(maxIndexAuthorized);18console.log(maxIndexAuthorized([1, 2, 3, 4, 5, 6], 5));19const { maxIndexAuthorized } = require('fast-check-monorepo');20console.log(maxIndexAuthorized);21console.log(maxIndexAuthorized([1, 2, 3, 4, 5, 6], 5));22const { maxIndexAuthorized } = require('fast-check-monorepo');23console.log(maxIndexAuthorized
Using AI Code Generation
1const { maxIndexAuthorized } = require("fast-check-monorepo");2console.log(maxIndexAuthorized(3, 4));3console.log(maxIndexAuthorized(4, 4));4const { maxIndexAuthorized } = require("fast-check-monorepo");5console.log(maxIndexAuthorized(3, 4));6console.log(maxIndexAuthorized(4, 4));
Using AI Code Generation
1const fc = require('fast-check');2const maxIndexAuthorized = require('fast-check-monorepo').maxIndexAuthorized;3const maxIndexAuthorized = require('fast-check-monorepo').maxIndexAuthorized;4const maxIndexAuthorized = require('fast-check-monorepo').maxIndexAuthorized;5const maxIndexAuthorized = require('fast-check-monorepo').maxIndexAuthorized;6const maxIndexAuthorized = require('fast-check-monorepo').maxIndexAuthorized;7const maxIndexAuthorized = require('fast-check-monorepo').maxIndexAuthorized;8const maxIndexAuthorized = require('fast-check-monorepo').maxIndexAuthorized;9const maxIndexAuthorized = require('fast-check-monorepo').maxIndexAuthorized;10const maxIndexAuthorized = require('fast-check-monorepo').maxIndexAuthorized;11const maxIndexAuthorized = require('fast-check-monorepo').maxIndexAuthorized;12const maxIndexAuthorized = require('fast-check-monorepo').maxIndexAuthorized;13const maxIndexAuthorized = require('fast-check-monorepo').maxIndexAuthorized;14const maxIndexAuthorized = require('fast-check-monorepo').maxIndexAuthorized;15const maxIndexAuthorized = require('fast-check-monorepo').maxIndexAuthorized;
Using AI Code Generation
1const fc = require('fast-check');2const { maxIndexAuthorized } = require('fast-check-monorepo');3const maxIndexAuthorizedProperty = fc.property(4 fc.array(fc.nat()),5 fc.nat(),6 (array, index) => {7 const maxIndex = maxIndexAuthorized(array, index);8 return maxIndex >= 0 && maxIndex < array.length;9 }10);11fc.assert(maxIndexAuthorizedProperty, { verbose: true });
Using AI Code Generation
1const {maxIndexAuthorized} = require('fast-check');2const {fc} = require('fast-check');3const myArbitrary = fc.array(fc.integer());4fc.assert(fc.property(myArbitrary, (myArray) => {5 return myArray.length <= maxIndexAuthorized(myArray);6}));7console.log('test3.js finished.');8const {maxIndexAuthorized} = require('fast-check');9const {fc} = require('fast-check');10const myArbitrary = fc.array(fc.integer());11fc.assert(fc.property(myArbitrary, (myArray) => {12 return myArray.length <= maxIndexAuthorized(myArray);13}));14console.log('test4.js finished.');15const {maxIndexAuthorized} = require('fast-check');16const {fc} = require('fast-check');17const myArbitrary = fc.array(fc.integer());18fc.assert(fc.property(myArbitrary, (myArray) => {19 return myArray.length <= maxIndexAuthorized(myArray);20}));21console.log('test5.js finished.');22const {maxIndexAuthorized} = require('fast-check');23const {fc} = require('fast-check');24const myArbitrary = fc.array(fc.integer());25fc.assert(fc.property(myArbitrary, (myArray) => {26 return myArray.length <= maxIndexAuthorized(myArray);27}));28console.log('test6.js finished.');29const {maxIndexAuthorized} = require('fast-check');30const {fc} = require('fast-check');31const myArbitrary = fc.array(fc.integer());32fc.assert(fc.property(myArbitrary, (myArray) => {33 return myArray.length <= maxIndexAuthorized(myArray);34}));35console.log('test7.js finished.');36const {maxIndexAuthorized} = require('fast-check');37const {fc} = require('fast-check');
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!!