diff options
author | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
---|---|---|
committer | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
commit | 554fd8c5195424bdbcabf5de30fdc183aba391bd (patch) | |
tree | 976dc5ab7fddf506dadce60ae936f43f58787092 /gcc/testsuite/go.test/test/chan/select3.go | |
download | cbb-gcc-4.6.4-15d2061ac0796199866debe9ac87130894b0cdd3.tar.bz2 cbb-gcc-4.6.4-15d2061ac0796199866debe9ac87130894b0cdd3.tar.xz |
obtained gcc-4.6.4.tar.bz2 from upstream website;upstream
verified gcc-4.6.4.tar.bz2.sig;
imported gcc-4.6.4 source tree from verified upstream tarball.
downloading a git-generated archive based on the 'upstream' tag
should provide you with a source tree that is binary identical
to the one extracted from the above tarball.
if you have obtained the source via the command 'git clone',
however, do note that line-endings of files in your working
directory might differ from line-endings of the respective
files in the upstream repository.
Diffstat (limited to 'gcc/testsuite/go.test/test/chan/select3.go')
-rw-r--r-- | gcc/testsuite/go.test/test/chan/select3.go | 203 |
1 files changed, 203 insertions, 0 deletions
diff --git a/gcc/testsuite/go.test/test/chan/select3.go b/gcc/testsuite/go.test/test/chan/select3.go new file mode 100644 index 000000000..a1a2ef50b --- /dev/null +++ b/gcc/testsuite/go.test/test/chan/select3.go @@ -0,0 +1,203 @@ +// $G $D/$F.go && $L $F.$A && ./$A.out + +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Tests verifying the semantics of the select statement +// for basic empty/non-empty cases. + +package main + +import "time" + +const always = "function did not" +const never = "function did" + + +func unreachable() { + panic("control flow shouldn't reach here") +} + + +// Calls f and verifies that f always/never panics depending on signal. +func testPanic(signal string, f func()) { + defer func() { + s := never + if recover() != nil { + s = always // f panicked + } + if s != signal { + panic(signal + " panic") + } + }() + f() +} + + +// Calls f and empirically verifies that f always/never blocks depending on signal. +func testBlock(signal string, f func()) { + c := make(chan string) + go func() { + f() + c <- never // f didn't block + }() + go func() { + time.Sleep(1e8) // 0.1s seems plenty long + c <- always // f blocked always + }() + if <-c != signal { + panic(signal + " block") + } +} + + +func main() { + const async = 1 // asynchronous channels + var nilch chan int + closedch := make(chan int) + close(closedch) + + // sending/receiving from a nil channel outside a select panics + testPanic(always, func() { + nilch <- 7 + }) + testPanic(always, func() { + <-nilch + }) + + // sending/receiving from a nil channel inside a select never panics + testPanic(never, func() { + select { + case nilch <- 7: + unreachable() + default: + } + }) + testPanic(never, func() { + select { + case <-nilch: + unreachable() + default: + } + }) + + // sending to an async channel with free buffer space never blocks + testBlock(never, func() { + ch := make(chan int, async) + ch <- 7 + }) + + // receiving (a small number of times) from a closed channel never blocks + testBlock(never, func() { + for i := 0; i < 10; i++ { + if <-closedch != 0 { + panic("expected zero value when reading from closed channel") + } + } + }) + + // sending (a small number of times) to a closed channel is not specified + // but the current implementation doesn't block: test that different + // implementations behave the same + testBlock(never, func() { + for i := 0; i < 10; i++ { + closedch <- 7 + } + }) + + // receiving from a non-ready channel always blocks + testBlock(always, func() { + ch := make(chan int) + <-ch + }) + + // empty selects always block + testBlock(always, func() { + select { + } + }) + + // selects with only nil channels always block + testBlock(always, func() { + select { + case <-nilch: + unreachable() + } + }) + testBlock(always, func() { + select { + case nilch <- 7: + unreachable() + } + }) + testBlock(always, func() { + select { + case <-nilch: + unreachable() + case nilch <- 7: + unreachable() + } + }) + + // selects with non-ready non-nil channels always block + testBlock(always, func() { + ch := make(chan int) + select { + case <-ch: + unreachable() + } + }) + + // selects with default cases don't block + testBlock(never, func() { + select { + default: + } + }) + testBlock(never, func() { + select { + case <-nilch: + unreachable() + default: + } + }) + testBlock(never, func() { + select { + case nilch <- 7: + unreachable() + default: + } + }) + + // selects with ready channels don't block + testBlock(never, func() { + ch := make(chan int, async) + select { + case ch <- 7: + default: + unreachable() + } + }) + testBlock(never, func() { + ch := make(chan int, async) + ch <- 7 + select { + case <-ch: + default: + unreachable() + } + }) + + // selects with closed channels don't block + testBlock(never, func() { + select { + case <-closedch: + } + }) + testBlock(never, func() { + select { + case closedch <- 7: + } + }) +} |