Source file src/math/rand/v2/pcg_test.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     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  	)
    28  	p.Seed(seed1, seed2)
    29  	data, err := p.MarshalBinary()
    30  	if string(data) != want || err != nil {
    31  		t.Errorf("MarshalBinary() = %q, %v, want %q, nil", data, err, want)
    32  	}
    33  
    34  	q := PCG{}
    35  	if err := q.UnmarshalBinary([]byte(want)); err != nil {
    36  		t.Fatalf("UnmarshalBinary(): %v", err)
    37  	}
    38  	if q != p {
    39  		t.Fatalf("after round trip, q = %#x, but p = %#x", q, p)
    40  	}
    41  
    42  	qu := q.Uint64()
    43  	pu := p.Uint64()
    44  	if qu != pu {
    45  		t.Errorf("after round trip, q.Uint64() = %#x, but p.Uint64() = %#x", qu, pu)
    46  	}
    47  }
    48  
    49  func TestPCG(t *testing.T) {
    50  	p := NewPCG(1, 2)
    51  	want := []uint64{
    52  		0xc4f5a58656eef510,
    53  		0x9dcec3ad077dec6c,
    54  		0xc8d04605312f8088,
    55  		0xcbedc0dcb63ac19a,
    56  		0x3bf98798cae97950,
    57  		0xa8c6d7f8d485abc,
    58  		0x7ffa3780429cd279,
    59  		0x730ad2626b1c2f8e,
    60  		0x21ff2330f4a0ad99,
    61  		0x2f0901a1947094b0,
    62  		0xa9735a3cfbe36cef,
    63  		0x71ddb0a01a12c84a,
    64  		0xf0e53e77a78453bb,
    65  		0x1f173e9663be1e9d,
    66  		0x657651da3ac4115e,
    67  		0xc8987376b65a157b,
    68  		0xbb17008f5fca28e7,
    69  		0x8232bd645f29ed22,
    70  		0x12be8f07ad14c539,
    71  		0x54908a48e8e4736e,
    72  	}
    73  
    74  	for i, x := range want {
    75  		if u := p.Uint64(); u != x {
    76  			t.Errorf("PCG #%d = %#x, want %#x", i, u, x)
    77  		}
    78  	}
    79  }
    80  

View as plain text