Source file
src/math/rand/v2/pcg_test.go
1
2
3
4
5 package rand_test
6
7 import (
8 . "math/rand/v2"
9 "testing"
10 )
11
12 func BenchmarkPCG_DXSM(b *testing.B) {
13 var p PCG
14 var t uint64
15 for n := b.N; n > 0; n-- {
16 t += p.Uint64()
17 }
18 Sink = t
19 }
20
21 func TestPCGMarshal(t *testing.T) {
22 var p PCG
23 const (
24 seed1 = 0x123456789abcdef0
25 seed2 = 0xfedcba9876543210
26 want = "pcg:\x12\x34\x56\x78\x9a\xbc\xde\xf0\xfe\xdc\xba\x98\x76\x54\x32\x10"
27 wantAppend = "\x00\x00\x00\x00" + want
28 )
29 p.Seed(seed1, seed2)
30 data, err := p.MarshalBinary()
31 if string(data) != want || err != nil {
32 t.Errorf("MarshalBinary() = %q, %v, want %q, nil", data, err, want)
33 }
34
35 dataAppend := make([]byte, 4, 32)
36 dataAppend, err = p.AppendBinary(dataAppend)
37 if string(dataAppend) != wantAppend || err != nil {
38 t.Errorf("AppendBinary() = %q, %v, want %q, nil", dataAppend, err, wantAppend)
39 }
40
41 q := PCG{}
42 if err := q.UnmarshalBinary([]byte(want)); err != nil {
43 t.Fatalf("UnmarshalBinary(): %v", err)
44 }
45 if q != p {
46 t.Fatalf("after round trip, q = %#x, but p = %#x", q, p)
47 }
48
49 qu := q.Uint64()
50 pu := p.Uint64()
51 if qu != pu {
52 t.Errorf("after round trip, q.Uint64() = %#x, but p.Uint64() = %#x", qu, pu)
53 }
54 }
55
56 func TestPCG(t *testing.T) {
57 p := NewPCG(1, 2)
58 want := []uint64{
59 0xc4f5a58656eef510,
60 0x9dcec3ad077dec6c,
61 0xc8d04605312f8088,
62 0xcbedc0dcb63ac19a,
63 0x3bf98798cae97950,
64 0xa8c6d7f8d485abc,
65 0x7ffa3780429cd279,
66 0x730ad2626b1c2f8e,
67 0x21ff2330f4a0ad99,
68 0x2f0901a1947094b0,
69 0xa9735a3cfbe36cef,
70 0x71ddb0a01a12c84a,
71 0xf0e53e77a78453bb,
72 0x1f173e9663be1e9d,
73 0x657651da3ac4115e,
74 0xc8987376b65a157b,
75 0xbb17008f5fca28e7,
76 0x8232bd645f29ed22,
77 0x12be8f07ad14c539,
78 0x54908a48e8e4736e,
79 }
80
81 for i, x := range want {
82 if u := p.Uint64(); u != x {
83 t.Errorf("PCG #%d = %#x, want %#x", i, u, x)
84 }
85 }
86 }
87
View as plain text