xorshiftr128plus_test.gno
3.24 Kb ยท 142 lines
1package xorshiftr128plus
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 rnd = New(987654321, 9876543210)
16 value3 := rnd.Uint64()
17
18 if value1 != 4368859828809982745 ||
19 value2 != 6152356058823566752 ||
20 value3 != 8285073084540510 ||
21 value1 == value2 ||
22 value2 == value3 ||
23 value1 == value3 {
24 t.Errorf("Expected three different values\n got: %d, %d, %d", value1, value2, value3)
25 }
26}
27
28func TestXorshiftr128PlusRand(t *testing.T) {
29 rnd := New(987654321)
30 rng := rand.New(rnd)
31
32 // Expected outputs for the first 5 random floats with the given seed
33 expected := []float64{
34 0.048735219800779106,
35 0.0372152171449619,
36 0.667254760531175,
37 0.16615979111253953,
38 0.27578895545492665,
39 0.48342823127830337,
40 0.7825693830495895,
41 0.14643955390763952,
42 0.29003469381875835,
43 0.726334398545258,
44 }
45
46 for i, exp := range expected {
47 val := rng.Float64()
48 if exp != val {
49 t.Errorf("Rand.Float64() at iteration %d: got %g, expected %g", i, val, exp)
50 }
51 }
52}
53
54func TestXorshiftr128PlusUint64(t *testing.T) {
55 rnd := New(987654321, 9876543210)
56
57 expected := []uint64{
58 8285073084540510,
59 97010855169053386,
60 11353359435625603792,
61 10289232744262291728,
62 14019961444418950453,
63 15829492476941720545,
64 2764732928842099222,
65 6871047144273883379,
66 16142204260470661970,
67 11803223757041229095,
68 }
69
70 for i, exp := range expected {
71 val := rnd.Uint64()
72 if exp != val {
73 t.Errorf("Xorshiftr128Plus.Uint64() at iteration %d: got %d, expected %d", i, val, exp)
74 }
75 }
76}
77
78func TestXorshiftr128PlusMarshalUnmarshal(t *testing.T) {
79 rnd := New(987654321, 9876543210)
80
81 expected1 := []uint64{
82 8285073084540510,
83 97010855169053386,
84 11353359435625603792,
85 10289232744262291728,
86 14019961444418950453,
87 }
88
89 expected2 := []uint64{
90 15829492476941720545,
91 2764732928842099222,
92 6871047144273883379,
93 16142204260470661970,
94 11803223757041229095,
95 }
96
97 for i, exp := range expected1 {
98 val := rnd.Uint64()
99 if exp != val {
100 t.Errorf("Xorshiftr128Plus.Uint64() at iteration %d: got %d, expected %d", i, val, exp)
101 }
102 }
103
104 marshalled, err := rnd.MarshalBinary()
105
106 // t.Logf("Original State: [%x]\n", rnd.seed)
107 // t.Logf("Marshalled State: [%x] -- %v\n", marshalled, err)
108 state_before := rnd.seed
109
110 if err != nil {
111 t.Errorf("Xorshiftr128Plus.MarshalBinary() error: %v", err)
112 }
113
114 // Advance state by one number; then check the next 5. The expectation is that they _will_ fail.
115 rnd.Uint64()
116
117 for i, exp := range expected2 {
118 val := rnd.Uint64()
119 if exp == val {
120 t.Errorf(" Iteration %d matched %d; which is from iteration %d; something strange is happening.", (i + 6), val, (i + 5))
121 }
122 }
123
124 // t.Logf("State before unmarshall: [%x]\n", rnd.seed)
125
126 // Now restore the state of the PRNG
127 err = rnd.UnmarshalBinary(marshalled)
128
129 // t.Logf("State after unmarshall: [%x]\n", rnd.seed)
130
131 if state_before != rnd.seed {
132 t.Errorf("States before and after marshal/unmarshal are not equal; go %x and %x", state_before, rnd.seed)
133 }
134
135 // Now we should be back on track for the last 5 numbers
136 for i, exp := range expected2 {
137 val := rnd.Uint64()
138 if exp != val {
139 t.Errorf("Xorshiftr128Plus.Uint64() at iteration %d: got %d, expected %d", (i + 5), val, exp)
140 }
141 }
142}