Skip to content

Commit

Permalink
Add maxSafeInteger and maxSafeNat
Browse files Browse the repository at this point in the history
Fixes #285
  • Loading branch information
dubzzz committed Jan 23, 2019
1 parent c4d396b commit a8eeb16
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
2 changes: 2 additions & 0 deletions documentation/Arbitraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Integer values:
- `fc.integer(min: number, max: number)` all possible integers between min (included) and max (included)
- `fc.nat()` all possible positive integers ie. from 0 (included) to 2147483647 (included)
- `fc.nat(max: number)` all possible positive integers between 0 (included) and max (included)
- `fc.maxSafeInteger()` all possible positive integers between `Number.MIN_SAFE_INTEGER` (included) and `Number.MAX_SAFE_INTEGER` (included)
- `fc.maxSafeNat()` all possible positive integers between 0 (included) and `Number.MAX_SAFE_INTEGER` (included)

Floating point numbers:

Expand Down
16 changes: 15 additions & 1 deletion src/check/arbitrary/IntegerArbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ function integer(a?: number, b?: number): ArbitraryWithShrink<number> {
return b === undefined ? new IntegerArbitrary(undefined, a) : new IntegerArbitrary(a, b);
}

/**
* For integers between Number.MIN_SAFE_INTEGER (included) and Number.MAX_SAFE_INTEGER (included)
*/
function maxSafeInteger(): ArbitraryWithShrink<number> {
return integer(Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
}

/**
* For positive integers between 0 (included) and 2147483647 (included)
*/
Expand All @@ -75,4 +82,11 @@ function nat(a?: number): ArbitraryWithShrink<number> {
return new IntegerArbitrary(0, a);
}

export { integer, nat };
/**
* For positive integers between 0 (included) and Number.MAX_SAFE_INTEGER (included)
*/
function maxSafeNat(): ArbitraryWithShrink<number> {
return nat(Number.MAX_SAFE_INTEGER);
}

export { integer, nat, maxSafeInteger, maxSafeNat };
4 changes: 3 additions & 1 deletion src/fast-check-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { dictionary } from './check/arbitrary/DictionaryArbitrary';
import { double, float } from './check/arbitrary/FloatingPointArbitrary';
import { frequency } from './check/arbitrary/FrequencyArbitrary';
import { compareBooleanFunc, compareFunc, func } from './check/arbitrary/FunctionArbitrary';
import { integer, nat } from './check/arbitrary/IntegerArbitrary';
import { integer, maxSafeInteger, maxSafeNat, nat } from './check/arbitrary/IntegerArbitrary';
import { lorem } from './check/arbitrary/LoremArbitrary';
import {
anything,
Expand Down Expand Up @@ -86,6 +86,8 @@ export {
double,
integer,
nat,
maxSafeInteger,
maxSafeNat,
bigIntN,
bigUintN,
bigInt,
Expand Down
19 changes: 18 additions & 1 deletion test/unit/check/arbitrary/IntegerArbitrary.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fc from '../../../../lib/fast-check';

import { Shrinkable } from '../../../../src/check/arbitrary/definition/Shrinkable';
import { integer, nat } from '../../../../src/check/arbitrary/IntegerArbitrary';
import { integer, nat, maxSafeNat, maxSafeInteger } from '../../../../src/check/arbitrary/IntegerArbitrary';

import * as genericHelper from './generic/GenericArbitraryHelper';

Expand Down Expand Up @@ -171,6 +171,15 @@ describe('IntegerArbitrary', () => {
);
});
});
describe('maxSafeInteger', () => {
describe('Given no constraints [between MIN_SAFE_INTEGER and MAX_SAFE_INTEGER]', () => {
genericHelper.isValidArbitrary(() => maxSafeInteger(), {
isStrictlySmallerValue: isStrictlySmallerInteger,
isValidValue: (g: number) =>
typeof g === 'number' && g >= Number.MIN_SAFE_INTEGER && g <= Number.MAX_SAFE_INTEGER
});
});
});
describe('nat', () => {
describe('Given no constraints [between 0 and 2**31 -1]', () => {
genericHelper.isValidArbitrary(() => nat(), {
Expand All @@ -186,4 +195,12 @@ describe('IntegerArbitrary', () => {
});
});
});
describe('maxSafeNat', () => {
describe('Given no constraints [between 0 and MAX_SAFE_INTEGER]', () => {
genericHelper.isValidArbitrary(() => maxSafeNat(), {
isStrictlySmallerValue: isStrictlySmallerInteger,
isValidValue: (g: number) => typeof g === 'number' && g >= 0 && g <= Number.MAX_SAFE_INTEGER
});
});
});
});

0 comments on commit a8eeb16

Please sign in to comment.