2012-04-20 15:45:43 +00:00
|
|
|
// runoutput
|
2011-01-30 20:46:02 +00:00
|
|
|
|
2016-04-10 21:32:26 +00:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
2011-01-30 20:46:02 +00:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Generate test of channel operations and simple selects.
|
2012-02-19 06:44:02 +00:00
|
|
|
// The output of this program is compiled and run to do the
|
|
|
|
// actual test.
|
|
|
|
|
|
|
|
// Each test does only one real send or receive at a time, but phrased
|
2011-01-30 20:46:02 +00:00
|
|
|
// in various ways that the compiler may or may not rewrite
|
|
|
|
// into simpler expressions.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2011-11-08 23:43:02 +00:00
|
|
|
"text/template"
|
2011-01-30 20:46:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
out := bufio.NewWriter(os.Stdout)
|
|
|
|
fmt.Fprintln(out, header)
|
|
|
|
a := new(arg)
|
|
|
|
|
2014-06-17 16:07:18 +00:00
|
|
|
// Generate each test as a separate function to avoid
|
2015-06-23 23:50:12 +00:00
|
|
|
// hitting the gc optimizer with one enormous function.
|
2011-01-30 20:46:02 +00:00
|
|
|
// If we name all the functions init we don't have to
|
|
|
|
// maintain a list of which ones to run.
|
|
|
|
do := func(t *template.Template) {
|
|
|
|
for ; next(); a.reset() {
|
2014-06-17 16:07:18 +00:00
|
|
|
fmt.Fprintln(out, `func init() {`)
|
2011-01-30 20:46:02 +00:00
|
|
|
run(t, a, out)
|
2014-06-17 16:07:18 +00:00
|
|
|
fmt.Fprintln(out, `}`)
|
2011-01-30 20:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-17 04:34:48 +00:00
|
|
|
|
2011-01-30 20:46:02 +00:00
|
|
|
do(recv)
|
|
|
|
do(send)
|
|
|
|
do(recvOrder)
|
|
|
|
do(sendOrder)
|
|
|
|
do(nonblock)
|
|
|
|
|
|
|
|
fmt.Fprintln(out, "//", a.nreset, "cases")
|
|
|
|
out.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(t *template.Template, a interface{}, out io.Writer) {
|
2011-02-09 22:23:01 +00:00
|
|
|
if err := t.Execute(out, a); err != nil {
|
2011-01-30 20:46:02 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-17 04:34:48 +00:00
|
|
|
type arg struct {
|
|
|
|
def bool
|
2011-01-30 20:46:02 +00:00
|
|
|
nreset int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *arg) Maybe() bool {
|
|
|
|
return maybe()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *arg) MaybeDefault() bool {
|
|
|
|
if a.def {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
a.def = maybe()
|
|
|
|
return a.def
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *arg) MustDefault() bool {
|
|
|
|
return !a.def
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *arg) reset() {
|
|
|
|
a.def = false
|
|
|
|
a.nreset++
|
|
|
|
}
|
|
|
|
|
|
|
|
const header = `// GENERATED BY select5.go; DO NOT EDIT
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
// channel is buffered so test is single-goroutine.
|
|
|
|
// we are not interested in the concurrency aspects
|
|
|
|
// of select, just testing that the right calls happen.
|
|
|
|
var c = make(chan int, 1)
|
|
|
|
var nilch chan int
|
|
|
|
var n = 1
|
|
|
|
var x int
|
|
|
|
var i interface{}
|
|
|
|
var dummy = make(chan int)
|
|
|
|
var m = make(map[int]int)
|
|
|
|
var order = 0
|
|
|
|
|
|
|
|
func f(p *int) *int {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// check order of operations by ensuring that
|
|
|
|
// successive calls to checkorder have increasing o values.
|
|
|
|
func checkorder(o int) {
|
|
|
|
if o <= order {
|
|
|
|
println("invalid order", o, "after", order)
|
|
|
|
panic("order")
|
|
|
|
}
|
|
|
|
order = o
|
|
|
|
}
|
|
|
|
|
|
|
|
func fc(c chan int, o int) chan int {
|
|
|
|
checkorder(o)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func fp(p *int, o int) *int {
|
|
|
|
checkorder(o)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func fn(n, o int) int {
|
|
|
|
checkorder(o)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func die(x int) {
|
|
|
|
println("have", x, "want", n)
|
|
|
|
panic("chan")
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// everything happens in init funcs
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2011-08-17 06:51:04 +00:00
|
|
|
func parse(name, s string) *template.Template {
|
|
|
|
t, err := template.New(name).Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("%q: %s", name, err))
|
2011-01-30 20:46:02 +00:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2011-08-17 06:51:04 +00:00
|
|
|
var recv = parse("recv", `
|
|
|
|
{{/* Send n, receive it one way or another into x, check that they match. */}}
|
2011-01-30 20:46:02 +00:00
|
|
|
c <- n
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
x = <-c
|
2011-08-17 06:51:04 +00:00
|
|
|
{{else}}
|
2011-01-30 20:46:02 +00:00
|
|
|
select {
|
2011-08-17 06:51:04 +00:00
|
|
|
{{/* Blocking or non-blocking, before the receive. */}}
|
|
|
|
{{/* The compiler implements two-case select where one is default with custom code, */}}
|
|
|
|
{{/* so test the default branch both before and after the send. */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .MaybeDefault}}
|
2011-01-30 20:46:02 +00:00
|
|
|
default:
|
|
|
|
panic("nonblock")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
|
|
|
{{/* Receive from c. Different cases are direct, indirect, :=, interface, and map assignment. */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case x = <-c:
|
2011-08-18 03:27:18 +00:00
|
|
|
{{else}}{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case *f(&x) = <-c:
|
2011-08-18 03:27:18 +00:00
|
|
|
{{else}}{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case y := <-c:
|
|
|
|
x = y
|
2011-08-18 03:27:18 +00:00
|
|
|
{{else}}{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case i = <-c:
|
|
|
|
x = i.(int)
|
2011-08-17 06:51:04 +00:00
|
|
|
{{else}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case m[13] = <-c:
|
|
|
|
x = m[13]
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}{{end}}{{end}}{{end}}
|
|
|
|
{{/* Blocking or non-blocking again, after the receive. */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .MaybeDefault}}
|
2011-01-30 20:46:02 +00:00
|
|
|
default:
|
|
|
|
panic("nonblock")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
|
|
|
{{/* Dummy send, receive to keep compiler from optimizing select. */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case dummy <- 1:
|
|
|
|
panic("dummy send")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case <-dummy:
|
|
|
|
panic("dummy receive")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
|
|
|
{{/* Nil channel send, receive to keep compiler from optimizing select. */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case nilch <- 1:
|
|
|
|
panic("nilch send")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case <-nilch:
|
|
|
|
panic("nilch recv")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-01-30 20:46:02 +00:00
|
|
|
}
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-01-30 20:46:02 +00:00
|
|
|
if x != n {
|
|
|
|
die(x)
|
|
|
|
}
|
|
|
|
n++
|
|
|
|
`)
|
|
|
|
|
2011-08-17 06:51:04 +00:00
|
|
|
var recvOrder = parse("recvOrder", `
|
|
|
|
{{/* Send n, receive it one way or another into x, check that they match. */}}
|
|
|
|
{{/* Check order of operations along the way by calling functions that check */}}
|
|
|
|
{{/* that the argument sequence is strictly increasing. */}}
|
2011-01-30 20:46:02 +00:00
|
|
|
order = 0
|
|
|
|
c <- n
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-08-17 06:51:04 +00:00
|
|
|
{{/* Outside of select, left-to-right rule applies. */}}
|
|
|
|
{{/* (Inside select, assignment waits until case is chosen, */}}
|
|
|
|
{{/* so right hand side happens before anything on left hand side. */}}
|
2011-01-30 20:46:02 +00:00
|
|
|
*fp(&x, 1) = <-fc(c, 2)
|
2011-08-18 03:27:18 +00:00
|
|
|
{{else}}{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
m[fn(13, 1)] = <-fc(c, 2)
|
|
|
|
x = m[13]
|
2011-08-17 06:51:04 +00:00
|
|
|
{{else}}
|
2011-01-30 20:46:02 +00:00
|
|
|
select {
|
2011-08-17 06:51:04 +00:00
|
|
|
{{/* Blocking or non-blocking, before the receive. */}}
|
|
|
|
{{/* The compiler implements two-case select where one is default with custom code, */}}
|
|
|
|
{{/* so test the default branch both before and after the send. */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .MaybeDefault}}
|
2011-01-30 20:46:02 +00:00
|
|
|
default:
|
|
|
|
panic("nonblock")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
|
|
|
{{/* Receive from c. Different cases are direct, indirect, :=, interface, and map assignment. */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case *fp(&x, 100) = <-fc(c, 1):
|
2011-08-18 03:27:18 +00:00
|
|
|
{{else}}{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case y := <-fc(c, 1):
|
|
|
|
x = y
|
2011-08-18 03:27:18 +00:00
|
|
|
{{else}}{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case i = <-fc(c, 1):
|
|
|
|
x = i.(int)
|
2011-08-17 06:51:04 +00:00
|
|
|
{{else}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case m[fn(13, 100)] = <-fc(c, 1):
|
|
|
|
x = m[13]
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}{{end}}{{end}}
|
|
|
|
{{/* Blocking or non-blocking again, after the receive. */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .MaybeDefault}}
|
2011-01-30 20:46:02 +00:00
|
|
|
default:
|
|
|
|
panic("nonblock")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
|
|
|
{{/* Dummy send, receive to keep compiler from optimizing select. */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case fc(dummy, 2) <- fn(1, 3):
|
|
|
|
panic("dummy send")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case <-fc(dummy, 4):
|
|
|
|
panic("dummy receive")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
|
|
|
{{/* Nil channel send, receive to keep compiler from optimizing select. */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case fc(nilch, 5) <- fn(1, 6):
|
|
|
|
panic("nilch send")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case <-fc(nilch, 7):
|
|
|
|
panic("nilch recv")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-01-30 20:46:02 +00:00
|
|
|
}
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}{{end}}
|
2011-01-30 20:46:02 +00:00
|
|
|
if x != n {
|
|
|
|
die(x)
|
|
|
|
}
|
|
|
|
n++
|
|
|
|
`)
|
|
|
|
|
2011-08-17 06:51:04 +00:00
|
|
|
var send = parse("send", `
|
|
|
|
{{/* Send n one way or another, receive it into x, check that they match. */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
c <- n
|
2011-08-17 06:51:04 +00:00
|
|
|
{{else}}
|
2011-01-30 20:46:02 +00:00
|
|
|
select {
|
2011-08-17 06:51:04 +00:00
|
|
|
{{/* Blocking or non-blocking, before the receive (same reason as in recv). */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .MaybeDefault}}
|
2011-01-30 20:46:02 +00:00
|
|
|
default:
|
|
|
|
panic("nonblock")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
|
|
|
{{/* Send c <- n. No real special cases here, because no values come back */}}
|
|
|
|
{{/* from the send operation. */}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case c <- n:
|
2011-08-17 06:51:04 +00:00
|
|
|
{{/* Blocking or non-blocking. */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .MaybeDefault}}
|
2011-01-30 20:46:02 +00:00
|
|
|
default:
|
|
|
|
panic("nonblock")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
|
|
|
{{/* Dummy send, receive to keep compiler from optimizing select. */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case dummy <- 1:
|
|
|
|
panic("dummy send")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case <-dummy:
|
|
|
|
panic("dummy receive")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
|
|
|
{{/* Nil channel send, receive to keep compiler from optimizing select. */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case nilch <- 1:
|
|
|
|
panic("nilch send")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case <-nilch:
|
|
|
|
panic("nilch recv")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-01-30 20:46:02 +00:00
|
|
|
}
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-01-30 20:46:02 +00:00
|
|
|
x = <-c
|
|
|
|
if x != n {
|
|
|
|
die(x)
|
|
|
|
}
|
|
|
|
n++
|
|
|
|
`)
|
|
|
|
|
2011-08-17 06:51:04 +00:00
|
|
|
var sendOrder = parse("sendOrder", `
|
|
|
|
{{/* Send n one way or another, receive it into x, check that they match. */}}
|
|
|
|
{{/* Check order of operations along the way by calling functions that check */}}
|
|
|
|
{{/* that the argument sequence is strictly increasing. */}}
|
2011-01-30 20:46:02 +00:00
|
|
|
order = 0
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
fc(c, 1) <- fn(n, 2)
|
2011-08-17 06:51:04 +00:00
|
|
|
{{else}}
|
2011-01-30 20:46:02 +00:00
|
|
|
select {
|
2011-08-17 06:51:04 +00:00
|
|
|
{{/* Blocking or non-blocking, before the receive (same reason as in recv). */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .MaybeDefault}}
|
2011-01-30 20:46:02 +00:00
|
|
|
default:
|
|
|
|
panic("nonblock")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
|
|
|
{{/* Send c <- n. No real special cases here, because no values come back */}}
|
|
|
|
{{/* from the send operation. */}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case fc(c, 1) <- fn(n, 2):
|
2011-08-17 06:51:04 +00:00
|
|
|
{{/* Blocking or non-blocking. */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .MaybeDefault}}
|
2011-01-30 20:46:02 +00:00
|
|
|
default:
|
|
|
|
panic("nonblock")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
|
|
|
{{/* Dummy send, receive to keep compiler from optimizing select. */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case fc(dummy, 3) <- fn(1, 4):
|
|
|
|
panic("dummy send")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case <-fc(dummy, 5):
|
|
|
|
panic("dummy receive")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
|
|
|
{{/* Nil channel send, receive to keep compiler from optimizing select. */}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case fc(nilch, 6) <- fn(1, 7):
|
|
|
|
panic("nilch send")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case <-fc(nilch, 8):
|
|
|
|
panic("nilch recv")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-01-30 20:46:02 +00:00
|
|
|
}
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-01-30 20:46:02 +00:00
|
|
|
x = <-c
|
|
|
|
if x != n {
|
|
|
|
die(x)
|
|
|
|
}
|
|
|
|
n++
|
|
|
|
`)
|
|
|
|
|
2011-08-17 06:51:04 +00:00
|
|
|
var nonblock = parse("nonblock", `
|
2011-01-30 20:46:02 +00:00
|
|
|
x = n
|
2011-08-17 06:51:04 +00:00
|
|
|
{{/* Test various combinations of non-blocking operations. */}}
|
|
|
|
{{/* Receive assignments must not edit or even attempt to compute the address of the lhs. */}}
|
2011-01-30 20:46:02 +00:00
|
|
|
select {
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .MaybeDefault}}
|
2011-01-30 20:46:02 +00:00
|
|
|
default:
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case dummy <- 1:
|
|
|
|
panic("dummy <- 1")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case nilch <- 1:
|
|
|
|
panic("nilch <- 1")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case <-dummy:
|
|
|
|
panic("<-dummy")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case x = <-dummy:
|
|
|
|
panic("<-dummy x")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case **(**int)(nil) = <-dummy:
|
|
|
|
panic("<-dummy (and didn't crash saving result!)")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case <-nilch:
|
|
|
|
panic("<-nilch")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case x = <-nilch:
|
|
|
|
panic("<-nilch x")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .Maybe}}
|
2011-01-30 20:46:02 +00:00
|
|
|
case **(**int)(nil) = <-nilch:
|
|
|
|
panic("<-nilch (and didn't crash saving result!)")
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-08-18 03:27:18 +00:00
|
|
|
{{if .MustDefault}}
|
2011-01-30 20:46:02 +00:00
|
|
|
default:
|
2011-08-17 06:51:04 +00:00
|
|
|
{{end}}
|
2011-01-30 20:46:02 +00:00
|
|
|
}
|
|
|
|
if x != n {
|
|
|
|
die(x)
|
|
|
|
}
|
|
|
|
n++
|
|
|
|
`)
|
|
|
|
|
|
|
|
// Code for enumerating all possible paths through
|
|
|
|
// some logic. The logic should call choose(n) when
|
|
|
|
// it wants to choose between n possibilities.
|
|
|
|
// On successive runs through the logic, choose(n)
|
|
|
|
// will return 0, 1, ..., n-1. The helper maybe() is
|
|
|
|
// similar but returns true and then false.
|
|
|
|
//
|
|
|
|
// Given a function gen that generates an output
|
|
|
|
// using choose and maybe, code can generate all
|
|
|
|
// possible outputs using
|
|
|
|
//
|
|
|
|
// for next() {
|
|
|
|
// gen()
|
|
|
|
// }
|
|
|
|
|
|
|
|
type choice struct {
|
|
|
|
i, n int
|
|
|
|
}
|
|
|
|
|
|
|
|
var choices []choice
|
|
|
|
var cp int = -1
|
|
|
|
|
|
|
|
func maybe() bool {
|
|
|
|
return choose(2) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func choose(n int) int {
|
|
|
|
if cp >= len(choices) {
|
|
|
|
// never asked this before: start with 0.
|
|
|
|
choices = append(choices, choice{0, n})
|
|
|
|
cp = len(choices)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
// otherwise give recorded answer
|
|
|
|
if n != choices[cp].n {
|
|
|
|
panic("inconsistent choices")
|
|
|
|
}
|
|
|
|
i := choices[cp].i
|
|
|
|
cp++
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func next() bool {
|
|
|
|
if cp < 0 {
|
|
|
|
// start a new round
|
|
|
|
cp = 0
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// increment last choice sequence
|
2011-08-17 04:34:48 +00:00
|
|
|
cp = len(choices) - 1
|
2011-01-30 20:46:02 +00:00
|
|
|
for cp >= 0 && choices[cp].i == choices[cp].n-1 {
|
|
|
|
cp--
|
|
|
|
}
|
|
|
|
if cp < 0 {
|
|
|
|
choices = choices[:0]
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
choices[cp].i++
|
|
|
|
choices = choices[:cp+1]
|
|
|
|
cp = 0
|
|
|
|
return true
|
|
|
|
}
|