github.com/zhiqiangxu/util@v0.0.0-20230112053021-0a7aee056cd5/uuid_test.go (about)

     1  package util
     2  
     3  import (
     4  	"testing"
     5  
     6  	"golang.org/x/exp/rand"
     7  	"gotest.tools/assert"
     8  )
     9  
    10  func TestUUID(t *testing.T) {
    11  
    12  	assert.Assert(t, PoorManUUID(true)%2 == 1)
    13  	assert.Assert(t, PoorManUUID(false)%2 == 0)
    14  	assert.Assert(t, FastRandN(1) == 0)
    15  	for i := 2; i < 100; i++ {
    16  		assert.Assert(t, FastRandN(uint32(i)) < uint32(i))
    17  	}
    18  
    19  	// test empty interface
    20  	var i interface{}
    21  
    22  	type test struct {
    23  		a int
    24  		b string
    25  	}
    26  
    27  	var s test
    28  	s.a = 1
    29  	s.b = "1"
    30  
    31  	i = s
    32  	s.a = 2
    33  	s.b = "2"
    34  	assert.Assert(t, i.(test).a == 1)
    35  
    36  	var i2 interface{}
    37  
    38  	i2 = i
    39  
    40  	i = s
    41  	s.a = 3
    42  	s.b = "3"
    43  
    44  	assert.Assert(t, i.(test).a == 2 && i2.(test).a == 1)
    45  
    46  	// // this will error
    47  	// i2.(test).a = 3
    48  
    49  	// test slice
    50  	{
    51  		encID := make([]byte, 0, 10)
    52  		_ = append(encID, 'a')
    53  		assert.Assert(t, len(encID) == 0 && cap(encID) == 10)
    54  	}
    55  
    56  }
    57  
    58  func BenchmarkFastRand(b *testing.B) {
    59  	for i := 0; i < b.N; i++ {
    60  		FastRand()
    61  	}
    62  }
    63  
    64  func BenchmarkPCG(b *testing.B) {
    65  	r := rand.PCGSource{}
    66  	for i := 0; i < b.N; i++ {
    67  		r.Uint64()
    68  	}
    69  }