-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
146 lines (139 loc) · 4.11 KB
/
index.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
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
const { RandomGenerator } = require('./index')
const random = RandomGenerator()
const N = 1000
describe('RandomGenerator()', () => {
describe('random.number()', () => {
it('returns a number [a,b]', () => {
for (let i = 0; i < N; i++) {
const got = random.number(0, 100)
expect(got).toBeGreaterThanOrEqual(0)
expect(got).toBeLessThanOrEqual(100)
}
})
it('checks a <= b', () => {
expect(() => random.number(3, 2)).toThrow(TypeError)
})
})
describe('random.roll()', () => {
it('returns the rolling results', () => {
for (let i = 0; i < N; i++) {
const got = random.roll('3d6')
expect(got.length).toEqual(3)
got.forEach(n => {
expect(n).toBeGreaterThanOrEqual(1)
expect(n).toBeLessThanOrEqual(6)
})
}
})
it('returns the rolling result (bis)', () => {
for (let i = 0; i < N; i++) {
const got = random.roll('D12')
expect(got.length).toEqual(1)
expect(got[0]).toBeGreaterThanOrEqual(1)
expect(got[0]).toBeLessThanOrEqual(12)
}
})
it('checks the input is (num)+ d|D num', () => {
;[
{ input: '4' },
{ input: 4 },
{ input: '4f10' },
{ input: null },
{ input: undefined },
{ input: ['3'] }
].forEach(({ input }) => {
// @ts-ignore
expect(() => random.roll(input)).toThrow(TypeError)
})
})
})
describe('random.pickOne()', () => {
it('returns one element from the array', () => {
for (let i = 0; i < N; i++) {
const got = random.pickOne([1, 2, 3, 4, 5])
expect(got).toBeGreaterThanOrEqual(1)
expect(got).toBeLessThanOrEqual(5)
}
})
it('returns one element from the array with weight', () => {
for (let i = 0; i < N; i++) {
const got = random.pickOne([
{ key: 1, weight: 10 },
{ key: 2, weight: 10 },
{ key: 3, weight: 10 },
{ key: 4, weight: 10 },
{ key: 5, weight: 10 }
])
expect(got.key).toBeGreaterThanOrEqual(1)
expect(got.key).toBeLessThanOrEqual(5)
}
})
it('returns one element from the array with weight (bis)', () => {
for (let i = 0; i < N; i++) {
const got = random.pickOne(
[
{ key: 1, w: 10 },
{ key: 2, w: 10 },
{ key: 3, w: 10 },
{ key: 4, w: 10 },
{ key: 5, w: 10 }
],
{ weightKey: 'w' }
)
expect(got.key).toBeGreaterThanOrEqual(1)
expect(got.key).toBeLessThanOrEqual(5)
}
})
})
describe('random.bet()', () => {
it('returns a hit x% of the times (error 1%)', () => {
let hits = 0
for (let i = 0; i < N * 1000; i++) {
if (random.bet(40.0)) {
hits++
}
}
// hits = 40.000 (error 1%)
expect(hits).toBeGreaterThanOrEqual(400000 - 10000)
expect(hits).toBeLessThanOrEqual(400000 + 10000)
})
it('returns a hit x% of the times (bis)', () => {
let hits = 0
for (let i = 0; i < N * 1000; i++) {
if (random.bet(99.99)) {
hits++
}
}
// hits = 99.990 (error 1%)
expect(hits).toBeGreaterThanOrEqual(999900 - 10000)
expect(hits).toBeLessThanOrEqual(999900 + 10000)
})
it('returns a hit for 100%', () => {
expect(random.bet(100.0)).toBe(true)
})
it('returns a loose for 0%', () => {
expect(random.bet(0.0)).toBe(false)
})
})
describe('random.seed', () => {
it('internal "seed" is accesible', () => {
expect(typeof random.seed).toEqual('number')
const random2 = RandomGenerator(42)
expect(random2.seed).toEqual(42)
})
it('defines the random generation', () => {
const r1 = RandomGenerator(42)
const r2 = RandomGenerator(42)
expect(r1.unit()).toEqual(r2.unit())
})
})
describe('random.unit()', () => {
it('returns the random unit (0, 1)', () => {
for (let i = 0; i < N; i++) {
const got = random.unit()
expect(got).toBeGreaterThan(0)
expect(got).toBeLessThan(1)
}
})
})
})