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 // Test the behavior of closures.
13 var c = make(chan int)
16 for i := 0; i < len(a); i++ {
19 println("want", a[i], "got", n, "at", i)
44 // Accumulator generator
45 func accum(n int) func(int) int {
46 return func(i int) int {
52 func g(a, b func(int) int) {
81 func newfunc() func(int) int { return func(x int) int { return x } }
87 check([]int{1, 4, 5, 4})
92 check([]int{2, 4, 6, 9})
95 check([]int{100, 200, 101, 201, 500, 101, 201, 500})
97 memstats := new(runtime.MemStats)
98 runtime.ReadMemStats(memstats)
99 n0 := memstats.Mallocs
101 x, y := newfunc(), newfunc()
102 if x(1) != 1 || y(2) != 2 {
103 println("newfunc returned broken funcs")
107 runtime.ReadMemStats(memstats)
108 if n0 != memstats.Mallocs {
109 println("newfunc allocated unexpectedly")