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");2const {longerSparse} = require("fast-check-monorepo");3const {longerSparse2} = require("fast-check-monorepo");4const {longerSparse3} = require("fast-check-monorepo");5const {longerSparse4} = require("fast-check-monorepo");6const {longerSparse5} = require("fast-check-monorepo");7const {longerSparse6} = require("fast-check-monorepo");8const {longerSparse7} = require("fast-check-monorepo");9const {longerSparse8} = require("fast-check-monorepo");10const {longerSparse9} = require("fast-check-monorepo");11const {longerSparse10} = require("fast-check-monorepo");12const {longerSparse11} = require("fast-check-monorepo");13const {longerSparse12} = require("fast-check-monorepo");14const {longerSparse13} = require("fast-check-monorepo");15const {longerSparse14} = require("fast-check-monorepo");16const {longerSparse15} = require("fast-check-monorepo");17const {longerSparse16} = require("fast-check-monorepo");18const {longerSparse17} = require("fast-check-monorepo");19const {longerSparse18} = require("fast-check-monorepo");20const {longerSparse19} = require("fast-check-monorepo");21const {longerSparse20} = require("fast-check-monorepo");22const {longerSparse21} = require("fast-check-monorepo");23const {longerSparse22} = require("fast-check-monorepo");24const {longerSparse23} = require("fast-check-monorepo");25const {longerSparse24} = require("fast-check-monorepo");26const {longerSparse25} = require("fast-check-monorepo");27const {longerSparse26} = require("fast-check-monorepo");28const {longerSparse27} = require("fast-check-monorepo");29const {longerSparse28} = require("fast-check-monorepo");30const {longerSparse29} = require("fast-check-monorepo");31const {longerSparse30} = require("fast-check-monorepo");32const {longerSparse31} = require("fast
Using AI Code Generation
1const fc = require("fast-check");2const { longerSparse } = require("fast-check-monorepo");3fc.assert(4 fc.property(longerSparse(), (a) => {5 return true;6 })7);8const fc = require("fast-check");9const { shorterSparse } = require("fast-check-monorepo");10fc.assert(11 fc.property(shorterSparse(), (a) => {12 return true;13 })14);15const fc = require("fast-check");16const { sparse } = require("fast-check-monorepo");17fc.assert(18 fc.property(sparse(), (a) => {19 return true;20 })21);22const fc = require("fast-check");23const { sparse } = require("fast-check-monorepo");24fc.assert(25 fc.property(sparse(), (a) => {26 return true;27 })28);29const fc = require("fast-check");30const { sparse } = require("fast-check-monorepo");31fc.assert(32 fc.property(sparse(), (a) => {33 return true;34 })35);36const fc = require("fast-check");37const { sparse } = require("fast-check-monorepo");38fc.assert(39 fc.property(sparse(), (a) => {40 return true;41 })42);43const fc = require("fast-check");44const { sparse } = require("fast-check-monorepo");45fc.assert(46 fc.property(sparse(), (a) => {47 return true;48 })49);50const fc = require("fast-check");51const { sparse } = require("fast-check-monorepo");52fc.assert(53 fc.property(sparse(), (a) => {54 return true;55 })56);
Using AI Code Generation
1const fc = require('fast-check');2const {longerSparse} = require('fast-check/lib/arbitrary/LongerSparseArbitrary');3const arb1 = fc.array(fc.integer(0, 100), 0, 10);4const arb2 = longerSparse(arb1);5fc.assert(fc.property(arb2, (arr) => {6 console.log(arr);7 return true;8})
Using AI Code Generation
1var fc = require("fast-check");2var { longerSparse } = require("fast-check-monorepo");3var { longSparse } = require("fast-check-monorepo");4fc.assert(5 fc.property(longerSparse(longSparse()), function (n) {6 return true;7 })8);9at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)10at Function.Module._load (internal/modules/cjs/loader.js:725:27)11at Module.require (internal/modules/cjs/loader.js:952:19)12at require (internal/modules/cjs/helpers.js:88:18)13at Object.<anonymous> (/home/.../test3.js:4:31)14at Module._compile (internal/modules/cjs/loader.js:1063:30)15at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)16at Module.load (internal/modules/cjs/loader.js:928:32)17at Function.Module._load (internal/modules/cjs/loader.js:769:14)18at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {19}20at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)21at Function.Module._load (internal/modules/cjs/loader.js:725:27)22at Module.require (internal/modules/cjs/loader.js:952
Using AI Code Generation
1const {longerSparse} = require('fast-check-monorepo');2describe('longerSparse', () => {3 it('should generate an array with more elements than sparse', () => {4 const arb = longerSparse();5 for (let i = 0; i < 100; i++) {6 const value = arb.generate(mrng.noBias);7 expect(value.length).toBeGreaterThan(value.filter(x => x === undefined).length);8 }9 });10});11const {shorterSparse} = require('fast-check-monorepo');12describe('shorterSparse', () => {13 it('should generate an array with less elements than sparse', () => {14 const arb = shorterSparse();15 for (let i = 0; i < 100; i++) {16 const value = arb.generate(mrng.noBias);17 expect(value.length).toBeLessThan(value.filter(x => x === undefined).length);18 }19 });20});21const {sparse} = require('fast-check-monorepo');22describe('sparse', () => {23 it('should generate an array with more or less elements than sparse', () => {24 const arb = sparse();25 for (let i = 0; i < 100; i++) {26 const value = arb.generate(mrng.noBias);27 expect(value.length).not.toBe(value.filter(x => x === undefined).length);28 }29 });30});31const {sparse} = require('fast-check');32describe('sparse', () => {33 it('should generate an array with more or less elements than sparse', () => {34 const arb = sparse();35 for (let i = 0; i < 100; i++) {36 const value = arb.generate(mrng.noBias);37 expect(value.length).not.toBe(value.filter(x => x === undefined).length);38 }39 });40});41const {sparse} = require('fast-check');42describe('sparse', () => {43 it('should generate an array with more or less elements than sparse', () => {44 const arb = sparse();
Using AI Code Generation
1const fc = require('fast-check');2fc.assert(3 fc.property(fc.longerSparseArray(fc.integer()), (arr) => {4 return arr.length > 0;5 })6);
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!!