Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Fuzzed string #4012

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions packages/fast-check/src/arbitrary/fuzzedString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';
import { Value } from '../check/arbitrary/definition/Value';
import { Random } from '../random/generator/Random';
import { Stream } from '../stream/Stream';
import { patternsToStringUnmapperFor } from './_internals/mappers/PatternsToString';
import { char } from './char';

const startSymbol = Symbol('start');
const endSymbol = Symbol('end');

// from => { to => weight }
type TransitionMap = Map<string | typeof startSymbol, Map<string | typeof endSymbol, number>>;

function multiFromToSingleFrom(fromMulti: (string | typeof startSymbol)[]): string | typeof startSymbol {
const nonStart = fromMulti.filter((i) => i !== startSymbol) as string[];
const startCount = fromMulti.length - nonStart.length;
return startCount + ':' + nonStart.join('');
}

function incrementInTransitionMap(
transitionMap: TransitionMap,
from: string | typeof startSymbol,
to: string | typeof endSymbol
): void {
const transitionsFromPrevious = transitionMap.get(from);
if (transitionsFromPrevious !== undefined) {
const pastValue = transitionsFromPrevious.get(to) || 0;
transitionsFromPrevious.set(to, pastValue + 1);
} else {
transitionMap.set(from, new Map([[to, 1]]));
}
}

function addIntoTransitionMap(transitionMap: TransitionMap, tokenizedCorpusItem: string[], depth: number): void {
const previousItems: (string | typeof startSymbol)[] = Array(depth).fill(startSymbol);
for (let index = 0; index !== tokenizedCorpusItem.length; ++index) {
const currentItem = tokenizedCorpusItem[index];
incrementInTransitionMap(transitionMap, multiFromToSingleFrom(previousItems), currentItem);
previousItems.shift();
previousItems.push(currentItem);
}
incrementInTransitionMap(transitionMap, multiFromToSingleFrom(previousItems), endSymbol);
}

class FuzzedString extends Arbitrary<string> {
private readonly transitionMap: TransitionMap;

constructor(
corpus: string[],
private readonly charArb: Arbitrary<string>,
private readonly strictness: 0 | 1 | 2,
private readonly depth: number
) {
super();

const tokenizedCorpus: string[][] = [];
const unmapper = patternsToStringUnmapperFor(this.charArb, {});
for (const corpusItem of corpus) {
const tokenizedCorpusItem = unmapper(corpusItem); // implicit throw
tokenizedCorpus.push(tokenizedCorpusItem);
}
if (tokenizedCorpus.length === 0) {
throw new Error(`Do not support empty corpus`);
}

this.transitionMap = new Map();
for (let d = 1; d <= this.depth; ++d) {
for (const tokenizedCorpusItem of tokenizedCorpus) {
addIntoTransitionMap(this.transitionMap, tokenizedCorpusItem, d);
}
}
}

private generateInternal(mrng: Random): string {
const previousItems: (string | typeof startSymbol)[] = Array(this.depth).fill(startSymbol);
let stringValue = '';

if (this.strictness !== 2) {
throw new Error('Strictness not being 2, not implemented');
}

// eslint-disable-next-line no-constant-condition
while (true) {
const allTransitions: [string | typeof endSymbol, number][] = [];
for (let d = 1; d <= this.depth; ++d) {
const transitions = this.transitionMap.get(
multiFromToSingleFrom(previousItems.slice(previousItems.length - d, previousItems.length))
);
if (transitions !== undefined) {
allTransitions.push(...transitions.entries());
}
}
const totalWeight = allTransitions.reduce((acc, transition) => acc + transition[1], 0);
const selectedWeight = mrng.nextInt(0, totalWeight - 1);
let postSelected = 1;
let totalWeightUpToPostSelected = allTransitions[0][1];
for (
;
postSelected !== allTransitions.length && totalWeightUpToPostSelected <= selectedWeight;
totalWeightUpToPostSelected += allTransitions[postSelected][1], ++postSelected
) {
// no-op
}
const item = allTransitions[postSelected - 1][0];
if (item === endSymbol) {
return stringValue;
}
previousItems.shift();
previousItems.push(item);
stringValue += item;
}
}

generate(mrng: Random, _biasFactor: number | undefined): Value<string> {
return new Value(this.generateInternal(mrng), undefined);
}

canShrinkWithoutContext(value: unknown): value is string {
return false;
}

shrink(_value: string, _context: unknown): Stream<Value<string>> {
return Stream.nil();
}
}

export function fuzzedString(corpus: string[]): Arbitrary<string> {
return new FuzzedString(corpus, char(), 2, 1);
}

export function fuzzedString10(corpus: string[]): Arbitrary<string> {
return new FuzzedString(corpus, char(), 2, 10);
}
3 changes: 3 additions & 0 deletions packages/fast-check/src/fast-check-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ import { bigInt64Array, BigIntArrayConstraints } from './arbitrary/bigInt64Array
import { bigUint64Array } from './arbitrary/bigUint64Array';
import { SchedulerAct } from './arbitrary/_internals/interfaces/Scheduler';
import { stringMatching, StringMatchingConstraints } from './arbitrary/stringMatching';
import { fuzzedString, fuzzedString10 } from './arbitrary/fuzzedString';

// Explicit cast into string to avoid to have __type: "__PACKAGE_TYPE__"
/**
Expand Down Expand Up @@ -251,6 +252,8 @@ export {
hexa,
base64,
mixedCase,
fuzzedString,
fuzzedString10,
string,
asciiString,
string16bits,
Expand Down
9 changes: 7 additions & 2 deletions packages/fast-check/test/e2e/documentation/Docs.md.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,20 @@ function refreshContent(originalContent: string): { content: string; numExecuted
.trim()
.replace(/;$/, '')
.replace(/;\n\/\/.*$/m, '\n//');
const evalCode = `${preparationPart}\nfc.sample(${santitizeArbitraryPart}\n, { numRuns: ${numRuns}, seed: ${seed} }).map(v => fc.stringify(v))`;
const evalCode = `${preparationPart}\nfc.sample(${santitizeArbitraryPart}\n, { numRuns: ${
santitizeArbitraryPart.includes('fuzzedString') ? 10 * numRuns : numRuns
}, seed: ${seed} }).map(v => fc.stringify(v))`;
try {
return eval(evalCode);
} catch (err) {
throw new Error(`Failed to run code snippet:\n\n${evalCode}\n\nWith error message: ${err}`);
}
})(fc);

const uniqueGeneratedValues = Array.from(new Set(generatedValues)).slice(0, TargetNumExamples);
const uniqueGeneratedValues = Array.from(new Set(generatedValues)).slice(
0,
snippet.includes('fuzzedString') ? 10 * TargetNumExamples : TargetNumExamples
);
// If the display for generated values is too long, we split it into a list of items
if (
uniqueGeneratedValues.some((value) => value.includes('\n')) ||
Expand Down
Loading
Loading