Source file src/runtime/rand_test.go

     1  // Copyright 2017 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 runtime_test
     6  
     7  import (
     8  	. "runtime"
     9  	"strconv"
    10  	"testing"
    11  	_ "unsafe" // for go:linkname
    12  )
    13  
    14  func TestReadRandom(t *testing.T) {
    15  	if *ReadRandomFailed {
    16  		switch GOOS {
    17  		default:
    18  			t.Fatalf("readRandom failed at startup")
    19  		case "plan9":
    20  			// ok
    21  		}
    22  	}
    23  }
    24  
    25  func BenchmarkCheaprand(b *testing.B) {
    26  	for b.Loop() {
    27  		Cheaprand()
    28  	}
    29  }
    30  
    31  func BenchmarkCheaprand64(b *testing.B) {
    32  	for b.Loop() {
    33  		Cheaprand64()
    34  	}
    35  }
    36  
    37  func BenchmarkFastrand(b *testing.B) {
    38  	b.RunParallel(func(pb *testing.PB) {
    39  		for pb.Next() {
    40  			Fastrand()
    41  		}
    42  	})
    43  }
    44  
    45  func BenchmarkFastrand64(b *testing.B) {
    46  	b.RunParallel(func(pb *testing.PB) {
    47  		for pb.Next() {
    48  			Fastrand64()
    49  		}
    50  	})
    51  }
    52  
    53  func BenchmarkFastrandHashiter(b *testing.B) {
    54  	var m = make(map[int]int, 10)
    55  	for i := 0; i < 10; i++ {
    56  		m[i] = i
    57  	}
    58  	b.RunParallel(func(pb *testing.PB) {
    59  		for pb.Next() {
    60  			for range m {
    61  				break
    62  			}
    63  		}
    64  	})
    65  }
    66  
    67  var sink32 uint32
    68  
    69  func BenchmarkFastrandn(b *testing.B) {
    70  	for n := uint32(2); n <= 5; n++ {
    71  		b.Run(strconv.Itoa(int(n)), func(b *testing.B) {
    72  			for i := 0; i < b.N; i++ {
    73  				sink32 = Fastrandn(n)
    74  			}
    75  		})
    76  	}
    77  }
    78  
    79  //go:linkname fastrand runtime.fastrand
    80  func fastrand() uint32
    81  
    82  //go:linkname fastrandn runtime.fastrandn
    83  func fastrandn(uint32) uint32
    84  
    85  //go:linkname fastrand64 runtime.fastrand64
    86  func fastrand64() uint64
    87  
    88  func TestLegacyFastrand(t *testing.T) {
    89  	// Testing mainly that the calls work at all,
    90  	// but check that all three don't return the same number (1 in 2^64 chance)
    91  	{
    92  		x, y, z := fastrand(), fastrand(), fastrand()
    93  		if x == y && y == z {
    94  			t.Fatalf("fastrand three times = %#x, %#x, %#x, want different numbers", x, y, z)
    95  		}
    96  	}
    97  	{
    98  		x, y, z := fastrandn(1e9), fastrandn(1e9), fastrandn(1e9)
    99  		if x == y && y == z {
   100  			t.Fatalf("fastrandn three times = %#x, %#x, %#x, want different numbers", x, y, z)
   101  		}
   102  	}
   103  	{
   104  		x, y, z := fastrand64(), fastrand64(), fastrand64()
   105  		if x == y && y == z {
   106  			t.Fatalf("fastrand64 three times = %#x, %#x, %#x, want different numbers", x, y, z)
   107  		}
   108  	}
   109  }
   110  

View as plain text