Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / gcc / testsuite / go.test / test / closure.go
blob54e4cf8eae3d707860e18f6d7ee30f1fe05687a7
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 package main
9 var c = make(chan int)
11 func check(a []int) {
12 for i := 0; i < len(a); i++ {
13 n := <-c
14 if n != a[i] {
15 println("want", a[i], "got", n, "at", i)
16 panic("fail")
21 func f() {
22 var i, j int
24 i = 1
25 j = 2
26 f := func() {
27 c <- i
28 i = 4
29 g := func() {
30 c <- i
31 c <- j
33 g()
34 c <- i
36 j = 5
37 f()
40 // Accumulator generator
41 func accum(n int) func(int) int {
42 return func(i int) int {
43 n += i
44 return n
48 func g(a, b func(int) int) {
49 c <- a(2)
50 c <- b(3)
51 c <- a(4)
52 c <- b(5)
55 func h() {
56 var x8 byte = 100
57 var x64 int64 = 200
59 c <- int(x8)
60 c <- int(x64)
61 f := func(z int) {
62 g := func() {
63 c <- int(x8)
64 c <- int(x64)
65 c <- z
67 g()
68 c <- int(x8)
69 c <- int(x64)
70 c <- int(z)
72 x8 = 101
73 x64 = 201
74 f(500)
77 func newfunc() func(int) int { return func(x int) int { return x } }
80 func main() {
81 go f()
82 check([]int{1, 4, 5, 4})
84 a := accum(0)
85 b := accum(1)
86 go g(a, b)
87 check([]int{2, 4, 6, 9})
89 go h()
90 check([]int{100, 200, 101, 201, 500, 101, 201, 500})
92 x, y := newfunc(), newfunc()
93 if x == y {
94 println("newfunc returned same func")
95 panic("fail")
97 if x(1) != 1 || y(2) != 2 {
98 println("newfunc returned broken funcs")
99 panic("fail")