github.com/gopherd/gonum@v0.0.4/mathext/prng/mt19937_test.go (about)

     1  // Copyright ©2019 The Gonum 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 prng
     6  
     7  import (
     8  	"testing"
     9  	"time"
    10  
    11  	"math/rand"
    12  )
    13  
    14  var _ rand.Source = (*MT19937)(nil)
    15  
    16  // Random values in tests are produced by 40 iterations of the C code
    17  // with or without an initial seed array.
    18  
    19  func TestMT19937(t *testing.T) {
    20  	t.Parallel()
    21  	want := []uint32{
    22  		3499211612, 581869302, 3890346734, 3586334585, 545404204,
    23  		4161255391, 3922919429, 949333985, 2715962298, 1323567403,
    24  		418932835, 2350294565, 1196140740, 809094426, 2348838239,
    25  		4264392720, 4112460519, 4279768804, 4144164697, 4156218106,
    26  		676943009, 3117454609, 4168664243, 4213834039, 4111000746,
    27  		471852626, 2084672536, 3427838553, 3437178460, 1275731771,
    28  		609397212, 20544909, 1811450929, 483031418, 3933054126,
    29  		2747762695, 3402504553, 3772830893, 4120988587, 2163214728,
    30  	}
    31  
    32  	mt := NewMT19937()
    33  	for i := range want {
    34  		got := mt.Uint32()
    35  		if got != want[i] {
    36  			t.Errorf("unexpected random value at iteration %d: got:%d want:%d", i, got, want[i])
    37  		}
    38  	}
    39  }
    40  
    41  func TestMT19937SeedFromKeys(t *testing.T) {
    42  	t.Parallel()
    43  	want := []uint32{
    44  		1067595299, 955945823, 477289528, 4107218783, 4228976476,
    45  		3344332714, 3355579695, 227628506, 810200273, 2591290167,
    46  		2560260675, 3242736208, 646746669, 1479517882, 4245472273,
    47  		1143372638, 3863670494, 3221021970, 1773610557, 1138697238,
    48  		1421897700, 1269916527, 2859934041, 1764463362, 3874892047,
    49  		3965319921, 72549643, 2383988930, 2600218693, 3237492380,
    50  		2792901476, 725331109, 605841842, 271258942, 715137098,
    51  		3297999536, 1322965544, 4229579109, 1395091102, 3735697720,
    52  	}
    53  
    54  	mt := NewMT19937()
    55  	mt.SeedFromKeys([]uint32{0x123, 0x234, 0x345, 0x456})
    56  	for i := range want {
    57  		got := mt.Uint32()
    58  		if got != want[i] {
    59  			t.Errorf("unexpected random value at iteration %d: got:%d want:%d", i, got, want[i])
    60  		}
    61  	}
    62  }
    63  
    64  func TestMT19937RoundTrip(t *testing.T) {
    65  	t.Parallel()
    66  	var src MT19937
    67  	src.Seed(uint64(time.Now().Unix()))
    68  
    69  	src.Uint64() // Step PRNG once to makes sure states are mixed.
    70  
    71  	buf, err := src.MarshalBinary()
    72  	if err != nil {
    73  		t.Errorf("unexpected error marshaling state: %v", err)
    74  	}
    75  
    76  	var dst MT19937
    77  	// Get dst into a non-zero state.
    78  	dst.Seed(1)
    79  	for i := 0; i < 10; i++ {
    80  		dst.Uint64()
    81  	}
    82  
    83  	err = dst.UnmarshalBinary(buf)
    84  	if err != nil {
    85  		t.Errorf("unexpected error unmarshaling state: %v", err)
    86  	}
    87  
    88  	if dst != src {
    89  		t.Errorf("mismatch between generator states: got:%+v want:%+v", dst, src)
    90  	}
    91  }