mirror of
https://github.com/golang/go
synced 2024-11-22 10:44:41 -07:00
reflect: fix Slice cap
R=golang-dev, dsymonds, r, rsc CC=golang-dev https://golang.org/cl/5483044
This commit is contained in:
parent
e6f5a90b5f
commit
3dc278d3e2
@ -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 {
|
|||||||
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}
|
||||||
|
Loading…
Reference in New Issue
Block a user