-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
prng.test.js
237 lines (220 loc) · 7.02 KB
/
prng.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { Xoroshiro128plus } from './prng/Xoroshiro128plus.js'
import * as prng from './prng.js'
import { MAX_SAFE_INTEGER } from './number.js'
import * as binary from './binary.js'
import * as t from './testing.js'
import { Xorshift32 } from './prng/Xorshift32.js'
import { Mt19937 } from './prng/Mt19937.js'
import * as dom from './dom.js'
import { isBrowser, production } from './environment.js'
import * as math from './math.js'
const genTestData = 5000
/**
* @param {t.TestCase} _tc
* @param {prng.PRNG} gen
*/
const runGenTest = (_tc, gen) => {
t.group('next - average distribution', () => {
let sum = 0
for (let i = 0; i < genTestData; i++) {
const next = gen.next()
if (next >= 1) {
t.fail('unexpected prng result')
}
sum += next
}
const avg = sum / genTestData
t.assert(avg >= 0.45)
t.assert(avg <= 0.55)
})
t.group('bool - bool distribution is fair', () => {
let head = 0
let tail = 0
let b
let i
for (i = 0; i < genTestData; i++) {
b = prng.bool(gen)
if (b) {
head++
} else {
tail++
}
}
t.info(`Generated ${head} heads and ${tail} tails.`)
t.assert(tail >= math.floor(genTestData * 0.45), 'Generated enough tails.')
t.assert(head >= math.floor(genTestData * 0.45), 'Generated enough heads.')
})
t.group('int31 - integers average correctly', () => {
let count = 0
let i
for (i = 0; i < genTestData; i++) {
count += prng.uint32(gen, 0, 100)
}
const average = count / genTestData
const expectedAverage = 100 / 2
t.info(`Average is: ${average}. Expected average is ${expectedAverage}.`)
t.assert(math.abs(average - expectedAverage) <= 2, 'Expected average is at most 1 off.')
})
t.group('int32 - generates integer with 32 bits', () => {
let largest = 0
let smallest = 0
let i
let newNum
for (i = 0; i < genTestData; i++) {
newNum = prng.int32(gen, -binary.BITS31, binary.BITS31)
if (newNum > largest) {
largest = newNum
}
if (newNum < smallest) {
smallest = newNum
}
}
t.assert(smallest < -1000, 'Smallest number is negative')
t.assert(largest > 1000, 'Largest number is positive')
t.info(`Largest number generated is ${largest} (0x${largest.toString(16)})`)
t.info(`Smallest number generated is ${smallest} (0x${smallest.toString(16)})`)
t.assert((smallest & binary.BIT32) !== 0, 'Largest number is 32 bits long') // largest.. assuming we convert int to uint
})
t.group('uint32 - generates unsigned integer with 32 bits', () => {
let num = 0
let i
let newNum
for (i = 0; i < genTestData; i++) {
newNum = prng.uint32(gen, 0, binary.BITS32)
if (newNum > num) {
num = newNum
}
}
t.info(`Largest number generated is ${num} (0x${num.toString(16)})`)
t.assert((num & binary.BIT32) !== 0, 'Largest number is 32 bits long.')
})
t.group('int53 - generates integer exceeding 32 bits', () => {
let largest = 0
let smallest = 0
let i
let newNum
for (i = 0; i < genTestData; i++) {
newNum = prng.int53(gen, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER)
if (newNum > largest) {
largest = newNum
}
if (newNum < smallest) {
smallest = newNum
}
}
t.assert(smallest < -1000, 'Smallest number is negative')
t.assert(largest > 1000, 'Largest number is positive')
t.info(`Largest number generated is ${largest}`)
t.info(`Smallest number generated is ${smallest}`)
t.assert(largest > (binary.BITS32 >>> 0), 'Largest number exceeds BITS32')
t.assert(smallest < binary.BITS32, 'Smallest Number is smaller than BITS32 (negative)')
})
t.group('uint53 - generates integer exceeding 32 bits', () => {
let largest = 0
let smallest = 10000
let i
let newNum
for (i = 0; i < genTestData; i++) {
newNum = prng.uint53(gen, 0, Number.MAX_SAFE_INTEGER)
if (newNum > largest) {
largest = newNum
}
/* c8 ignore next */
if (newNum < smallest) {
smallest = newNum
}
}
t.assert(smallest >= 0, 'Smallest number is not negative')
t.assert(largest > 1000, 'Largest number is positive')
t.info(`Largest number generated is ${largest}`)
t.info(`Smallest number generated is ${smallest}`)
t.assert(largest > (binary.BITS32 >>> 0), 'Largest number exceeds BITS32')
})
t.group('int31 - generates integer with 31 bits', () => {
let num = 0
let i
let newNum
for (i = 0; i < genTestData; i++) {
newNum = prng.uint32(gen, 0, binary.BITS31)
if (newNum > num) {
num = newNum
}
}
t.info(`Largest number generated is ${num} (0x${num.toString(16)})`)
t.assert((num & binary.BIT31) !== 0, 'Largest number is 31 bits long.')
})
t.group('real - has 53 bit resolution', () => {
let num = 0
let i
let newNum
for (i = 0; i < genTestData; i++) {
newNum = prng.real53(gen) * MAX_SAFE_INTEGER
if (newNum > num) {
num = newNum
}
}
t.info(`Largest number generated is ${num}.`)
t.assert((MAX_SAFE_INTEGER - num) / MAX_SAFE_INTEGER < 0.01, 'Largest number is close to MAX_SAFE_INTEGER (at most 1% off).')
})
t.group('char - generates all ascii characters', () => {
const charSet = new Set()
const chars = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[/]^_`abcdefghijklmnopqrstuvwxyz{|}~"'
for (let i = chars.length - 1; i >= 0; i--) {
charSet.add(chars[i])
}
for (let i = 0; i < genTestData; i++) {
const char = prng.char(gen)
charSet.delete(char)
}
t.info(`Charactes missing: ${charSet.size} - generating all of "${chars}"`)
t.assert(charSet.size === 0, 'Generated all documented characters.')
})
}
/**
* @param {t.TestCase} tc
*/
export const testGeneratorXoroshiro128plus = tc => runGenTest(tc, new Xoroshiro128plus(tc.seed))
/**
* @param {t.TestCase} tc
*/
export const testGeneratorXorshift32 = tc => {
t.skip(!production)
runGenTest(tc, new Xorshift32(tc.seed))
}
/**
* @param {t.TestCase} tc
*/
export const testGeneratorMt19937 = tc => {
t.skip(!production)
runGenTest(tc, new Mt19937(tc.seed))
}
/* c8 ignore next */
/**
* @param {prng.PRNG} gen
* @param {t.TestCase} _tc
*/
const printDistribution = (gen, _tc) => {
const DIAMETER = genTestData / 50
const canvas = dom.canvas(DIAMETER * 3, DIAMETER)
const ctx = canvas.getContext('2d')
if (ctx == null) {
return t.skip()
}
ctx.fillStyle = 'blue'
for (let i = 0; i < genTestData; i++) {
const x = prng.int32(gen, 0, DIAMETER * 3)
const y = prng.int32(gen, 0, DIAMETER)
ctx.fillRect(x, y, 1, 2)
}
t.printCanvas(canvas, DIAMETER)
}
/* c8 ignore next */
/**
* @param {t.TestCase} tc
*/
export const testNumberDistributions = tc => {
t.skip(!isBrowser)
t.group('Xoroshiro128plus', () => printDistribution(new Xoroshiro128plus(tc.seed), tc))
t.group('Xorshift32', () => printDistribution(new Xorshift32(tc.seed), tc))
t.group('MT19937', () => printDistribution(new Mt19937(tc.seed), tc))
}