fast-check
Version:
Property based testing framework for JavaScript (like QuickCheck)
42 lines (41 loc) • 1.48 kB
JavaScript
import { ArrayArbitrary } from './ArrayArbitrary.js';
function subArrayContains(tab, upperBound, includeValue) {
for (let idx = 0; idx < upperBound; ++idx) {
if (includeValue(tab[idx]))
return true;
}
return false;
}
function swap(tab, idx1, idx2) {
const temp = tab[idx1];
tab[idx1] = tab[idx2];
tab[idx2] = temp;
}
export function buildCompareFilter(compare) {
return (tab) => {
let finalLength = tab.length;
for (let idx = tab.length - 1; idx !== -1; --idx) {
if (subArrayContains(tab, idx, (t) => compare(t.value_, tab[idx].value_))) {
--finalLength;
swap(tab, idx, finalLength);
}
}
return tab.slice(0, finalLength);
};
}
function set(arb, aLength, bLength, compareFn) {
const minLength = bLength == null || typeof bLength !== 'number' ? 0 : aLength;
const maxLength = aLength == null || typeof aLength !== 'number' ? 10 : typeof bLength === 'number' ? bLength : aLength;
const compare = compareFn != null
? compareFn
: typeof bLength === 'function'
? bLength
: typeof aLength === 'function'
? aLength
: (a, b) => a === b;
const arrayArb = new ArrayArbitrary(arb, minLength, maxLength, buildCompareFilter(compare));
if (minLength === 0)
return arrayArb;
return arrayArb.filter((tab) => tab.length >= minLength);
}
export { set };