forked from p93542168/wheat-cache
test(hashx): add unsafe.Point test
This commit is contained in:
parent
400d620aa4
commit
5105a62bca
|
@ -1,7 +1,10 @@
|
||||||
package hashx
|
package hashx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
@ -60,3 +63,27 @@ func TestHashX_Add(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, s, "2")
|
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))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue