2012-02-17 08:48:57 +04:00
|
|
|
// run
|
2008-11-07 09:27:32 +03:00
|
|
|
|
|
|
|
// Copyright 2009 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.
|
|
|
|
|
2012-02-19 10:44:02 +04:00
|
|
|
// Test simple select.
|
|
|
|
|
2008-11-07 09:27:32 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
var counter uint
|
|
|
|
var shift uint
|
|
|
|
|
2009-01-21 01:40:40 +03:00
|
|
|
func GetValue() uint {
|
2010-03-25 02:46:53 +03:00
|
|
|
counter++
|
2009-08-18 00:30:22 +04:00
|
|
|
return 1 << shift
|
2008-11-07 09:27:32 +03:00
|
|
|
}
|
|
|
|
|
2009-01-21 01:40:40 +03:00
|
|
|
func Send(a, b chan uint) int {
|
2010-03-25 02:46:53 +03:00
|
|
|
var i int
|
2009-08-18 00:30:22 +04:00
|
|
|
|
|
|
|
LOOP:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case a <- GetValue():
|
2010-03-25 02:46:53 +03:00
|
|
|
i++
|
|
|
|
a = nil
|
2009-08-18 00:30:22 +04:00
|
|
|
case b <- GetValue():
|
2010-03-25 02:46:53 +03:00
|
|
|
i++
|
|
|
|
b = nil
|
2009-08-18 00:30:22 +04:00
|
|
|
default:
|
2010-03-25 02:46:53 +03:00
|
|
|
break LOOP
|
2009-08-18 00:30:22 +04:00
|
|
|
}
|
2010-03-25 02:46:53 +03:00
|
|
|
shift++
|
2009-08-18 00:30:22 +04:00
|
|
|
}
|
2010-03-25 02:46:53 +03:00
|
|
|
return i
|
2008-11-07 09:27:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2010-03-25 02:46:53 +03:00
|
|
|
a := make(chan uint, 1)
|
|
|
|
b := make(chan uint, 1)
|
2009-08-18 00:30:22 +04:00
|
|
|
if v := Send(a, b); v != 2 {
|
2010-03-25 02:46:53 +03:00
|
|
|
println("Send returned", v, "!= 2")
|
|
|
|
panic("fail")
|
2009-08-18 00:30:22 +04:00
|
|
|
}
|
2010-03-25 02:46:53 +03:00
|
|
|
if av, bv := <-a, <-b; av|bv != 3 {
|
|
|
|
println("bad values", av, bv)
|
|
|
|
panic("fail")
|
2009-08-18 00:30:22 +04:00
|
|
|
}
|
|
|
|
if v := Send(a, nil); v != 1 {
|
2010-03-25 02:46:53 +03:00
|
|
|
println("Send returned", v, "!= 1")
|
|
|
|
panic("fail")
|
2009-08-18 00:30:22 +04:00
|
|
|
}
|
|
|
|
if counter != 10 {
|
2010-03-25 02:46:53 +03:00
|
|
|
println("counter is", counter, "!= 10")
|
|
|
|
panic("fail")
|
2009-08-18 00:30:22 +04:00
|
|
|
}
|
2008-11-07 09:27:32 +03:00
|
|
|
}
|