-
-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UnbiasedProperty class to force non-biased values
- Loading branch information
Showing
2 changed files
with
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Random from '../../random/generator/Random'; | ||
import Arbitrary from '../arbitrary/definition/Arbitrary'; | ||
import Shrinkable from '../arbitrary/definition/Shrinkable'; | ||
import IProperty from './IProperty'; | ||
|
||
/** @hidden */ | ||
export class UnbiasedProperty<Ts> implements IProperty<Ts> { | ||
constructor(readonly property: IProperty<Ts>) {} | ||
isAsync = () => this.property.isAsync(); | ||
generate = (mrng: Random, freq?: number) => this.property.generate(mrng); | ||
run = (v: Ts) => this.property.run(v); | ||
} |
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,51 @@ | ||
import * as assert from 'assert'; | ||
|
||
import Arbitrary from '../../../../src/check/arbitrary/definition/Arbitrary'; | ||
import Shrinkable from '../../../../src/check/arbitrary/definition/Shrinkable'; | ||
import IProperty from '../../../../src/check/property/IProperty'; | ||
import { UnbiasedProperty } from '../../../../src/check/property/UnbiasedProperty'; | ||
|
||
import * as stubRng from '../../stubs/generators'; | ||
|
||
describe('UnbiasedProperty', () => { | ||
it('Should forward parameters correctly (asynchronous property)', () => { | ||
let calledWithFreq: number | undefined = undefined; | ||
const pAsync = new class implements IProperty<number> { | ||
isAsync = () => true; | ||
generate = (mrng: any, freq?: number) => { | ||
calledWithFreq = freq; | ||
return new Shrinkable(42); | ||
}; | ||
run = (v: number) => 'pAsync:' + v; | ||
}(); | ||
|
||
const unbiasedAsyncProp = new UnbiasedProperty(pAsync); | ||
|
||
assert.equal(unbiasedAsyncProp.isAsync(), true); | ||
assert.equal(unbiasedAsyncProp.generate(stubRng.mutable.nocall()).value, 42); | ||
assert.strictEqual(calledWithFreq, undefined); | ||
assert.equal(unbiasedAsyncProp.generate(stubRng.mutable.nocall(), 52).value, 42); | ||
assert.strictEqual(calledWithFreq, undefined); | ||
assert.equal(unbiasedAsyncProp.run(47), 'pAsync:47'); | ||
}); | ||
it('Should forward parameters correctly (synchronous property)', () => { | ||
let calledWithFreq: number | undefined = undefined; | ||
const pSync = new class implements IProperty<number> { | ||
isAsync = () => false; | ||
generate = (mrng: any, freq?: number) => { | ||
calledWithFreq = freq; | ||
return new Shrinkable(48); | ||
}; | ||
run = (v: number) => 'pSync:' + v; | ||
}(); | ||
|
||
const unbiasedSyncProp = new UnbiasedProperty(pSync); | ||
|
||
assert.equal(unbiasedSyncProp.isAsync(), false); | ||
assert.equal(unbiasedSyncProp.generate(stubRng.mutable.nocall()).value, 48); | ||
assert.strictEqual(calledWithFreq, undefined); | ||
assert.equal(unbiasedSyncProp.generate(stubRng.mutable.nocall(), 52).value, 48); | ||
assert.strictEqual(calledWithFreq, undefined); | ||
assert.equal(unbiasedSyncProp.run(29), 'pSync:29'); | ||
}); | ||
}); |