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 } }
88 check([]int{1, 4, 5, 4})
93 check([]int{2, 4, 6, 9})
96 check([]int{100, 200, 101, 201, 500, 101, 201, 500})
98 memstats := new(runtime.MemStats)
99 runtime.ReadMemStats(memstats)
100 n0 := memstats.Mallocs
102 x, y := newfunc(), newfunc()
103 if x(1) != 1 || y(2) != 2 {
104 println("newfunc returned broken funcs")
108 runtime.ReadMemStats(memstats)
109 if n0 != memstats.Mallocs {
110 println("newfunc allocated unexpectedly")