1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 00:22:41 +08:00
Files
go-mixed/rand/rand_test.go
2022-03-31 11:41:41 +08:00

152 lines
2.4 KiB
Go

package rand_test
import (
"bytes"
"testing"
"time"
mrnd "math/rand"
"github.com/charlienet/go-mixed/rand"
)
func TestRandString(t *testing.T) {
t.Log(rand.AllChars.Generate(20))
// b, err := rand.RandBytes(32)
// t.Log(err)
// t.Log(hex.EncodeToString(b))
}
func TestRandHex(t *testing.T) {
h := rand.Hex.Generate(8)
t.Log(h)
}
func TestRandMax(t *testing.T) {
mrnd.Seed(time.Now().UnixNano())
}
func TestCryptRand(t *testing.T) {
t.Log(rand.CryptoIntn(56545))
}
func TestGenericsInterger(t *testing.T) {
var max int32 = 55
for i := 0; i < 1000; i++ {
if rand.Intn(max) >= max {
t.Fatal("生成的值大于最大值:", max)
}
}
}
func TestRange(t *testing.T) {
min := 20
max := 200000
for i := 0; i < 100000000; i++ {
n := rand.IntRange(min, max)
if n < min {
t.Fatal("生成的值小于最小值:", min, n)
}
if n >= max {
t.Fatal("生成的值大于最大值:", min, n)
}
}
}
func BenchmarkParallel(b *testing.B) {
rand.Hex.Generate(16)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
rand.Hex.Generate(16)
}
})
}
func BenchmarkNoop(b *testing.B) {
for i := 0; i < b.N; i++ {
rand.AllChars.Generate(16)
}
}
func BenchmarkRandString(b *testing.B) {
for i := 0; i < 10; i++ {
rand.Hex.Generate(10)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
rand.Hex.Generate(20)
}
}
func BenchmarkString(b *testing.B) {
elems := []byte("abcdefghijk")
b.Run("1", func(b *testing.B) {
a := []byte{}
for i := 0; i < b.N; i++ {
for _, elem := range elems {
a = append(a, elem)
}
}
})
b.Run("2", func(b *testing.B) {
a := make([]byte, len(elems))
for i := 0; i < b.N; i++ {
for _, elem := range elems {
a = append(a, elem)
}
}
})
b.Run("3", func(b *testing.B) {
a := make([]byte, len(elems))
for i := 0; i < b.N; i++ {
a = append(a, elems...)
}
})
}
func BenchmarkConcatString(b *testing.B) {
elems := []string{"1", "2", "3", "4", "5"}
b.Run("add", func(b *testing.B) {
for i := 0; i < b.N; i++ {
ret := ""
for _, elem := range elems {
ret += elem
}
}
})
b.Run("buffer", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var buf bytes.Buffer
for _, elem := range elems {
buf.WriteString(elem)
}
}
})
}
func BenchmarkRand(b *testing.B) {
b.Run("math/rand", func(b *testing.B) {
for i := 0; i < b.N; i++ {
rand.Intn(100)
}
})
b.Run("crypto/rand", func(b *testing.B) {
for i := 0; i < b.N; i++ {
rand.CryptoIntn(100)
}
})
}