test(hashx): add unsafe.Point test

This commit is contained in:
bandl 2021-11-13 20:04:21 +08:00
parent 400d620aa4
commit 5105a62bca
1 changed files with 27 additions and 0 deletions

View File

@ -1,7 +1,10 @@
package hashx
import (
"fmt"
"reflect"
"testing"
"unsafe"
"github.com/stretchr/testify/require"
)
@ -60,3 +63,27 @@ func TestHashX_Add(t *testing.T) {
require.NoError(t, err)
require.Equal(t, s, "2")
}
func Test_Pointer(t *testing.T) {
s := make([]int, 9, 20)
lens := *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + uintptr(8)))
fmt.Println(lens, len(s))
mp := make(map[string]int)
mp["qcrao"] = 100
mp["stefno"] = 18
count := **(**uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(&mp)) + uintptr(16)))
fmt.Println(count, len(mp)) // 2
}
func string2bytes(s string) []byte {
stringHeader := (*reflect.StringHeader)(unsafe.Pointer(&s))
result := reflect.SliceHeader{
Data: stringHeader.Data,
Len: stringHeader.Len,
Cap: stringHeader.Len,
}
return *(*[]byte)(unsafe.Pointer(&result))
}