-
-
Notifications
You must be signed in to change notification settings - Fork 950
/
Copy pathmersenne.spec.ts
211 lines (189 loc) · 5.26 KB
/
mersenne.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { beforeAll, beforeEach, describe, expect, it } from 'vitest';
import { Mersenne } from '../src/mersenne';
type SeededRun = {
seed: number | number[];
expectations: SeededRunExpectations;
};
type SeededRunExpectations = {
rand: {
noArgs: number;
minMax: { max?: number; min?: number; expected?: number; skip?: boolean }[];
};
};
const seededRuns: SeededRun[] = [
{
seed: 42,
expectations: {
rand: {
noArgs: 12272,
minMax: [
{ max: 100, min: 0, expected: 37 },
{ max: undefined, min: 0, expected: 12272 },
{ max: 100, min: undefined, expected: 37, skip: true },
],
},
},
},
{
seed: 1337,
expectations: {
rand: {
noArgs: 8586,
minMax: [
{ max: 100, min: 0, expected: 26 },
{ max: undefined, min: 0, expected: 8586 },
{ max: 100, min: undefined, expected: 26, skip: true },
],
},
},
},
{
seed: 1211,
expectations: {
rand: {
noArgs: 30425,
minMax: [
{ max: 100, min: 0, expected: 92 },
{ max: undefined, min: 0, expected: 30425 },
{ max: 100, min: undefined, expected: 92, skip: true },
],
},
},
},
{
seed: [42, 1, 2],
expectations: {
rand: {
noArgs: 28056,
minMax: [
{ max: 100, min: 0, expected: 85 },
{ max: undefined, min: 0, expected: 28056 },
{ max: 100, min: undefined, expected: 85, skip: true },
],
},
},
},
{
seed: [1337, 1, 2],
expectations: {
rand: {
noArgs: 5895,
minMax: [
{ max: 100, min: 0, expected: 17 },
{ max: undefined, min: 0, expected: 5895 },
{ max: 100, min: undefined, expected: 17, skip: true },
],
},
},
},
{
seed: [1211, 1, 2],
expectations: {
rand: {
noArgs: 29217,
minMax: [
{ max: 100, min: 0, expected: 89 },
{ max: undefined, min: 0, expected: 29217 },
{ max: 100, min: undefined, expected: 89, skip: true },
],
},
},
},
];
const functionNames = ['rand'];
const NON_SEEDED_BASED_RUN = 25;
describe('mersenne twister', () => {
let mersenne: Mersenne;
beforeEach(() => {
mersenne = new Mersenne();
});
for (const { seed, expectations } of seededRuns) {
describe(`seed: ${JSON.stringify(seed)}`, () => {
beforeEach(() => {
if (Array.isArray(seed)) {
mersenne.seed_array(seed);
} else {
mersenne.seed(seed);
}
});
for (const functionName of functionNames) {
it(`${functionName}()`, () => {
const actual = mersenne[functionName]();
expect(actual).toEqual(expectations[functionName].noArgs);
});
}
for (const { min, max, expected } of expectations.rand.minMax.filter(
(x) => !x.skip
)) {
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);
expect(actual).toEqual(0);
});
});
}
// Create and log-back the seed for debug purposes
const seeds = [
Math.ceil(Math.random() * 1_000_000_000),
[
Math.ceil(Math.random() * 1_000_000_000),
Math.ceil(Math.random() * 1_000_000_000),
],
];
for (const seed of seeds) {
describe(`random seeded tests ${JSON.stringify(seed)}`, () => {
beforeAll(() => {
if (Array.isArray(seed)) {
mersenne.seed_array(seed);
} else {
mersenne.seed(seed);
}
});
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('rand', () => {
it('should return a random number without given min / max arguments', () => {
const randomNumber = mersenne.rand();
expect(randomNumber).toBeTypeOf('number');
});
it('should return random number from interval [min, max)', () => {
const actual = mersenne.rand(0, 2);
expect(actual).toBeGreaterThanOrEqual(0);
expect(actual).toBeLessThan(2);
});
});
}
});
}
it('should throw an error when attempting to seed() a non-integer', () => {
expect(() =>
mersenne.seed(
// @ts-expect-error: non-integer error
'abc'
)
).toThrowError(Error('seed(S) must take numeric argument; is string'));
});
it('should throw an error when attempting to seed() a non-integer', () => {
expect(() =>
mersenne.seed_array(
// @ts-expect-error: non-integer error
'abc'
)
).toThrowError(
Error('seed_array(A) must take array of numbers; is string')
);
});
});