v8.run/go/exp@v0.0.26-0.20230226010534-afcdbd3f782d/fastrand/rng_test.go (about)

     1  package fastrand
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestFastRandReader_Read(t *testing.T) {
     8  	rng := AcquireRNG()
     9  	defer rng.Release()
    10  	r := &FastRandReader{RNG: rng}
    11  	b := make([]byte, 1024)
    12  	n, err := r.Read(b)
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  	if n != 1024 {
    17  		t.Fatal("wrong number of bytes read")
    18  	}
    19  }
    20  
    21  func BenchmarkFastRandReader_Read(b *testing.B) {
    22  	b.RunParallel(func(p *testing.PB) {
    23  		rng := AcquireRNG()
    24  		defer rng.Release()
    25  		r := &FastRandReader{RNG: rng}
    26  		buf := make([]byte, 4096)
    27  		b.SetBytes(4096)
    28  		for p.Next() {
    29  			_, _ = r.Read(buf)
    30  		}
    31  	})
    32  }