github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/math/rand/auto_test.go (about)

     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"
     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 Seed(1) result.
    20  	var out []int64
    21  	for i := 0; i < 10; i++ {
    22  		out = append(out, Int63())
    23  	}
    24  
    25  	// Look for out in Seed(1)'s 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  	Seed(1)
    30  	found := 0
    31  	for i := 0; i < 1000; i++ {
    32  		x := Int63()
    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  }