github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zstring/rand_test.go (about)

     1  package zstring
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/sohaha/zlsgo"
     7  )
     8  
     9  func TestRand(T *testing.T) {
    10  	t := zlsgo.NewTest(T)
    11  	t.Log(Rand(4), Rand(10), Rand(4, "a1"))
    12  	t.Log(RandInt(4, 10), RandInt(1, 10), RandInt(1, 2), RandInt(1, 0))
    13  	t.Log(RandUint32Max(10), RandUint32Max(100), RandUint32Max(1000), RandUint32Max(10000))
    14  	t.Log(UUID())
    15  }
    16  
    17  func TestUniqueID(T *testing.T) {
    18  	t := zlsgo.NewTest(T)
    19  	t.Log(UniqueID(4), UniqueID(10), UniqueID(0), UniqueID(-6))
    20  }
    21  
    22  func TestWeightedRand(T *testing.T) {
    23  	t := zlsgo.NewTest(T)
    24  
    25  	c := map[interface{}]uint32{
    26  		"a": 1,
    27  		"b": 6,
    28  		"z": 8,
    29  		"c": 3,
    30  	}
    31  
    32  	t.Log(WeightedRand(c))
    33  
    34  	w, err := NewWeightedRand(c)
    35  	t.NoError(err)
    36  	t.Log(w.Pick())
    37  
    38  	_, err = NewWeightedRand(map[interface{}]uint32{
    39  		"a": ^uint32(0),
    40  		"b": 6,
    41  	})
    42  	t.Log(err)
    43  	t.EqualTrue(err != nil)
    44  }
    45  
    46  func BenchmarkRandStr(b *testing.B) {
    47  	for i := 0; i < b.N; i++ {
    48  		Rand(1)
    49  	}
    50  }
    51  
    52  func BenchmarkRandStr2(b *testing.B) {
    53  	for i := 0; i < b.N; i++ {
    54  		Rand(10)
    55  	}
    56  }
    57  
    58  func BenchmarkRandInt(b *testing.B) {
    59  	for i := 0; i < b.N; i++ {
    60  		RandInt(10, 99)
    61  	}
    62  }
    63  
    64  func BenchmarkRandInt2(b *testing.B) {
    65  	for i := 0; i < b.N; i++ {
    66  		RandInt(10000, 99999)
    67  	}
    68  }