1
2
3
4
5
6
7
8
9 package main
10
11 import "testing"
12
13 const N = 1000000
14
15 type X struct {
16 s []int
17 }
18
19 func TestSlice(t *testing.T) {
20 done := make(chan struct{})
21 a := make([]int, N+10)
22
23 x := &X{a}
24
25 go func() {
26 for i := 0; i < N; i++ {
27 x.s = x.s[1:9]
28 }
29 done <- struct{}{}
30 }()
31 go func() {
32 for i := 0; i < N; i++ {
33 x.s = x.s[0:8]
34 }
35 done <- struct{}{}
36 }()
37 <-done
38 <-done
39
40 if cap(x.s) != cap(a)-N {
41 t.Errorf("wanted cap=%d, got %d\n", cap(a)-N, cap(x.s))
42 }
43 if &x.s[0] != &a[N] {
44 t.Errorf("wanted ptr=%p, got %p\n", &a[N], &x.s[0])
45 }
46 }
47
View as plain text