URL
https://opencores.org/ocsvn/openrisc/openrisc/trunk
Subversion Repositories openrisc
[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [go.test/] [test/] [closure.go] - Rev 700
Compare with Previous | Blame | View Log
// $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 mainimport "runtime"var c = make(chan int)func check(a []int) {for i := 0; i < len(a); i++ {n := <-cif n != a[i] {println("want", a[i], "got", n, "at", i)panic("fail")}}}func f() {var i, j inti = 1j = 2f := func() {c <- ii = 4g := func() {c <- ic <- j}g()c <- i}j = 5f()}// Accumulator generatorfunc accum(n int) func(int) int {return func(i int) int {n += ireturn 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 = 100var x64 int64 = 200c <- 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 = 101x64 = 201f(500)}func newfunc() func(int) int { return func(x int) int { return x } }func main() {var fail boolgo 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})runtime.UpdateMemStats()n0 := runtime.MemStats.Mallocsx, y := newfunc(), newfunc()if x(1) != 1 || y(2) != 2 {println("newfunc returned broken funcs")fail = true}runtime.UpdateMemStats()if n0 != runtime.MemStats.Mallocs {println("newfunc allocated unexpectedly")fail = true}ff(1)if fail {panic("fail")}}func ff(x int) {call(func() {_ = x})}func call(func()) {}
