-
-
Notifications
You must be signed in to change notification settings - Fork 186
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
3 changed files
with
86 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Arbitrary from './definition/Arbitrary' | ||
import Shrinkable from './definition/Shrinkable' | ||
import MutableRandomGenerator from '../../random/generator/MutableRandomGenerator' | ||
import { nat } from './IntegerArbitrary' | ||
|
||
export interface WeightedArbitrary<T> { | ||
weight: number, | ||
arbitrary: Arbitrary<T> | ||
}; | ||
|
||
class FrequencyArbitrary<T> extends Arbitrary<T> { | ||
readonly summedWarbs: WeightedArbitrary<T>[]; | ||
readonly idArb: Arbitrary<number>; | ||
constructor(readonly warbs: WeightedArbitrary<T>[]) { | ||
super(); | ||
this.summedWarbs = warbs | ||
.reduce((p: WeightedArbitrary<T>[],c) => | ||
p.concat({weight: p[p.length -1].weight + c.weight, arbitrary: c.arbitrary}), | ||
[{weight: 0, arbitrary: warbs[0].arbitrary}] | ||
) | ||
.slice(1); | ||
this.idArb = nat(this.summedWarbs[this.summedWarbs.length -1].weight -1); | ||
} | ||
generate(mrng: MutableRandomGenerator): Shrinkable<T> { | ||
const selected = this.idArb.generate(mrng).value; | ||
return this.summedWarbs | ||
.find(warb => selected < warb.weight)!.arbitrary | ||
.generate(mrng); | ||
} | ||
} | ||
|
||
function frequency<T>(warb1: WeightedArbitrary<T>, ...warbs: WeightedArbitrary<T>[]): Arbitrary<T> { | ||
return new FrequencyArbitrary([warb1, ...warbs]); | ||
} | ||
|
||
export { frequency }; |
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,48 @@ | ||
import * as assert from 'power-assert'; | ||
import * as fc from '../../../../lib/fast-check'; | ||
|
||
import { frequency } from '../../../../src/check/arbitrary/FrequencyArbitrary'; | ||
import { oneof } from '../../../../src/check/arbitrary/OneOfArbitrary'; | ||
|
||
import * as stubArb from '../../stubs/arbitraries'; | ||
import * as stubRng from '../../stubs/generators'; | ||
|
||
describe('FrequencyArbitrary', () => { | ||
describe('frequency', () => { | ||
const MAX_WEIGHT = 100; | ||
const weightArb = () => fc.tuple(fc.integer(), fc.integer(1, MAX_WEIGHT)); | ||
const rng = (seed) => stubRng.mutable.fastincrease(seed); | ||
it('Should produce the same as oneof when called on weights of 1', () => fc.assert( | ||
fc.property(fc.integer(), fc.integer(), fc.array(fc.integer()), (seed, choice1, others) => { | ||
const gFreq = frequency( | ||
{weight: 1, arbitrary: stubArb.counter(choice1)}, | ||
...others.map(c => Object({weight: 1, arbitrary: stubArb.counter(c)})) | ||
).generate(rng(seed)).value; | ||
const gOneOf = oneof( | ||
stubArb.counter(choice1), | ||
...others.map(stubArb.counter) | ||
).generate(rng(seed)).value; | ||
return gFreq == gOneOf; | ||
}) | ||
)); | ||
it('Should produce the same as oneof with sum of weights elements', () => fc.assert( | ||
fc.property(fc.integer(), weightArb(), fc.array(weightArb()), (seed, choice1, others) => { | ||
const expand = (value: number, num: number): number[] => [...Array(num)].map(() => value); | ||
|
||
const othersOneOf = [choice1, ...others] | ||
.reduce((p,c) => p.concat(...expand(c[0], c[1])), []) | ||
.slice(1); | ||
|
||
const gFreq = frequency( | ||
{weight: choice1[1], arbitrary: stubArb.counter(choice1[0])}, | ||
...others.map(c => Object({weight: c[1], arbitrary: stubArb.counter(c[0])})) | ||
).generate(rng(seed)).value; | ||
const gOneOf = oneof( | ||
stubArb.counter(choice1[0]), | ||
...othersOneOf.map(stubArb.counter) | ||
).generate(rng(seed)).value; | ||
return gFreq == gOneOf; | ||
}) | ||
)); | ||
}); | ||
}); |