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
11 changed files
with
325 additions
and
19 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
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,58 @@ | ||
/** @hidden */ | ||
interface Count { | ||
value: boolean; | ||
count: number; | ||
} | ||
|
||
/** @hidden */ | ||
export class ReplayPath { | ||
static parse(replayPathStr: string): boolean[] { | ||
const [serializedCount, serializedChanges] = replayPathStr.split(':'); | ||
const counts = serializedCount.split('').map(c => this.b64ToInt(c) + 1); | ||
const changesInt = serializedChanges.split('').map(c => this.b64ToInt(c)); | ||
const changes: boolean[] = []; | ||
for (let idx = 0; idx !== changesInt.length; ++idx) { | ||
let current = changesInt[idx]; | ||
for (let n = 0; n !== 6; ++n, current >>= 1) { | ||
changes.push(current % 2 === 1); | ||
} | ||
} | ||
|
||
const replayPath: boolean[] = []; | ||
for (let idx = 0; idx !== counts.length; ++idx) { | ||
const count = counts[idx]; | ||
const value = changes[idx]; | ||
for (let num = 0; num !== count; ++num) replayPath.push(value); | ||
} | ||
return replayPath; | ||
} | ||
static stringify(replayPath: boolean[]): string { | ||
const aggregatedPath = replayPath.reduce((counts: Count[], cur: boolean) => { | ||
if (counts.length === 0 || counts[counts.length - 1].count === 64 || counts[counts.length - 1].value !== cur) | ||
counts.push({ value: cur, count: 1 }); | ||
else counts[counts.length - 1].count += 1; | ||
return counts; | ||
}, []); | ||
const serializedCount = aggregatedPath.map(({ count }) => this.intToB64(count - 1)).join(''); | ||
let serializedChanges = ''; | ||
for (let idx = 0; idx < aggregatedPath.length; idx += 6) { | ||
const changesInt = aggregatedPath | ||
.slice(idx, idx + 6) | ||
.reduceRight((prev: number, cur: Count) => prev * 2 + (cur.value ? 1 : 0), 0); | ||
serializedChanges += this.intToB64(changesInt); | ||
} | ||
return `${serializedCount}:${serializedChanges}`; | ||
} | ||
static intToB64(n: number): string { | ||
if (n < 26) return String.fromCharCode(n + 65); // A-Z | ||
if (n < 52) return String.fromCharCode(n + 97 - 26); // a-z | ||
if (n < 62) return String.fromCharCode(n + 48 - 52); // 0-9 | ||
return String.fromCharCode(n === 62 ? 43 : 47); // +/ | ||
} | ||
static b64ToInt(c: string): number { | ||
if ('A' <= c && c <= 'Z') return c.charCodeAt(0) - 65; | ||
if ('a' <= c && c <= 'z') return c.charCodeAt(0) - 97 + 26; | ||
if ('0' <= c && c <= '9') return c.charCodeAt(0) - 48 + 52; | ||
return c === '+' ? 62 : 63; | ||
} | ||
} |
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,19 @@ | ||
/** | ||
* Parameters for fc.commands | ||
*/ | ||
export interface CommandsSettings { | ||
/** | ||
* Maximal number of commands to generate per run | ||
*/ | ||
maxCommands?: number; | ||
/** | ||
* Do not show replayPath in the output | ||
*/ | ||
disableReplayLog?: boolean; | ||
/** | ||
* Hint for replay purposes only | ||
* | ||
* Should be used in conjonction with { seed, path } of fc.assert | ||
*/ | ||
replayPath?: string; | ||
} |
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,50 @@ | ||
import * as fc from '../../src/fast-check'; | ||
|
||
// Fake commands | ||
type Model = { counter: number }; | ||
type Real = {}; | ||
class IncBy implements fc.Command<Model, Real> { | ||
constructor(readonly v: number) {} | ||
check = (m: Readonly<Model>) => true; | ||
run = (m: Model, r: Real) => (m.counter += this.v); | ||
toString = () => `IncBy(${this.v})`; | ||
} | ||
class DecPosBy implements fc.Command<Model, Real> { | ||
constructor(readonly v: number) {} | ||
check = (m: Readonly<Model>) => m.counter > 0; | ||
run = (m: Model, r: Real) => (m.counter -= this.v); | ||
toString = () => `DecPosBy(${this.v})`; | ||
} | ||
class AlwaysPos implements fc.Command<Model, Real> { | ||
check = (m: Readonly<Model>) => true; | ||
run = (m: Model, r: Real) => { | ||
if (m.counter < 0) throw new Error('counter is supposed to be always greater or equal to zero'); | ||
}; | ||
toString = () => `AlwaysPos()`; | ||
} | ||
|
||
const seed = Date.now(); | ||
describe(`ReplayCommands (seed: ${seed})`, () => { | ||
it('Should be able to replay commands by specifying replayPath in fc.commands', () => { | ||
const buildProp = (replayPath?: string) => { | ||
return fc.property( | ||
fc.commands( | ||
[fc.nat().map(v => new IncBy(v)), fc.nat().map(v => new DecPosBy(v)), fc.constant(new AlwaysPos())], | ||
{ replayPath } | ||
), | ||
cmds => fc.modelRun(() => ({ model: { counter: 0 }, real: {} }), cmds) | ||
); | ||
}; | ||
|
||
const out = fc.check(buildProp(), { seed: seed }); | ||
expect(out.failed).toBe(true); | ||
|
||
const path = out.counterexamplePath!; | ||
const replayPath = /\/\*replayPath=['"](.*)['"]\*\//.exec(out.counterexample![0].toString())![1]; | ||
|
||
const outReplayed = fc.check(buildProp(replayPath), { seed, path }); | ||
expect(outReplayed.counterexamplePath).toEqual(out.counterexamplePath); | ||
expect(outReplayed.counterexample![0].toString()).toEqual(out.counterexample![0].toString()); | ||
expect(outReplayed.numRuns).toEqual(1); | ||
}); | ||
}); |
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,23 @@ | ||
import * as fc from '../../../../lib/fast-check'; | ||
|
||
import { ReplayPath } from '../../../../src/check/model/ReplayPath'; | ||
|
||
const biasedBoolean = fc.frequency( | ||
{ weight: 1000, arbitrary: fc.constant(true) }, | ||
{ weight: 1, arbitrary: fc.constant(false) } | ||
); | ||
|
||
describe('ReplayPath', () => { | ||
it('Should be able to read back itself', () => | ||
fc.assert( | ||
fc.property(fc.array(fc.boolean(), 0, 1000), (replayPath: boolean[]) => { | ||
expect(ReplayPath.parse(ReplayPath.stringify(replayPath))).toEqual(replayPath); | ||
}) | ||
)); | ||
it('Should be able to read back itself (biased boolean)', () => | ||
fc.assert( | ||
fc.property(fc.array(biasedBoolean, 0, 1000), (replayPath: boolean[]) => { | ||
expect(ReplayPath.parse(ReplayPath.stringify(replayPath))).toEqual(replayPath); | ||
}) | ||
)); | ||
}); |
Oops, something went wrong.