1
0
mirror of https://github.com/golang/go synced 2024-11-21 20:04:44 -07:00

single argument panic

note that sortmain.go has been run through hg gofmt;
only the formatting of the day initializers changed.
i'm happy to revert that formatting if you'd prefer.

stop on error in doc/progs/run

R=r
CC=golang-dev
https://golang.org/cl/850041
This commit is contained in:
Russ Cox 2010-03-30 10:34:57 -07:00
parent 5d0ec6c076
commit 00f9f0c056
60 changed files with 929 additions and 813 deletions

View File

@ -3,6 +3,8 @@
# Use of this source code is governed by a BSD-style # Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file. # license that can be found in the LICENSE file.
set -e
GOBIN="${GOBIN:-$HOME/bin}" GOBIN="${GOBIN:-$HOME/bin}"
. "$GOROOT"/src/Make.$GOARCH . "$GOROOT"/src/Make.$GOARCH

View File

@ -14,7 +14,7 @@ func ints() {
a := sort.IntArray(data) a := sort.IntArray(data)
sort.Sort(a) sort.Sort(a)
if !sort.IsSorted(a) { if !sort.IsSorted(a) {
panic() panic("fail")
} }
} }
@ -23,7 +23,7 @@ func strings() {
a := sort.StringArray(data) a := sort.StringArray(data)
sort.Sort(a) sort.Sort(a)
if !sort.IsSorted(a) { if !sort.IsSorted(a) {
panic() panic("fail")
} }
} }
@ -53,7 +53,7 @@ func days() {
a := dayArray{data} a := dayArray{data}
sort.Sort(&a) sort.Sort(&a)
if !sort.IsSorted(&a) { if !sort.IsSorted(&a) {
panic() panic("fail")
} }
for _, d := range data { for _, d := range data {
fmt.Printf("%s ", d.longName) fmt.Printf("%s ", d.longName)

View File

@ -157,7 +157,7 @@ func walk(x interface{}, p *Prog, context string) {
// everything else just recurs // everything else just recurs
default: default:
error(noPos, "unexpected type %T in walk", x) error(noPos, "unexpected type %T in walk", x)
panic() panic("unexpected type")
case nil: case nil:

View File

@ -119,7 +119,7 @@ func init() {
// sanity check: if nKinds is too large, the SpotInfo // sanity check: if nKinds is too large, the SpotInfo
// accessor functions may need to be updated // accessor functions may need to be updated
if nKinds > 8 { if nKinds > 8 {
panic() panic("nKinds > 8")
} }
} }

View File

@ -55,7 +55,7 @@ func download(pkg string) (string, os.Error) {
v = &svn v = &svn
default: default:
// regexp only allows hg, svn to get through // regexp only allows hg, svn to get through
panic("missing case in download: ", pkg) panic("missing case in download: " + pkg)
} }
if err := vcsCheckout(v, root+m[1], "http://"+m[1], m[1]); err != nil { if err := vcsCheckout(v, root+m[1], "http://"+m[1], m[1]); err != nil {
return "", err return "", err

View File

@ -75,7 +75,7 @@ func NewReader(rd io.Reader) *Reader {
b, err := NewReaderSize(rd, defaultBufSize) b, err := NewReaderSize(rd, defaultBufSize)
if err != nil { if err != nil {
// cannot happen - defaultBufSize is a valid size // cannot happen - defaultBufSize is a valid size
panic("bufio: NewReader: ", err.String()) panic(err)
} }
return b return b
} }
@ -353,7 +353,7 @@ func NewWriter(wr io.Writer) *Writer {
b, err := NewWriterSize(wr, defaultBufSize) b, err := NewWriterSize(wr, defaultBufSize)
if err != nil { if err != nil {
// cannot happen - defaultBufSize is valid size // cannot happen - defaultBufSize is valid size
panic("bufio: NewWriter: ", err.String()) panic(err)
} }
return b return b
} }

View File

@ -188,7 +188,8 @@ func bmIndex(b *testing.B, index func([]byte, byte) int, n int) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
j := index(buf, 'x') j := index(buf, 'x')
if j != n-1 { if j != n-1 {
panic("bad index", j) println("bad index", j)
panic("bad index")
} }
} }
buf[n-1] = '0' buf[n-1] = '0'

View File

@ -37,7 +37,7 @@ func NewCMAC(c Cipher) hash.Hash {
case 128 / 8: case 128 / 8:
r = r128 r = r128
default: default:
panic("crypto/block: NewCMAC: invalid cipher block size", n) panic("crypto/block: NewCMAC: invalid cipher block size")
} }
d := new(cmac) d := new(cmac)

View File

@ -196,7 +196,7 @@ func toValue(val interface{}) Value {
return &funcV{val} return &funcV{val}
} }
log.Crashf("toValue(%T) not implemented", val) log.Crashf("toValue(%T) not implemented", val)
panic() panic("unreachable")
} }
/* /*

View File

@ -642,7 +642,7 @@ func (a *exprCompiler) compile(x ast.Expr, callCtx bool) *expr {
return ei.compileUnaryExpr(x.Op, v) return ei.compileUnaryExpr(x.Op, v)
} }
log.Crashf("unexpected ast node type %T", x) log.Crashf("unexpected ast node type %T", x)
panic() panic("unreachable")
typeexpr: typeexpr:
if !callCtx { if !callCtx {
@ -704,7 +704,7 @@ func (a *exprInfo) compileIdent(b *block, constant bool, callCtx bool, name stri
return nil return nil
} }
log.Crashf("name %s has unknown type %T", name, def) log.Crashf("name %s has unknown type %T", name, def)
panic() panic("unreachable")
} }
func (a *exprInfo) compileVariable(level int, v *Variable) *expr { func (a *exprInfo) compileVariable(level int, v *Variable) *expr {
@ -1424,7 +1424,7 @@ func (a *exprInfo) compileBuiltinCallExpr(b *block, ft *FuncType, as []*expr) *e
} }
log.Crashf("unexpected built-in function '%s'", ft.builtin) log.Crashf("unexpected built-in function '%s'", ft.builtin)
panic() panic("unreachable")
} }
func (a *exprInfo) compileStarExpr(v *expr) *expr { func (a *exprInfo) compileStarExpr(v *expr) *expr {

View File

@ -12,9 +12,15 @@ import (
* "As" functions. These retrieve evaluator functions from an * "As" functions. These retrieve evaluator functions from an
* expr, panicking if the requested evaluator has the wrong type. * expr, panicking if the requested evaluator has the wrong type.
*/ */
func (a *expr) asBool() func(*Thread) bool { return a.eval.(func(*Thread) bool) } func (a *expr) asBool() func(*Thread) bool {
func (a *expr) asUint() func(*Thread) uint64 { return a.eval.(func(*Thread) uint64) } return a.eval.(func(*Thread) bool)
func (a *expr) asInt() func(*Thread) int64 { return a.eval.(func(*Thread) int64) } }
func (a *expr) asUint() func(*Thread) uint64 {
return a.eval.(func(*Thread) uint64)
}
func (a *expr) asInt() func(*Thread) int64 {
return a.eval.(func(*Thread) int64)
}
func (a *expr) asIdealInt() func() *bignum.Integer { func (a *expr) asIdealInt() func() *bignum.Integer {
return a.eval.(func() *bignum.Integer) return a.eval.(func() *bignum.Integer)
} }
@ -33,10 +39,18 @@ func (a *expr) asArray() func(*Thread) ArrayValue {
func (a *expr) asStruct() func(*Thread) StructValue { func (a *expr) asStruct() func(*Thread) StructValue {
return a.eval.(func(*Thread) StructValue) return a.eval.(func(*Thread) StructValue)
} }
func (a *expr) asPtr() func(*Thread) Value { return a.eval.(func(*Thread) Value) } func (a *expr) asPtr() func(*Thread) Value {
func (a *expr) asFunc() func(*Thread) Func { return a.eval.(func(*Thread) Func) } return a.eval.(func(*Thread) Value)
func (a *expr) asSlice() func(*Thread) Slice { return a.eval.(func(*Thread) Slice) } }
func (a *expr) asMap() func(*Thread) Map { return a.eval.(func(*Thread) Map) } func (a *expr) asFunc() func(*Thread) Func {
return a.eval.(func(*Thread) Func)
}
func (a *expr) asSlice() func(*Thread) Slice {
return a.eval.(func(*Thread) Slice)
}
func (a *expr) asMap() func(*Thread) Map {
return a.eval.(func(*Thread) Map)
}
func (a *expr) asMulti() func(*Thread) []Value { func (a *expr) asMulti() func(*Thread) []Value {
return a.eval.(func(*Thread) []Value) return a.eval.(func(*Thread) []Value)
} }
@ -72,7 +86,7 @@ func (a *expr) asInterface() func(*Thread) interface{} {
default: default:
log.Crashf("unexpected expression node type %T at %v", a.eval, a.pos) log.Crashf("unexpected expression node type %T at %v", a.eval, a.pos)
} }
panic() panic("fail")
} }
/* /*
@ -210,26 +224,17 @@ func (a *expr) genUnaryOpNeg(v *expr) {
switch a.t.lit().(type) { switch a.t.lit().(type) {
case *uintType: case *uintType:
vf := v.asUint() vf := v.asUint()
a.eval = func(t *Thread) uint64 { a.eval = func(t *Thread) uint64 { v := vf(t); return -v }
v := vf(t)
return -v
}
case *intType: case *intType:
vf := v.asInt() vf := v.asInt()
a.eval = func(t *Thread) int64 { a.eval = func(t *Thread) int64 { v := vf(t); return -v }
v := vf(t)
return -v
}
case *idealIntType: case *idealIntType:
v := v.asIdealInt()() v := v.asIdealInt()()
val := v.Neg() val := v.Neg()
a.eval = func() *bignum.Integer { return val } a.eval = func() *bignum.Integer { return val }
case *floatType: case *floatType:
vf := v.asFloat() vf := v.asFloat()
a.eval = func(t *Thread) float64 { a.eval = func(t *Thread) float64 { v := vf(t); return -v }
v := vf(t)
return -v
}
case *idealFloatType: case *idealFloatType:
v := v.asIdealFloat()() v := v.asIdealFloat()()
val := v.Neg() val := v.Neg()
@ -243,10 +248,7 @@ func (a *expr) genUnaryOpNot(v *expr) {
switch a.t.lit().(type) { switch a.t.lit().(type) {
case *boolType: case *boolType:
vf := v.asBool() vf := v.asBool()
a.eval = func(t *Thread) bool { a.eval = func(t *Thread) bool { v := vf(t); return !v }
v := vf(t)
return !v
}
default: default:
log.Crashf("unexpected type %v at %v", a.t, a.pos) log.Crashf("unexpected type %v at %v", a.t, a.pos)
} }
@ -256,16 +258,10 @@ func (a *expr) genUnaryOpXor(v *expr) {
switch a.t.lit().(type) { switch a.t.lit().(type) {
case *uintType: case *uintType:
vf := v.asUint() vf := v.asUint()
a.eval = func(t *Thread) uint64 { a.eval = func(t *Thread) uint64 { v := vf(t); return ^v }
v := vf(t)
return ^v
}
case *intType: case *intType:
vf := v.asInt() vf := v.asInt()
a.eval = func(t *Thread) int64 { a.eval = func(t *Thread) int64 { v := vf(t); return ^v }
v := vf(t)
return ^v
}
case *idealIntType: case *idealIntType:
v := v.asIdealInt()() v := v.asIdealInt()()
val := v.Neg().Sub(bignum.Int(1)) val := v.Neg().Sub(bignum.Int(1))
@ -1905,5 +1901,5 @@ func genAssign(lt Type, r *expr) func(lv Value, t *Thread) {
default: default:
log.Crashf("unexpected left operand type %v at %v", lt, r.pos) log.Crashf("unexpected left operand type %v at %v", lt, r.pos)
} }
panic() panic("fail")
} }

View File

@ -103,12 +103,12 @@ var binOps = []Op{
Op{Name: "Sub", Expr: "l - r", ConstExpr: "l.Sub(r)", Types: numbers}, Op{Name: "Sub", Expr: "l - r", ConstExpr: "l.Sub(r)", Types: numbers},
Op{Name: "Mul", Expr: "l * r", ConstExpr: "l.Mul(r)", Types: numbers}, Op{Name: "Mul", Expr: "l * r", ConstExpr: "l.Mul(r)", Types: numbers},
Op{Name: "Quo", Op{Name: "Quo",
Body: "if r == 0 { t.Abort(DivByZeroError{}) } ret = l / r", Body: "if r == 0 { t.Abort(DivByZeroError{}) }; ret = l / r",
ConstExpr: "l.Quo(r)", ConstExpr: "l.Quo(r)",
Types: numbers, Types: numbers,
}, },
Op{Name: "Rem", Op{Name: "Rem",
Body: "if r == 0 { t.Abort(DivByZeroError{}) } ret = l % r", Body: "if r == 0 { t.Abort(DivByZeroError{}) }; ret = l % r",
ConstExpr: "l.Rem(r)", ConstExpr: "l.Rem(r)",
Types: integers, Types: integers,
}, },
@ -186,7 +186,7 @@ func (a *expr) asInterface() (func(*Thread) interface{}) {
default: default:
log.Crashf("unexpected expression node type %T at %v", a.eval, a.pos); log.Crashf("unexpected expression node type %T at %v", a.eval, a.pos);
} }
panic(); panic("fail");
} }
/* /*
@ -357,7 +357,7 @@ func genAssign(lt Type, r *expr) (func(lv Value, t *Thread)) {
default: default:
log.Crashf("unexpected left operand type %v at %v", lt, r.pos); log.Crashf("unexpected left operand type %v at %v", lt, r.pos);
} }
panic(); panic("fail");
} }
` `

View File

@ -231,7 +231,7 @@ func (t *uintType) Zero() Value {
res := uint64V(0) res := uint64V(0)
return &res return &res
} }
panic("unexpected uint bit count: ", t.Bits) panic("unexpected uint bit count")
} }
func (t *uintType) minVal() *bignum.Rational { return bignum.Rat(0, 1) } func (t *uintType) minVal() *bignum.Rational { return bignum.Rat(0, 1) }
@ -304,7 +304,7 @@ func (t *intType) Zero() Value {
res := intV(0) res := intV(0)
return &res return &res
} }
panic("unexpected int bit count: ", t.Bits) panic("unexpected int bit count")
} }
func (t *intType) minVal() *bignum.Rational { func (t *intType) minVal() *bignum.Rational {
@ -390,7 +390,7 @@ func (t *floatType) Zero() Value {
res := floatV(0) res := floatV(0)
return &res return &res
} }
panic("unexpected float bit count: ", t.Bits) panic("unexpected float bit count")
} }
var maxFloat32Val = bignum.MakeRat(bignum.Int(0xffffff).Shl(127-23), bignum.Nat(1)) var maxFloat32Val = bignum.MakeRat(bignum.Int(0xffffff).Shl(127-23), bignum.Nat(1))
@ -410,7 +410,7 @@ func (t *floatType) minVal() *bignum.Rational {
return minFloat64Val return minFloat64Val
} }
log.Crashf("unexpected floating point bit count: %d", bits) log.Crashf("unexpected floating point bit count: %d", bits)
panic() panic("unreachable")
} }
func (t *floatType) maxVal() *bignum.Rational { func (t *floatType) maxVal() *bignum.Rational {
@ -425,7 +425,7 @@ func (t *floatType) maxVal() *bignum.Rational {
return maxFloat64Val return maxFloat64Val
} }
log.Crashf("unexpected floating point bit count: %d", bits) log.Crashf("unexpected floating point bit count: %d", bits)
panic() panic("unreachable")
} }
/* /*

View File

@ -232,7 +232,7 @@ func (v remoteFloat) aGet(a aborter) float64 {
case 8: case 8:
return v.r.p.ToFloat64(bits) return v.r.p.ToFloat64(bits)
} }
panic("Unexpected float size ", v.size) panic("Unexpected float size")
} }
func (v remoteFloat) Set(t *eval.Thread, x float64) { func (v remoteFloat) Set(t *eval.Thread, x float64) {
@ -247,7 +247,7 @@ func (v remoteFloat) aSet(a aborter, x float64) {
case 8: case 8:
bits = v.r.p.FromFloat64(x) bits = v.r.p.FromFloat64(x)
default: default:
panic("Unexpected float size ", v.size) panic("Unexpected float size")
} }
v.r.Set(a, v.size, bits) v.r.Set(a, v.size, bits)
} }

View File

@ -63,7 +63,7 @@ func (v remoteFramePtr) Get(t *eval.Thread) eval.Value {
} }
t.Abort(NotOnStack{v.fn, g}) t.Abort(NotOnStack{v.fn, g})
panic() panic("fail")
} }
func (v remoteFramePtr) Set(t *eval.Thread, x eval.Value) { func (v remoteFramePtr) Set(t *eval.Thread, x eval.Value) {

View File

@ -316,7 +316,7 @@ func Walk(v Visitor, node interface{}) {
default: default:
fmt.Printf("ast.Walk: unexpected type %T", n) fmt.Printf("ast.Walk: unexpected type %T", n)
panic() panic("ast.Walk")
} }
v.Visit(nil) v.Visit(nil)

View File

@ -1738,8 +1738,7 @@ func (p *parser) parseForStmt() ast.Stmt {
return &ast.ForStmt{pos, s1, p.makeExpr(s2), s3, body} return &ast.ForStmt{pos, s1, p.makeExpr(s2), s3, body}
} }
panic() // unreachable panic("unreachable")
return nil
} }

View File

@ -107,7 +107,7 @@ func (p *printer) internalError(msg ...interface{}) {
if debug { if debug {
fmt.Print(p.pos.String() + ": ") fmt.Print(p.pos.String() + ": ")
fmt.Println(msg) fmt.Println(msg)
panic() panic("go/printer")
} }
} }
@ -791,7 +791,7 @@ func (p *printer) print(args ...interface{}) {
} }
default: default:
fmt.Fprintf(os.Stderr, "print: unsupported argument type %T\n", f) fmt.Fprintf(os.Stderr, "print: unsupported argument type %T\n", f)
panic() panic("go/printer type")
} }
p.pos = next p.pos = next

View File

@ -777,7 +777,7 @@ func init() {
case unsafe.Sizeof(float64(0)): case unsafe.Sizeof(float64(0)):
op = decFloat64 op = decFloat64
default: default:
panic("gob: unknown size of float", unsafe.Sizeof(float(0))) panic("gob: unknown size of float")
} }
decOpMap[valueKind(float(0))] = op decOpMap[valueKind(float(0))] = op
@ -791,7 +791,7 @@ func init() {
op = decInt64 op = decInt64
uop = decUint64 uop = decUint64
default: default:
panic("gob: unknown size of int/uint", unsafe.Sizeof(int(0))) panic("gob: unknown size of int/uint")
} }
decOpMap[valueKind(int(0))] = op decOpMap[valueKind(int(0))] = op
decOpMap[valueKind(uint(0))] = uop decOpMap[valueKind(uint(0))] = uop
@ -803,7 +803,7 @@ func init() {
case unsafe.Sizeof(uint64(0)): case unsafe.Sizeof(uint64(0)):
uop = decUint64 uop = decUint64
default: default:
panic("gob: unknown size of uintptr", unsafe.Sizeof(uintptr(0))) panic("gob: unknown size of uintptr")
} }
decOpMap[valueKind(uintptr(0))] = uop decOpMap[valueKind(uintptr(0))] = uop
} }

View File

@ -10,20 +10,16 @@ func isSeparator(c byte) bool {
switch c { switch c {
case '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=', '{', '}', ' ', '\t': case '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=', '{', '}', ' ', '\t':
return true return true
default:
return false
} }
panic() return false
} }
func isSpace(c byte) bool { func isSpace(c byte) bool {
switch c { switch c {
case ' ', '\t', '\r', '\n': case ' ', '\t', '\r', '\n':
return true return true
default:
return false
} }
panic() return false
} }
func isCtl(c byte) bool { return (0 <= c && c <= 31) || c == 127 } func isCtl(c byte) bool { return (0 <= c && c <= 31) || c == 127 }

View File

@ -11,6 +11,7 @@ import (
"os" "os"
"sync" "sync"
"syscall" "syscall"
"time"
) )
// Network file descriptor. // Network file descriptor.
@ -176,12 +177,7 @@ func (s *pollServer) WakeFD(fd *netFD, mode int) {
} }
func (s *pollServer) Now() int64 { func (s *pollServer) Now() int64 {
sec, nsec, err := os.Time() return time.Nanoseconds()
if err != nil {
panic("net: os.Time: ", err.String())
}
nsec += sec * 1e9
return nsec
} }
func (s *pollServer) CheckDeadlines() { func (s *pollServer) CheckDeadlines() {

View File

@ -25,7 +25,7 @@ func unixSocket(net string, laddr, raddr *UnixAddr, mode string) (fd *netFD, err
var la, ra syscall.Sockaddr var la, ra syscall.Sockaddr
switch mode { switch mode {
default: default:
panic("unixSocket", mode) panic("unixSocket mode " + mode)
case "dial": case "dial":
if laddr != nil { if laddr != nil {

View File

@ -571,7 +571,7 @@ func (v *ArrayValue) Elem(i int) Value {
typ := v.typ.(*ArrayType).Elem() typ := v.typ.(*ArrayType).Elem()
n := v.Len() n := v.Len()
if i < 0 || i >= n { if i < 0 || i >= n {
panic("index", i, "in array len", n) panic("array index out of bounds")
} }
p := addr(uintptr(v.addr()) + uintptr(i)*typ.Size()) p := addr(uintptr(v.addr()) + uintptr(i)*typ.Size())
return newValue(typ, p, v.canSet) return newValue(typ, p, v.canSet)
@ -642,7 +642,7 @@ func (v *SliceValue) Get() uintptr {
func (v *SliceValue) Slice(beg, end int) *SliceValue { func (v *SliceValue) Slice(beg, end int) *SliceValue {
cap := v.Cap() cap := v.Cap()
if beg < 0 || end < beg || end > cap { if beg < 0 || end < beg || end > cap {
panic("slice bounds [", beg, ":", end, "] with capacity ", cap) panic("slice index out of bounds")
} }
typ := v.typ.(*SliceType) typ := v.typ.(*SliceType)
s := new(SliceHeader) s := new(SliceHeader)

View File

@ -101,7 +101,8 @@ var ftoatests = []ftoaTest{
func TestFtoa(t *testing.T) { func TestFtoa(t *testing.T) {
if FloatSize != 32 { if FloatSize != 32 {
panic("floatsize: ", FloatSize) println("floatsize: ", FloatSize)
panic("floatsize")
} }
for i := 0; i < len(ftoatests); i++ { for i := 0; i < len(ftoatests); i++ {
test := &ftoatests[i] test := &ftoatests[i]

View File

@ -983,7 +983,7 @@ func ParseFile(filename string, fmap FormatterMap) (t *Template, err os.Error) {
func MustParse(s string, fmap FormatterMap) *Template { func MustParse(s string, fmap FormatterMap) *Template {
t, err := Parse(s, fmap) t, err := Parse(s, fmap)
if err != nil { if err != nil {
panic("template parse error: ", err.String()) panic("template.MustParse error: " + err.String())
} }
return t return t
} }
@ -993,7 +993,7 @@ func MustParse(s string, fmap FormatterMap) *Template {
func MustParseFile(filename string, fmap FormatterMap) *Template { func MustParseFile(filename string, fmap FormatterMap) *Template {
b, err := ioutil.ReadFile(filename) b, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
panic("template parse error: ", err.String()) panic("template.MustParseFile error: " + err.String())
} }
return MustParse(string(b), fmap) return MustParse(string(b), fmap)
} }

View File

@ -15,7 +15,7 @@ import (
func Seconds() int64 { func Seconds() int64 {
sec, _, err := os.Time() sec, _, err := os.Time()
if err != nil { if err != nil {
panic("time: os.Time: ", err.String()) panic(err)
} }
return sec return sec
} }
@ -25,7 +25,7 @@ func Seconds() int64 {
func Nanoseconds() int64 { func Nanoseconds() int64 {
sec, nsec, err := os.Time() sec, nsec, err := os.Time()
if err != nil { if err != nil {
panic("time: os.Time: ", err.String()) panic(err)
} }
return sec*1e9 + nsec return sec*1e9 + nsec
} }

View File

@ -62,7 +62,7 @@ func getKeyNumber(s string) (r uint32) {
func (f Handler) ServeHTTP(c *http.Conn, req *http.Request) { func (f Handler) ServeHTTP(c *http.Conn, req *http.Request) {
rwc, buf, err := c.Hijack() rwc, buf, err := c.Hijack()
if err != nil { if err != nil {
panic("Hijack failed: ", err.String()) panic("Hijack failed: " + err.String())
return return
} }
// The server should abort the WebSocket connection if it finds // The server should abort the WebSocket connection if it finds
@ -200,7 +200,7 @@ func (f Draft75Handler) ServeHTTP(c *http.Conn, req *http.Request) {
rwc, buf, err := c.Hijack() rwc, buf, err := c.Hijack()
if err != nil { if err != nil {
panic("Hijack failed: ", err.String()) panic("Hijack failed: " + err.String())
return return
} }
defer rwc.Close() defer rwc.Close()

View File

@ -6,34 +6,34 @@
package main package main
type T chan uint64; type T chan uint64
func M(f uint64) (in, out T) { func M(f uint64) (in, out T) {
in = make(T, 100); in = make(T, 100)
out = make(T, 100); out = make(T, 100)
go func(in, out T, f uint64) { go func(in, out T, f uint64) {
for { for {
out <- f * <-in; out <- f*<-in
} }
}(in, out, f); }(in, out, f)
return in, out; return in, out
} }
func min(xs []uint64) uint64 { func min(xs []uint64) uint64 {
m := xs[0]; m := xs[0]
for i := 1; i < len(xs); i++ { for i := 1; i < len(xs); i++ {
if xs[i] < m { if xs[i] < m {
m = xs[i]; m = xs[i]
} }
} }
return m; return m
} }
func main() { func main() {
F := []uint64{2, 3, 5}; F := []uint64{2, 3, 5}
var n = len(F); var n = len(F)
OUT := []uint64{ OUT := []uint64{
2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, 30, 32, 36, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, 30, 32, 36,
40, 45, 48, 50, 54, 60, 64, 72, 75, 80, 81, 90, 96, 100, 108, 120, 125, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80, 81, 90, 96, 100, 108, 120, 125,
@ -41,27 +41,32 @@ func main() {
256, 270, 288, 300, 320, 324, 360, 375, 384, 400, 405, 432, 450, 480, 256, 270, 288, 300, 320, 324, 360, 375, 384, 400, 405, 432, 450, 480,
486, 500, 512, 540, 576, 600, 625, 640, 648, 675, 720, 729, 750, 768, 486, 500, 512, 540, 576, 600, 625, 640, 648, 675, 720, 729, 750, 768,
800, 810, 864, 900, 960, 972, 1000, 1024, 1080, 1125, 1152, 1200, 1215, 800, 810, 864, 900, 960, 972, 1000, 1024, 1080, 1125, 1152, 1200, 1215,
1250, 1280, 1296, 1350, 1440, 1458, 1500, 1536, 1600 }; 1250, 1280, 1296, 1350, 1440, 1458, 1500, 1536, 1600}
x := uint64(1); x := uint64(1)
ins := make([]T, n); ins := make([]T, n)
outs := make([]T, n); outs := make([]T, n)
xs := make([]uint64, n); xs := make([]uint64, n)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
ins[i], outs[i] = M(F[i]); ins[i], outs[i] = M(F[i])
xs[i] = x; xs[i] = x
} }
for i := 0; i < len(OUT); i++ { for i := 0; i < len(OUT); i++ {
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
ins[i] <- x; ins[i] <- x
} }
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
if xs[i] == x { xs[i] = <- outs[i]; } if xs[i] == x {
xs[i] = <-outs[i]
}
} }
x = min(xs); x = min(xs)
if x != OUT[i] { panic("bad: ", x, " should be ", OUT[i]); } if x != OUT[i] {
println("bad: ", x, " should be ", OUT[i])
panic("235")
}
} }
} }

View File

@ -56,7 +56,8 @@ func recver(in <-chan int) {
break break
} }
if _, ok := seen[v]; ok { if _, ok := seen[v]; ok {
panic("got duplicate value: ", v) println("got duplicate value: ", v)
panic("fail")
} }
seen[v] = true seen[v] = true
} }

View File

@ -13,132 +13,198 @@ import "runtime"
import "time" import "time"
func i32receiver(c chan int32, strobe chan bool) { func i32receiver(c chan int32, strobe chan bool) {
if <-c != 123 { panic("i32 value") } if <-c != 123 {
panic("i32 value")
}
strobe <- true strobe <- true
} }
func i32sender(c chan int32, strobe chan bool) { func i32sender(c chan int32, strobe chan bool) {
c <- 234; c <- 234
strobe <- true strobe <- true
} }
func i64receiver(c chan int64, strobe chan bool) { func i64receiver(c chan int64, strobe chan bool) {
if <-c != 123456 { panic("i64 value") } if <-c != 123456 {
panic("i64 value")
}
strobe <- true strobe <- true
} }
func i64sender(c chan int64, strobe chan bool) { func i64sender(c chan int64, strobe chan bool) {
c <- 234567; c <- 234567
strobe <- true strobe <- true
} }
func breceiver(c chan bool, strobe chan bool) { func breceiver(c chan bool, strobe chan bool) {
if ! <-c { panic("b value") } if !<-c {
panic("b value")
}
strobe <- true strobe <- true
} }
func bsender(c chan bool, strobe chan bool) { func bsender(c chan bool, strobe chan bool) {
c <- true; c <- true
strobe <- true strobe <- true
} }
func sreceiver(c chan string, strobe chan bool) { func sreceiver(c chan string, strobe chan bool) {
if <-c != "hello" { panic("s value") } if <-c != "hello" {
panic("s value")
}
strobe <- true strobe <- true
} }
func ssender(c chan string, strobe chan bool) { func ssender(c chan string, strobe chan bool) {
c <- "hello again"; c <- "hello again"
strobe <- true strobe <- true
} }
var ticker = time.Tick(10*1000); // 10 us var ticker = time.Tick(10 * 1000) // 10 us
func sleep() { func sleep() {
<-ticker; <-ticker
<-ticker; <-ticker
runtime.Gosched(); runtime.Gosched()
runtime.Gosched(); runtime.Gosched()
runtime.Gosched(); runtime.Gosched()
} }
func main() { func main() {
var i32 int32; var i32 int32
var i64 int64; var i64 int64
var b bool; var b bool
var s string; var s string
var ok bool; var ok bool
var sync = make(chan bool); var sync = make(chan bool)
for buffer := 0; buffer < 2; buffer++ { for buffer := 0; buffer < 2; buffer++ {
c32 := make(chan int32, buffer); c32 := make(chan int32, buffer)
c64 := make(chan int64, buffer); c64 := make(chan int64, buffer)
cb := make(chan bool, buffer); cb := make(chan bool, buffer)
cs := make(chan string, buffer); cs := make(chan string, buffer)
i32, ok = <-c32; i32, ok = <-c32
if ok { panic("blocked i32sender") } if ok {
panic("blocked i32sender")
}
i64, ok = <-c64; i64, ok = <-c64
if ok { panic("blocked i64sender") } if ok {
panic("blocked i64sender")
}
b, ok = <-cb; b, ok = <-cb
if ok { panic("blocked bsender") } if ok {
panic("blocked bsender")
}
s, ok = <-cs; s, ok = <-cs
if ok { panic("blocked ssender") } if ok {
panic("blocked ssender")
}
go i32receiver(c32, sync); go i32receiver(c32, sync)
sleep(); sleep()
ok = c32 <- 123; ok = c32 <- 123
if !ok { panic("i32receiver buffer=", buffer) } if !ok {
<-sync; println("i32receiver buffer=", buffer)
panic("fail")
}
<-sync
go i32sender(c32, sync); go i32sender(c32, sync)
if buffer > 0 { <-sync } else { sleep() } if buffer > 0 {
i32, ok = <-c32; <-sync
if !ok { panic("i32sender buffer=", buffer) } } else {
if i32 != 234 { panic("i32sender value") } sleep()
if buffer == 0 { <-sync } }
i32, ok = <-c32
if !ok {
println("i32sender buffer=", buffer)
panic("fail")
}
if i32 != 234 {
panic("i32sender value")
}
if buffer == 0 {
<-sync
}
go i64receiver(c64, sync); go i64receiver(c64, sync)
sleep(); sleep()
ok = c64 <- 123456; ok = c64 <- 123456
if !ok { panic("i64receiver") } if !ok {
<-sync; panic("i64receiver")
}
<-sync
go i64sender(c64, sync); go i64sender(c64, sync)
if buffer > 0 { <-sync } else { sleep() } if buffer > 0 {
i64, ok = <-c64; <-sync
if !ok { panic("i64sender") } } else {
if i64 != 234567 { panic("i64sender value") } sleep()
if buffer == 0 { <-sync } }
i64, ok = <-c64
if !ok {
panic("i64sender")
}
if i64 != 234567 {
panic("i64sender value")
}
if buffer == 0 {
<-sync
}
go breceiver(cb, sync); go breceiver(cb, sync)
sleep(); sleep()
ok = cb <- true; ok = cb <- true
if !ok { panic("breceiver") } if !ok {
<-sync; panic("breceiver")
}
<-sync
go bsender(cb, sync); go bsender(cb, sync)
if buffer > 0 { <-sync } else { sleep() } if buffer > 0 {
b, ok = <-cb; <-sync
if !ok { panic("bsender") } } else {
if !b{ panic("bsender value") } sleep()
if buffer == 0 { <-sync } }
b, ok = <-cb
if !ok {
panic("bsender")
}
if !b {
panic("bsender value")
}
if buffer == 0 {
<-sync
}
go sreceiver(cs, sync); go sreceiver(cs, sync)
sleep(); sleep()
ok = cs <- "hello"; ok = cs <- "hello"
if !ok { panic("sreceiver") } if !ok {
<-sync; panic("sreceiver")
}
<-sync
go ssender(cs, sync); go ssender(cs, sync)
if buffer > 0 { <-sync } else { sleep() } if buffer > 0 {
s, ok = <-cs; <-sync
if !ok { panic("ssender") } } else {
if s != "hello again" { panic("ssender value") } sleep()
if buffer == 0 { <-sync } }
s, ok = <-cs
if !ok {
panic("ssender")
}
if s != "hello again" {
panic("ssender value")
}
if buffer == 0 {
<-sync
}
} }
print("PASS\n") print("PASS\n")
} }

View File

@ -29,23 +29,26 @@ func Filter(in <-chan int, out chan<- int, prime int) {
// The prime sieve: Daisy-chain Filter processes together. // The prime sieve: Daisy-chain Filter processes together.
func Sieve(primes chan<- int) { func Sieve(primes chan<- int) {
ch := make(chan int); // Create a new channel. ch := make(chan int) // Create a new channel.
go Generate(ch); // Start Generate() as a subprocess. go Generate(ch) // Start Generate() as a subprocess.
for { for {
// Note that ch is different on each iteration. // Note that ch is different on each iteration.
prime := <-ch; prime := <-ch
primes <- prime; primes <- prime
ch1 := make(chan int); ch1 := make(chan int)
go Filter(ch, ch1, prime); go Filter(ch, ch1, prime)
ch = ch1 ch = ch1
} }
} }
func main() { func main() {
primes := make(chan int); primes := make(chan int)
go Sieve(primes); go Sieve(primes)
a := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; a := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}
for i := 0; i < len(a); i++ { for i := 0; i < len(a); i++ {
if x := <-primes; x != a[i] { panic(x, " != ", a[i]) } if x := <-primes; x != a[i] {
println(x, " != ", a[i])
panic("fail")
}
} }
} }

View File

@ -165,7 +165,8 @@ func main() {
a := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97} a := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}
for i := 0; i < len(a); i++ { for i := 0; i < len(a); i++ {
if x := <-primes; x != a[i] { if x := <-primes; x != a[i] {
panic(x, " != ", a[i]) println(x, " != ", a[i])
panic("fail")
} }
} }
} }

View File

@ -70,6 +70,7 @@ func main() {
m[ic] = 1 m[ic] = 1
m[id] = 2 m[id] = 2
if m[ic] != 2 { if m[ic] != 2 {
panic("m[ic] = ", m[ic]) println("m[ic] = ", m[ic])
panic("bad m[ic]")
} }
} }

View File

@ -7,9 +7,12 @@
package main package main
func main() { func main() {
x := 0; x := 0
x = ^x; // unary ^ not yet implemented x = ^x // unary ^ not yet implemented
if x != ^0 { panic(x, " ", ^0) } if x != ^0 {
println(x, " ", ^0)
panic("fail")
}
} }
/* /*

View File

@ -7,18 +7,21 @@
package main package main
type Service struct { type Service struct {
rpc [2]int; rpc [2]int
} }
func (s *Service) Serve(a int64) { func (s *Service) Serve(a int64) {
if a != 1234 { panic(a, " not 1234\n") } if a != 1234 {
print(a, " not 1234\n")
panic("fail")
}
} }
var arith Service var arith Service
func main() { func main() {
c := make(chan string); c := make(chan string)
a := new(Service); a := new(Service)
go a.Serve(1234); go a.Serve(1234)
_ = c; _ = c
} }

View File

@ -6,16 +6,22 @@
package main package main
type A []int; type A []int
func main() { func main() {
var a [3]A; var a [3]A
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
a[i] = A{i}; a[i] = A{i}
}
if a[0][0] != 0 {
panic("fail a[0][0]")
}
if a[1][0] != 1 {
panic("fail a[1][0]")
}
if a[2][0] != 2 {
panic("fail a[2][0]")
} }
if a[0][0] != 0 { panic(); }
if a[1][0] != 1 { panic(); }
if a[2][0] != 2 { panic(); }
} }
/* /*

View File

@ -14,6 +14,7 @@ func f() {
func main() { func main() {
if a != 0 { if a != 0 {
panic("a=", a) println("a=", a)
panic("fail")
} }
} }

View File

@ -5,8 +5,12 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package main package main
type S struct { a int }
type S struct {
a int
}
type PS *S type PS *S
func (p *S) get() int { func (p *S) get() int {
return p.a return p.a
} }
@ -18,8 +22,8 @@ func fn(p PS) int {
return p.get() // ERROR "undefined" return p.get() // ERROR "undefined"
} }
func main() { func main() {
s := S{1}; s := S{1}
if s.get() != 1 { if s.get() != 1 {
panic() panic("fail")
} }
} }

View File

@ -6,13 +6,14 @@
package main package main
var g byte = 123; var g byte = 123
var f *byte = &g; var f *byte = &g
var b = make([]byte, 5); var b = make([]byte, 5)
func main() { func main() {
b[0:1][0] = *f; b[0:1][0] = *f
if b[0] != 123 { if b[0] != 123 {
panic("want 123 got ", b[0]); println("want 123 got", b[0])
panic("fail")
} }
} }

View File

@ -20,13 +20,16 @@ func main() {
if v1 != 1 || v2.f != 2 || v3[0] != 3 || v3[1] != 4 || if v1 != 1 || v2.f != 2 || v3[0] != 3 || v3[1] != 4 ||
v4[0] != 5 || v4[1] != 6 || v5[0] != 7 || v5[1] != 8 || v4[0] != 5 || v4[1] != 6 || v5[0] != 7 || v5[1] != 8 ||
v6.f != 9 || v7[0] != 10 || v8[0] != 11 { v6.f != 9 || v7[0] != 10 || v8[0] != 11 {
panic() panic("fail")
} }
} }
type T1 int type T1 int
type T2 struct { f int } type T2 struct {
f int
}
type T3 []int type T3 []int
type T4 [2]int type T4 [2]int
type T5 map[int]int type T5 map[int]int
const f = 0 const f = 0

View File

@ -12,10 +12,11 @@
package main package main
var gen = 'a' var gen = 'a'
func f(n int) string { func f(n int) string {
s := string(gen) + string(n+'A'-1); s := string(gen) + string(n+'A'-1)
gen++; gen++
return s; return s
} }
func g(x, y string) string { func g(x, y string) string {
@ -23,16 +24,19 @@ func g(x, y string) string {
} }
func main() { func main() {
s := f(1) + f(2); s := f(1) + f(2)
if s != "aAbB" { if s != "aAbB" {
panic("BUG: bug221a: ", s); println("BUG: bug221a: ", s)
panic("fail")
} }
s = g(f(3), f(4)); s = g(f(3), f(4))
if s != "cCdD" { if s != "cCdD" {
panic("BUG: bug221b: ", s); println("BUG: bug221b: ", s)
panic("fail")
} }
s = f(5) + f(6) + f(7) + f(8) + f(9); s = f(5) + f(6) + f(7) + f(8) + f(9)
if s != "eEfFgGhHiI" { if s != "eEfFgGhHiI" {
panic("BUG: bug221c: ", s); println("BUG: bug221c: ", s)
panic("fail")
} }
} }

View File

@ -26,9 +26,11 @@ func f() int {
func main() { func main() {
if nf != 3 || x != 1 || y != 2 || z != 3 { if nf != 3 || x != 1 || y != 2 || z != 3 {
panic("nf=", nf, " x=", x, " y=", y) println("nf=", nf, " x=", x, " y=", y)
panic("fail")
} }
if a != "A" || aok != true || b != "" || bok != false { if a != "A" || aok != true || b != "" || bok != false {
panic("a=", a, " aok=", aok, " b=", b, " bok=", bok) println("a=", a, " aok=", aok, " b=", b, " bok=", bok)
panic("fail")
} }
} }

View File

@ -11,10 +11,12 @@ func main() {
c <- 100 c <- 100
x, ok := <-c x, ok := <-c
if x != 100 || !ok { if x != 100 || !ok {
panic("x=", x, " ok=", ok, " want 100, true") println("x=", x, " ok=", ok, " want 100, true")
panic("fail")
} }
x, ok = <-c x, ok = <-c
if x != 0 || ok { if x != 0 || ok {
panic("x=", x, " ok=", ok, " want 0, false") println("x=", x, " ok=", ok, " want 0, false")
panic("fail")
} }
} }

View File

@ -25,6 +25,7 @@ var v, ok = m[g()]
func main() { func main() {
if x != 1 || y != 2 || z != 3 || nf != 1 || v != 0 || ok != false || ng != 1 { if x != 1 || y != 2 || z != 3 || nf != 1 || v != 0 || ok != false || ng != 1 {
panic("x=", x, " y=", y, " z=", z, " nf=", nf, " v=", v, " ok=", ok, " ng=", ng) println("x=", x, " y=", y, " z=", z, " nf=", nf, " v=", v, " ok=", ok, " ng=", ng)
panic("fail")
} }
} }

View File

@ -20,9 +20,10 @@ type S4 struct {
S3 S3
S1 S1
} }
func main() { func main() {
var s4 S4 var s4 S4
if s4.i != 0 { // .i refers to s4.S1.i, unambiguously if s4.i != 0 { // .i refers to s4.S1.i, unambiguously
panic() panic("fail")
} }
} }

View File

@ -40,13 +40,15 @@ func main() {
m := make(map[string]int) m := make(map[string]int)
m[f()], *g() = strconv.Atoi(h()) m[f()], *g() = strconv.Atoi(h())
if m["abc"] != 123 || trace != "fgh" { if m["abc"] != 123 || trace != "fgh" {
panic("BUG", m["abc"], trace) println("BUG", m["abc"], trace)
panic("fail")
} }
mm := make(map[string]os.Error) mm := make(map[string]os.Error)
trace = "" trace = ""
mm["abc"] = os.EINVAL mm["abc"] = os.EINVAL
*i(), mm[f()] = strconv.Atoi(h()) *i(), mm[f()] = strconv.Atoi(h())
if mm["abc"] != nil || trace != "ifh" { if mm["abc"] != nil || trace != "ifh" {
panic("BUG1", mm["abc"], trace) println("BUG1", mm["abc"], trace)
panic("fail")
} }
} }

View File

@ -11,9 +11,9 @@ func caller(f func(int, int) int, a, b int, c chan int) {
} }
func gocall(f func(int, int) int, a, b int) int { func gocall(f func(int, int) int, a, b int) int {
c := make(chan int); c := make(chan int)
go caller(f, a, b, c); go caller(f, a, b, c)
return <-c; return <-c
} }
func call(f func(int, int) int, a, b int) int { func call(f func(int, int) int, a, b int) int {
@ -30,7 +30,7 @@ func add(x, y int) int {
return x + y return x + y
} }
func fn() (func(int, int) int) { func fn() func(int, int) int {
return f return f
} }
@ -40,50 +40,50 @@ func addc(x, y int, c chan int) {
c <- x+y c <- x+y
} }
func fnc() (func(int, int, chan int)) { func fnc() func(int, int, chan int) {
return fc return fc
} }
func three(x int) { func three(x int) {
if x != 3 { if x != 3 {
panic("wrong val", x) println("wrong val", x)
panic("fail")
} }
} }
var notmain func() var notmain func()
func emptyresults() () {} func emptyresults() {}
func noresults() {} func noresults() {}
var nothing func() var nothing func()
func main() { func main() {
three(call(add, 1, 2)); three(call(add, 1, 2))
three(call1(add, 1, 2)); three(call1(add, 1, 2))
f = add; f = add
three(call(f, 1, 2)); three(call(f, 1, 2))
three(call1(f, 1, 2)); three(call1(f, 1, 2))
three(call(fn(), 1, 2)); three(call(fn(), 1, 2))
three(call1(fn(), 1, 2)); three(call1(fn(), 1, 2))
three(call(func(a,b int) int {return a+b}, 1, 2)); three(call(func(a, b int) int { return a + b }, 1, 2))
three(call1(func(a,b int) int {return a+b}, 1, 2)); three(call1(func(a, b int) int { return a + b }, 1, 2))
fc = addc; fc = addc
c := make(chan int); c := make(chan int)
go addc(1, 2, c); go addc(1, 2, c)
three(<-c); three(<-c)
go fc(1, 2, c); go fc(1, 2, c)
three(<-c); three(<-c)
go fnc()(1, 2, c); go fnc()(1, 2, c)
three(<-c); three(<-c)
go func(a, b int, c chan int){c <- a+b}(1, 2, c); go func(a, b int, c chan int) { c <- a+b }(1, 2, c)
three(<-c); three(<-c)
emptyresults(); emptyresults()
noresults(); noresults()
nothing = emptyresults; nothing = emptyresults
nothing(); nothing()
nothing = noresults; nothing = noresults
nothing(); nothing()
} }

View File

@ -189,6 +189,6 @@ bar
bar bar
bal bal
bal bal
barCount != 1 panic: barCount != 1
panic PC=xxx panic PC=xxx
BUG BUG

View File

@ -9,21 +9,28 @@
package main package main
type Stringer interface { String() string } type Stringer interface {
type StringLengther interface { String() string; Length() int } String() string
}
type StringLengther interface {
String() string
Length() int
}
type Empty interface{} type Empty interface{}
type T string type T string
func (t T) String() string { func (t T) String() string {
return string(t); return string(t)
} }
func (t T) Length() int { func (t T) Length() int {
return len(t); return len(t)
} }
type U string type U string
func (u U) String() string { func (u U) String() string {
return string(u); return string(u)
} }
var t = T("hello") var t = T("hello")
@ -36,104 +43,105 @@ var ok bool
func hello(s string) { func hello(s string) {
if s != "hello" { if s != "hello" {
panic("not hello: ", s); println("not hello: ", s)
panic("fail")
} }
} }
func five(i int) { func five(i int) {
if i != 5 { if i != 5 {
panic("not 5: ", i); println("not 5: ", i)
panic("fail")
} }
} }
func true(ok bool) { func true(ok bool) {
if !ok { if !ok {
panic("not true"); panic("not true")
} }
} }
func false(ok bool) { func false(ok bool) {
if ok { if ok {
panic("not false"); panic("not false")
} }
} }
func main() { func main() {
// T2I // T2I
s = t; s = t
hello(s.String()); hello(s.String())
// I2T // I2T
t = s.(T); t = s.(T)
hello(t.String()); hello(t.String())
// T2E // T2E
e = t; e = t
// E2T // E2T
t = e.(T); t = e.(T)
hello(t.String()); hello(t.String())
// T2I again // T2I again
sl = t; sl = t
hello(sl.String()); hello(sl.String())
five(sl.Length()); five(sl.Length())
// I2I static // I2I static
s = sl; s = sl
hello(s.String()); hello(s.String())
// I2I dynamic // I2I dynamic
sl = s.(StringLengther); sl = s.(StringLengther)
hello(sl.String()); hello(sl.String())
five(sl.Length()); five(sl.Length())
// I2E (and E2T) // I2E (and E2T)
e = s; e = s
hello(e.(T).String()); hello(e.(T).String())
// E2I // E2I
s = e.(Stringer); s = e.(Stringer)
hello(s.String()); hello(s.String())
// I2T2 true // I2T2 true
t, ok = s.(T); t, ok = s.(T)
true(ok); true(ok)
hello(t.String()); hello(t.String())
// I2T2 false // I2T2 false
_, ok = s.(U); _, ok = s.(U)
false(ok); false(ok)
// I2I2 true // I2I2 true
sl, ok = s.(StringLengther); sl, ok = s.(StringLengther)
true(ok); true(ok)
hello(sl.String()); hello(sl.String())
five(sl.Length()); five(sl.Length())
// I2I2 false (and T2I) // I2I2 false (and T2I)
s = u; s = u
sl, ok = s.(StringLengther); sl, ok = s.(StringLengther)
false(ok); false(ok)
// E2T2 true // E2T2 true
t, ok = e.(T); t, ok = e.(T)
true(ok); true(ok)
hello(t.String()); hello(t.String())
// E2T2 false // E2T2 false
i, ok = e.(int); i, ok = e.(int)
false(ok); false(ok)
// E2I2 true // E2I2 true
sl, ok = e.(StringLengther); sl, ok = e.(StringLengther)
true(ok); true(ok)
hello(sl.String()); hello(sl.String())
five(sl.Length()); five(sl.Length())
// E2I2 false (and T2E) // E2I2 false (and T2E)
e = u; e = u
sl, ok = e.(StringLengther); sl, ok = e.(StringLengther)
false(ok); false(ok)
} }

View File

@ -22,7 +22,8 @@ func (t T) V() {
func (t *T) P() { func (t *T) P() {
if *t != 42 { if *t != 42 {
panic(t, *t) println(t, *t)
panic("fail")
} }
np++ np++
} }

View File

@ -6,139 +6,128 @@
package main package main
func func setpd(a []int) {
setpd(a []int) {
// print("setpd a=", a, " len=", len(a), " cap=", cap(a), "\n"); // print("setpd a=", a, " len=", len(a), " cap=", cap(a), "\n");
for i := 0; i < len(a); i++ { for i := 0; i < len(a); i++ {
a[i] = i; a[i] = i
} }
} }
func func sumpd(a []int) int {
sumpd(a []int) int {
// print("sumpd a=", a, " len=", len(a), " cap=", cap(a), "\n"); // print("sumpd a=", a, " len=", len(a), " cap=", cap(a), "\n");
t := 0; t := 0
for i := 0; i < len(a); i++ { for i := 0; i < len(a); i++ {
t += a[i]; t += a[i]
} }
// print("sumpd t=", t, "\n"); // print("sumpd t=", t, "\n");
return t; return t
} }
func func setpf(a *[20]int) {
setpf(a *[20]int) {
// print("setpf a=", a, " len=", len(a), " cap=", cap(a), "\n"); // print("setpf a=", a, " len=", len(a), " cap=", cap(a), "\n");
for i := 0; i < len(a); i++ { for i := 0; i < len(a); i++ {
a[i] = i; a[i] = i
} }
} }
func func sumpf(a *[20]int) int {
sumpf(a *[20]int) int {
// print("sumpf a=", a, " len=", len(a), " cap=", cap(a), "\n"); // print("sumpf a=", a, " len=", len(a), " cap=", cap(a), "\n");
t := 0; t := 0
for i := 0; i < len(a); i++ { for i := 0; i < len(a); i++ {
t += a[i]; t += a[i]
} }
// print("sumpf t=", t, "\n"); // print("sumpf t=", t, "\n");
return t; return t
} }
func func res(t int, lb, hb int) {
res(t int, lb, hb int) { sb := (hb - lb) * (hb + lb - 1) / 2
sb := (hb-lb)*(hb+lb-1)/2;
if t != sb { if t != sb {
print("lb=", lb, print("lb=", lb,
"; hb=", hb, "; hb=", hb,
"; t=", t, "; t=", t,
"; sb=", sb, "; sb=", sb,
"\n"); "\n")
panic("res") panic("res")
} }
} }
// call ptr dynamic with ptr dynamic // call ptr dynamic with ptr dynamic
func func testpdpd() {
testpdpd() { a := make([]int, 10, 100)
a := make([]int, 10, 100);
if len(a) != 10 && cap(a) != 100 { if len(a) != 10 && cap(a) != 100 {
panic("len and cap from new: ", len(a), " ", cap(a), "\n"); print("len and cap from new: ", len(a), " ", cap(a), "\n")
panic("fail")
} }
a = a[0:100]; a = a[0:100]
setpd(a); setpd(a)
a = a[0:10]; a = a[0:10]
res(sumpd(a), 0, 10); res(sumpd(a), 0, 10)
a = a[5:25]; a = a[5:25]
res(sumpd(a), 5, 25); res(sumpd(a), 5, 25)
} }
// call ptr fixed with ptr fixed // call ptr fixed with ptr fixed
func func testpfpf() {
testpfpf() { var a [20]int
var a [20]int;
setpf(&a); setpf(&a)
res(sumpf(&a), 0, 20); res(sumpf(&a), 0, 20)
} }
// call ptr dynamic with ptr fixed from new // call ptr dynamic with ptr fixed from new
func func testpdpf1() {
testpdpf1() { a := new([40]int)
a := new([40]int); setpd(a)
setpd(a); res(sumpd(a), 0, 40)
res(sumpd(a), 0, 40);
b := (*a)[5:30]; b := (*a)[5:30]
res(sumpd(b), 5, 30); res(sumpd(b), 5, 30)
} }
// call ptr dynamic with ptr fixed from var // call ptr dynamic with ptr fixed from var
func func testpdpf2() {
testpdpf2() { var a [80]int
var a [80]int;
setpd(&a); setpd(&a)
res(sumpd(&a), 0, 80); res(sumpd(&a), 0, 80)
} }
// generate bounds error with ptr dynamic // generate bounds error with ptr dynamic
func func testpdfault() {
testpdfault() { a := make([]int, 100)
a := make([]int, 100);
print("good\n"); print("good\n")
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
a[i] = 0; a[i] = 0
} }
print("should fault\n"); print("should fault\n")
a[100] = 0; a[100] = 0
print("bad\n"); print("bad\n")
} }
// generate bounds error with ptr fixed // generate bounds error with ptr fixed
func func testfdfault() {
testfdfault() { var a [80]int
var a [80]int;
print("good\n"); print("good\n")
for i := 0; i < 80; i++ { for i := 0; i < 80; i++ {
a[i] = 0; a[i] = 0
} }
print("should fault\n"); print("should fault\n")
x := 80; x := 80
a[x] = 0; a[x] = 0
print("bad\n"); print("bad\n")
} }
func func main() {
main() { testpdpd()
testpdpd(); testpfpf()
testpfpf(); testpdpf1()
testpdpf1(); testpdpf2()
testpdpf2();
// print("testpdfault\n"); testpdfault(); // print("testpdfault\n"); testpdfault();
// print("testfdfault\n"); testfdfault(); // print("testfdfault\n"); testfdfault();
} }

View File

@ -10,36 +10,33 @@ import "os"
import "runtime" import "runtime"
import "sync" import "sync"
var randx int; var randx int
func func nrand(n int) int {
nrand(n int) int { randx += 10007
randx += 10007;
if randx >= 1000000 { if randx >= 1000000 {
randx -= 1000000; randx -= 1000000
} }
return randx%n; return randx % n
} }
type Chan struct { type Chan struct {
sc,rc chan int; // send and recv chan sc, rc chan int // send and recv chan
sv,rv int; // send and recv seq sv, rv int // send and recv seq
} }
var var (
( nproc int
nproc int; nprocLock sync.Mutex
nprocLock sync.Mutex; cval int
cval int; end int = 10000
end int = 10000; totr, tots int
totr,tots int; totLock sync.Mutex
totLock sync.Mutex; nc *Chan
nc *Chan;
) )
func func init() {
init() { nc = new(Chan)
nc = new(Chan);
} }
func changeNproc(adjust int) int { func changeNproc(adjust int) int {
@ -50,54 +47,52 @@ func changeNproc(adjust int) int {
return ret return ret
} }
func func mkchan(c, n int) []*Chan {
mkchan(c,n int) []*Chan { ca := make([]*Chan, n)
ca := make([]*Chan, n);
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
cval = cval+100; cval = cval + 100
ch := new(Chan); ch := new(Chan)
ch.sc = make(chan int, c); ch.sc = make(chan int, c)
ch.rc = ch.sc; ch.rc = ch.sc
ch.sv = cval; ch.sv = cval
ch.rv = cval; ch.rv = cval
ca[i] = ch; ca[i] = ch
} }
return ca; return ca
} }
func func expect(v, v0 int) (newv int) {
expect(v, v0 int) (newv int) {
if v == v0 { if v == v0 {
if v%100 == 75 { if v%100 == 75 {
return end; return end
} }
return v+1; return v + 1
} }
panic("got ", v, " expected ", v0+1, "\n"); print("got ", v, " expected ", v0+1, "\n")
panic("fail")
} }
func (c *Chan) send() bool { func (c *Chan) send() bool {
// print("send ", c.sv, "\n"); // print("send ", c.sv, "\n");
totLock.Lock(); totLock.Lock()
tots++; tots++
totLock.Unlock(); totLock.Unlock()
c.sv = expect(c.sv, c.sv); c.sv = expect(c.sv, c.sv)
if c.sv == end { if c.sv == end {
c.sc = nil; c.sc = nil
return true; return true
} }
return false; return false
} }
func func send(c *Chan) {
send(c *Chan) {
for { for {
for r := nrand(10); r >= 0; r-- { for r := nrand(10); r >= 0; r-- {
runtime.Gosched(); runtime.Gosched()
} }
c.sc <- c.sv; c.sc <- c.sv
if c.send() { if c.send() {
break; break
} }
} }
changeNproc(-1) changeNproc(-1)
@ -105,225 +100,230 @@ send(c *Chan) {
func (c *Chan) recv(v int) bool { func (c *Chan) recv(v int) bool {
// print("recv ", v, "\n"); // print("recv ", v, "\n");
totLock.Lock(); totLock.Lock()
totr++; totr++
totLock.Unlock(); totLock.Unlock()
c.rv = expect(c.rv, v); c.rv = expect(c.rv, v)
if c.rv == end { if c.rv == end {
c.rc = nil; c.rc = nil
return true; return true
} }
return false; return false
} }
func func recv(c *Chan) {
recv(c *Chan) { var v int
var v int;
for { for {
for r := nrand(10); r >= 0; r-- { for r := nrand(10); r >= 0; r-- {
runtime.Gosched(); runtime.Gosched()
} }
v = <-c.rc; v = <-c.rc
if c.recv(v) { if c.recv(v) {
break; break
} }
} }
changeNproc(-1); changeNproc(-1)
} }
func func sel(r0, r1, r2, r3, s0, s1, s2, s3 *Chan) {
sel(r0,r1,r2,r3, s0,s1,s2,s3 *Chan) { var v int
var v int;
a := 0; // local chans running a := 0 // local chans running
if r0.rc != nil { a++ } if r0.rc != nil {
if r1.rc != nil { a++ } a++
if r2.rc != nil { a++ } }
if r3.rc != nil { a++ } if r1.rc != nil {
if s0.sc != nil { a++ } a++
if s1.sc != nil { a++ } }
if s2.sc != nil { a++ } if r2.rc != nil {
if s3.sc != nil { a++ } a++
}
if r3.rc != nil {
a++
}
if s0.sc != nil {
a++
}
if s1.sc != nil {
a++
}
if s2.sc != nil {
a++
}
if s3.sc != nil {
a++
}
for { for {
for r := nrand(5); r >= 0; r-- { for r := nrand(5); r >= 0; r-- {
runtime.Gosched(); runtime.Gosched()
} }
select { select {
case v = <-r0.rc: case v = <-r0.rc:
if r0.recv(v) { if r0.recv(v) {
a--; a--
} }
case v = <-r1.rc: case v = <-r1.rc:
if r1.recv(v) { if r1.recv(v) {
a--; a--
} }
case v = <-r2.rc: case v = <-r2.rc:
if r2.recv(v) { if r2.recv(v) {
a--; a--
} }
case v = <-r3.rc: case v = <-r3.rc:
if r3.recv(v) { if r3.recv(v) {
a--; a--
} }
case s0.sc <- s0.sv: case s0.sc <- s0.sv:
if s0.send() { if s0.send() {
a--; a--
} }
case s1.sc <- s1.sv: case s1.sc <- s1.sv:
if s1.send() { if s1.send() {
a--; a--
} }
case s2.sc <- s2.sv: case s2.sc <- s2.sv:
if s2.send() { if s2.send() {
a--; a--
} }
case s3.sc <- s3.sv: case s3.sc <- s3.sv:
if s3.send() { if s3.send() {
a--; a--
} }
} }
if a == 0 { if a == 0 {
break; break
} }
} }
changeNproc(-1); changeNproc(-1)
} }
// direct send to direct recv // direct send to direct recv
func func test1(c *Chan) {
test1(c *Chan) {
changeNproc(2) changeNproc(2)
go send(c); go send(c)
go recv(c); go recv(c)
} }
// direct send to select recv // direct send to select recv
func func test2(c int) {
test2(c int) { ca := mkchan(c, 4)
ca := mkchan(c,4);
changeNproc(4) changeNproc(4)
go send(ca[0]); go send(ca[0])
go send(ca[1]); go send(ca[1])
go send(ca[2]); go send(ca[2])
go send(ca[3]); go send(ca[3])
changeNproc(1) changeNproc(1)
go sel(ca[0],ca[1],ca[2],ca[3], nc,nc,nc,nc); go sel(ca[0], ca[1], ca[2], ca[3], nc, nc, nc, nc)
} }
// select send to direct recv // select send to direct recv
func func test3(c int) {
test3(c int) { ca := mkchan(c, 4)
ca := mkchan(c,4);
changeNproc(4) changeNproc(4)
go recv(ca[0]); go recv(ca[0])
go recv(ca[1]); go recv(ca[1])
go recv(ca[2]); go recv(ca[2])
go recv(ca[3]); go recv(ca[3])
changeNproc(1) changeNproc(1)
go sel(nc,nc,nc,nc, ca[0],ca[1],ca[2],ca[3]); go sel(nc, nc, nc, nc, ca[0], ca[1], ca[2], ca[3])
} }
// select send to select recv // select send to select recv
func func test4(c int) {
test4(c int) { ca := mkchan(c, 4)
ca := mkchan(c,4);
changeNproc(2) changeNproc(2)
go sel(nc,nc,nc,nc, ca[0],ca[1],ca[2],ca[3]); go sel(nc, nc, nc, nc, ca[0], ca[1], ca[2], ca[3])
go sel(ca[0],ca[1],ca[2],ca[3], nc,nc,nc,nc); go sel(ca[0], ca[1], ca[2], ca[3], nc, nc, nc, nc)
} }
func func test5(c int) {
test5(c int) { ca := mkchan(c, 8)
ca := mkchan(c,8);
changeNproc(2) changeNproc(2)
go sel(ca[4],ca[5],ca[6],ca[7], ca[0],ca[1],ca[2],ca[3]); go sel(ca[4], ca[5], ca[6], ca[7], ca[0], ca[1], ca[2], ca[3])
go sel(ca[0],ca[1],ca[2],ca[3], ca[4],ca[5],ca[6],ca[7]); go sel(ca[0], ca[1], ca[2], ca[3], ca[4], ca[5], ca[6], ca[7])
} }
func func test6(c int) {
test6(c int) { ca := mkchan(c, 12)
ca := mkchan(c,12);
changeNproc(4) changeNproc(4)
go send(ca[4]); go send(ca[4])
go send(ca[5]); go send(ca[5])
go send(ca[6]); go send(ca[6])
go send(ca[7]); go send(ca[7])
changeNproc(4) changeNproc(4)
go recv(ca[8]); go recv(ca[8])
go recv(ca[9]); go recv(ca[9])
go recv(ca[10]); go recv(ca[10])
go recv(ca[11]); go recv(ca[11])
changeNproc(2) changeNproc(2)
go sel(ca[4],ca[5],ca[6],ca[7], ca[0],ca[1],ca[2],ca[3]); go sel(ca[4], ca[5], ca[6], ca[7], ca[0], ca[1], ca[2], ca[3])
go sel(ca[0],ca[1],ca[2],ca[3], ca[8],ca[9],ca[10],ca[11]); go sel(ca[0], ca[1], ca[2], ca[3], ca[8], ca[9], ca[10], ca[11])
} }
// wait for outstanding tests to finish // wait for outstanding tests to finish
func func wait() {
wait() { runtime.Gosched()
runtime.Gosched();
for changeNproc(0) != 0 { for changeNproc(0) != 0 {
runtime.Gosched(); runtime.Gosched()
} }
} }
// run all tests with specified buffer size // run all tests with specified buffer size
func func tests(c int) {
tests(c int) { ca := mkchan(c, 4)
ca := mkchan(c,4); test1(ca[0])
test1(ca[0]); test1(ca[1])
test1(ca[1]); test1(ca[2])
test1(ca[2]); test1(ca[3])
test1(ca[3]); wait()
wait();
test2(c); test2(c)
wait(); wait()
test3(c); test3(c)
wait(); wait()
test4(c); test4(c)
wait(); wait()
test5(c); test5(c)
wait(); wait()
test6(c); test6(c)
wait(); wait()
} }
// run all test with 4 buffser sizes // run all test with 4 buffser sizes
func func main() {
main() {
tests(0); tests(0)
tests(1); tests(1)
tests(10); tests(10)
tests(100); tests(100)
t := 4 * // buffer sizes t := 4 * // buffer sizes
(4*4 + // tests 1,2,3,4 channels (4*4 + // tests 1,2,3,4 channels
8 + // test 5 channels 8 + // test 5 channels
12) * // test 6 channels 12) * // test 6 channels
76; // sends/recvs on a channel 76 // sends/recvs on a channel
if tots != t || totr != t { if tots != t || totr != t {
print("tots=", tots, " totr=", totr, " sb=", t, "\n"); print("tots=", tots, " totr=", totr, " sb=", t, "\n")
os.Exit(1); os.Exit(1)
} }
os.Exit(0); os.Exit(0)
} }

View File

@ -7,106 +7,112 @@
package main package main
func func main() {
main() { var c string
var c string;
a := `abc`; a := `abc`
b := `xyz`; b := `xyz`
/* print a literal */ /* print a literal */
print(`abc`); print(`abc`)
/* print a variable */ /* print a variable */
print(b, "-"); print(b, "-")
/* catenate literals */ /* catenate literals */
print(`abc` + `xyz`, "-"); print(`abc`+`xyz`, "-")
/* catenate variables */ /* catenate variables */
print(a+b, "-"); print(a+b, "-")
/* compare literals */ /* compare literals */
if `abc` == `xyz` || `abc` != "abc" || `abc` > `xyz` { if `abc` == `xyz` || `abc` != "abc" || `abc` > `xyz` {
panic("compare literals"); panic("compare literals")
} }
/* compare variables */ /* compare variables */
if a == b || a != a || a > b { if a == b || a != a || a > b {
panic("compare variables"); panic("compare variables")
} }
/* cat */ /* cat */
c = a+b; c = a + b
print(c, "-"); print(c, "-")
/* catequal */ /* catequal */
c = a; c = a
c += b; c += b
print(c, "-"); print(c, "-")
/* clumsy evaluation */ /* clumsy evaluation */
c = b; c = b
c = a + c; c = a + c
print(c, "-"); print(c, "-")
/* len */ /* len */
if len(c) != 6 { if len(c) != 6 {
panic("len ", len(c)); print("len ", len(c))
panic("fail")
} }
/* index strings */ /* index strings */
for i := 0; i < len(c); i = i + 1 { for i := 0; i < len(c); i = i + 1 {
if c[i] != (a + b)[i] { if c[i] != (a + b)[i] {
panic("index ", i, " ", c[i], " ", (a+b)[i]); print("index ", i, " ", c[i], " ", (a + b)[i])
panic("fail")
} }
} }
/* slice strings */ /* slice strings */
print(c[0:3], c[3:]); print(c[0:3], c[3:])
print("\n"); print("\n")
/* create string with integer constant */ /* create string with integer constant */
c = string('x'); c = string('x')
if c != "x" { if c != "x" {
panic("create int ", c); print("create int ", c)
panic("fail")
} }
/* create string with integer variable */ /* create string with integer variable */
v := 'x'; v := 'x'
c = string(v); c = string(v)
if c != "x" { if c != "x" {
panic("create int ", c); print("create int ", c)
panic("fail")
} }
/* create string with byte array */ /* create string with byte array */
var z1 [3]byte; var z1 [3]byte
z1[0] = 'a'; z1[0] = 'a'
z1[1] = 'b'; z1[1] = 'b'
z1[2] = 'c'; z1[2] = 'c'
c = string(&z1); c = string(&z1)
if c != "abc" { if c != "abc" {
panic("create byte array ", c); print("create byte array ", c)
panic("fail")
} }
/* create string with int array */ /* create string with int array */
var z2 [3]int; var z2 [3]int
z2[0] = 'a'; z2[0] = 'a'
z2[1] = '\u1234'; z2[1] = '\u1234'
z2[2] = 'c'; z2[2] = 'c'
c = string(&z2); c = string(&z2)
if c != "a\u1234c" { if c != "a\u1234c" {
panic("create int array ", c); print("create int array ", c)
panic("fail")
} }
/* create string with byte array pointer */ /* create string with byte array pointer */
z3 := new([3]byte); z3 := new([3]byte)
z3[0] = 'a'; z3[0] = 'a'
z3[1] = 'b'; z3[1] = 'b'
z3[2] = 'c'; z3[2] = 'c'
c = string(z3); c = string(z3)
if c != "abc" { if c != "abc" {
panic("create array pointer ", c); print("create array pointer ", c)
panic("fail")
} }
} }

View File

@ -58,9 +58,10 @@ func main() {
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
runtime.GC() runtime.GC()
runtime.Gosched() runtime.Gosched()
time.Sleep(1e6); time.Sleep(1e6)
} }
if nfinal < N*8/10 { if nfinal < N*8/10 {
panic("not enough finalizing:", nfinal, "/", N) println("not enough finalizing:", nfinal, "/", N)
panic("fail")
} }
} }

View File

@ -47,7 +47,8 @@ func main() {
during := runtime.MemStats.Alloc during := runtime.MemStats.Alloc
runtime.Free(b) runtime.Free(b)
if a := runtime.MemStats.Alloc; a != 0 { if a := runtime.MemStats.Alloc; a != 0 {
panic("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)") println("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)")
panic("fail")
} }
bigger() bigger()
} }

View File

@ -15,51 +15,51 @@ type Number struct {
// Peano primitives // Peano primitives
func zero() *Number { func zero() *Number {
return nil; return nil
} }
func is_zero(x *Number) bool { func is_zero(x *Number) bool {
return x == nil; return x == nil
} }
func add1(x *Number) *Number { func add1(x *Number) *Number {
e := new(Number); e := new(Number)
e.next = x; e.next = x
return e; return e
} }
func sub1(x *Number) *Number { func sub1(x *Number) *Number {
return x.next; return x.next
} }
func add(x, y *Number) *Number { func add(x, y *Number) *Number {
if is_zero(y) { if is_zero(y) {
return x; return x
} }
return add(add1(x), sub1(y)); return add(add1(x), sub1(y))
} }
func mul(x, y *Number) *Number { func mul(x, y *Number) *Number {
if is_zero(x) || is_zero(y) { if is_zero(x) || is_zero(y) {
return zero(); return zero()
} }
return add(mul(x, sub1(y)), x); return add(mul(x, sub1(y)), x)
} }
func fact(n *Number) *Number { func fact(n *Number) *Number {
if is_zero(n) { if is_zero(n) {
return add1(zero()); return add1(zero())
} }
return mul(fact(sub1(n)), n); return mul(fact(sub1(n)), n)
} }
@ -68,26 +68,27 @@ func fact(n *Number) *Number {
func gen(n int) *Number { func gen(n int) *Number {
if n > 0 { if n > 0 {
return add1(gen(n - 1)); return add1(gen(n - 1))
} }
return zero(); return zero()
} }
func count(x *Number) int { func count(x *Number) int {
if is_zero(x) { if is_zero(x) {
return 0; return 0
} }
return count(sub1(x)) + 1; return count(sub1(x)) + 1
} }
func check(x *Number, expected int) { func check(x *Number, expected int) {
var c = count(x); var c = count(x)
if c != expected { if c != expected {
panic("error: found ", c, "; expected ", expected, "\n"); print("error: found ", c, "; expected ", expected, "\n")
panic("fail")
} }
} }
@ -96,24 +97,24 @@ func check(x *Number, expected int) {
// Test basic functionality // Test basic functionality
func verify() { func verify() {
check(zero(), 0); check(zero(), 0)
check(add1(zero()), 1); check(add1(zero()), 1)
check(gen(10), 10); check(gen(10), 10)
check(add(gen(3), zero()), 3); check(add(gen(3), zero()), 3)
check(add(zero(), gen(4)), 4); check(add(zero(), gen(4)), 4)
check(add(gen(3), gen(4)), 7); check(add(gen(3), gen(4)), 7)
check(mul(zero(), zero()), 0); check(mul(zero(), zero()), 0)
check(mul(gen(3), zero()), 0); check(mul(gen(3), zero()), 0)
check(mul(zero(), gen(4)), 0); check(mul(zero(), gen(4)), 0)
check(mul(gen(3), add1(zero())), 3); check(mul(gen(3), add1(zero())), 3)
check(mul(add1(zero()), gen(4)), 4); check(mul(add1(zero()), gen(4)), 4)
check(mul(gen(3), gen(4)), 12); check(mul(gen(3), gen(4)), 12)
check(fact(zero()), 1); check(fact(zero()), 1)
check(fact(add1(zero())), 1); check(fact(add1(zero())), 1)
check(fact(gen(5)), 120); check(fact(gen(5)), 120)
} }
@ -123,9 +124,8 @@ func verify() {
func main() { func main() {
verify(); verify()
for i := 0; i <= 9; i++ { for i := 0; i <= 9; i++ {
print(i, "! = ", count(fact(gen(i))), "\n"); print(i, "! = ", count(fact(gen(i))), "\n")
} }
} }

View File

@ -6,19 +6,17 @@
package main package main
var a,b,c,d,e,f,g,h,i int; var a, b, c, d, e, f, g, h, i int
func func printit() {
printit() { println(a, b, c, d, e, f, g, h, i)
println(a,b,c,d,e,f,g,h,i);
} }
func func testit(permuteok bool) bool {
testit(permuteok bool) bool {
if a+b+c+d+e+f+g+h+i != 45 { if a+b+c+d+e+f+g+h+i != 45 {
print("sum does not add to 45\n"); print("sum does not add to 45\n")
printit(); printit()
return false; return false
} }
return permuteok || return permuteok ||
a == 1 && a == 1 &&
@ -29,51 +27,51 @@ testit(permuteok bool) bool {
f == 6 && f == 6 &&
g == 7 && g == 7 &&
h == 8 && h == 8 &&
i == 9; i == 9
} }
func func swap(x, y int) (u, v int) {
swap(x, y int) (u, v int) {
return y, x return y, x
} }
func func main() {
main() { a = 1
a = 1; b = 2
b = 2; c = 3
c = 3; d = 4
d = 4; e = 5
e = 5; f = 6
f = 6; g = 7
g = 7; h = 8
h = 8; i = 9
i = 9;
if !testit(false) { panic("init val\n"); } if !testit(false) {
panic("init val\n")
}
for z := 0; z < 100; z++ { for z := 0; z < 100; z++ {
a,b,c,d, e,f,g,h,i = b,c,d,a, i,e,f,g,h; a, b, c, d, e, f, g, h, i = b, c, d, a, i, e, f, g, h
if !testit(z%20 != 19) { if !testit(z%20 != 19) {
print("on ", z, "th iteration\n"); print("on ", z, "th iteration\n")
printit(); printit()
panic(); panic("fail")
} }
} }
if !testit(false) { if !testit(false) {
print("final val\n"); print("final val\n")
printit(); printit()
panic(); panic("fail")
} }
a, b = swap(1, 2); a, b = swap(1, 2)
if a != 2 || b != 1 { if a != 2 || b != 1 {
panic("bad swap"); panic("bad swap")
} }
a, b = swap(swap(a, b)); a, b = swap(swap(a, b))
if a != 2 || b != 1 { if a != 2 || b != 1 {
panic("bad swap"); panic("bad swap")
} }
} }

View File

@ -9,76 +9,75 @@ package main
import "fmt" import "fmt"
const ( const (
a = iota; a = iota
b; b
c; c
d; d
e; e
) )
var x = []int{1, 2, 3} var x = []int{1, 2, 3}
func f(x int, len *byte) { func f(x int, len *byte) {
*len = byte(x); *len = byte(x)
} }
func whatis(x interface{}) string { func whatis(x interface{}) string {
switch xx := x.(type) { switch xx := x.(type) {
default: default:
return fmt.Sprint("default ", xx); return fmt.Sprint("default ", xx)
case int, int8, int16, int32: case int, int8, int16, int32:
return fmt.Sprint("signed ", xx); return fmt.Sprint("signed ", xx)
case int64: case int64:
return fmt.Sprint("signed64 ", int64(xx)); return fmt.Sprint("signed64 ", int64(xx))
case uint, uint8, uint16, uint32: case uint, uint8, uint16, uint32:
return fmt.Sprint("unsigned ", xx); return fmt.Sprint("unsigned ", xx)
case uint64: case uint64:
return fmt.Sprint("unsigned64 ", uint64(xx)); return fmt.Sprint("unsigned64 ", uint64(xx))
case nil: case nil:
return fmt.Sprint("nil ", xx); return fmt.Sprint("nil ", xx)
} }
panic("not reached"); panic("not reached")
} }
func whatis1(x interface{}) string { func whatis1(x interface{}) string {
xx := x; xx := x
switch xx.(type) { switch xx.(type) {
default: default:
return fmt.Sprint("default ", xx); return fmt.Sprint("default ", xx)
case int, int8, int16, int32: case int, int8, int16, int32:
return fmt.Sprint("signed ", xx); return fmt.Sprint("signed ", xx)
case int64: case int64:
return fmt.Sprint("signed64 ", xx.(int64)); return fmt.Sprint("signed64 ", xx.(int64))
case uint, uint8, uint16, uint32: case uint, uint8, uint16, uint32:
return fmt.Sprint("unsigned ", xx); return fmt.Sprint("unsigned ", xx)
case uint64: case uint64:
return fmt.Sprint("unsigned64 ", xx.(uint64)); return fmt.Sprint("unsigned64 ", xx.(uint64))
case nil: case nil:
return fmt.Sprint("nil ", xx); return fmt.Sprint("nil ", xx)
} }
panic("not reached"); panic("not reached")
} }
func check(x interface{}, s string) { func check(x interface{}, s string) {
w := whatis(x); w := whatis(x)
if w != s { if w != s {
fmt.Println("whatis", x, "=>", w, "!=", s); fmt.Println("whatis", x, "=>", w, "!=", s)
panic(); panic("fail")
} }
w = whatis1(x); w = whatis1(x)
if w != s { if w != s {
fmt.Println("whatis1", x, "=>", w, "!=", s); fmt.Println("whatis1", x, "=>", w, "!=", s)
panic(); panic("fail")
} }
} }
func main() { func main() {
check(1, "signed 1"); check(1, "signed 1")
check(uint(1), "unsigned 1"); check(uint(1), "unsigned 1")
check(int64(1), "signed64 1"); check(int64(1), "signed64 1")
check(uint64(1), "unsigned64 1"); check(uint64(1), "unsigned64 1")
check(1.5, "default 1.5"); check(1.5, "default 1.5")
check(nil, "nil <nil>"); check(nil, "nil <nil>")
} }

View File

@ -7,14 +7,23 @@
package main package main
func main() { func main() {
var x int = 1; var x int = 1
if x != 1 { panic("found ", x, ", expected 1\n"); } if x != 1 {
{ print("found ", x, ", expected 1\n")
var x int = x + 1; panic("fail")
if x != 2 { panic("found ", x, ", expected 2\n"); }
} }
{ {
x := x + 1; var x int = x + 1
if x != 2 { panic("found ", x, ", expected 2\n"); } if x != 2 {
print("found ", x, ", expected 2\n")
panic("fail")
}
}
{
x := x + 1
if x != 2 {
print("found ", x, ", expected 2\n")
panic("fail")
}
} }
} }

View File

@ -10,42 +10,45 @@ import "container/vector"
type S struct { type S struct {
val int; val int
} }
func (p *S) Init(val int) *S { func (p *S) Init(val int) *S {
p.val = val; p.val = val
return p; return p
} }
func test0() { func test0() {
v := new(vector.Vector); v := new(vector.Vector)
if v.Len() != 0 { if v.Len() != 0 {
panic("len = ", v.Len(), "\n") print("len = ", v.Len(), "\n")
panic("fail")
} }
} }
func test1() { func test1() {
var a [1000]*S; var a [1000]*S
for i := 0; i < len(a); i++ { for i := 0; i < len(a); i++ {
a[i] = new(S).Init(i) a[i] = new(S).Init(i)
} }
v := new(vector.Vector); v := new(vector.Vector)
for i := 0; i < len(a); i++ { for i := 0; i < len(a); i++ {
v.Insert(0, a[i]); v.Insert(0, a[i])
if v.Len() != i+1 { if v.Len() != i+1 {
panic("len = ", v.Len(), "\n") print("len = ", v.Len(), "\n")
panic("fail")
} }
} }
for i := 0; i < v.Len(); i++ { for i := 0; i < v.Len(); i++ {
x := v.At(i).(*S); x := v.At(i).(*S)
if x.val != v.Len()-i-1 { if x.val != v.Len()-i-1 {
panic("expected ", i, ", found ", x.val, "\n") print("expected ", i, ", found ", x.val, "\n")
panic("fail")
} }
} }
@ -56,6 +59,6 @@ func test1() {
func main() { func main() {
test0(); test0()
test1(); test1()
} }