-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.test.js
54 lines (42 loc) · 1.7 KB
/
random.test.js
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
const Random = require('./random');
describe('Random', () => {
describe('value()', () => {
it('generates a number between 0 inclusive and 1 exclusive', () => {
const value = Random.value();
expect(value).toBeGreaterThanOrEqual(0);
expect(value).toBeLessThan(1);
});
});
describe('range(min, max)', () => {
it('generates a number within the given range', () => {
const value = Random.range(10, 100);
expect(value).toBeGreaterThanOrEqual(10);
expect(value).toBeLessThan(100);
});
it('generates a number including min value and excluding max value', () => {
expect(Random.range(0, 1)).toEqual(0);
});
it('distributes values evenly', () => {
const occurances = new Array(100);
/** Generate a large array of values. */
const values = new Uint32Array(10000).map((value) => {
return Random.range(0, 100);
});
/** Count how many times does each value in the array occur. */
values.forEach((value) => {
occurances[value] = (occurances[value] || 0) + 1;
});
const range = new Uint32Array(100).map((value, index) => {
return index;
});
const ratio = occurances.length / values.length;
/** Contains each of the values at least once. */
range.forEach((value) => {
expect(values.includes(value)).toBeTruthy();
});
occurances.forEach((value) => {
expect(value / values.length).toBeCloseTo(ratio, 2);
});
});
});
});