-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
204 lines (164 loc) · 4.99 KB
/
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
import test from 'ava';
import seeded from './lib/seeded.js';
import generator from './lib/generator.js';
import hash from './lib/hash.js';
import hash0 from './lib/hash0.js';
import range from './lib/transforms/range.js';
import uint from './lib/transforms/uint.js';
import list from './lib/transforms/list.js';
import bool from './lib/transforms/bool.js';
import clamp from './lib/transforms/clamp.js';
import { ME } from './lib/constants.js';
test.skip('Find interger limits', (t) => {
// This is slow but usefull to find where the min & max integers are to confirm the range of the PRNG is [0,0xffffffff]
t.timeout('200s');
const limit = 0xffffffff; // 4294967295
let min = undefined;
let max = undefined;
let n = 0;
let r = 0;
for (n = 0; n <= limit; n++) {
r = hash0(n);
if (r == 0) {
min = n;
}
if (r == limit) {
max = n;
}
if (r < 0 || r > limit) {
t.fail(`Unexpected result: ${r}`);
}
}
console.log(`Limits found: ${min} ${max}`);
t.pass(`Limits found: ${min} ${max}`);
});
test('Test found integer limits', (t) => {
// Locations found from the above test
const minSeed = 3920205903;
const maxSeed = 3110158695;
t.is(hash0(minSeed), 0);
t.is(hash0(maxSeed), 0xffffffff); // 4294967295
});
test('Test 32 bit overflows', (t) => {
t.is(hash0(0), hash0(0xffffffff + 1));
t.is(hash0(-1), hash0(0xffffffff));
});
test('Test hash[] and hash0 are equal', (t) => {
t.is(hash0(0), hash(0));
t.is(hash0(10), hash(10, []));
t.is(hash0(0xffffffff), hash(0xffffffff, []));
});
test('Test random seed works', (t) => {
const A = seeded(0);
const B = seeded(0);
t.is(A.random(), B.random());
t.is(A.random(), B.random());
t.not(A.random(), A.random());
});
test('Test random increment works', (t) => {
const A = seeded(0);
t.not(A.random(), A.random());
});
test('Test randomRange inclusive [0-1] range is different to default random [0-1) float exclusive range', (t) => {
// Setup 2 seeded PRNGs from the same seed
const A = seeded(0);
const B = seeded(0);
t.is(A.random(), B.random());
t.is(A.random(range(0, 1)), B.random(range(0, 1)));
t.is(A.random(range(10, 100)), B.random(range(10, 100)));
t.not(A.random(), B.random(range(0, 1)));
t.not(A.random(range(0, 1)), B.random(range(10, 100)));
});
test('Test we can save and load state', (t) => {
const A = seeded(0);
A.random();
A.random();
const B = seeded(0, A.state());
t.is(A.random(), B.random());
A.random();
A.random();
t.not(A.random(), B.random());
A.state(50);
B.state(50);
t.is(A.state(), 50);
t.is(B.state(), 50);
t.is(A.random(), B.random());
});
test('Snapshot floats', (t) => {
const A = seeded(0);
for (let n = 0; n < 10; n++) t.snapshot(A.random());
});
test('Snapshot uints', (t) => {
const A = seeded(0);
for (let n = 0; n < 10; n++) t.snapshot(A.random(uint()));
});
test('Snapshot range', (t) => {
const A = seeded(0);
// Note that range(0,0) == 0 is correct!
for (let n = 0; n < 10; n++) t.snapshot(A.random(range(0, n)));
// Ranges should go -ve & float
for (let n = 0; n < 10; n++) t.snapshot(A.random(range(-n / 2.53, n * 2.74)));
});
test('Snapshot clamp', (t) => {
const A = seeded(0);
// Note that clamp(0,0) == 0 is correct!
for (let n = 0; n < 10; n++) t.snapshot(A.random(clamp(0, n)));
});
test('Snapshot bool', (t) => {
const A = seeded(0);
for (let n = 0; n < 10; n++) t.snapshot(A.random(bool()));
});
test('Snapshot list', (t) => {
const A = seeded(0);
for (let n = 0; n < 10; n++) t.snapshot(A.random(list(n)));
});
test('Snapshot uint list', (t) => {
const A = seeded(0);
for (let n = 0; n < 10; n++) t.snapshot(A.random(list(n, uint())));
});
test('Snapshot 2d list of uints', (t) => {
const A = seeded(0);
t.snapshot(A.random(list(3, list(3, uint()))));
});
test('Test uint transform works', (t) => {
const A = seeded(0);
const B = seeded(0);
//console.log(A.random(uint()), B.random());
// Snapshot values for seeded(0,0).random() and seeded(0,0).random(uint())
t.is(0.03452833951450884, 148298089 * ME);
t.is(A.random(uint()), 148298089);
t.is(B.random(), 0.03452833951450884);
t.not(A.random(uint()), B.random());
});
test('Test list transform', (t) => {
const A = seeded(0);
const l = A.random(list(3));
// First 3 snapshot floats
t.is(l[0], 0.03452833951450884);
t.is(l[1], 0.9520792111288756);
t.is(l[2], 0.12287149345502257);
});
test('Test list uint transform', (t) => {
const A = seeded(0);
const l = A.random(list(3, uint()));
// First 3 snapshot uints
t.is(l[0], 148298089);
t.is(l[1], 4089149075);
t.is(l[2], 527729046);
});
test('Test bool transform', (t) => {
const A = seeded(0);
// First 3 snapshot bools
t.is(A.random(bool()), true);
t.is(A.random(bool()), true);
t.is(A.random(bool()), false);
});
test('Generator', (t) => {
// Remember to provide an end counter if you want it to stop
const A = generator(0, 0, 5);
// for (const rnd of A) {
// console.log(rnd);
// }
//A.next().value;
t.snapshot(Array.from(A));
});