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

     1  // Copyright 2016 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  	"sync"
    10  	"testing"
    11  )
    12  
    13  // TestConcurrent exercises the rand API concurrently, triggering situations
    14  // where the race detector is likely to detect issues.
    15  func TestConcurrent(t *testing.T) {
    16  	const (
    17  		numRoutines = 10
    18  		numCycles   = 10
    19  	)
    20  	var wg sync.WaitGroup
    21  	defer wg.Wait()
    22  	wg.Add(numRoutines)
    23  	for i := 0; i < numRoutines; i++ {
    24  		go func(i int) {
    25  			defer wg.Done()
    26  			var seed int64
    27  			for j := 0; j < numCycles; j++ {
    28  				seed += int64(ExpFloat64())
    29  				seed += int64(Float32())
    30  				seed += int64(Float64())
    31  				seed += int64(IntN(Int()))
    32  				seed += int64(Int32N(Int32()))
    33  				seed += int64(Int64N(Int64()))
    34  				seed += int64(NormFloat64())
    35  				seed += int64(Uint32())
    36  				seed += int64(Uint64())
    37  				for _, p := range Perm(10) {
    38  					seed += int64(p)
    39  				}
    40  			}
    41  			_ = seed
    42  		}(i)
    43  	}
    44  }
    45  

View as plain text