github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/soliton/fastrand/random_test.go (about) 1 // Copyright 2020-present WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package fastrand 15 16 import ( 17 "fmt" 18 "math/rand" 19 "testing" 20 21 . "github.com/whtcorpsinc/check" 22 ) 23 24 var _ = Suite(&testRandSuite{}) 25 26 type testRandSuite struct { 27 } 28 29 func (s *testRandSuite) TestRand(c *C) { 30 x := Uint32N(1024) 31 c.Assert(x < 1024, IsTrue) 32 y := Uint64N(1 << 63) 33 c.Assert(y < 1<<63, IsTrue) 34 35 _ = Buf(20) 36 var arr [256]bool 37 for i := 0; i < 1024; i++ { 38 idx := Uint32N(256) 39 arr[idx] = true 40 } 41 sum := 0 42 for i := 0; i < 256; i++ { 43 if arr[i] == false { 44 sum++ 45 } 46 } 47 fmt.Println(sum) 48 c.Assert(sum < 24, IsTrue) 49 } 50 51 func BenchmarkFastRand(b *testing.B) { 52 b.RunParallel(func(pb *testing.PB) { 53 for pb.Next() { 54 Uint32() 55 } 56 }) 57 b.Log(Uint32()) 58 } 59 60 func BenchmarkGlobalRand(b *testing.B) { 61 b.RunParallel(func(pb *testing.PB) { 62 for pb.Next() { 63 rand.Int() 64 } 65 }) 66 b.Log(rand.Int()) 67 }