mirror of
https://github.com/golang/go
synced 2024-11-02 11:50:30 +00: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
2 changed files with 18 additions and 6 deletions
|
@ -1557,14 +1557,26 @@ func TestSmallNegativeInt(t *testing.T) {
|
|||
func TestSlice(t *testing.T) {
|
||||
xs := []int{1, 2, 3, 4, 5, 6, 7, 8}
|
||||
v := ValueOf(xs).Slice(3, 5).Interface().([]int)
|
||||
if len(v) != 2 || v[0] != 4 || v[1] != 5 {
|
||||
t.Errorf("xs.Slice(3, 5) = %v", v)
|
||||
if len(v) != 2 {
|
||||
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)
|
||||
if len(v) != 3 || v[0] != 30 || v[1] != 40 || v[2] != 50 {
|
||||
t.Errorf("xa.Slice(2, 5) = %v", v)
|
||||
if len(v) != 3 {
|
||||
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.Data = uintptr(base) + uintptr(beg)*toCommonType(typ.elem).Size()
|
||||
s.Len = end - beg
|
||||
s.Cap = end - beg
|
||||
s.Cap = cap - beg
|
||||
|
||||
fl := v.flag&flagRO | flagIndir | flag(Slice)<<flagKindShift
|
||||
return Value{typ.common(), unsafe.Pointer(&x), fl}
|
||||
|
|
Loading…
Reference in a new issue