xorshift64star_test.gno
3.04 Kb ยท 134 lines
1package xorshift64star
2
3import (
4 "math/rand"
5 "testing"
6)
7
8func TestXorshift64StarSeeding(t *testing.T) {
9 rnd := New()
10 value1 := rnd.Uint64()
11
12 rnd = New(987654321)
13 value2 := rnd.Uint64()
14
15 if value1 != 3506338452768534464 || value2 != 18211065302896784785 || value1 == value2 {
16 t.Errorf("Expected 2 different values; got: %d and %d", value1, value2)
17 }
18}
19
20func TestXorshift64StarRand(t *testing.T) {
21 rnd := New(987654321)
22 rng := rand.New(rnd)
23
24 // Expected outputs for the first 5 random floats with the given seed
25 expected := []float64{
26 .8344002228310946,
27 0.01777174153236205,
28 0.23521769507865276,
29 0.5387610198576143,
30 0.631539862225968,
31 0.9369068148346704,
32 0.6387002315083188,
33 0.5047507613688854,
34 0.5208486273732391,
35 0.25023746271541747,
36 }
37
38 for i, exp := range expected {
39 val := rng.Float64()
40 if exp != val {
41 t.Errorf("Rand.Float64() at iteration %d: got %g, expected %g", i, val, exp)
42 }
43 }
44}
45
46func TestXorshift64StarUint64(t *testing.T) {
47 rnd := New(1000)
48
49 expected := []uint64{
50 12487186097140327178,
51 14661465266242046735,
52 14694269887751719025,
53 15763651124252725051,
54 6358063137690011177,
55 16123467710013993794,
56 8086526499083208127,
57 5907916440057635441,
58 7074828965897564835,
59 219959441764368518,
60 }
61
62 for i, exp := range expected {
63 val := rnd.Uint64()
64 if exp != val {
65 t.Errorf("Xorshift64Star.Uint64() at iteration %d: got %d, expected %d", i, val, exp)
66 }
67 }
68}
69
70func TestXorshift64StarMarshalUnmarshal(t *testing.T) {
71 rnd := New(1001)
72
73 expected1 := []uint64{
74 17667678392346722343,
75 8998941448230025236,
76 6038719778092581458,
77 9916057400810746083,
78 3240504040424884895,
79 }
80
81 expected2 := []uint64{
82 3977048231667561376,
83 18438555156602529247,
84 2172795924893074637,
85 12043507788481457357,
86 8032279100325099159,
87 }
88
89 for i, exp := range expected1 {
90 val := rnd.Uint64()
91 if exp != val {
92 t.Errorf("Xorshift64Star.Uint64() at iteration %d: got %d, expected %d", i, val, exp)
93 }
94 }
95
96 marshalled, err := rnd.MarshalBinary()
97
98 // t.Logf("Original State: [%x]\n", rnd.seed)
99 // t.Logf("Marshalled State: [%x] -- %v\n", marshalled, err)
100 state_before := rnd.seed
101
102 if err != nil {
103 t.Errorf("Xorshift64Star.MarshalBinary() error: %v", err)
104 }
105
106 // Advance state by one number; then check the next 5. The expectation is that they _will_ fail.
107 rnd.Uint64()
108
109 for i, exp := range expected2 {
110 val := rnd.Uint64()
111 if exp == val {
112 t.Errorf(" Iteration %d matched %d; which is from iteration %d; something strange is happening.", (i + 6), val, (i + 5))
113 }
114 }
115
116 // t.Logf("State before unmarshall: [%x]\n", rnd.seed)
117
118 // Now restore the state of the PRNG
119 err = rnd.UnmarshalBinary(marshalled)
120
121 // t.Logf("State after unmarshall: [%x]\n", rnd.seed)
122
123 if state_before != rnd.seed {
124 t.Errorf("States before and after marshal/unmarshal are not equal; go %x and %x", state_before, rnd.seed)
125 }
126
127 // Now we should be back on track for the last 5 numbers
128 for i, exp := range expected2 {
129 val := rnd.Uint64()
130 if exp != val {
131 t.Errorf("Xorshift64Star.Uint64() at iteration %d: got %d, expected %d", (i + 5), val, exp)
132 }
133 }
134}