github.com/PandaGoAdmin/utils@v0.0.0-20211208134815-d5461603a00f/function_test.go (about) 1 package kgo 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "testing" 6 ) 7 8 func TestFun_GetVariateType(t *testing.T) { 9 var res string 10 11 res = GetVariateType(1) 12 assert.Equal(t, "int", res) 13 14 res = GetVariateType(intAstronomicalUnit) 15 assert.Equal(t, "int64", res) 16 17 res = GetVariateType(flPi1) 18 assert.Equal(t, "float32", res) 19 20 res = GetVariateType(floAvogadro) 21 assert.Equal(t, "float64", res) 22 23 res = GetVariateType(strHello) 24 assert.Equal(t, "string", res) 25 26 res = GetVariateType(true) 27 assert.Equal(t, "bool", res) 28 29 res = GetVariateType(rune('你')) 30 assert.Equal(t, "int32", res) 31 32 res = GetVariateType('你') 33 assert.Equal(t, "int32", res) 34 35 res = GetVariateType([]byte("你好")) 36 assert.Equal(t, "[]uint8", res) 37 } 38 39 func BenchmarkFun_GetVariateType(b *testing.B) { 40 b.ResetTimer() 41 for i := 0; i < b.N; i++ { 42 GetVariateType(intAstronomicalUnit) 43 } 44 } 45 46 func TestFun_GetVariatePointerAddr(t *testing.T) { 47 var tests = []struct { 48 input interface{} 49 expected float64 50 }{ 51 {intSpeedLight, 0}, 52 {strHello, 0}, 53 {crowd, 0}, 54 } 55 for _, test := range tests { 56 actual := GetVariatePointerAddr(test.input) 57 assert.Greater(t, actual, int64(test.expected)) 58 } 59 } 60 61 func BenchmarkFun_GetVariatePointerAddr(b *testing.B) { 62 b.ResetTimer() 63 for i := 0; i < b.N; i++ { 64 GetVariatePointerAddr(intSpeedLight) 65 } 66 } 67 68 func TestFun_IsPointer(t *testing.T) { 69 var chk bool 70 71 //非指针 72 chk = IsPointer(itfObj, false) 73 assert.False(t, chk) 74 75 //指针 76 chk = IsPointer(orgS1, false) 77 assert.True(t, chk) 78 79 //非nil指针 80 chk = IsPointer(orgS1, true) 81 assert.True(t, chk) 82 83 //空指针 84 chk = IsPointer(itfObj, true) 85 assert.False(t, chk) 86 } 87 88 func BenchmarkFun_IsPointer(b *testing.B) { 89 b.ResetTimer() 90 for i := 0; i < b.N; i++ { 91 IsPointer(orgS1, true) 92 } 93 }