-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_splitmix64.ts
More file actions
142 lines (129 loc) · 4.03 KB
/
test_splitmix64.ts
File metadata and controls
142 lines (129 loc) · 4.03 KB
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
/**
* Tests for splitmix64 — mirrors the C99 test suite.
* Requires ES2020+ (BigInt).
*/
declare const process: { exit(code: number): never };
import { SplitMix64 } from "./splitmix64";
function assert(cond: boolean, msg: string): void {
if (!cond) {
console.error(`FAIL: ${msg}`);
process.exit(1);
}
}
function testDeterministic(): void {
const a = new SplitMix64(42n);
const b = new SplitMix64(42n);
for (let i = 0; i < 1000; i++) {
assert(a.next() === b.next(), "same seed must produce identical sequence");
}
console.log(" deterministic ........... OK");
}
function testDifferentSeeds(): void {
const a = new SplitMix64(1n);
const b = new SplitMix64(2n);
let differ = 0;
for (let i = 0; i < 100; i++) {
if (a.next() !== b.next()) differ++;
}
assert(differ > 90, "different seeds should produce different values");
console.log(" different seeds ......... OK");
}
function testNextDouble(): void {
const s = new SplitMix64(123n);
for (let i = 0; i < 10000; i++) {
const d = s.nextDouble();
assert(d >= 0.0 && d < 1.0, "nextDouble must be in [0, 1)");
}
console.log(" nextDouble range ........ OK");
}
function testRandomRange(): void {
const s = new SplitMix64(77n);
for (let i = 0; i < 10000; i++) {
const r = s.randomRange(10n);
assert(r >= 0n && r < 10n, "randomRange(10) must be in [0, 10)");
}
// range of 1 must always return 0
for (let i = 0; i < 100; i++) {
assert(s.randomRange(1n) === 0n, "randomRange(1) must be 0");
}
console.log(" randomRange ............. OK");
}
function testRandomInt(): void {
const s = new SplitMix64(999n);
for (let i = 0; i < 10000; i++) {
const v = s.randomInt(-5n, 5n);
assert(v >= -5n && v <= 5n, "randomInt(-5,5) out of range");
}
// single-value range
for (let i = 0; i < 100; i++) {
assert(s.randomInt(7n, 7n) === 7n, "randomInt(7,7) must be 7");
}
console.log(" randomInt ............... OK");
}
function testToss(): void {
const s = new SplitMix64(0n);
let zeros = 0;
let ones = 0;
for (let i = 0; i < 10000; i++) {
const t = s.toss();
assert(t === 0n || t === 1n, "toss must be 0 or 1");
if (t === 0n) zeros++;
else ones++;
}
assert(zeros > 4000 && ones > 4000, "toss should be roughly balanced");
console.log(" toss .................... OK");
}
function testGetState(): void {
const s = new SplitMix64(12345n);
assert(s.getState() === 12345n, "getState right after seed");
s.next();
assert(s.getState() !== 12345n, "state changes after next()");
console.log(" getState ................ OK");
}
function testKnownDoubles(): void {
const s = new SplitMix64(0n);
const expected = [
0.88331080821364260647,
0.43152799704850997031,
0.02643377159259774345,
0.97088197815382848432,
0.10634669156721243688,
];
const EPSILON = 1e-15;
for (let i = 0; i < expected.length; i++) {
const v = s.nextDouble();
if (Math.abs(v - expected[i]) > EPSILON) {
console.error(`FAIL: nextDouble()[${i}] = ${v}, expected ${expected[i]}`);
process.exit(1);
}
}
console.log(" known doubles ........... OK");
}
function testKnownValues(): void {
const s = new SplitMix64(0n);
const expected = [
16294208416658607535n,
7960286522194355700n,
487617019471545679n,
17909611376780542444n,
];
for (let i = 0; i < expected.length; i++) {
const v = s.next();
if (v !== expected[i]) {
console.error(`FAIL: next()[${i}] = ${v}, expected ${expected[i]}`);
process.exit(1);
}
}
console.log(" known values ............ OK");
}
console.log("splitmix64 TypeScript tests:");
testDeterministic();
testDifferentSeeds();
testNextDouble();
testRandomRange();
testRandomInt();
testToss();
testGetState();
testKnownDoubles();
testKnownValues();
console.log("All tests passed.");