OSDN Git Service

Merge branch 'trunk' of git://gcc.gnu.org/git/gcc into rework
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / go.test / test / chan / fifo.go
1 // $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 // Verify that unbuffered channels act as pure fifos.
8
9 package main
10
11 import "os"
12
13 const N = 10
14
15 func AsynchFifo() {
16         ch := make(chan int, N)
17         for i := 0; i < N; i++ {
18                 ch <- i
19         }
20         for i := 0; i < N; i++ {
21                 if <-ch != i {
22                         print("bad receive\n")
23                         os.Exit(1)
24                 }
25         }
26 }
27
28 func Chain(ch <-chan int, val int, in <-chan int, out chan<- int) {
29         <-in
30         if <-ch != val {
31                 panic(val)
32         }
33         out <- 1
34 }
35
36 // thread together a daisy chain to read the elements in sequence
37 func SynchFifo() {
38         ch := make(chan int)
39         in := make(chan int)
40         start := in
41         for i := 0; i < N; i++ {
42                 out := make(chan int)
43                 go Chain(ch, i, in, out)
44                 in = out
45         }
46         start <- 0
47         for i := 0; i < N; i++ {
48                 ch <- i
49         }
50         <-in
51 }
52
53 func main() {
54         AsynchFifo()
55         SynchFifo()
56 }
57