-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseedlab_test.go
More file actions
383 lines (315 loc) · 8.62 KB
/
Copy pathseedlab_test.go
File metadata and controls
383 lines (315 loc) · 8.62 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package seedlab
import (
"encoding/json"
"testing"
)
// TestLCGDeterminism verifies that LCG produces the same sequence with the same seed
func TestLCGDeterminism(t *testing.T) {
seed := uint64(12345)
gen1 := NewLCG(seed)
gen2 := NewLCG(seed)
for i := 0; i < 100; i++ {
v1 := gen1.Next()
v2 := gen2.Next()
if v1 != v2 {
t.Errorf("LCG not deterministic at iteration %d: %d != %d", i, v1, v2)
}
}
}
// TestPCGDeterminism verifies that PCG produces the same sequence with the same seed
func TestPCGDeterminism(t *testing.T) {
seed := uint64(67890)
gen1 := NewPCG(seed)
gen2 := NewPCG(seed)
for i := 0; i < 100; i++ {
v1 := gen1.Next()
v2 := gen2.Next()
if v1 != v2 {
t.Errorf("PCG not deterministic at iteration %d: %d != %d", i, v1, v2)
}
}
}
// TestXoshiro256Determinism verifies that Xoshiro256 produces the same sequence with the same seed
func TestXoshiro256Determinism(t *testing.T) {
seed := uint64(11111)
gen1 := NewXoshiro256(seed)
gen2 := NewXoshiro256(seed)
for i := 0; i < 100; i++ {
v1 := gen1.Next()
v2 := gen2.Next()
if v1 != v2 {
t.Errorf("Xoshiro256 not deterministic at iteration %d: %d != %d", i, v1, v2)
}
}
}
// TestStateReplay verifies that generators can be restored to previous states
func TestStateReplay(t *testing.T) {
tests := []struct {
name string
gen Generator
}{
{"LCG", NewLCG(12345)},
{"PCG", NewPCG(67890)},
{"Xoshiro256", NewXoshiro256(11111)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Generate some values
for i := 0; i < 10; i++ {
tt.gen.Next()
}
// Save state
state := tt.gen.GetState()
// Generate more values and record them
expected := make([]uint64, 10)
for i := 0; i < 10; i++ {
expected[i] = tt.gen.Next()
}
// Restore state
err := tt.gen.SetState(state)
if err != nil {
t.Fatalf("Failed to restore state: %v", err)
}
// Verify we get the same values
for i := 0; i < 10; i++ {
val := tt.gen.Next()
if val != expected[i] {
t.Errorf("State replay failed at position %d: got %d, want %d", i, val, expected[i])
}
}
})
}
}
// TestReseed verifies that reseeding works correctly
func TestReseed(t *testing.T) {
seed1 := uint64(11111)
seed2 := uint64(22222)
gen := NewLCG(seed1)
val1 := gen.Next()
gen.Seed(seed1)
val2 := gen.Next()
if val1 != val2 {
t.Errorf("Reseed with same seed failed: %d != %d", val1, val2)
}
gen.Seed(seed2)
val3 := gen.Next()
if val3 == val1 {
t.Errorf("Reseed with different seed produced same value")
}
}
// TestNextFloat64 verifies that NextFloat64 returns values in [0.0, 1.0)
func TestNextFloat64(t *testing.T) {
generators := []Generator{
NewLCG(12345),
NewPCG(67890),
NewXoshiro256(11111),
}
for _, gen := range generators {
for i := 0; i < 1000; i++ {
val := gen.NextFloat64()
if val < 0.0 || val >= 1.0 {
t.Errorf("%s: NextFloat64 out of range: %f", gen.Algorithm(), val)
}
}
}
}
// TestNextInRange verifies that NextInRange returns values in [min, max)
func TestNextInRange(t *testing.T) {
generators := []Generator{
NewLCG(12345),
NewPCG(67890),
NewXoshiro256(11111),
}
min := int64(10)
max := int64(20)
for _, gen := range generators {
for i := 0; i < 1000; i++ {
val := gen.NextInRange(min, max)
if val < min || val >= max {
t.Errorf("%s: NextInRange out of range: %d not in [%d, %d)", gen.Algorithm(), val, min, max)
}
}
}
}
// TestExportImportStream verifies stream export and import
func TestExportImportStream(t *testing.T) {
seed := uint64(12345)
gen1 := NewLCG(seed)
// Export stream
stream := ExportStream(gen1, 50)
if stream.Length != 50 {
t.Errorf("Stream length mismatch: got %d, want 50", stream.Length)
}
if len(stream.Values) != 50 {
t.Errorf("Stream values length mismatch: got %d, want 50", len(stream.Values))
}
if stream.Algorithm != "LCG" {
t.Errorf("Stream algorithm mismatch: got %s, want LCG", stream.Algorithm)
}
// Verify values match a fresh generator
gen2 := NewLCG(seed)
for i := 0; i < 50; i++ {
expected := gen2.Next()
if stream.Values[i] != expected {
t.Errorf("Stream value mismatch at %d: got %d, want %d", i, stream.Values[i], expected)
}
}
}
// TestStreamSerialization verifies JSON serialization of streams
func TestStreamSerialization(t *testing.T) {
seed := uint64(12345)
gen := NewPCG(seed)
stream := ExportStream(gen, 10)
// Marshal to JSON
data, err := MarshalStream(stream)
if err != nil {
t.Fatalf("Failed to marshal stream: %v", err)
}
// Unmarshal from JSON
stream2, err := UnmarshalStream(data)
if err != nil {
t.Fatalf("Failed to unmarshal stream: %v", err)
}
// Verify fields match
if stream2.Algorithm != stream.Algorithm {
t.Errorf("Algorithm mismatch after serialization")
}
if stream2.Seed != stream.Seed {
t.Errorf("Seed mismatch after serialization")
}
if stream2.Length != stream.Length {
t.Errorf("Length mismatch after serialization")
}
for i := 0; i < len(stream.Values); i++ {
if stream2.Values[i] != stream.Values[i] {
t.Errorf("Value mismatch at %d after serialization", i)
}
}
}
// TestEntropyAnalysis verifies entropy metrics calculation
func TestEntropyAnalysis(t *testing.T) {
gen := NewXoshiro256(12345)
values := make([]uint64, 1000)
for i := 0; i < 1000; i++ {
values[i] = gen.Next()
}
metrics := AnalyzeEntropy(values)
// Check that metrics are in reasonable ranges
if metrics.Mean < 0.0 || metrics.Mean > 1.0 {
t.Errorf("Mean out of expected range: %f", metrics.Mean)
}
if metrics.Variance < 0.0 {
t.Errorf("Variance is negative: %f", metrics.Variance)
}
if metrics.StandardDeviation < 0.0 {
t.Errorf("Standard deviation is negative: %f", metrics.StandardDeviation)
}
// Shannon entropy should be close to 8.0 for good randomness (8 bits per byte)
if metrics.ShannonEntropy < 7.0 || metrics.ShannonEntropy > 8.0 {
t.Logf("Warning: Shannon entropy may indicate bias: %f", metrics.ShannonEntropy)
}
// Chi-square should not be too extreme
if metrics.ChiSquare < 0.0 {
t.Errorf("Chi-square is negative: %f", metrics.ChiSquare)
}
}
// TestBranchSeed verifies seed branching
func TestBranchSeed(t *testing.T) {
originalSeed := uint64(12345)
branch1 := BranchSeed(originalSeed, 1)
branch2 := BranchSeed(originalSeed, 2)
if branch1 == branch2 {
t.Errorf("Different branch IDs produced same seed")
}
if branch1 == originalSeed {
t.Errorf("Branch seed same as original seed")
}
// Verify determinism
branch1_again := BranchSeed(originalSeed, 1)
if branch1 != branch1_again {
t.Errorf("Branch seed not deterministic")
}
}
// TestStateSerialization verifies state can be serialized and deserialized
func TestStateSerialization(t *testing.T) {
gen := NewPCG(12345)
// Generate some values
for i := 0; i < 10; i++ {
gen.Next()
}
// Get state
state := gen.GetState()
// Serialize state
data, err := json.Marshal(state)
if err != nil {
t.Fatalf("Failed to marshal state: %v", err)
}
// Deserialize state
var state2 State
err = json.Unmarshal(data, &state2)
if err != nil {
t.Fatalf("Failed to unmarshal state: %v", err)
}
// Create new generator and restore state
gen2 := NewPCG(0)
err = gen2.SetState(state2)
if err != nil {
t.Fatalf("Failed to restore state: %v", err)
}
// Verify they produce the same values
for i := 0; i < 10; i++ {
v1 := gen.Next()
v2 := gen2.Next()
if v1 != v2 {
t.Errorf("State serialization failed at iteration %d: %d != %d", i, v1, v2)
}
}
}
// TestIncompatibleState verifies error handling for incompatible states
func TestIncompatibleState(t *testing.T) {
gen := NewLCG(12345)
state := NewPCG(67890).GetState()
err := gen.SetState(state)
if err == nil {
t.Error("Expected error when setting incompatible state, got nil")
}
}
// TestXoshiroJump verifies the jump function
func TestXoshiroJump(t *testing.T) {
seed := uint64(12345)
gen1 := NewXoshiro256(seed)
gen2 := NewXoshiro256(seed)
// Jump gen1
gen1.Jump()
// Manually advance gen2 by a large number of steps
// Just verify they're different after jump
v1 := gen1.Next()
v2 := gen2.Next()
if v1 == v2 {
t.Error("Jump should produce different sequence")
}
}
// BenchmarkLCG benchmarks the LCG generator
func BenchmarkLCG(b *testing.B) {
gen := NewLCG(12345)
b.ResetTimer()
for i := 0; i < b.N; i++ {
gen.Next()
}
}
// BenchmarkPCG benchmarks the PCG generator
func BenchmarkPCG(b *testing.B) {
gen := NewPCG(12345)
b.ResetTimer()
for i := 0; i < b.N; i++ {
gen.Next()
}
}
// BenchmarkXoshiro256 benchmarks the Xoshiro256** generator
func BenchmarkXoshiro256(b *testing.B) {
gen := NewXoshiro256(12345)
b.ResetTimer()
for i := 0; i < b.N; i++ {
gen.Next()
}
}