Skip to content

Commit

Permalink
Move stubs into test/stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed Jan 23, 2018
1 parent c88c962 commit e7bb7e5
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 123 deletions.
17 changes: 9 additions & 8 deletions test/check/arbitrary/ArrayArbitrary.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as assert from 'power-assert';
import { DummyRandomGenerator } from './TestRandomGenerator'
import { FastIncreaseRandomGenerator } from '../../stubs/generators';
import MutableRandomGenerator from '../../../src/random/generator/MutableRandomGenerator';
import Arbitrary from '../../../src/check/arbitrary/definition/Arbitrary';
import Shrinkable from '../../../src/check/arbitrary/definition/Shrinkable';
import { array } from '../../../src/check/arbitrary/ArrayArbitrary';
import { integer } from '../../../src/check/arbitrary/IntegerArbitrary';
import * as fc from '../../../src/fast-check';
import {} from '../../stubs/arbitraries'

class DummyArbitrary extends Arbitrary<any> {
constructor(public value:() => number) {
Expand All @@ -20,23 +21,23 @@ describe("ArrayArbitrary", () => {
describe('array', () => {
it('Should generate an array using specified arbitrary', () => fc.assert(
fc.property(fc.integer(), (seed) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const g = array(new DummyArbitrary(() => 42)).generate(mrng).value;
assert.deepEqual(g, [...Array(g.length)].map(() => new Object({key: 42})));
return true;
})
));
it('Should generate the same array with the same random', () => fc.assert(
fc.property(fc.integer(), (seed) => {
const mrng1 = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng2 = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng1 = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const mrng2 = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
assert.deepEqual(array(integer()).generate(mrng1).value, array(integer()).generate(mrng2).value);
return true;
})
));
it('Should generate an array calling multiple times arbitrary generator', () => fc.assert(
fc.property(fc.integer(), (seed) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
let num = 0;
const g = array(new DummyArbitrary(() => ++num)).generate(mrng).value;
let numBis = 0;
Expand All @@ -46,22 +47,22 @@ describe("ArrayArbitrary", () => {
));
it('Should generate an array given maximal length', () => fc.assert(
fc.property(fc.integer(), fc.integer(0, 10000), (seed, maxLength) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const g = array(new DummyArbitrary(() => 42), maxLength).generate(mrng).value;
return g.length <= maxLength;
})
));
it('Should shrink values in the defined range', () => fc.assert(
fc.property(fc.integer(), fc.integer(), fc.nat(), (seed, min, num) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const arb = array(integer(min, min + num));
const shrinkable = arb.generate(mrng);
return shrinkable.shrink().every(s => s.value.every(vv => min <= vv && vv <= min + num));
})
));
it('Should not suggest input in shrinked values', () => fc.assert(
fc.property(fc.integer(), fc.integer(), fc.nat(), (seed, min, num) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const arb = array(integer(min, min + num));
const shrinkable = arb.generate(mrng);
const tab = shrinkable.value;
Expand Down
26 changes: 13 additions & 13 deletions test/check/arbitrary/CharacterArbitrary.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'power-assert';
import { DummyRandomGenerator, IncrementRandomGenerator } from './TestRandomGenerator'
import { FastIncreaseRandomGenerator, CounterRandomGenerator } from '../../stubs/generators';
import MutableRandomGenerator from '../../../src/random/generator/MutableRandomGenerator';
import { char, ascii, unicode, hexa, base64 } from '../../../src/check/arbitrary/CharacterArbitrary';
import * as fc from '../../../src/fast-check';
Expand All @@ -8,14 +8,14 @@ describe("CharacterArbitrary", () => {
describe('char', () => {
it('Should generate a single printable character', () => fc.assert(
fc.property(fc.integer(), (seed) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const g = char().generate(mrng).value;
return g.length === 1 && 0x20 <= g.charCodeAt(0) && g.charCodeAt(0) <= 0x7e;
})
));
it('Should be able to produce any printable character', () => fc.assert(
fc.property(fc.integer(), fc.integer(32, 126), (seed, selected) => {
const mrng = new MutableRandomGenerator(new IncrementRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new CounterRandomGenerator(seed));
const arb = char();
const waitingFor = String.fromCharCode(selected);
for (let t = 0 ; t !== 96 ; ++t) { // check for equiprobable at the same time
Expand All @@ -30,14 +30,14 @@ describe("CharacterArbitrary", () => {
describe('ascii', () => {
it('Should generate a single ascii character', () => fc.assert(
fc.property(fc.integer(), (seed) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const g = ascii().generate(mrng).value;
return g.length === 1 && 0x00 <= g.charCodeAt(0) && g.charCodeAt(0) <= 0x7f;
})
));
it('Should be able to produce any character from ascii', () => fc.assert(
fc.property(fc.integer(), fc.integer(0, 127), (seed, selected) => {
const mrng = new MutableRandomGenerator(new IncrementRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new CounterRandomGenerator(seed));
const arb = ascii();
const waitingFor = String.fromCharCode(selected);
for (let t = 0 ; t !== 128 ; ++t) { // check for equiprobable at the same time
Expand All @@ -52,14 +52,14 @@ describe("CharacterArbitrary", () => {
describe('unicode', () => {
it('Should generate a single unicode character', () => fc.assert(
fc.property(fc.integer(), (seed) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const g = unicode().generate(mrng).value;
return g.length === 1 && 0x0000 <= g.charCodeAt(0) && g.charCodeAt(0) <= 0xffff;
})
));
it('Should be able to produce any character from unicode', () => fc.assert(
fc.property(fc.integer(), fc.integer(0, 65535), (seed, selected) => {
const mrng = new MutableRandomGenerator(new IncrementRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new CounterRandomGenerator(seed));
const arb = unicode();
const waitingFor = String.fromCharCode(selected);
for (let t = 0 ; t !== 65536 ; ++t) { // check for equiprobable at the same time
Expand All @@ -74,14 +74,14 @@ describe("CharacterArbitrary", () => {
describe('hexa', () => {
it('Should generate a single hexa character', () => fc.assert(
fc.property(fc.integer(), (seed) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const g = hexa().generate(mrng).value;
return g.length === 1 && (('0' <= g && g <= '9') || ('a' <= g && g <= 'f'));
})
));
it('Should be able to produce any character from hexa', () => fc.assert(
fc.property(fc.integer(), fc.integer(0, 15), (seed, selected) => {
const mrng = new MutableRandomGenerator(new IncrementRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new CounterRandomGenerator(seed));
const arb = hexa();
const waitingFor = '0123456789abcdef'[selected];
for (let t = 0 ; t !== 16 ; ++t) { // check for equiprobable at the same time
Expand All @@ -94,7 +94,7 @@ describe("CharacterArbitrary", () => {
));
it('Should shrink within hexa characters', () => fc.assert(
fc.property(fc.integer(), (seed) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const shrinkable = hexa().generate(mrng);
return shrinkable.shrink().every(s =>
s.value.length === 1 && (('0' <= s.value && s.value <= '9') || ('a' <= s.value && s.value <= 'f'))
Expand All @@ -105,7 +105,7 @@ describe("CharacterArbitrary", () => {
describe('base64', () => {
it('Should generate a single base64 character', () => fc.assert(
fc.property(fc.integer(), (seed) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const g = base64().generate(mrng).value;
return g.length === 1 && (
('a' <= g && g <= 'z') ||
Expand All @@ -117,7 +117,7 @@ describe("CharacterArbitrary", () => {
));
it('Should be able to produce any character from base64', () => fc.assert(
fc.property(fc.integer(), fc.integer(0, 63), (seed, selected) => {
const mrng = new MutableRandomGenerator(new IncrementRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new CounterRandomGenerator(seed));
const arb = base64();
const waitingFor = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'[selected];
for (let t = 0 ; t !== 64 ; ++t) { // check for equiprobable at the same time
Expand All @@ -130,7 +130,7 @@ describe("CharacterArbitrary", () => {
));
it('Should shrink within base64 characters', () => fc.assert(
fc.property(fc.integer(), (seed) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const shrinkable = base64().generate(mrng);
return shrinkable.shrink().every(s =>
s.value.length === 1 && (
Expand Down
2 changes: 1 addition & 1 deletion test/check/arbitrary/ConstantArbitrary.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'power-assert';
import { NoCallGenerator } from './TestRandomGenerator'
import { NoCallGenerator } from '../../stubs/generators';
import MutableRandomGenerator from '../../../src/random/generator/MutableRandomGenerator';
import { constant } from '../../../src/check/arbitrary/ConstantArbitrary';

Expand Down
26 changes: 13 additions & 13 deletions test/check/arbitrary/IntegerArbitrary.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'power-assert';
import { DummyRandomGenerator } from './TestRandomGenerator'
import { FastIncreaseRandomGenerator } from '../../stubs/generators';
import MutableRandomGenerator from '../../../src/random/generator/MutableRandomGenerator';
import { integer, nat } from '../../../src/check/arbitrary/IntegerArbitrary';
import * as fc from '../../../src/fast-check';
Expand All @@ -8,51 +8,51 @@ describe("IntegerArbitrary", () => {
describe('integer', () => {
it('Should generate values between -2**31 and 2**31 -1 by default', () => fc.assert(
fc.property(fc.integer(), (seed) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const g = integer().generate(mrng).value;
return -0x80000000 <= g && g <= 0x7fffffff;
})
));
it('Should generate values between -2**31 and max', () => fc.assert(
fc.property(fc.integer(), fc.integer(), (seed, max) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const g = integer(max).generate(mrng).value;
return -0x80000000 <= g && g <= max;
})
));
it('Should generate values between min and max', () => fc.assert(
fc.property(fc.integer(), fc.integer(), fc.nat(), (seed, min, num) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const g = integer(min, min + num).generate(mrng).value;
return min <= g && g <= min + num;
})
));
it('Should not fail on single value range', () => fc.assert(
fc.property(fc.integer(), fc.nat(), (seed, value) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const g = integer(value, value).generate(mrng).value;
return g == value;
})
));
it('Should shrink values between min and max', () => fc.assert(
fc.property(fc.integer(), fc.integer(), fc.nat(), (seed, min, num) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const arb = integer(min, min + num);
const shrinkable = arb.generate(mrng);
return shrinkable.shrink().every(s => min <= s.value && s.value <= min + num);
})
));
it('Should not suggest input in shrinked values', () => fc.assert(
fc.property(fc.integer(), fc.integer(), fc.nat(), (seed, min, num) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const arb = integer(min, min + num);
const shrinkable = arb.generate(mrng);
return shrinkable.shrink().every(s => s.value != shrinkable.value);
})
));
it('Should shrink towards zero', () => fc.assert(
fc.property(fc.integer(), fc.integer(), fc.nat(), (seed, min, num) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const arb = integer(min, min + num);
const shrinkable = arb.generate(mrng);
return shrinkable.value >= 0
Expand All @@ -62,7 +62,7 @@ describe("IntegerArbitrary", () => {
));
it('Should be able to call shrink multiple times', () => fc.assert(
fc.property(fc.integer(), fc.integer(), fc.nat(), (seed, min, num) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const arb = integer(min, min + num);
const shrinkable = arb.generate(mrng);
const s1 = [...shrinkable.shrink()].map(s => s.value);
Expand All @@ -72,7 +72,7 @@ describe("IntegerArbitrary", () => {
));
it('Should always suggest one shrinked value if it can go towards zero', () => fc.assert(
fc.property(fc.integer(), fc.integer(), fc.nat(), (seed, min, num) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const arb = integer(min, min + num);
const shrinkable = arb.generate(mrng);
const v = shrinkable.value;
Expand All @@ -84,7 +84,7 @@ describe("IntegerArbitrary", () => {
));
it('Should produce the same values for shrink on instance and on arbitrary', () => fc.assert(
fc.property(fc.integer(), fc.integer(), fc.nat(), (seed, min, num) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const arb = integer(min, min + num);
const shrinkable = arb.generate(mrng);
const shrinksInstance = [...shrinkable.shrink()].map(s => s.value);
Expand All @@ -96,14 +96,14 @@ describe("IntegerArbitrary", () => {
describe('nat', () => {
it('Should generate values between 0 and 2**31 -1 by default', () => fc.assert(
fc.property(fc.integer(), (seed) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const g = nat().generate(mrng).value;
return 0 <= g && g <= 0x7fffffff;
})
));
it('Should generate values between 0 and max', () => fc.assert(
fc.property(fc.integer(), fc.nat(), (seed, max) => {
const mrng = new MutableRandomGenerator(new DummyRandomGenerator(seed));
const mrng = new MutableRandomGenerator(new FastIncreaseRandomGenerator(seed));
const g = nat(max).generate(mrng).value;
return 0 <= g && g <= max;
})
Expand Down
Loading

0 comments on commit e7bb7e5

Please sign in to comment.