reflect: handle String kind in Value.{Pointer,UnsafePointer}

Updates #61308

Change-Id: I92d459383c520d137787ce5c8f135d205af74e5d
Reviewed-on: https://go-review.googlesource.com/c/go/+/516596
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Cuong Manh Le 2023-08-08 09:51:22 +07:00 committed by Gopher Robot
parent e074fcc945
commit 3d61f24835
2 changed files with 47 additions and 3 deletions

View file

@ -8512,3 +8512,39 @@ func TestClear(t *testing.T) {
})
}
}
func TestValuePointerAndUnsafePointer(t *testing.T) {
ptr := new(int)
ch := make(chan int)
m := make(map[int]int)
unsafePtr := unsafe.Pointer(ptr)
slice := make([]int, 1)
fn := func() {}
s := "foo"
tests := []struct {
name string
val Value
wantUnsafePointer unsafe.Pointer
}{
{"pointer", ValueOf(ptr), unsafe.Pointer(ptr)},
{"channel", ValueOf(ch), *(*unsafe.Pointer)(unsafe.Pointer(&ch))},
{"map", ValueOf(m), *(*unsafe.Pointer)(unsafe.Pointer(&m))},
{"unsafe.Pointer", ValueOf(unsafePtr), unsafePtr},
{"function", ValueOf(fn), **(**unsafe.Pointer)(unsafe.Pointer(&fn))},
{"slice", ValueOf(slice), unsafe.Pointer(unsafe.SliceData(slice))},
{"string", ValueOf(s), unsafe.Pointer(unsafe.StringData(s))},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
if got := tc.val.Pointer(); got != uintptr(tc.wantUnsafePointer) {
t.Errorf("unexpected uintptr result, got %#x, want %#x", got, uintptr(tc.wantUnsafePointer))
}
if got := tc.val.UnsafePointer(); got != tc.wantUnsafePointer {
t.Errorf("unexpected unsafe.Pointer result, got %#x, want %#x", got, tc.wantUnsafePointer)
}
})
}
}