OSDN Git Service

Add Go frontend, libgo library, and Go testsuite.
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / go.test / test / stack.go
1 // $G $D/$F.go && $L $F.$A && ./$A.out
2
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.
6
7 // Try to tickle stack splitting bugs by doing
8 // go, defer, and closure calls at different stack depths.
9
10 package main
11
12 type T [20]int
13
14 func g(c chan int, t T) {
15         s := 0
16         for i := 0; i < len(t); i++ {
17                 s += t[i]
18         }
19         c <- s
20 }
21
22 func d(t T) {
23         s := 0
24         for i := 0; i < len(t); i++ {
25                 s += t[i]
26         }
27         if s != len(t) {
28                 println("bad defer", s)
29                 panic("fail")
30         }
31 }
32
33 var c = make(chan int)
34 var t T
35 var b = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
36
37 func recur(n int) {
38         ss := string(b)
39         if len(ss) != len(b) {
40                 panic("bad []byte -> string")
41         }
42         go g(c, t)
43         s := <-c
44         if s != len(t) {
45                 println("bad go", s)
46                 panic("fail")
47         }
48         f := func(t T) int {
49                 s := 0
50                 for i := 0; i < len(t); i++ {
51                         s += t[i]
52                 }
53                 s += n
54                 return s
55         }
56         s = f(t)
57         if s != len(t)+n {
58                 println("bad func", s, "at level", n)
59                 panic("fail")
60         }
61         if n > 0 {
62                 recur(n - 1)
63         }
64         defer d(t)
65 }
66
67 func main() {
68         for i := 0; i < len(t); i++ {
69                 t[i] = 1
70         }
71         recur(8000)
72 }