summaryrefslogtreecommitdiff
path: root/gcc/testsuite/go.test/test/closure.go
diff options
context:
space:
mode:
authorupstream source tree <ports@midipix.org>2015-03-15 20:14:05 -0400
committerupstream source tree <ports@midipix.org>2015-03-15 20:14:05 -0400
commit554fd8c5195424bdbcabf5de30fdc183aba391bd (patch)
tree976dc5ab7fddf506dadce60ae936f43f58787092 /gcc/testsuite/go.test/test/closure.go
downloadcbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.bz2
cbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.xz
obtained gcc-4.6.4.tar.bz2 from upstream website;upstream
verified gcc-4.6.4.tar.bz2.sig; imported gcc-4.6.4 source tree from verified upstream tarball. downloading a git-generated archive based on the 'upstream' tag should provide you with a source tree that is binary identical to the one extracted from the above tarball. if you have obtained the source via the command 'git clone', however, do note that line-endings of files in your working directory might differ from line-endings of the respective files in the upstream repository.
Diffstat (limited to 'gcc/testsuite/go.test/test/closure.go')
-rw-r--r--gcc/testsuite/go.test/test/closure.go101
1 files changed, 101 insertions, 0 deletions
diff --git a/gcc/testsuite/go.test/test/closure.go b/gcc/testsuite/go.test/test/closure.go
new file mode 100644
index 000000000..54e4cf8ea
--- /dev/null
+++ b/gcc/testsuite/go.test/test/closure.go
@@ -0,0 +1,101 @@
+// $G $D/$F.go && $L $F.$A && ./$A.out
+
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+var c = make(chan int)
+
+func check(a []int) {
+ for i := 0; i < len(a); i++ {
+ n := <-c
+ if n != a[i] {
+ println("want", a[i], "got", n, "at", i)
+ panic("fail")
+ }
+ }
+}
+
+func f() {
+ var i, j int
+
+ i = 1
+ j = 2
+ f := func() {
+ c <- i
+ i = 4
+ g := func() {
+ c <- i
+ c <- j
+ }
+ g()
+ c <- i
+ }
+ j = 5
+ f()
+}
+
+// Accumulator generator
+func accum(n int) func(int) int {
+ return func(i int) int {
+ n += i
+ return n
+ }
+}
+
+func g(a, b func(int) int) {
+ c <- a(2)
+ c <- b(3)
+ c <- a(4)
+ c <- b(5)
+}
+
+func h() {
+ var x8 byte = 100
+ var x64 int64 = 200
+
+ c <- int(x8)
+ c <- int(x64)
+ f := func(z int) {
+ g := func() {
+ c <- int(x8)
+ c <- int(x64)
+ c <- z
+ }
+ g()
+ c <- int(x8)
+ c <- int(x64)
+ c <- int(z)
+ }
+ x8 = 101
+ x64 = 201
+ f(500)
+}
+
+func newfunc() func(int) int { return func(x int) int { return x } }
+
+
+func main() {
+ go f()
+ check([]int{1, 4, 5, 4})
+
+ a := accum(0)
+ b := accum(1)
+ go g(a, b)
+ check([]int{2, 4, 6, 9})
+
+ go h()
+ check([]int{100, 200, 101, 201, 500, 101, 201, 500})
+
+ x, y := newfunc(), newfunc()
+ if x == y {
+ println("newfunc returned same func")
+ panic("fail")
+ }
+ if x(1) != 1 || y(2) != 2 {
+ println("newfunc returned broken funcs")
+ panic("fail")
+ }
+}