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

     1  // Copyright 2022 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  // This test is first, in its own file with an alphabetically early name,
    13  // to try to make sure that it runs early. It has the best chance of
    14  // detecting deterministic seeding if it's the first test that runs.
    15  
    16  func TestAuto(t *testing.T) {
    17  	// Pull out 10 int64s from the global source
    18  	// and then check that they don't appear in that
    19  	// order in the deterministic seeded result.
    20  	var out []int64
    21  	for i := 0; i < 10; i++ {
    22  		out = append(out, Int64())
    23  	}
    24  
    25  	// Look for out in seeded output.
    26  	// Strictly speaking, we should look for them in order,
    27  	// but this is good enough and not significantly more
    28  	// likely to have a false positive.
    29  	r := New(NewPCG(1, 0))
    30  	found := 0
    31  	for i := 0; i < 1000; i++ {
    32  		x := r.Int64()
    33  		if x == out[found] {
    34  			found++
    35  			if found == len(out) {
    36  				t.Fatalf("found unseeded output in Seed(1) output")
    37  			}
    38  		}
    39  	}
    40  }
    41  

View as plain text