github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/test/testdata/slice_test.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // This test makes sure that t.s = t.s[0:x] doesn't write 6 // either the slice pointer or the capacity. 7 // See issue #14855. 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] // should only write len 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 }