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

Commit 2d5cc60

Browse files
committed
fix: ensure non-integers can be used as input to fuzzers
1 parent 4a2bcf4 commit 2d5cc60

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const target = t.union([t.string, t.type({ n: t.number, b: t.boolean })]);
3030
// Builds a particular fuzzer from the registry.
3131
const fuzzer = fuzz.exampleGenerator(r, target);
3232

33-
// Make examples. The input integer and context
33+
// Make examples. The input number and context
3434
// fully determines the output example.
3535
for (const n of new Array(10).keys()) {
3636
console.log(JSON.stringify(fuzzer.encode([n, fuzz.fuzzContext()])));

src/core.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ function gen<T, C extends t.Decoder<unknown, T> & BasicType>(
7878
}
7979

8080
export function fuzzBoolean(_: FuzzContext, n: number): boolean {
81-
return n % 2 === 0;
81+
return rng(n).int32() % 2 === 0;
8282
}
8383

8484
export function fuzzNumber(_: FuzzContext, n: number): number {
85-
return n;
85+
return n / Math.PI;
8686
}
8787

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

9292
export function fuzzString(_: FuzzContext, n: number): string {

test/extra-fuzzers/test-io-ts-types.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { isRight, Right } from 'fp-ts/lib/Either';
1111
import { date } from 'io-ts-types/lib/date';
1212
import { Encode } from 'io-ts';
13+
import { rngi } from '../../src/rng';
1314

1415
const count = 100;
1516

@@ -27,9 +28,9 @@ describe('extra-fuzzers', () => {
2728
r.register(...fz);
2829
p = exampleGenerator(r, b).encode;
2930
});
30-
it(`generates decodable examples for inputs '[0, ${count})`, () => {
31+
it(`generates decodable examples for seeds '[0, ${count})`, () => {
3132
for (const n of new Array(count).keys()) {
32-
const v = p([n, fuzzContext()]);
33+
const v = p([rngi(n) / Math.PI, fuzzContext()]);
3334
const d = b.decode(v);
3435
assert.ok(isRight(d), `must decode ${JSON.stringify(v)}`);
3536
assert.deepStrictEqual(
@@ -44,7 +45,7 @@ describe('extra-fuzzers', () => {
4445
.slow(500);
4546
it(`generates same examples 2nd time`, () => {
4647
for (const n of new Array(count).keys()) {
47-
const v = p([n, fuzzContext()]);
48+
const v = p([rngi(n) / Math.PI, fuzzContext()]);
4849
assert.deepStrictEqual(v, old[n]);
4950
}
5051
})

test/test-index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { exampleGenerator, fuzzContext } from '../src/';
55
import { isRight, Right } from 'fp-ts/lib/Either';
66
import { types } from './tested-types';
77
import { Encode } from 'io-ts';
8+
import { rngi } from '../src/rng';
89

910
const count = 100;
1011

@@ -18,9 +19,9 @@ describe('io-ts-fuzzer', () => {
1819
const r = lib.createCoreRegistry()!;
1920
p = exampleGenerator(r, b).encode;
2021
});
21-
it(`generates decodable examples for inputs '[0, ${count})`, () => {
22+
it(`generates decodable examples for seeds '[0, ${count})`, () => {
2223
for (const n of new Array(count).keys()) {
23-
const v = p([n, fuzzContext()]);
24+
const v = p([rngi(n) / Math.PI, fuzzContext()]);
2425
const d = b.decode(v);
2526
assert.ok(isRight(d), `must decode ${JSON.stringify(v)}`);
2627
assert.deepStrictEqual(
@@ -35,7 +36,7 @@ describe('io-ts-fuzzer', () => {
3536
.slow(500);
3637
it(`generates same examples 2nd time`, () => {
3738
for (const n of new Array(count).keys()) {
38-
const v = p([n, fuzzContext()]);
39+
const v = p([rngi(n) / Math.PI, fuzzContext()]);
3940
assert.deepStrictEqual(v, old[n]);
4041
}
4142
})

0 commit comments

Comments
 (0)