Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Commit

Permalink
fix: ensure non-integers can be used as input to fuzzers
Browse files Browse the repository at this point in the history
  • Loading branch information
holvonixAdvay committed Aug 8, 2019
1 parent 4a2bcf4 commit 2d5cc60
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const target = t.union([t.string, t.type({ n: t.number, b: t.boolean })]);
// Builds a particular fuzzer from the registry.
const fuzzer = fuzz.exampleGenerator(r, target);

// Make examples. The input integer and context
// Make examples. The input number and context
// fully determines the output example.
for (const n of new Array(10).keys()) {
console.log(JSON.stringify(fuzzer.encode([n, fuzz.fuzzContext()])));
Expand Down
6 changes: 3 additions & 3 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ function gen<T, C extends t.Decoder<unknown, T> & BasicType>(
}

export function fuzzBoolean(_: FuzzContext, n: number): boolean {
return n % 2 === 0;
return rng(n).int32() % 2 === 0;
}

export function fuzzNumber(_: FuzzContext, n: number): number {
return n;
return n / Math.PI;
}

export function fuzzInt(_: FuzzContext, n: number): t.TypeOf<typeof t.Int> {
return Math.floor(n) as t.TypeOf<typeof t.Int>;
return rng(n).int32() as t.TypeOf<typeof t.Int>;
}

export function fuzzString(_: FuzzContext, n: number): string {
Expand Down
7 changes: 4 additions & 3 deletions test/extra-fuzzers/test-io-ts-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { isRight, Right } from 'fp-ts/lib/Either';
import { date } from 'io-ts-types/lib/date';
import { Encode } from 'io-ts';
import { rngi } from '../../src/rng';

const count = 100;

Expand All @@ -27,9 +28,9 @@ describe('extra-fuzzers', () => {
r.register(...fz);
p = exampleGenerator(r, b).encode;
});
it(`generates decodable examples for inputs '[0, ${count})`, () => {
it(`generates decodable examples for seeds '[0, ${count})`, () => {
for (const n of new Array(count).keys()) {
const v = p([n, fuzzContext()]);
const v = p([rngi(n) / Math.PI, fuzzContext()]);
const d = b.decode(v);
assert.ok(isRight(d), `must decode ${JSON.stringify(v)}`);
assert.deepStrictEqual(
Expand All @@ -44,7 +45,7 @@ describe('extra-fuzzers', () => {
.slow(500);
it(`generates same examples 2nd time`, () => {
for (const n of new Array(count).keys()) {
const v = p([n, fuzzContext()]);
const v = p([rngi(n) / Math.PI, fuzzContext()]);
assert.deepStrictEqual(v, old[n]);
}
})
Expand Down
7 changes: 4 additions & 3 deletions test/test-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { exampleGenerator, fuzzContext } from '../src/';
import { isRight, Right } from 'fp-ts/lib/Either';
import { types } from './tested-types';
import { Encode } from 'io-ts';
import { rngi } from '../src/rng';

const count = 100;

Expand All @@ -18,9 +19,9 @@ describe('io-ts-fuzzer', () => {
const r = lib.createCoreRegistry()!;
p = exampleGenerator(r, b).encode;
});
it(`generates decodable examples for inputs '[0, ${count})`, () => {
it(`generates decodable examples for seeds '[0, ${count})`, () => {
for (const n of new Array(count).keys()) {
const v = p([n, fuzzContext()]);
const v = p([rngi(n) / Math.PI, fuzzContext()]);
const d = b.decode(v);
assert.ok(isRight(d), `must decode ${JSON.stringify(v)}`);
assert.deepStrictEqual(
Expand All @@ -35,7 +36,7 @@ describe('io-ts-fuzzer', () => {
.slow(500);
it(`generates same examples 2nd time`, () => {
for (const n of new Array(count).keys()) {
const v = p([n, fuzzContext()]);
const v = p([rngi(n) / Math.PI, fuzzContext()]);
assert.deepStrictEqual(v, old[n]);
}
})
Expand Down

0 comments on commit 2d5cc60

Please sign in to comment.