Commit 3dc278d3 authored by Gustavo Niemeyer's avatar Gustavo Niemeyer

reflect: fix Slice cap

R=golang-dev, dsymonds, r, rsc
CC=golang-dev
https://golang.org/cl/5483044
parent e6f5a90b
...@@ -1557,14 +1557,26 @@ func TestSmallNegativeInt(t *testing.T) { ...@@ -1557,14 +1557,26 @@ func TestSmallNegativeInt(t *testing.T) {
func TestSlice(t *testing.T) { func TestSlice(t *testing.T) {
xs := []int{1, 2, 3, 4, 5, 6, 7, 8} xs := []int{1, 2, 3, 4, 5, 6, 7, 8}
v := ValueOf(xs).Slice(3, 5).Interface().([]int) v := ValueOf(xs).Slice(3, 5).Interface().([]int)
if len(v) != 2 || v[0] != 4 || v[1] != 5 { if len(v) != 2 {
t.Errorf("xs.Slice(3, 5) = %v", v) t.Errorf("len(xs.Slice(3, 5)) = %d", len(v))
}
if cap(v) != 5 {
t.Errorf("cap(xs.Slice(3, 5)) = %d", cap(v))
}
if !DeepEqual(v[0:5], xs[3:]) {
t.Errorf("xs.Slice(3, 5)[0:5] = %v", v[0:5])
} }
xa := [7]int{10, 20, 30, 40, 50, 60, 70} xa := [8]int{10, 20, 30, 40, 50, 60, 70, 80}
v = ValueOf(&xa).Elem().Slice(2, 5).Interface().([]int) v = ValueOf(&xa).Elem().Slice(2, 5).Interface().([]int)
if len(v) != 3 || v[0] != 30 || v[1] != 40 || v[2] != 50 { if len(v) != 3 {
t.Errorf("xa.Slice(2, 5) = %v", v) t.Errorf("len(xa.Slice(2, 5)) = %d", len(v))
}
if cap(v) != 6 {
t.Errorf("cap(xa.Slice(2, 5)) = %d", cap(v))
}
if !DeepEqual(v[0:6], xa[2:]) {
t.Errorf("xs.Slice(2, 5)[0:6] = %v", v[0:6])
} }
} }
......
...@@ -1356,7 +1356,7 @@ func (v Value) Slice(beg, end int) Value { ...@@ -1356,7 +1356,7 @@ func (v Value) Slice(beg, end int) Value {
s := (*SliceHeader)(unsafe.Pointer(&x)) s := (*SliceHeader)(unsafe.Pointer(&x))
s.Data = uintptr(base) + uintptr(beg)*toCommonType(typ.elem).Size() s.Data = uintptr(base) + uintptr(beg)*toCommonType(typ.elem).Size()
s.Len = end - beg s.Len = end - beg
s.Cap = end - beg s.Cap = cap - beg
fl := v.flag&flagRO | flagIndir | flag(Slice)<<flagKindShift fl := v.flag&flagRO | flagIndir | flag(Slice)<<flagKindShift
return Value{typ.common(), unsafe.Pointer(&x), fl} return Value{typ.common(), unsafe.Pointer(&x), fl}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment