Update to current master Go testsuite.
[official-gcc.git] / gcc / testsuite / go.test / test / stack.go
blob1fd57161ff2903aff1bd3b15ff38cff561e8f8a1
1 // $G $D/$F.go && $L $F.$A && ./$A.out
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Try to tickle stack splitting bugs by doing
8 // go, defer, and closure calls at different stack depths.
10 package main
12 type T [20]int
14 func g(c chan int, t T) {
15 s := 0
16 for i := 0; i < len(t); i++ {
17 s += t[i]
19 c <- s
22 func d(t T) {
23 s := 0
24 for i := 0; i < len(t); i++ {
25 s += t[i]
27 if s != len(t) {
28 println("bad defer", s)
29 panic("fail")
33 func f0() {
34 // likely to make a new stack for f0,
35 // because the call to f1 puts 3000 bytes
36 // in our frame.
37 f1()
40 func f1() [3000]byte {
41 // likely to make a new stack for f1,
42 // because 3000 bytes were used by f0
43 // and we need 3000 more for the call
44 // to f2. if the call to morestack in f1
45 // does not pass the frame size, the new
46 // stack (default size 5k) will not be big
47 // enough for the frame, and the morestack
48 // check in f2 will die, if we get that far
49 // without faulting.
50 f2()
51 return [3000]byte{}
54 func f2() [3000]byte {
55 // just take up space
56 return [3000]byte{}
59 var c = make(chan int)
60 var t T
61 var b = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
63 func recur(n int) {
64 ss := string(b)
65 if len(ss) != len(b) {
66 panic("bad []byte -> string")
68 go g(c, t)
69 f0()
70 s := <-c
71 if s != len(t) {
72 println("bad go", s)
73 panic("fail")
75 f := func(t T) int {
76 s := 0
77 for i := 0; i < len(t); i++ {
78 s += t[i]
80 s += n
81 return s
83 s = f(t)
84 if s != len(t)+n {
85 println("bad func", s, "at level", n)
86 panic("fail")
88 if n > 0 {
89 recur(n - 1)
91 defer d(t)
94 func main() {
95 for i := 0; i < len(t); i++ {
96 t[i] = 1
98 recur(8000)