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

     1  package fastrand
     2  
     3  import "testing"
     4  
     5  func TestFloat64Range(t *testing.T) {
     6  	for i := 0; i < 1000000; i++ {
     7  		f := Float64()
     8  		if !(f < 1 && f >= 0) {
     9  			t.Fatalf("got %f, want [0, 1)", f)
    10  		}
    11  	}
    12  }
    13  
    14  func TestFloat32Range(t *testing.T) {
    15  	for i := 0; i < 1000000; i++ {
    16  		f := Float32()
    17  		if !(f < 1 && f >= 0) {
    18  			t.Fatalf("got %f, want [0, 1)", f)
    19  		}
    20  	}
    21  }
    22  
    23  func BenchmarkFloat64(b *testing.B) {
    24  	b.RunParallel(func(p *testing.PB) {
    25  		rng := AcquireRNG()
    26  		for p.Next() {
    27  			f := rng.Float64()
    28  			if !(f < 1 && f >= 0) {
    29  				b.Fatalf("got %f, want [0, 1)", f)
    30  			}
    31  		}
    32  		ReleaseRNG(rng)
    33  	})
    34  }
    35  
    36  func BenchmarkFloat32(b *testing.B) {
    37  	b.RunParallel(func(p *testing.PB) {
    38  		rng := AcquireRNG()
    39  		for p.Next() {
    40  			f := rng.Float32()
    41  			if !(f < 1 && f >= 0) {
    42  				b.Fatalf("got %f, want [0, 1)", f)
    43  			}
    44  		}
    45  		ReleaseRNG(rng)
    46  	})
    47  }