OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [go.test/] [test/] [closure.go] - Blame information for rev 700

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 700 jeremybenn
// $G $D/$F.go && $L $F.$A && ./$A.out
2
 
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.
6
 
7
package main
8
 
9
import "runtime"
10
 
11
var c = make(chan int)
12
 
13
func check(a []int) {
14
        for i := 0; i < len(a); i++ {
15
                n := <-c
16
                if n != a[i] {
17
                        println("want", a[i], "got", n, "at", i)
18
                        panic("fail")
19
                }
20
        }
21
}
22
 
23
func f() {
24
        var i, j int
25
 
26
        i = 1
27
        j = 2
28
        f := func() {
29
                c <- i
30
                i = 4
31
                g := func() {
32
                        c <- i
33
                        c <- j
34
                }
35
                g()
36
                c <- i
37
        }
38
        j = 5
39
        f()
40
}
41
 
42
// Accumulator generator
43
func accum(n int) func(int) int {
44
        return func(i int) int {
45
                n += i
46
                return n
47
        }
48
}
49
 
50
func g(a, b func(int) int) {
51
        c <- a(2)
52
        c <- b(3)
53
        c <- a(4)
54
        c <- b(5)
55
}
56
 
57
func h() {
58
        var x8 byte = 100
59
        var x64 int64 = 200
60
 
61
        c <- int(x8)
62
        c <- int(x64)
63
        f := func(z int) {
64
                g := func() {
65
                        c <- int(x8)
66
                        c <- int(x64)
67
                        c <- z
68
                }
69
                g()
70
                c <- int(x8)
71
                c <- int(x64)
72
                c <- int(z)
73
        }
74
        x8 = 101
75
        x64 = 201
76
        f(500)
77
}
78
 
79
func newfunc() func(int) int { return func(x int) int { return x } }
80
 
81
func main() {
82
        var fail bool
83
 
84
        go f()
85
        check([]int{1, 4, 5, 4})
86
 
87
        a := accum(0)
88
        b := accum(1)
89
        go g(a, b)
90
        check([]int{2, 4, 6, 9})
91
 
92
        go h()
93
        check([]int{100, 200, 101, 201, 500, 101, 201, 500})
94
 
95
        runtime.UpdateMemStats()
96
        n0 := runtime.MemStats.Mallocs
97
 
98
        x, y := newfunc(), newfunc()
99
        if x(1) != 1 || y(2) != 2 {
100
                println("newfunc returned broken funcs")
101
                fail = true
102
        }
103
 
104
        runtime.UpdateMemStats()
105
        if n0 != runtime.MemStats.Mallocs {
106
                println("newfunc allocated unexpectedly")
107
                fail = true
108
        }
109
 
110
        ff(1)
111
 
112
        if fail {
113
                panic("fail")
114
        }
115
}
116
 
117
func ff(x int) {
118
        call(func() {
119
                _ = x
120
        })
121
}
122
 
123
func call(func()) {
124
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.