Skip to content

Commit

Permalink
fix: mersenne rand invalid input argument (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
xDivisionByZerox committed Mar 21, 2022
1 parent c51fb15 commit 486c76e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 29 deletions.
13 changes: 6 additions & 7 deletions src/mersenne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@ export class Mersenne {
* Generates a random number between `[min, max)`.
*
* @param max The maximum number. Defaults to `0`.
* @param min The minimum number. Defaults to `32768`. Required if `max` is set.
* @param min The minimum number. Defaults to `32768`.
*
* @example
* faker.mersenne.rand() // 15515
* faker.mersenne.rand(500, 1000) // 578
*/
rand(max?: number, min?: number): number {
// TODO @Shinigami92 2022-01-11: This is buggy, cause if min is not passed but only max,
// then min will be undefined and this result in NaN for the whole function
if (max === undefined) {
min = 0;
max = 32768;
rand(max = 32768, min = 0): number {
if (min > max) {
const temp = min;
min = max;
max = temp;
}

return Math.floor(this.gen.genrandReal2() * (max - min) + min);
Expand Down
30 changes: 8 additions & 22 deletions test/mersenne.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type SeededRun = {
type SeededRunExpectations = {
rand: {
noArgs: number;
minMax: { max?: number; min?: number; expected?: number; skip?: boolean }[];
minMax: { max?: number; min?: number; expected?: number }[];
};
};

Expand All @@ -21,7 +21,7 @@ const seededRuns: SeededRun[] = [
minMax: [
{ max: 100, min: 0, expected: 37 },
{ max: undefined, min: 0, expected: 12272 },
{ max: 100, min: undefined, expected: 37, skip: true },
{ max: 100, min: undefined, expected: 37 },
],
},
},
Expand All @@ -34,7 +34,7 @@ const seededRuns: SeededRun[] = [
minMax: [
{ max: 100, min: 0, expected: 26 },
{ max: undefined, min: 0, expected: 8586 },
{ max: 100, min: undefined, expected: 26, skip: true },
{ max: 100, min: undefined, expected: 26 },
],
},
},
Expand All @@ -47,7 +47,7 @@ const seededRuns: SeededRun[] = [
minMax: [
{ max: 100, min: 0, expected: 92 },
{ max: undefined, min: 0, expected: 30425 },
{ max: 100, min: undefined, expected: 92, skip: true },
{ max: 100, min: undefined, expected: 92 },
],
},
},
Expand All @@ -60,7 +60,7 @@ const seededRuns: SeededRun[] = [
minMax: [
{ max: 100, min: 0, expected: 85 },
{ max: undefined, min: 0, expected: 28056 },
{ max: 100, min: undefined, expected: 85, skip: true },
{ max: 100, min: undefined, expected: 85 },
],
},
},
Expand All @@ -73,7 +73,7 @@ const seededRuns: SeededRun[] = [
minMax: [
{ max: 100, min: 0, expected: 17 },
{ max: undefined, min: 0, expected: 5895 },
{ max: 100, min: undefined, expected: 17, skip: true },
{ max: 100, min: undefined, expected: 17 },
],
},
},
Expand All @@ -86,7 +86,7 @@ const seededRuns: SeededRun[] = [
minMax: [
{ max: 100, min: 0, expected: 89 },
{ max: undefined, min: 0, expected: 29217 },
{ max: 100, min: undefined, expected: 89, skip: true },
{ max: 100, min: undefined, expected: 89 },
],
},
},
Expand Down Expand Up @@ -122,28 +122,14 @@ describe('mersenne twister', () => {
});
}

for (const { min, max, expected } of expectations.rand.minMax.filter(
(x) => !x.skip
)) {
for (const { min, max, expected } of expectations.rand.minMax) {
it(`should return ${expected} for rand(${max}, ${min})`, () => {
const actual = mersenne.rand(max, min);

expect(actual).toEqual(expected);
});
}

// TODO @piotrekn 2022-02-13: There is a bug: https://github.com/faker-js/faker/issues/479
// remove minMax.skip when fixed
for (const { min, max, expected } of expectations.rand.minMax.filter(
(x) => x.skip
)) {
it.todo(`should return ${expected} for rand(${max}, ${min})`, () => {
const actual = mersenne.rand(max, min);

expect(actual).toEqual(expected);
});
}

it.todo(`should return 0 for rand(1)`, () => {
const actual = mersenne.rand(1);

Expand Down

0 comments on commit 486c76e

Please sign in to comment.