]> Cypherpunks.ru repositories - gostls13.git/blob - test/stack.go
closures - runtime and debugger support, test case
[gostls13.git] / 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                 panicln("bad defer", s);
29         }
30 }
31
32 var c = make(chan int);
33 var t T;
34
35 func recur(n int) {
36         go g(c, t);
37         s := <-c;
38         if s != len(t) {
39                 panicln("bad go", s);
40         }
41         f := func(t T) int {
42                 s := 0;
43                 for i := 0; i < len(t); i++ {
44                         s += t[i];
45                 }
46                 s += n;
47                 return s;
48         };
49         s = f(t);
50         if s != len(t) + n {
51                 panicln("bad func", s, "at level", n);
52         }
53         if n > 0 {
54                 recur(n-1);
55         }
56         defer d(t);
57 }
58
59 func main() {
60         for i := 0; i < len(t); i++ {
61                 t[i] = 1;
62         }
63         recur(10000);
64 }