Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Arbitrary } from './definition/Arbitrary'; | ||
import { nat } from './IntegerArbitrary'; | ||
|
||
/** @hidden */ | ||
function computeNumChoices<T>(options: { num: number; build: (idInGroup: number) => T }[]): number { | ||
if (options.length === 0) throw new Error(`fc.mapToConstant expects at least one option`); | ||
let numChoices = 0; | ||
for (let idx = 0; idx !== options.length; ++idx) { | ||
if (options[idx].num < 0) | ||
throw new Error(`fc.mapToConstant expects all options to have a number of entries greater or equal to zero`); | ||
numChoices += options[idx].num; | ||
} | ||
if (numChoices === 0) throw new Error(`fc.mapToConstant expects at least one choice among options`); | ||
return numChoices; | ||
} | ||
|
||
/** | ||
* Generate non-contiguous ranges of values | ||
* by mapping integer values to constant | ||
* | ||
* @param options Builders to be called to generate the values | ||
* | ||
* @example | ||
* ``` | ||
* // generate alphanumeric values (a-z0-9) | ||
* mapToConstant( | ||
* { num: 26, build: v => String.fromCharCode(v + 0x61) }, | ||
* { num: 10, build: v => String.fromCharCode(v + 0x30) }, | ||
* ) | ||
* ``` | ||
*/ | ||
export function mapToConstant<T>(...entries: { num: number; build: (idInGroup: number) => T }[]): Arbitrary<T> { | ||
const numChoices = computeNumChoices(entries); | ||
return nat(numChoices - 1).map(choice => { | ||
let idx = -1; | ||
let numSkips = 0; | ||
while (choice >= numSkips) { | ||
numSkips += entries[++idx].num; | ||
} | ||
return entries[idx].build(choice - numSkips + entries[idx].num); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import * as prand from 'pure-rand'; | ||
import * as fc from '../../../../lib/fast-check'; | ||
|
||
import { mapToConstant } from '../../../../src/check/arbitrary/MapToConstantArbitrary'; | ||
import { Random } from '../../../../src/random/generator/Random'; | ||
import { minMax } from './generic/GenericArbitraryHelper'; | ||
import { nat } from '../../../../src/check/arbitrary/IntegerArbitrary'; | ||
|
||
describe('MapToConstantArbitrary', () => { | ||
describe('mapToConstant', () => { | ||
it('Single non-zero builder should be equivalent to nat and map', () => | ||
fc.assert( | ||
fc.property( | ||
fc.integer(), | ||
fc.integer(1, Number.MAX_SAFE_INTEGER), | ||
minMax(fc.nat(100)), | ||
(seed, numValues, others) => { | ||
const { min: pos, max: numBuilders } = others; | ||
fc.pre(pos < numBuilders); | ||
|
||
const entries: { num: number; build: (v: number) => string }[] = []; | ||
for (let idx = 0; idx !== numBuilders; ++idx) { | ||
entries.push({ num: idx === pos ? numValues : 0, build: (v: number) => `Builder #${idx}: ${v}` }); | ||
} | ||
|
||
const refArb = nat(numValues - 1).map(v => entries[pos].build(v)); | ||
const arb = mapToConstant(...entries); | ||
|
||
const refMrng = new Random(prand.xorshift128plus(seed)); | ||
const mrng = new Random(prand.xorshift128plus(seed)); | ||
|
||
expect(arb.generate(mrng).value).toEqual(refArb.generate(refMrng).value); | ||
} | ||
) | ||
)); | ||
it('Should call the right builder', () => | ||
fc.assert( | ||
fc.property(fc.integer(), fc.array(fc.nat(100)), (seed, builderSizes) => { | ||
const totalSize = builderSizes.reduce((a, b) => a + b, 0); | ||
fc.pre(totalSize > 0); | ||
|
||
const entries: { num: number; build: (v: number) => any }[] = []; | ||
for (let idx = 0, currentSize = 0; idx !== builderSizes.length; currentSize += builderSizes[idx], ++idx) { | ||
entries.push({ num: builderSizes[idx], build: v => v + currentSize }); | ||
} | ||
|
||
const refArb = nat(totalSize - 1); | ||
const arb = mapToConstant(...entries); | ||
|
||
const refMrng = new Random(prand.xorshift128plus(seed)); | ||
const mrng = new Random(prand.xorshift128plus(seed)); | ||
|
||
expect(arb.generate(mrng).value).toEqual(refArb.generate(refMrng).value); | ||
}) | ||
)); | ||
}); | ||
}); |