Russ Cox | d2cc988 | 2012-02-16 23:50:37 -0500 | [diff] [blame] | 1 | // run |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 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 | |
Rob Pike | 83976e3 | 2012-02-19 14:28:53 +1100 | [diff] [blame] | 7 | // Test functions and goroutines. |
| 8 | |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 9 | package main |
| 10 | |
| 11 | func caller(f func(int, int) int, a, b int, c chan int) { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 12 | c <- f(a, b) |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 13 | } |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 14 | |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 15 | func gocall(f func(int, int) int, a, b int) int { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 16 | c := make(chan int) |
| 17 | go caller(f, a, b, c) |
| 18 | return <-c |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | func call(f func(int, int) int, a, b int) int { |
| 22 | return f(a, b) |
| 23 | } |
| 24 | |
| 25 | func call1(f func(int, int) int, a, b int) int { |
| 26 | return call(f, a, b) |
| 27 | } |
| 28 | |
| 29 | var f func(int, int) int |
| 30 | |
| 31 | func add(x, y int) int { |
| 32 | return x + y |
| 33 | } |
| 34 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 35 | func fn() func(int, int) int { |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 36 | return f |
| 37 | } |
| 38 | |
| 39 | var fc func(int, int, chan int) |
| 40 | |
| 41 | func addc(x, y int, c chan int) { |
| 42 | c <- x+y |
| 43 | } |
| 44 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 45 | func fnc() func(int, int, chan int) { |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 46 | return fc |
| 47 | } |
| 48 | |
| 49 | func three(x int) { |
| 50 | if x != 3 { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 51 | println("wrong val", x) |
| 52 | panic("fail") |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | var notmain func() |
| 57 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 58 | func emptyresults() {} |
| 59 | func noresults() {} |
Rob Pike | 5e11bb2 | 2009-09-14 13:09:53 -0700 | [diff] [blame] | 60 | |
| 61 | var nothing func() |
| 62 | |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 63 | func main() { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 64 | three(call(add, 1, 2)) |
| 65 | three(call1(add, 1, 2)) |
| 66 | f = add |
| 67 | three(call(f, 1, 2)) |
| 68 | three(call1(f, 1, 2)) |
| 69 | three(call(fn(), 1, 2)) |
| 70 | three(call1(fn(), 1, 2)) |
| 71 | three(call(func(a, b int) int { return a + b }, 1, 2)) |
| 72 | three(call1(func(a, b int) int { return a + b }, 1, 2)) |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 73 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 74 | fc = addc |
| 75 | c := make(chan int) |
| 76 | go addc(1, 2, c) |
| 77 | three(<-c) |
| 78 | go fc(1, 2, c) |
| 79 | three(<-c) |
| 80 | go fnc()(1, 2, c) |
| 81 | three(<-c) |
| 82 | go func(a, b int, c chan int) { c <- a+b }(1, 2, c) |
| 83 | three(<-c) |
Rob Pike | 5e11bb2 | 2009-09-14 13:09:53 -0700 | [diff] [blame] | 84 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 85 | emptyresults() |
| 86 | noresults() |
| 87 | nothing = emptyresults |
| 88 | nothing() |
| 89 | nothing = noresults |
| 90 | nothing() |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 91 | } |