Commit 4f61fc96 authored by Rob Pike's avatar Rob Pike

test: remove semiocolons.

The ken directory is untouched so we have some examples with explicit semis.

R=gri
CC=golang-dev
https://golang.org/cl/2157041
parent cd8f4cd2
...@@ -15,9 +15,9 @@ ...@@ -15,9 +15,9 @@
package main package main
import ( import (
"bufio"; "bufio"
"fmt"; "fmt"
"os"; "os"
) )
var bout *bufio.Writer var bout *bufio.Writer
...@@ -27,19 +27,19 @@ var bout *bufio.Writer ...@@ -27,19 +27,19 @@ var bout *bufio.Writer
// if the compiler has buggy or missing 64-bit support. // if the compiler has buggy or missing 64-bit support.
type Uint64 struct { type Uint64 struct {
hi uint32; hi uint32
lo uint32; lo uint32
} }
type Int64 struct { type Int64 struct {
hi int32; hi int32
lo uint32; lo uint32
} }
func (a Uint64) Int64() (c Int64) { func (a Uint64) Int64() (c Int64) {
c.hi = int32(a.hi); c.hi = int32(a.hi)
c.lo = a.lo; c.lo = a.lo
return; return
} }
func (a Uint64) Cmp(b Uint64) int { func (a Uint64) Cmp(b Uint64) int {
...@@ -53,80 +53,80 @@ func (a Uint64) Cmp(b Uint64) int { ...@@ -53,80 +53,80 @@ func (a Uint64) Cmp(b Uint64) int {
case a.lo > b.lo: case a.lo > b.lo:
return 1 return 1
} }
return 0; return 0
} }
func (a Uint64) LeftShift(b uint) (c Uint64) { func (a Uint64) LeftShift(b uint) (c Uint64) {
switch { switch {
case b >= 64: case b >= 64:
c.hi = 0; c.hi = 0
c.lo = 0; c.lo = 0
case b >= 32: case b >= 32:
c.hi = a.lo << (b - 32); c.hi = a.lo << (b - 32)
c.lo = 0; c.lo = 0
default: default:
c.hi = a.hi<<b | a.lo>>(32-b); c.hi = a.hi<<b | a.lo>>(32-b)
c.lo = a.lo << b; c.lo = a.lo << b
} }
return; return
} }
func (a Uint64) RightShift(b uint) (c Uint64) { func (a Uint64) RightShift(b uint) (c Uint64) {
switch { switch {
case b >= 64: case b >= 64:
c.hi = 0; c.hi = 0
c.lo = a.hi; c.lo = a.hi
case b >= 32: case b >= 32:
c.hi = 0; c.hi = 0
c.lo = a.hi >> (b - 32); c.lo = a.hi >> (b - 32)
default: default:
c.hi = a.hi >> b; c.hi = a.hi >> b
c.lo = a.hi<<(32-b) | a.lo>>b; c.lo = a.hi<<(32-b) | a.lo>>b
} }
return; return
} }
func (a Uint64) LeftShift64(b Uint64) (c Uint64) { func (a Uint64) LeftShift64(b Uint64) (c Uint64) {
if b.hi != 0 || b.lo >= 64 { if b.hi != 0 || b.lo >= 64 {
return return
} }
return a.LeftShift(uint(b.lo)); return a.LeftShift(uint(b.lo))
} }
func (a Uint64) RightShift64(b Uint64) (c Uint64) { func (a Uint64) RightShift64(b Uint64) (c Uint64) {
if b.hi != 0 || b.lo >= 64 { if b.hi != 0 || b.lo >= 64 {
return return
} }
return a.RightShift(uint(b.lo)); return a.RightShift(uint(b.lo))
} }
func (a Uint64) Plus(b Uint64) (c Uint64) { func (a Uint64) Plus(b Uint64) (c Uint64) {
var carry uint32; var carry uint32
if c.lo = a.lo + b.lo; c.lo < a.lo { if c.lo = a.lo + b.lo; c.lo < a.lo {
carry = 1 carry = 1
} }
c.hi = a.hi + b.hi + carry; c.hi = a.hi + b.hi + carry
return; return
} }
func (a Uint64) Minus(b Uint64) (c Uint64) { func (a Uint64) Minus(b Uint64) (c Uint64) {
var borrow uint32; var borrow uint32
if c.lo = a.lo - b.lo; c.lo > a.lo { if c.lo = a.lo - b.lo; c.lo > a.lo {
borrow = 1 borrow = 1
} }
c.hi = a.hi - b.hi - borrow; c.hi = a.hi - b.hi - borrow
return; return
} }
func (a Uint64) Neg() (c Uint64) { func (a Uint64) Neg() (c Uint64) {
var zero Uint64; var zero Uint64
return zero.Minus(a); return zero.Minus(a)
} }
func (a Uint64) Com() (c Uint64) { func (a Uint64) Com() (c Uint64) {
c.hi = ^a.hi; c.hi = ^a.hi
c.lo = ^a.lo; c.lo = ^a.lo
return; return
} }
func (a Uint64) Len() int { func (a Uint64) Len() int {
...@@ -144,7 +144,7 @@ func (a Uint64) Len() int { ...@@ -144,7 +144,7 @@ func (a Uint64) Len() int {
} }
} }
} }
return 0; return 0
} }
func (a Uint64) HasBit(b uint) bool { func (a Uint64) HasBit(b uint) bool {
...@@ -154,7 +154,7 @@ func (a Uint64) HasBit(b uint) bool { ...@@ -154,7 +154,7 @@ func (a Uint64) HasBit(b uint) bool {
case b >= 32: case b >= 32:
return a.hi&(1<<(b-32)) != 0 return a.hi&(1<<(b-32)) != 0
} }
return a.lo&(1<<b) != 0; return a.lo&(1<<b) != 0
} }
func (a Uint64) Times(b Uint64) (c Uint64) { func (a Uint64) Times(b Uint64) (c Uint64) {
...@@ -163,56 +163,56 @@ func (a Uint64) Times(b Uint64) (c Uint64) { ...@@ -163,56 +163,56 @@ func (a Uint64) Times(b Uint64) (c Uint64) {
c = c.Plus(a.LeftShift(i)) c = c.Plus(a.LeftShift(i))
} }
} }
return; return
} }
func (a Uint64) DivMod(b Uint64) (quo, rem Uint64) { func (a Uint64) DivMod(b Uint64) (quo, rem Uint64) {
n := a.Len() - b.Len(); n := a.Len() - b.Len()
if n >= 0 { if n >= 0 {
b = b.LeftShift(uint(n)); b = b.LeftShift(uint(n))
for i := 0; i <= n; i++ { for i := 0; i <= n; i++ {
quo = quo.LeftShift(1); quo = quo.LeftShift(1)
if b.Cmp(a) <= 0 { // b <= a if b.Cmp(a) <= 0 { // b <= a
quo.lo |= 1; quo.lo |= 1
a = a.Minus(b); a = a.Minus(b)
} }
b = b.RightShift(1); b = b.RightShift(1)
} }
} }
rem = a; rem = a
return; return
} }
func (a Uint64) And(b Uint64) (c Uint64) { func (a Uint64) And(b Uint64) (c Uint64) {
c.hi = a.hi & b.hi; c.hi = a.hi & b.hi
c.lo = a.lo & b.lo; c.lo = a.lo & b.lo
return; return
} }
func (a Uint64) AndNot(b Uint64) (c Uint64) { func (a Uint64) AndNot(b Uint64) (c Uint64) {
c.hi = a.hi &^ b.hi; c.hi = a.hi &^ b.hi
c.lo = a.lo &^ b.lo; c.lo = a.lo &^ b.lo
return; return
} }
func (a Uint64) Or(b Uint64) (c Uint64) { func (a Uint64) Or(b Uint64) (c Uint64) {
c.hi = a.hi | b.hi; c.hi = a.hi | b.hi
c.lo = a.lo | b.lo; c.lo = a.lo | b.lo
return; return
} }
func (a Uint64) Xor(b Uint64) (c Uint64) { func (a Uint64) Xor(b Uint64) (c Uint64) {
c.hi = a.hi ^ b.hi; c.hi = a.hi ^ b.hi
c.lo = a.lo ^ b.lo; c.lo = a.lo ^ b.lo
return; return
} }
func (a Uint64) String() string { return fmt.Sprintf("%#x%08x", a.hi, a.lo) } func (a Uint64) String() string { return fmt.Sprintf("%#x%08x", a.hi, a.lo) }
func (a Int64) Uint64() (c Uint64) { func (a Int64) Uint64() (c Uint64) {
c.hi = uint32(a.hi); c.hi = uint32(a.hi)
c.lo = a.lo; c.lo = a.lo
return; return
} }
func (a Int64) Cmp(b Int64) int { func (a Int64) Cmp(b Int64) int {
...@@ -229,7 +229,7 @@ func (a Int64) Cmp(b Int64) int { ...@@ -229,7 +229,7 @@ func (a Int64) Cmp(b Int64) int {
case a.lo > b.lo: case a.lo > b.lo:
return 1 return 1
} }
return 0; return 0
} }
func (a Int64) LeftShift(b uint) (c Int64) { return a.Uint64().LeftShift(b).Int64() } func (a Int64) LeftShift(b uint) (c Int64) { return a.Uint64().LeftShift(b).Int64() }
...@@ -237,30 +237,30 @@ func (a Int64) LeftShift(b uint) (c Int64) { return a.Uint64().LeftShift(b).Int6 ...@@ -237,30 +237,30 @@ func (a Int64) LeftShift(b uint) (c Int64) { return a.Uint64().LeftShift(b).Int6
func (a Int64) RightShift(b uint) (c Int64) { func (a Int64) RightShift(b uint) (c Int64) {
switch { switch {
case b >= 64: case b >= 64:
c.hi = a.hi >> 31; // sign extend c.hi = a.hi >> 31 // sign extend
c.lo = uint32(c.hi); c.lo = uint32(c.hi)
case b >= 32: case b >= 32:
c.hi = a.hi >> 31; // sign extend c.hi = a.hi >> 31 // sign extend
c.lo = uint32(a.hi >> (b - 32)); c.lo = uint32(a.hi >> (b - 32))
default: default:
c.hi = a.hi >> b; c.hi = a.hi >> b
c.lo = uint32(a.hi<<(32-b)) | a.lo>>b; c.lo = uint32(a.hi<<(32-b)) | a.lo>>b
} }
return; return
} }
func (a Int64) LeftShift64(b Uint64) (c Int64) { func (a Int64) LeftShift64(b Uint64) (c Int64) {
if b.hi != 0 || b.lo >= 64 { if b.hi != 0 || b.lo >= 64 {
return return
} }
return a.LeftShift(uint(b.lo)); return a.LeftShift(uint(b.lo))
} }
func (a Int64) RightShift64(b Uint64) (c Int64) { func (a Int64) RightShift64(b Uint64) (c Int64) {
if b.hi != 0 || b.lo >= 64 { if b.hi != 0 || b.lo >= 64 {
return a.RightShift(64) return a.RightShift(64)
} }
return a.RightShift(uint(b.lo)); return a.RightShift(uint(b.lo))
} }
func (a Int64) Plus(b Int64) (c Int64) { return a.Uint64().Plus(b.Uint64()).Int64() } func (a Int64) Plus(b Int64) (c Int64) { return a.Uint64().Plus(b.Uint64()).Int64() }
...@@ -274,23 +274,23 @@ func (a Int64) Com() (c Int64) { return a.Uint64().Com().Int64() } ...@@ -274,23 +274,23 @@ func (a Int64) Com() (c Int64) { return a.Uint64().Com().Int64() }
func (a Int64) Times(b Int64) (c Int64) { return a.Uint64().Times(b.Uint64()).Int64() } func (a Int64) Times(b Int64) (c Int64) { return a.Uint64().Times(b.Uint64()).Int64() }
func (a Int64) DivMod(b Int64) (quo Int64, rem Int64) { func (a Int64) DivMod(b Int64) (quo Int64, rem Int64) {
var zero Int64; var zero Int64
quoSign := +1; quoSign := +1
remSign := +1; remSign := +1
if a.Cmp(zero) < 0 { if a.Cmp(zero) < 0 {
quoSign = -1; quoSign = -1
remSign = -1; remSign = -1
a = a.Neg(); a = a.Neg()
} }
if b.Cmp(zero) < 0 { if b.Cmp(zero) < 0 {
quoSign = -quoSign; quoSign = -quoSign
b = b.Neg(); b = b.Neg()
} }
q, r := a.Uint64().DivMod(b.Uint64()); q, r := a.Uint64().DivMod(b.Uint64())
quo = q.Int64(); quo = q.Int64()
rem = r.Int64(); rem = r.Int64()
if quoSign < 0 { if quoSign < 0 {
quo = quo.Neg() quo = quo.Neg()
...@@ -298,7 +298,7 @@ func (a Int64) DivMod(b Int64) (quo Int64, rem Int64) { ...@@ -298,7 +298,7 @@ func (a Int64) DivMod(b Int64) (quo Int64, rem Int64) {
if remSign < 0 { if remSign < 0 {
rem = rem.Neg() rem = rem.Neg()
} }
return; return
} }
func (a Int64) And(b Int64) (c Int64) { return a.Uint64().And(b.Uint64()).Int64() } func (a Int64) And(b Int64) (c Int64) { return a.Uint64().And(b.Uint64()).Int64() }
...@@ -313,7 +313,7 @@ func (a Int64) String() string { ...@@ -313,7 +313,7 @@ func (a Int64) String() string {
if a.hi < 0 { if a.hi < 0 {
return fmt.Sprintf("-%s", a.Neg().Uint64()) return fmt.Sprintf("-%s", a.Neg().Uint64())
} }
return a.Uint64().String(); return a.Uint64().String()
} }
var int64Values = []Int64{ var int64Values = []Int64{
...@@ -507,56 +507,56 @@ const prolog = "\n" + ...@@ -507,56 +507,56 @@ const prolog = "\n" +
"\n" "\n"
func varTests() { func varTests() {
fmt.Fprint(bout, prolog); fmt.Fprint(bout, prolog)
for _, a := range int64Values { for _, a := range int64Values {
fmt.Fprintf(bout, "func test%v() {\n", ntest); fmt.Fprintf(bout, "func test%v() {\n", ntest)
ntest++; ntest++
fmt.Fprintf(bout, "\ttestInt64Unary(%v, %v, %v, %v);\n", a, a, a.Com(), a.Neg()); fmt.Fprintf(bout, "\ttestInt64Unary(%v, %v, %v, %v);\n", a, a, a.Com(), a.Neg())
for _, b := range int64Values { for _, b := range int64Values {
var div, mod Int64; var div, mod Int64
dodiv := false; dodiv := false
var zero Int64; var zero Int64
if b.Cmp(zero) != 0 { // b != 0 if b.Cmp(zero) != 0 { // b != 0
// Can't divide by zero but also can't divide -0x8000...000 by -1. // Can't divide by zero but also can't divide -0x8000...000 by -1.
var bigneg = Int64{-0x80000000, 0}; var bigneg = Int64{-0x80000000, 0}
var minus1 = Int64{-1, ^uint32(0)}; var minus1 = Int64{-1, ^uint32(0)}
if a.Cmp(bigneg) != 0 || b.Cmp(minus1) != 0 { // a != -1<<63 || b != -1 if a.Cmp(bigneg) != 0 || b.Cmp(minus1) != 0 { // a != -1<<63 || b != -1
div, mod = a.DivMod(b); div, mod = a.DivMod(b)
dodiv = true; dodiv = true
} }
} }
fmt.Fprintf(bout, "\ttestInt64Binary(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n", fmt.Fprintf(bout, "\ttestInt64Binary(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n",
a, b, a.Plus(b), a.Minus(b), a.Times(b), div, mod, a, b, a.Plus(b), a.Minus(b), a.Times(b), div, mod,
a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv); a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv)
} }
for _, b := range shiftValues { for _, b := range shiftValues {
fmt.Fprintf(bout, "\ttestInt64Shift(%v, %v, %v, %v);\n", fmt.Fprintf(bout, "\ttestInt64Shift(%v, %v, %v, %v);\n",
a, b, a.LeftShift64(b), a.RightShift64(b)) a, b, a.LeftShift64(b), a.RightShift64(b))
} }
fmt.Fprintf(bout, "}\n"); fmt.Fprintf(bout, "}\n")
} }
for _, a := range uint64Values { for _, a := range uint64Values {
fmt.Fprintf(bout, "func test%v() {\n", ntest); fmt.Fprintf(bout, "func test%v() {\n", ntest)
ntest++; ntest++
fmt.Fprintf(bout, "\ttestUint64Unary(%v, %v, %v, %v);\n", a, a, a.Com(), a.Neg()); fmt.Fprintf(bout, "\ttestUint64Unary(%v, %v, %v, %v);\n", a, a, a.Com(), a.Neg())
for _, b := range uint64Values { for _, b := range uint64Values {
var div, mod Uint64; var div, mod Uint64
dodiv := false; dodiv := false
var zero Uint64; var zero Uint64
if b.Cmp(zero) != 0 { // b != 0 if b.Cmp(zero) != 0 { // b != 0
div, mod = a.DivMod(b); div, mod = a.DivMod(b)
dodiv = true; dodiv = true
} }
fmt.Fprintf(bout, "\ttestUint64Binary(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n", fmt.Fprintf(bout, "\ttestUint64Binary(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n",
a, b, a.Plus(b), a.Minus(b), a.Times(b), div, mod, a, b, a.Plus(b), a.Minus(b), a.Times(b), div, mod,
a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv); a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv)
} }
for _, b := range shiftValues { for _, b := range shiftValues {
fmt.Fprintf(bout, "\ttestUint64Shift(%v, %v, %v, %v);\n", fmt.Fprintf(bout, "\ttestUint64Shift(%v, %v, %v, %v);\n",
a, b, a.LeftShift64(b), a.RightShift64(b)) a, b, a.LeftShift64(b), a.RightShift64(b))
} }
fmt.Fprintf(bout, "}\n"); fmt.Fprintf(bout, "}\n")
} }
} }
...@@ -622,88 +622,88 @@ const shiftConstR = "func test%vShiftR%v(a, left, right %v) {\n" + ...@@ -622,88 +622,88 @@ const shiftConstR = "func test%vShiftR%v(a, left, right %v) {\n" +
func constTests() { func constTests() {
for i, a := range int64Values { for i, a := range int64Values {
fmt.Fprintf(bout, binaryConstL, "Int64", i, "int64", "int64", a, "int64"); fmt.Fprintf(bout, binaryConstL, "Int64", i, "int64", "int64", a, "int64")
fmt.Fprintf(bout, binaryConstR, "Int64", i, "int64", "int64", a, "int64"); fmt.Fprintf(bout, binaryConstR, "Int64", i, "int64", "int64", a, "int64")
fmt.Fprintf(bout, shiftConstL, "Int64", i, "int64", "int64", a, "int64"); fmt.Fprintf(bout, shiftConstL, "Int64", i, "int64", "int64", a, "int64")
} }
for i, a := range uint64Values { for i, a := range uint64Values {
fmt.Fprintf(bout, binaryConstL, "Uint64", i, "uint64", "uint64", a, "uint64"); fmt.Fprintf(bout, binaryConstL, "Uint64", i, "uint64", "uint64", a, "uint64")
fmt.Fprintf(bout, binaryConstR, "Uint64", i, "uint64", "uint64", a, "uint64"); fmt.Fprintf(bout, binaryConstR, "Uint64", i, "uint64", "uint64", a, "uint64")
fmt.Fprintf(bout, shiftConstL, "Uint64", i, "uint64", "uint64", a, "uint64"); fmt.Fprintf(bout, shiftConstL, "Uint64", i, "uint64", "uint64", a, "uint64")
} }
for i, a := range shiftValues { for i, a := range shiftValues {
fmt.Fprintf(bout, shiftConstR, "Int64", i, "int64", a, "int64"); fmt.Fprintf(bout, shiftConstR, "Int64", i, "int64", a, "int64")
fmt.Fprintf(bout, shiftConstR, "Uint64", i, "uint64", a, "uint64"); fmt.Fprintf(bout, shiftConstR, "Uint64", i, "uint64", a, "uint64")
} }
for i, a := range int64Values { for i, a := range int64Values {
fmt.Fprintf(bout, "func test%v() {\n", ntest); fmt.Fprintf(bout, "func test%v() {\n", ntest)
ntest++; ntest++
for j, b := range int64Values { for j, b := range int64Values {
var div, mod Int64; var div, mod Int64
dodiv := false; dodiv := false
var zero Int64; var zero Int64
if b.Cmp(zero) != 0 { // b != 0 if b.Cmp(zero) != 0 { // b != 0
// Can't divide by zero but also can't divide -0x8000...000 by -1. // Can't divide by zero but also can't divide -0x8000...000 by -1.
var bigneg = Int64{-0x80000000, 0}; var bigneg = Int64{-0x80000000, 0}
var minus1 = Int64{-1, ^uint32(0)}; var minus1 = Int64{-1, ^uint32(0)}
if a.Cmp(bigneg) != 0 || b.Cmp(minus1) != 0 { // a != -1<<63 || b != -1 if a.Cmp(bigneg) != 0 || b.Cmp(minus1) != 0 { // a != -1<<63 || b != -1
div, mod = a.DivMod(b); div, mod = a.DivMod(b)
dodiv = true; dodiv = true
} }
} }
fmt.Fprintf(bout, "\ttestInt64BinaryL%v(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n", fmt.Fprintf(bout, "\ttestInt64BinaryL%v(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n",
i, b, a.Plus(b), a.Minus(b), a.Times(b), div, mod, i, b, a.Plus(b), a.Minus(b), a.Times(b), div, mod,
a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv); a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv)
fmt.Fprintf(bout, "\ttestInt64BinaryR%v(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n", fmt.Fprintf(bout, "\ttestInt64BinaryR%v(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n",
j, a, a.Plus(b), a.Minus(b), a.Times(b), div, mod, j, a, a.Plus(b), a.Minus(b), a.Times(b), div, mod,
a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv); a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv)
} }
for j, b := range shiftValues { for j, b := range shiftValues {
fmt.Fprintf(bout, "\ttestInt64ShiftL%v(%v, %v, %v);\n", fmt.Fprintf(bout, "\ttestInt64ShiftL%v(%v, %v, %v);\n",
i, b, a.LeftShift64(b), a.RightShift64(b)); i, b, a.LeftShift64(b), a.RightShift64(b))
fmt.Fprintf(bout, "\ttestInt64ShiftR%v(%v, %v, %v);\n", fmt.Fprintf(bout, "\ttestInt64ShiftR%v(%v, %v, %v);\n",
j, a, a.LeftShift64(b), a.RightShift64(b)); j, a, a.LeftShift64(b), a.RightShift64(b))
} }
fmt.Fprintf(bout, "}\n"); fmt.Fprintf(bout, "}\n")
} }
for i, a := range uint64Values { for i, a := range uint64Values {
fmt.Fprintf(bout, "func test%v() {\n", ntest); fmt.Fprintf(bout, "func test%v() {\n", ntest)
ntest++; ntest++
for j, b := range uint64Values { for j, b := range uint64Values {
var div, mod Uint64; var div, mod Uint64
dodiv := false; dodiv := false
var zero Uint64; var zero Uint64
if b.Cmp(zero) != 0 { // b != 0 if b.Cmp(zero) != 0 { // b != 0
div, mod = a.DivMod(b); div, mod = a.DivMod(b)
dodiv = true; dodiv = true
} }
fmt.Fprintf(bout, "\ttestUint64BinaryL%v(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n", fmt.Fprintf(bout, "\ttestUint64BinaryL%v(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n",
i, b, a.Plus(b), a.Minus(b), a.Times(b), div, mod, i, b, a.Plus(b), a.Minus(b), a.Times(b), div, mod,
a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv); a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv)
fmt.Fprintf(bout, "\ttestUint64BinaryR%v(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n", fmt.Fprintf(bout, "\ttestUint64BinaryR%v(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n",
j, a, a.Plus(b), a.Minus(b), a.Times(b), div, mod, j, a, a.Plus(b), a.Minus(b), a.Times(b), div, mod,
a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv); a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv)
} }
for j, b := range shiftValues { for j, b := range shiftValues {
fmt.Fprintf(bout, "\ttestUint64ShiftL%v(%v, %v, %v);\n", fmt.Fprintf(bout, "\ttestUint64ShiftL%v(%v, %v, %v);\n",
i, b, a.LeftShift64(b), a.RightShift64(b)); i, b, a.LeftShift64(b), a.RightShift64(b))
fmt.Fprintf(bout, "\ttestUint64ShiftR%v(%v, %v, %v);\n", fmt.Fprintf(bout, "\ttestUint64ShiftR%v(%v, %v, %v);\n",
j, a, a.LeftShift64(b), a.RightShift64(b)); j, a, a.LeftShift64(b), a.RightShift64(b))
} }
fmt.Fprintf(bout, "}\n"); fmt.Fprintf(bout, "}\n")
} }
} }
func main() { func main() {
bout = bufio.NewWriter(os.Stdout); bout = bufio.NewWriter(os.Stdout)
varTests(); varTests()
constTests(); constTests()
fmt.Fprintf(bout, "func main() {\n"); fmt.Fprintf(bout, "func main() {\n")
for i := 0; i < ntest; i++ { for i := 0; i < ntest; i++ {
fmt.Fprintf(bout, "\ttest%v();\n", i) fmt.Fprintf(bout, "\ttest%v();\n", i)
} }
fmt.Fprintf(bout, "\tif !ok { os.Exit(1) }\n"); fmt.Fprintf(bout, "\tif !ok { os.Exit(1) }\n")
fmt.Fprintf(bout, "}\n"); fmt.Fprintf(bout, "}\n")
bout.Flush(); bout.Flush()
} }
...@@ -9,45 +9,45 @@ package main ...@@ -9,45 +9,45 @@ package main
import "sync" import "sync"
type T struct { type T struct {
int; int
sync.Mutex; sync.Mutex
} }
func main() { func main() {
{ {
var x, y sync.Mutex; var x, y sync.Mutex
x = y; // ERROR "assignment.*Mutex" x = y // ERROR "assignment.*Mutex"
_ = x; _ = x
} }
{ {
var x, y T; var x, y T
x = y; // ERROR "assignment.*Mutex" x = y // ERROR "assignment.*Mutex"
_ = x; _ = x
} }
{ {
var x, y [2]sync.Mutex; var x, y [2]sync.Mutex
x = y; // ERROR "assignment.*Mutex" x = y // ERROR "assignment.*Mutex"
_ = x; _ = x
} }
{ {
var x, y [2]T; var x, y [2]T
x = y; // ERROR "assignment.*Mutex" x = y // ERROR "assignment.*Mutex"
_ = x; _ = x
} }
{ {
x := sync.Mutex{0, 0}; // ERROR "assignment.*Mutex" x := sync.Mutex{0, 0} // ERROR "assignment.*Mutex"
_ = x; _ = x
} }
{ {
x := sync.Mutex{key: 0}; // ERROR "(unknown|assignment).*Mutex" x := sync.Mutex{key: 0} // ERROR "(unknown|assignment).*Mutex"
_ = x; _ = x
} }
{ {
x := &sync.Mutex{}; // ok x := &sync.Mutex{} // ok
var y sync.Mutex; // ok var y sync.Mutex // ok
y = *x; // ERROR "assignment.*Mutex" y = *x // ERROR "assignment.*Mutex"
*x = y; // ERROR "assignment.*Mutex" *x = y // ERROR "assignment.*Mutex"
_ = x; _ = x
_ = y; _ = y
} }
} }
...@@ -7,35 +7,35 @@ ...@@ -7,35 +7,35 @@
package main package main
type T struct { type T struct {
a float64; a float64
b int64; b int64
c string; c string
d byte; d byte
} }
var a = []int{ 1, 2, 3 } var a = []int{ 1, 2, 3 }
var NIL []int; var NIL []int
func arraycmptest() { func arraycmptest() {
if NIL != nil { if NIL != nil {
println("fail1:", NIL, "!= nil"); println("fail1:", NIL, "!= nil")
} }
if nil != NIL { if nil != NIL {
println("fail2: nil !=", NIL); println("fail2: nil !=", NIL)
} }
if a == nil || nil == a { if a == nil || nil == a {
println("fail3:", a, "== nil"); println("fail3:", a, "== nil")
} }
} }
func SameArray(a, b []int) bool { func SameArray(a, b []int) bool {
if len(a) != len(b) || cap(a) != cap(b) { if len(a) != len(b) || cap(a) != cap(b) {
return false; return false
} }
if len(a) > 0 && &a[0] != &b[0] { if len(a) > 0 && &a[0] != &b[0] {
return false; return false
} }
return true; return true
} }
var t = T{1.5, 123, "hello", 255} var t = T{1.5, 123, "hello", 255}
...@@ -43,16 +43,16 @@ var mt = make(map[int]T) ...@@ -43,16 +43,16 @@ var mt = make(map[int]T)
var ma = make(map[int][]int) var ma = make(map[int][]int)
func maptest() { func maptest() {
mt[0] = t; mt[0] = t
t1 := mt[0]; t1 := mt[0]
if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d { if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d {
println("fail: map val struct", t1.a, t1.b, t1.c, t1.d); println("fail: map val struct", t1.a, t1.b, t1.c, t1.d)
} }
ma[1] = a; ma[1] = a
a1 := ma[1]; a1 := ma[1]
if !SameArray(a, a1) { if !SameArray(a, a1) {
println("fail: map val array", a, a1); println("fail: map val array", a, a1)
} }
} }
...@@ -60,21 +60,21 @@ var ct = make(chan T) ...@@ -60,21 +60,21 @@ var ct = make(chan T)
var ca = make(chan []int) var ca = make(chan []int)
func send() { func send() {
ct <- t; ct <- t
ca <- a; ca <- a
} }
func chantest() { func chantest() {
go send(); go send()
t1 := <-ct; t1 := <-ct
if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d { if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d {
println("fail: map val struct", t1.a, t1.b, t1.c, t1.d); println("fail: map val struct", t1.a, t1.b, t1.c, t1.d)
} }
a1 := <-ca; a1 := <-ca
if !SameArray(a, a1) { if !SameArray(a, a1) {
println("fail: map val array", a, a1); println("fail: map val array", a, a1)
} }
} }
...@@ -82,36 +82,36 @@ type E struct { } ...@@ -82,36 +82,36 @@ type E struct { }
var e E var e E
func interfacetest() { func interfacetest() {
var i interface{}; var i interface{}
i = a; i = a
a1 := i.([]int); a1 := i.([]int)
if !SameArray(a, a1) { if !SameArray(a, a1) {
println("interface <-> []int", a, a1); println("interface <-> []int", a, a1)
} }
pa := new([]int); pa := new([]int)
*pa = a; *pa = a
i = pa; i = pa
a1 = *i.(*[]int); a1 = *i.(*[]int)
if !SameArray(a, a1) { if !SameArray(a, a1) {
println("interface <-> *[]int", a, a1); println("interface <-> *[]int", a, a1)
} }
i = t; i = t
t1 := i.(T); t1 := i.(T)
if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d { if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d {
println("interface <-> struct", t1.a, t1.b, t1.c, t1.d); println("interface <-> struct", t1.a, t1.b, t1.c, t1.d)
} }
i = e; i = e
e1 := i.(E); e1 := i.(E)
// nothing to check; just verify it doesn't crash // nothing to check; just verify it doesn't crash
_ = e1; _ = e1
} }
func main() { func main() {
arraycmptest(); arraycmptest()
maptest(); maptest()
chantest(); chantest()
interfacetest(); interfacetest()
} }
...@@ -11,7 +11,7 @@ import _ "fmt" ...@@ -11,7 +11,7 @@ import _ "fmt"
var call string var call string
type T struct { type T struct {
_, _, _ int; _, _, _ int
} }
func (T) _() { func (T) _() {
...@@ -21,11 +21,11 @@ func (T) _() { ...@@ -21,11 +21,11 @@ func (T) _() {
} }
const ( const (
c0 = iota; c0 = iota
_; _
_; _
_; _
c4; c4
) )
var ints = []string { var ints = []string {
...@@ -35,12 +35,12 @@ var ints = []string { ...@@ -35,12 +35,12 @@ var ints = []string {
} }
func f() (int, int) { func f() (int, int) {
call += "f"; call += "f"
return 1,2 return 1,2
} }
func g() (float, float) { func g() (float, float) {
call += "g"; call += "g"
return 3,4 return 3,4
} }
...@@ -48,54 +48,54 @@ func h(_ int, _ float) { ...@@ -48,54 +48,54 @@ func h(_ int, _ float) {
} }
func i() int { func i() int {
call += "i"; call += "i"
return 23; return 23
} }
var _ = i(); var _ = i()
func main() { func main() {
if call != "i" {panic("init did not run")} if call != "i" {panic("init did not run")}
call = ""; call = ""
_, _ = f(); _, _ = f()
a, _ := f(); a, _ := f()
if a != 1 {panic(a)} if a != 1 {panic(a)}
b, _ := g(); b, _ := g()
if b != 3 {panic(b)} if b != 3 {panic(b)}
_, a = f(); _, a = f()
if a != 2 {panic(a)} if a != 2 {panic(a)}
_, b = g(); _, b = g()
if b != 4 {panic(b)} if b != 4 {panic(b)}
_ = i(); _ = i()
if call != "ffgfgi" {panic(call)} if call != "ffgfgi" {panic(call)}
if c4 != 4 {panic(c4)} if c4 != 4 {panic(c4)}
out := ""; out := ""
for _, s := range ints { for _, s := range ints {
out += s; out += s
} }
if out != "123" {panic(out)} if out != "123" {panic(out)}
sum := 0; sum := 0
for s, _ := range ints { for s, _ := range ints {
sum += s; sum += s
} }
if sum != 3 {panic(sum)} if sum != 3 {panic(sum)}
h(a,b); h(a,b)
} }
// useless but legal // useless but legal
var _ int = 1; var _ int = 1
var _ = 2; var _ = 2
var _, _ = 3, 4; var _, _ = 3, 4
const _ = 3; const _ = 3
const _, _ = 4, 5; const _, _ = 4, 5
type _ int; type _ int
func _() { func _() {
panic("oops") panic("oops")
} }
func ff() { func ff() {
var _ int = 1; var _ int = 1
} }
...@@ -7,6 +7,6 @@ ...@@ -7,6 +7,6 @@
package _ // ERROR "invalid package name _" package _ // ERROR "invalid package name _"
func main() { func main() {
_(); // ERROR "cannot use _ as value" _() // ERROR "cannot use _ as value"
x := _+1; // ERROR "cannot use _ as value" x := _+1 // ERROR "cannot use _ as value"
} }
...@@ -13,20 +13,20 @@ import "os" ...@@ -13,20 +13,20 @@ import "os"
const N = 10 const N = 10
func AsynchFifo() { func AsynchFifo() {
ch := make(chan int, N); ch := make(chan int, N)
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
ch <- i ch <- i
} }
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
if <-ch != i { if <-ch != i {
print("bad receive\n"); print("bad receive\n")
os.Exit(1); os.Exit(1)
} }
} }
} }
func Chain(ch <-chan int, val int, in <-chan int, out chan<- int) { func Chain(ch <-chan int, val int, in <-chan int, out chan<- int) {
<-in; <-in
if <-ch != val { if <-ch != val {
panic(val) panic(val)
} }
...@@ -35,15 +35,15 @@ func Chain(ch <-chan int, val int, in <-chan int, out chan<- int) { ...@@ -35,15 +35,15 @@ func Chain(ch <-chan int, val int, in <-chan int, out chan<- int) {
// thread together a daisy chain to read the elements in sequence // thread together a daisy chain to read the elements in sequence
func SynchFifo() { func SynchFifo() {
ch := make(chan int); ch := make(chan int)
in := make(chan int); in := make(chan int)
start := in; start := in
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
out := make(chan int); out := make(chan int)
go Chain(ch, i, in, out); go Chain(ch, i, in, out)
in = out; in = out
} }
start <- 0; start <- 0
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
ch <- i ch <- i
} }
...@@ -51,7 +51,7 @@ func SynchFifo() { ...@@ -51,7 +51,7 @@ func SynchFifo() {
} }
func main() { func main() {
AsynchFifo(); AsynchFifo()
SynchFifo(); SynchFifo()
} }
...@@ -10,32 +10,32 @@ ...@@ -10,32 +10,32 @@
package main package main
import ( import (
"os"; "os"
"strconv"; "strconv"
) )
func f(left, right chan int) { func f(left, right chan int) {
left <- <-right; left <- <-right
} }
func main() { func main() {
var n = 10000; var n = 10000
if len(os.Args) > 1 { if len(os.Args) > 1 {
var err os.Error; var err os.Error
n, err = strconv.Atoi(os.Args[1]); n, err = strconv.Atoi(os.Args[1])
if err != nil { if err != nil {
print("bad arg\n"); print("bad arg\n")
os.Exit(1); os.Exit(1)
} }
} }
leftmost := make(chan int); leftmost := make(chan int)
right := leftmost; right := leftmost
left := leftmost; left := leftmost
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
right = make(chan int); right = make(chan int)
go f(left, right); go f(left, right)
left = right; left = right
} }
go func(c chan int) { c <- 1 }(right); go func(c chan int) { c <- 1 }(right)
<-leftmost; <-leftmost
} }
...@@ -7,51 +7,51 @@ ...@@ -7,51 +7,51 @@
package main package main
var ( var (
cr <-chan int; cr <-chan int
cs chan<- int; cs chan<- int
c chan int; c chan int
) )
func main() { func main() {
cr = c; // ok cr = c // ok
cs = c; // ok cs = c // ok
c = cr; // ERROR "illegal types|incompatible|cannot" c = cr // ERROR "illegal types|incompatible|cannot"
c = cs; // ERROR "illegal types|incompatible|cannot" c = cs // ERROR "illegal types|incompatible|cannot"
cr = cs; // ERROR "illegal types|incompatible|cannot" cr = cs // ERROR "illegal types|incompatible|cannot"
cs = cr; // ERROR "illegal types|incompatible|cannot" cs = cr // ERROR "illegal types|incompatible|cannot"
c <- 0; // ok c <- 0 // ok
ok := c <- 0; // ok ok := c <- 0 // ok
_ = ok; _ = ok
<-c; // ok <-c // ok
x, ok := <-c; // ok x, ok := <-c // ok
_, _ = x, ok; _, _ = x, ok
cr <- 0; // ERROR "send" cr <- 0 // ERROR "send"
ok = cr <- 0; // ERROR "send" ok = cr <- 0 // ERROR "send"
_ = ok; _ = ok
<-cr; // ok <-cr // ok
x, ok = <-cr; // ok x, ok = <-cr // ok
_, _ = x, ok; _, _ = x, ok
cs <- 0; // ok cs <- 0 // ok
ok = cs <- 0; // ok ok = cs <- 0 // ok
_ = ok; _ = ok
<-cs; // ERROR "receive" <-cs // ERROR "receive"
x, ok = <-cs; // ERROR "receive" x, ok = <-cs // ERROR "receive"
_, _ = x, ok; _, _ = x, ok
select { select {
case c <- 0: // ok case c <- 0: // ok
case x := <-c: // ok case x := <-c: // ok
_ = x; _ = x
case cr <- 0: // ERROR "send" case cr <- 0: // ERROR "send"
case x := <-cr: // ok case x := <-cr: // ok
_ = x; _ = x
case cs <- 0: // ok; case cs <- 0: // ok
case x := <-cs: // ERROR "receive" case x := <-cs: // ERROR "receive"
_ = x; _ = x
} }
} }
...@@ -16,7 +16,7 @@ package main ...@@ -16,7 +16,7 @@ package main
import "os" import "os"
type rat struct { type rat struct {
num, den int64; // numerator, denominator num, den int64 // numerator, denominator
} }
func (u rat) pr() { func (u rat) pr() {
...@@ -33,9 +33,9 @@ func (u rat) eq(c rat) bool { ...@@ -33,9 +33,9 @@ func (u rat) eq(c rat) bool {
} }
type dch struct { type dch struct {
req chan int; req chan int
dat chan rat; dat chan rat
nam int; nam int
} }
type dch2 [2] *dch type dch2 [2] *dch
...@@ -45,20 +45,20 @@ var chnameserial int ...@@ -45,20 +45,20 @@ var chnameserial int
var seqno int var seqno int
func mkdch() *dch { func mkdch() *dch {
c := chnameserial % len(chnames); c := chnameserial % len(chnames)
chnameserial++; chnameserial++
d := new(dch); d := new(dch)
d.req = make(chan int); d.req = make(chan int)
d.dat = make(chan rat); d.dat = make(chan rat)
d.nam = c; d.nam = c
return d; return d
} }
func mkdch2() *dch2 { func mkdch2() *dch2 {
d2 := new(dch2); d2 := new(dch2)
d2[0] = mkdch(); d2[0] = mkdch()
d2[1] = mkdch(); d2[1] = mkdch()
return d2; return d2
} }
// split reads a single demand channel and replicates its // split reads a single demand channel and replicates its
...@@ -76,98 +76,97 @@ func mkdch2() *dch2 { ...@@ -76,98 +76,97 @@ func mkdch2() *dch2 {
// generation to begin servicing out[1]. // generation to begin servicing out[1].
func dosplit(in *dch, out *dch2, wait chan int ) { func dosplit(in *dch, out *dch2, wait chan int ) {
var t *dch; both := false // do not service both channels
both := false; // do not service both channels
select { select {
case <-out[0].req: case <-out[0].req:
;
case <-wait: case <-wait:
both = true; both = true
select { select {
case <-out[0].req: case <-out[0].req:
;
case <-out[1].req: case <-out[1].req:
t=out[0]; out[0]=out[1]; out[1]=t; out[0], out[1] = out[1], out[0]
} }
} }
seqno++; seqno++
in.req <- seqno; in.req <- seqno
release := make(chan int); release := make(chan int)
go dosplit(in, out, release); go dosplit(in, out, release)
dat := <-in.dat; dat := <-in.dat
out[0].dat <- dat; out[0].dat <- dat
if !both { if !both {
<-wait <-wait
} }
<-out[1].req; <-out[1].req
out[1].dat <- dat; out[1].dat <- dat
release <- 0; release <- 0
} }
func split(in *dch, out *dch2) { func split(in *dch, out *dch2) {
release := make(chan int); release := make(chan int)
go dosplit(in, out, release); go dosplit(in, out, release)
release <- 0; release <- 0
} }
func put(dat rat, out *dch) { func put(dat rat, out *dch) {
<-out.req; <-out.req
out.dat <- dat; out.dat <- dat
} }
func get(in *dch) rat { func get(in *dch) rat {
seqno++; seqno++
in.req <- seqno; in.req <- seqno
return <-in.dat; return <-in.dat
} }
// Get one rat from each of n demand channels // Get one rat from each of n demand channels
func getn(in []*dch) []rat { func getn(in []*dch) []rat {
n := len(in); n := len(in)
if n != 2 { panic("bad n in getn") }; if n != 2 { panic("bad n in getn") }
req := new([2] chan int); req := new([2] chan int)
dat := new([2] chan rat); dat := new([2] chan rat)
out := make([]rat, 2); out := make([]rat, 2)
var i int; var i int
var it rat; var it rat
for i=0; i<n; i++ { for i=0; i<n; i++ {
req[i] = in[i].req; req[i] = in[i].req
dat[i] = nil; dat[i] = nil
} }
for n=2*n; n>0; n-- { for n=2*n; n>0; n-- {
seqno++; seqno++
select { select {
case req[0] <- seqno: case req[0] <- seqno:
dat[0] = in[0].dat; dat[0] = in[0].dat
req[0] = nil; req[0] = nil
case req[1] <- seqno: case req[1] <- seqno:
dat[1] = in[1].dat; dat[1] = in[1].dat
req[1] = nil; req[1] = nil
case it = <-dat[0]: case it = <-dat[0]:
out[0] = it; out[0] = it
dat[0] = nil; dat[0] = nil
case it = <-dat[1]: case it = <-dat[1]:
out[1] = it; out[1] = it
dat[1] = nil; dat[1] = nil
} }
} }
return out; return out
} }
// Get one rat from each of 2 demand channels // Get one rat from each of 2 demand channels
func get2(in0 *dch, in1 *dch) []rat { func get2(in0 *dch, in1 *dch) []rat {
return getn([]*dch{in0, in1}); return getn([]*dch{in0, in1})
} }
func copy(in *dch, out *dch) { func copy(in *dch, out *dch) {
for { for {
<-out.req; <-out.req
out.dat <- get(in); out.dat <- get(in)
} }
} }
...@@ -177,8 +176,8 @@ func repeat(dat rat, out *dch) { ...@@ -177,8 +176,8 @@ func repeat(dat rat, out *dch) {
} }
} }
type PS *dch; // power series type PS *dch // power series
type PS2 *[2] PS; // pair of power series type PS2 *[2] PS // pair of power series
var Ones PS var Ones PS
var Twos PS var Twos PS
...@@ -208,29 +207,29 @@ func gcd (u, v int64) int64 { ...@@ -208,29 +207,29 @@ func gcd (u, v int64) int64 {
// Make a rational from two ints and from one int // Make a rational from two ints and from one int
func i2tor(u, v int64) rat { func i2tor(u, v int64) rat {
g := gcd(u,v); g := gcd(u,v)
var r rat; var r rat
if v > 0 { if v > 0 {
r.num = u/g; r.num = u/g
r.den = v/g; r.den = v/g
} else { } else {
r.num = -u/g; r.num = -u/g
r.den = -v/g; r.den = -v/g
} }
return r; return r
} }
func itor(u int64) rat { func itor(u int64) rat {
return i2tor(u, 1); return i2tor(u, 1)
} }
var zero rat; var zero rat
var one rat; var one rat
// End mark and end test // End mark and end test
var finis rat; var finis rat
func end(u rat) int64 { func end(u rat) int64 {
if u.den==0 { return 1 } if u.den==0 { return 1 }
...@@ -240,68 +239,68 @@ func end(u rat) int64 { ...@@ -240,68 +239,68 @@ func end(u rat) int64 {
// Operations on rationals // Operations on rationals
func add(u, v rat) rat { func add(u, v rat) rat {
g := gcd(u.den,v.den); g := gcd(u.den,v.den)
return i2tor(u.num*(v.den/g)+v.num*(u.den/g),u.den*(v.den/g)); return i2tor(u.num*(v.den/g)+v.num*(u.den/g),u.den*(v.den/g))
} }
func mul(u, v rat) rat { func mul(u, v rat) rat {
g1 := gcd(u.num,v.den); g1 := gcd(u.num,v.den)
g2 := gcd(u.den,v.num); g2 := gcd(u.den,v.num)
var r rat; var r rat
r.num = (u.num/g1)*(v.num/g2); r.num = (u.num/g1)*(v.num/g2)
r.den = (u.den/g2)*(v.den/g1); r.den = (u.den/g2)*(v.den/g1)
return r; return r
} }
func neg(u rat) rat { func neg(u rat) rat {
return i2tor(-u.num, u.den); return i2tor(-u.num, u.den)
} }
func sub(u, v rat) rat { func sub(u, v rat) rat {
return add(u, neg(v)); return add(u, neg(v))
} }
func inv(u rat) rat { // invert a rat func inv(u rat) rat { // invert a rat
if u.num == 0 { panic("zero divide in inv") } if u.num == 0 { panic("zero divide in inv") }
return i2tor(u.den, u.num); return i2tor(u.den, u.num)
} }
// print eval in floating point of PS at x=c to n terms // print eval in floating point of PS at x=c to n terms
func evaln(c rat, U PS, n int) { func evaln(c rat, U PS, n int) {
xn := float64(1); xn := float64(1)
x := float64(c.num)/float64(c.den); x := float64(c.num)/float64(c.den)
val := float64(0); val := float64(0)
for i:=0; i<n; i++ { for i:=0; i<n; i++ {
u := get(U); u := get(U)
if end(u) != 0 { if end(u) != 0 {
break; break
} }
val = val + x * float64(u.num)/float64(u.den); val = val + x * float64(u.num)/float64(u.den)
xn = xn*x; xn = xn*x
} }
print(val, "\n"); print(val, "\n")
} }
// Print n terms of a power series // Print n terms of a power series
func printn(U PS, n int) { func printn(U PS, n int) {
done := false; done := false
for ; !done && n>0; n-- { for ; !done && n>0; n-- {
u := get(U); u := get(U)
if end(u) != 0 { if end(u) != 0 {
done = true done = true
} else { } else {
u.pr() u.pr()
} }
} }
print(("\n")); print(("\n"))
} }
// Evaluate n terms of power series U at x=c // Evaluate n terms of power series U at x=c
func eval(c rat, U PS, n int) rat { func eval(c rat, U PS, n int) rat {
if n==0 { return zero } if n==0 { return zero }
y := get(U); y := get(U)
if end(y) != 0 { return zero } if end(y) != 0 { return zero }
return add(y,mul(c,eval(c,U,n-1))); return add(y,mul(c,eval(c,U,n-1)))
} }
// Power-series constructors return channels on which power // Power-series constructors return channels on which power
...@@ -311,105 +310,105 @@ func eval(c rat, U PS, n int) rat { ...@@ -311,105 +310,105 @@ func eval(c rat, U PS, n int) rat {
// Make a pair of power series identical to a given power series // Make a pair of power series identical to a given power series
func Split(U PS) *dch2 { func Split(U PS) *dch2 {
UU := mkdch2(); UU := mkdch2()
go split(U,UU); go split(U,UU)
return UU; return UU
} }
// Add two power series // Add two power series
func Add(U, V PS) PS { func Add(U, V PS) PS {
Z := mkPS(); Z := mkPS()
go func() { go func() {
var uv []rat; var uv []rat
for { for {
<-Z.req; <-Z.req
uv = get2(U,V); uv = get2(U,V)
switch end(uv[0])+2*end(uv[1]) { switch end(uv[0])+2*end(uv[1]) {
case 0: case 0:
Z.dat <- add(uv[0], uv[1]); Z.dat <- add(uv[0], uv[1])
case 1: case 1:
Z.dat <- uv[1]; Z.dat <- uv[1]
copy(V,Z); copy(V,Z)
case 2: case 2:
Z.dat <- uv[0]; Z.dat <- uv[0]
copy(U,Z); copy(U,Z)
case 3: case 3:
Z.dat <- finis; Z.dat <- finis
} }
} }
}(); }()
return Z; return Z
} }
// Multiply a power series by a constant // Multiply a power series by a constant
func Cmul(c rat,U PS) PS { func Cmul(c rat,U PS) PS {
Z := mkPS(); Z := mkPS()
go func() { go func() {
done := false; done := false
for !done { for !done {
<-Z.req; <-Z.req
u := get(U); u := get(U)
if end(u) != 0 { if end(u) != 0 {
done = true done = true
} else { } else {
Z.dat <- mul(c,u) Z.dat <- mul(c,u)
} }
} }
Z.dat <- finis; Z.dat <- finis
}(); }()
return Z; return Z
} }
// Subtract // Subtract
func Sub(U, V PS) PS { func Sub(U, V PS) PS {
return Add(U, Cmul(neg(one), V)); return Add(U, Cmul(neg(one), V))
} }
// Multiply a power series by the monomial x^n // Multiply a power series by the monomial x^n
func Monmul(U PS, n int) PS { func Monmul(U PS, n int) PS {
Z := mkPS(); Z := mkPS()
go func() { go func() {
for ; n>0; n-- { put(zero,Z) } for ; n>0; n-- { put(zero,Z) }
copy(U,Z); copy(U,Z)
}(); }()
return Z; return Z
} }
// Multiply by x // Multiply by x
func Xmul(U PS) PS { func Xmul(U PS) PS {
return Monmul(U,1); return Monmul(U,1)
} }
func Rep(c rat) PS { func Rep(c rat) PS {
Z := mkPS(); Z := mkPS()
go repeat(c,Z); go repeat(c,Z)
return Z; return Z
} }
// Monomial c*x^n // Monomial c*x^n
func Mon(c rat, n int) PS { func Mon(c rat, n int) PS {
Z:=mkPS(); Z:=mkPS()
go func() { go func() {
if(c.num!=0) { if(c.num!=0) {
for ; n>0; n=n-1 { put(zero,Z) } for ; n>0; n=n-1 { put(zero,Z) }
put(c,Z); put(c,Z)
} }
put(finis,Z); put(finis,Z)
}(); }()
return Z; return Z
} }
func Shift(c rat, U PS) PS { func Shift(c rat, U PS) PS {
Z := mkPS(); Z := mkPS()
go func() { go func() {
put(c,Z); put(c,Z)
copy(U,Z); copy(U,Z)
}(); }()
return Z; return Z
} }
// simple pole at 1: 1/(1-x) = 1 1 1 1 1 ... // simple pole at 1: 1/(1-x) = 1 1 1 1 1 ...
...@@ -419,17 +418,17 @@ func Shift(c rat, U PS) PS { ...@@ -419,17 +418,17 @@ func Shift(c rat, U PS) PS {
/* /*
func Poly(a []rat) PS { func Poly(a []rat) PS {
Z:=mkPS(); Z:=mkPS()
begin func(a []rat, Z PS) { begin func(a []rat, Z PS) {
j:=0; j:=0
done:=0; done:=0
for j=len(a); !done&&j>0; j=j-1) for j=len(a); !done&&j>0; j=j-1)
if(a[j-1].num!=0) done=1; if(a[j-1].num!=0) done=1
i:=0; i:=0
for(; i<j; i=i+1) put(a[i],Z); for(; i<j; i=i+1) put(a[i],Z)
put(finis,Z); put(finis,Z)
}(); }()
return Z; return Z
} }
*/ */
...@@ -439,82 +438,82 @@ func Poly(a []rat) PS { ...@@ -439,82 +438,82 @@ func Poly(a []rat) PS {
// then UV = u*v + x*(u*VV+v*UU) + x*x*UU*VV // then UV = u*v + x*(u*VV+v*UU) + x*x*UU*VV
func Mul(U, V PS) PS { func Mul(U, V PS) PS {
Z:=mkPS(); Z:=mkPS()
go func() { go func() {
<-Z.req; <-Z.req
uv := get2(U,V); uv := get2(U,V)
if end(uv[0])!=0 || end(uv[1]) != 0 { if end(uv[0])!=0 || end(uv[1]) != 0 {
Z.dat <- finis; Z.dat <- finis
} else { } else {
Z.dat <- mul(uv[0],uv[1]); Z.dat <- mul(uv[0],uv[1])
UU := Split(U); UU := Split(U)
VV := Split(V); VV := Split(V)
W := Add(Cmul(uv[0],VV[0]),Cmul(uv[1],UU[0])); W := Add(Cmul(uv[0],VV[0]),Cmul(uv[1],UU[0]))
<-Z.req; <-Z.req
Z.dat <- get(W); Z.dat <- get(W)
copy(Add(W,Mul(UU[1],VV[1])),Z); copy(Add(W,Mul(UU[1],VV[1])),Z)
} }
}(); }()
return Z; return Z
} }
// Differentiate // Differentiate
func Diff(U PS) PS { func Diff(U PS) PS {
Z:=mkPS(); Z:=mkPS()
go func() { go func() {
<-Z.req; <-Z.req
u := get(U); u := get(U)
if end(u) == 0 { if end(u) == 0 {
done:=false; done:=false
for i:=1; !done; i++ { for i:=1; !done; i++ {
u = get(U); u = get(U)
if end(u) != 0 { if end(u) != 0 {
done = true done = true
} else { } else {
Z.dat <- mul(itor(int64(i)),u); Z.dat <- mul(itor(int64(i)),u)
<-Z.req; <-Z.req
} }
} }
} }
Z.dat <- finis; Z.dat <- finis
}(); }()
return Z; return Z
} }
// Integrate, with const of integration // Integrate, with const of integration
func Integ(c rat,U PS) PS { func Integ(c rat,U PS) PS {
Z:=mkPS(); Z:=mkPS()
go func() { go func() {
put(c,Z); put(c,Z)
done:=false; done:=false
for i:=1; !done; i++ { for i:=1; !done; i++ {
<-Z.req; <-Z.req
u := get(U); u := get(U)
if end(u) != 0 { done= true } if end(u) != 0 { done= true }
Z.dat <- mul(i2tor(1,int64(i)),u); Z.dat <- mul(i2tor(1,int64(i)),u)
} }
Z.dat <- finis; Z.dat <- finis
}(); }()
return Z; return Z
} }
// Binomial theorem (1+x)^c // Binomial theorem (1+x)^c
func Binom(c rat) PS { func Binom(c rat) PS {
Z:=mkPS(); Z:=mkPS()
go func() { go func() {
n := 1; n := 1
t := itor(1); t := itor(1)
for c.num!=0 { for c.num!=0 {
put(t,Z); put(t,Z)
t = mul(mul(t,c),i2tor(1,int64(n))); t = mul(mul(t,c),i2tor(1,int64(n)))
c = sub(c,one); c = sub(c,one)
n++; n++
} }
put(finis,Z); put(finis,Z)
}(); }()
return Z; return Z
} }
// Reciprocal of a power series // Reciprocal of a power series
...@@ -523,19 +522,19 @@ func Binom(c rat) PS { ...@@ -523,19 +522,19 @@ func Binom(c rat) PS {
// (u+x*UU)*(z+x*ZZ) = 1 // (u+x*UU)*(z+x*ZZ) = 1
// z = 1/u // z = 1/u
// u*ZZ + z*UU +x*UU*ZZ = 0 // u*ZZ + z*UU +x*UU*ZZ = 0
// ZZ = -UU*(z+x*ZZ)/u; // ZZ = -UU*(z+x*ZZ)/u
func Recip(U PS) PS { func Recip(U PS) PS {
Z:=mkPS(); Z:=mkPS()
go func() { go func() {
ZZ:=mkPS2(); ZZ:=mkPS2()
<-Z.req; <-Z.req
z := inv(get(U)); z := inv(get(U))
Z.dat <- z; Z.dat <- z
split(Mul(Cmul(neg(z),U),Shift(z,ZZ[0])),ZZ); split(Mul(Cmul(neg(z),U),Shift(z,ZZ[0])),ZZ)
copy(ZZ[1],Z); copy(ZZ[1],Z)
}(); }()
return Z; return Z
} }
// Exponential of a power series with constant term 0 // Exponential of a power series with constant term 0
...@@ -546,9 +545,9 @@ func Recip(U PS) PS { ...@@ -546,9 +545,9 @@ func Recip(U PS) PS {
// integrate to get Z // integrate to get Z
func Exp(U PS) PS { func Exp(U PS) PS {
ZZ := mkPS2(); ZZ := mkPS2()
split(Integ(one,Mul(ZZ[0],Diff(U))),ZZ); split(Integ(one,Mul(ZZ[0],Diff(U))),ZZ)
return ZZ[1]; return ZZ[1]
} }
// Substitute V for x in U, where the leading term of V is zero // Substitute V for x in U, where the leading term of V is zero
...@@ -558,69 +557,69 @@ func Exp(U PS) PS { ...@@ -558,69 +557,69 @@ func Exp(U PS) PS {
// bug: a nonzero constant term is ignored // bug: a nonzero constant term is ignored
func Subst(U, V PS) PS { func Subst(U, V PS) PS {
Z:= mkPS(); Z:= mkPS()
go func() { go func() {
VV := Split(V); VV := Split(V)
<-Z.req; <-Z.req
u := get(U); u := get(U)
Z.dat <- u; Z.dat <- u
if end(u) == 0 { if end(u) == 0 {
if end(get(VV[0])) != 0 { if end(get(VV[0])) != 0 {
put(finis,Z); put(finis,Z)
} else { } else {
copy(Mul(VV[0],Subst(U,VV[1])),Z); copy(Mul(VV[0],Subst(U,VV[1])),Z)
} }
} }
}(); }()
return Z; return Z
} }
// Monomial Substition: U(c x^n) // Monomial Substition: U(c x^n)
// Each Ui is multiplied by c^i and followed by n-1 zeros // Each Ui is multiplied by c^i and followed by n-1 zeros
func MonSubst(U PS, c0 rat, n int) PS { func MonSubst(U PS, c0 rat, n int) PS {
Z:= mkPS(); Z:= mkPS()
go func() { go func() {
c := one; c := one
for { for {
<-Z.req; <-Z.req
u := get(U); u := get(U)
Z.dat <- mul(u, c); Z.dat <- mul(u, c)
c = mul(c, c0); c = mul(c, c0)
if end(u) != 0 { if end(u) != 0 {
Z.dat <- finis; Z.dat <- finis
break; break
} }
for i := 1; i < n; i++ { for i := 1; i < n; i++ {
<-Z.req; <-Z.req
Z.dat <- zero; Z.dat <- zero
} }
} }
}(); }()
return Z; return Z
} }
func Init() { func Init() {
chnameserial = -1; chnameserial = -1
seqno = 0; seqno = 0
chnames = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; chnames = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
zero = itor(0); zero = itor(0)
one = itor(1); one = itor(1)
finis = i2tor(1,0); finis = i2tor(1,0)
Ones = Rep(one); Ones = Rep(one)
Twos = Rep(itor(2)); Twos = Rep(itor(2))
} }
func check(U PS, c rat, count int, str string) { func check(U PS, c rat, count int, str string) {
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
r := get(U); r := get(U)
if !r.eq(c) { if !r.eq(c) {
print("got: "); print("got: ")
r.pr(); r.pr()
print("should get "); print("should get ")
c.pr(); c.pr()
print("\n"); print("\n")
panic(str) panic(str)
} }
} }
...@@ -629,82 +628,82 @@ func check(U PS, c rat, count int, str string) { ...@@ -629,82 +628,82 @@ func check(U PS, c rat, count int, str string) {
const N=10 const N=10
func checka(U PS, a []rat, str string) { func checka(U PS, a []rat, str string) {
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
check(U, a[i], 1, str); check(U, a[i], 1, str)
} }
} }
func main() { func main() {
Init(); Init()
if len(os.Args) > 1 { // print if len(os.Args) > 1 { // print
print("Ones: "); printn(Ones, 10); print("Ones: "); printn(Ones, 10)
print("Twos: "); printn(Twos, 10); print("Twos: "); printn(Twos, 10)
print("Add: "); printn(Add(Ones, Twos), 10); print("Add: "); printn(Add(Ones, Twos), 10)
print("Diff: "); printn(Diff(Ones), 10); print("Diff: "); printn(Diff(Ones), 10)
print("Integ: "); printn(Integ(zero, Ones), 10); print("Integ: "); printn(Integ(zero, Ones), 10)
print("CMul: "); printn(Cmul(neg(one), Ones), 10); print("CMul: "); printn(Cmul(neg(one), Ones), 10)
print("Sub: "); printn(Sub(Ones, Twos), 10); print("Sub: "); printn(Sub(Ones, Twos), 10)
print("Mul: "); printn(Mul(Ones, Ones), 10); print("Mul: "); printn(Mul(Ones, Ones), 10)
print("Exp: "); printn(Exp(Ones), 15); print("Exp: "); printn(Exp(Ones), 15)
print("MonSubst: "); printn(MonSubst(Ones, neg(one), 2), 10); print("MonSubst: "); printn(MonSubst(Ones, neg(one), 2), 10)
print("ATan: "); printn(Integ(zero, MonSubst(Ones, neg(one), 2)), 10); print("ATan: "); printn(Integ(zero, MonSubst(Ones, neg(one), 2)), 10)
} else { // test } else { // test
check(Ones, one, 5, "Ones"); check(Ones, one, 5, "Ones")
check(Add(Ones, Ones), itor(2), 0, "Add Ones Ones"); // 1 1 1 1 1 check(Add(Ones, Ones), itor(2), 0, "Add Ones Ones") // 1 1 1 1 1
check(Add(Ones, Twos), itor(3), 0, "Add Ones Twos"); // 3 3 3 3 3 check(Add(Ones, Twos), itor(3), 0, "Add Ones Twos") // 3 3 3 3 3
a := make([]rat, N); a := make([]rat, N)
d := Diff(Ones); d := Diff(Ones)
for i:=0; i < N; i++ { for i:=0; i < N; i++ {
a[i] = itor(int64(i+1)) a[i] = itor(int64(i+1))
} }
checka(d, a, "Diff"); // 1 2 3 4 5 checka(d, a, "Diff") // 1 2 3 4 5
in := Integ(zero, Ones); in := Integ(zero, Ones)
a[0] = zero; // integration constant a[0] = zero // integration constant
for i:=1; i < N; i++ { for i:=1; i < N; i++ {
a[i] = i2tor(1, int64(i)) a[i] = i2tor(1, int64(i))
} }
checka(in, a, "Integ"); // 0 1 1/2 1/3 1/4 1/5 checka(in, a, "Integ") // 0 1 1/2 1/3 1/4 1/5
check(Cmul(neg(one), Twos), itor(-2), 10, "CMul"); // -1 -1 -1 -1 -1 check(Cmul(neg(one), Twos), itor(-2), 10, "CMul") // -1 -1 -1 -1 -1
check(Sub(Ones, Twos), itor(-1), 0, "Sub Ones Twos"); // -1 -1 -1 -1 -1 check(Sub(Ones, Twos), itor(-1), 0, "Sub Ones Twos") // -1 -1 -1 -1 -1
m := Mul(Ones, Ones); m := Mul(Ones, Ones)
for i:=0; i < N; i++ { for i:=0; i < N; i++ {
a[i] = itor(int64(i+1)) a[i] = itor(int64(i+1))
} }
checka(m, a, "Mul"); // 1 2 3 4 5 checka(m, a, "Mul") // 1 2 3 4 5
e := Exp(Ones); e := Exp(Ones)
a[0] = itor(1); a[0] = itor(1)
a[1] = itor(1); a[1] = itor(1)
a[2] = i2tor(3,2); a[2] = i2tor(3,2)
a[3] = i2tor(13,6); a[3] = i2tor(13,6)
a[4] = i2tor(73,24); a[4] = i2tor(73,24)
a[5] = i2tor(167,40); a[5] = i2tor(167,40)
a[6] = i2tor(4051,720); a[6] = i2tor(4051,720)
a[7] = i2tor(37633,5040); a[7] = i2tor(37633,5040)
a[8] = i2tor(43817,4480); a[8] = i2tor(43817,4480)
a[9] = i2tor(4596553,362880); a[9] = i2tor(4596553,362880)
checka(e, a, "Exp"); // 1 1 3/2 13/6 73/24 checka(e, a, "Exp") // 1 1 3/2 13/6 73/24
at := Integ(zero, MonSubst(Ones, neg(one), 2)); at := Integ(zero, MonSubst(Ones, neg(one), 2))
for c, i := 1, 0; i < N; i++ { for c, i := 1, 0; i < N; i++ {
if i%2 == 0 { if i%2 == 0 {
a[i] = zero a[i] = zero
} else { } else {
a[i] = i2tor(int64(c), int64(i)); a[i] = i2tor(int64(c), int64(i))
c *= -1 c *= -1
} }
} }
checka(at, a, "ATan"); // 0 -1 0 -1/3 0 -1/5 checka(at, a, "ATan") // 0 -1 0 -1/3 0 -1/5
/* /*
t := Revert(Integ(zero, MonSubst(Ones, neg(one), 2))); t := Revert(Integ(zero, MonSubst(Ones, neg(one), 2)))
a[0] = zero; a[0] = zero
a[1] = itor(1); a[1] = itor(1)
a[2] = zero; a[2] = zero
a[3] = i2tor(1,3); a[3] = i2tor(1,3)
a[4] = zero; a[4] = zero
a[5] = i2tor(2,15); a[5] = i2tor(2,15)
a[6] = zero; a[6] = zero
a[7] = i2tor(17,315); a[7] = i2tor(17,315)
a[8] = zero; a[8] = zero
a[9] = i2tor(62,2835); a[9] = i2tor(62,2835)
checka(t, a, "Tan"); // 0 1 0 1/3 0 2/15 checka(t, a, "Tan") // 0 1 0 1/3 0 2/15
*/ */
} }
} }
...@@ -19,12 +19,12 @@ package main ...@@ -19,12 +19,12 @@ package main
import "os" import "os"
type rat struct { type rat struct {
num, den int64; // numerator, denominator num, den int64 // numerator, denominator
} }
type item interface { type item interface {
pr(); pr()
eq(c item) bool; eq(c item) bool
} }
func (u *rat) pr(){ func (u *rat) pr(){
...@@ -37,14 +37,14 @@ func (u *rat) pr(){ ...@@ -37,14 +37,14 @@ func (u *rat) pr(){
} }
func (u *rat) eq(c item) bool { func (u *rat) eq(c item) bool {
c1 := c.(*rat); c1 := c.(*rat)
return u.num == c1.num && u.den == c1.den return u.num == c1.num && u.den == c1.den
} }
type dch struct { type dch struct {
req chan int; req chan int
dat chan item; dat chan item
nam int; nam int
} }
type dch2 [2] *dch type dch2 [2] *dch
...@@ -54,20 +54,20 @@ var chnameserial int ...@@ -54,20 +54,20 @@ var chnameserial int
var seqno int var seqno int
func mkdch() *dch { func mkdch() *dch {
c := chnameserial % len(chnames); c := chnameserial % len(chnames)
chnameserial++; chnameserial++
d := new(dch); d := new(dch)
d.req = make(chan int); d.req = make(chan int)
d.dat = make(chan item); d.dat = make(chan item)
d.nam = c; d.nam = c
return d; return d
} }
func mkdch2() *dch2 { func mkdch2() *dch2 {
d2 := new(dch2); d2 := new(dch2)
d2[0] = mkdch(); d2[0] = mkdch()
d2[1] = mkdch(); d2[1] = mkdch()
return d2; return d2
} }
// split reads a single demand channel and replicates its // split reads a single demand channel and replicates its
...@@ -85,98 +85,97 @@ func mkdch2() *dch2 { ...@@ -85,98 +85,97 @@ func mkdch2() *dch2 {
// generation to begin servicing out[1]. // generation to begin servicing out[1].
func dosplit(in *dch, out *dch2, wait chan int ){ func dosplit(in *dch, out *dch2, wait chan int ){
var t *dch; both := false // do not service both channels
both := false; // do not service both channels
select { select {
case <-out[0].req: case <-out[0].req:
;
case <-wait: case <-wait:
both = true; both = true
select { select {
case <-out[0].req: case <-out[0].req:
;
case <-out[1].req: case <-out[1].req:
t=out[0]; out[0]=out[1]; out[1]=t; out[0],out[1] = out[1], out[0]
} }
} }
seqno++; seqno++
in.req <- seqno; in.req <- seqno
release := make(chan int); release := make(chan int)
go dosplit(in, out, release); go dosplit(in, out, release)
dat := <-in.dat; dat := <-in.dat
out[0].dat <- dat; out[0].dat <- dat
if !both { if !both {
<-wait <-wait
} }
<-out[1].req; <-out[1].req
out[1].dat <- dat; out[1].dat <- dat
release <- 0; release <- 0
} }
func split(in *dch, out *dch2){ func split(in *dch, out *dch2){
release := make(chan int); release := make(chan int)
go dosplit(in, out, release); go dosplit(in, out, release)
release <- 0; release <- 0
} }
func put(dat item, out *dch){ func put(dat item, out *dch){
<-out.req; <-out.req
out.dat <- dat; out.dat <- dat
} }
func get(in *dch) *rat { func get(in *dch) *rat {
seqno++; seqno++
in.req <- seqno; in.req <- seqno
return (<-in.dat).(*rat); return (<-in.dat).(*rat)
} }
// Get one item from each of n demand channels // Get one item from each of n demand channels
func getn(in []*dch) []item { func getn(in []*dch) []item {
n:=len(in); n:=len(in)
if n != 2 { panic("bad n in getn") }; if n != 2 { panic("bad n in getn") }
req := make([] chan int, 2); req := make([] chan int, 2)
dat := make([] chan item, 2); dat := make([] chan item, 2)
out := make([]item, 2); out := make([]item, 2)
var i int; var i int
var it item; var it item
for i=0; i<n; i++ { for i=0; i<n; i++ {
req[i] = in[i].req; req[i] = in[i].req
dat[i] = nil; dat[i] = nil
} }
for n=2*n; n>0; n-- { for n=2*n; n>0; n-- {
seqno++; seqno++
select{ select{
case req[0] <- seqno: case req[0] <- seqno:
dat[0] = in[0].dat; dat[0] = in[0].dat
req[0] = nil; req[0] = nil
case req[1] <- seqno: case req[1] <- seqno:
dat[1] = in[1].dat; dat[1] = in[1].dat
req[1] = nil; req[1] = nil
case it = <-dat[0]: case it = <-dat[0]:
out[0] = it; out[0] = it
dat[0] = nil; dat[0] = nil
case it = <-dat[1]: case it = <-dat[1]:
out[1] = it; out[1] = it
dat[1] = nil; dat[1] = nil
} }
} }
return out; return out
} }
// Get one item from each of 2 demand channels // Get one item from each of 2 demand channels
func get2(in0 *dch, in1 *dch) []item { func get2(in0 *dch, in1 *dch) []item {
return getn([]*dch{in0, in1}); return getn([]*dch{in0, in1})
} }
func copy(in *dch, out *dch){ func copy(in *dch, out *dch){
for { for {
<-out.req; <-out.req
out.dat <- get(in); out.dat <- get(in)
} }
} }
...@@ -186,8 +185,8 @@ func repeat(dat item, out *dch){ ...@@ -186,8 +185,8 @@ func repeat(dat item, out *dch){
} }
} }
type PS *dch; // power series type PS *dch // power series
type PS2 *[2] PS; // pair of power series type PS2 *[2] PS // pair of power series
var Ones PS var Ones PS
var Twos PS var Twos PS
...@@ -217,29 +216,29 @@ func gcd (u, v int64) int64{ ...@@ -217,29 +216,29 @@ func gcd (u, v int64) int64{
// Make a rational from two ints and from one int // Make a rational from two ints and from one int
func i2tor(u, v int64) *rat{ func i2tor(u, v int64) *rat{
g := gcd(u,v); g := gcd(u,v)
r := new(rat); r := new(rat)
if v > 0 { if v > 0 {
r.num = u/g; r.num = u/g
r.den = v/g; r.den = v/g
} else { } else {
r.num = -u/g; r.num = -u/g
r.den = -v/g; r.den = -v/g
} }
return r; return r
} }
func itor(u int64) *rat{ func itor(u int64) *rat{
return i2tor(u, 1); return i2tor(u, 1)
} }
var zero *rat; var zero *rat
var one *rat; var one *rat
// End mark and end test // End mark and end test
var finis *rat; var finis *rat
func end(u *rat) int64 { func end(u *rat) int64 {
if u.den==0 { return 1 } if u.den==0 { return 1 }
...@@ -249,72 +248,72 @@ func end(u *rat) int64 { ...@@ -249,72 +248,72 @@ func end(u *rat) int64 {
// Operations on rationals // Operations on rationals
func add(u, v *rat) *rat { func add(u, v *rat) *rat {
g := gcd(u.den,v.den); g := gcd(u.den,v.den)
return i2tor(u.num*(v.den/g)+v.num*(u.den/g),u.den*(v.den/g)); return i2tor(u.num*(v.den/g)+v.num*(u.den/g),u.den*(v.den/g))
} }
func mul(u, v *rat) *rat{ func mul(u, v *rat) *rat{
g1 := gcd(u.num,v.den); g1 := gcd(u.num,v.den)
g2 := gcd(u.den,v.num); g2 := gcd(u.den,v.num)
r := new(rat); r := new(rat)
r.num =(u.num/g1)*(v.num/g2); r.num =(u.num/g1)*(v.num/g2)
r.den = (u.den/g2)*(v.den/g1); r.den = (u.den/g2)*(v.den/g1)
return r; return r
} }
func neg(u *rat) *rat{ func neg(u *rat) *rat{
return i2tor(-u.num, u.den); return i2tor(-u.num, u.den)
} }
func sub(u, v *rat) *rat{ func sub(u, v *rat) *rat{
return add(u, neg(v)); return add(u, neg(v))
} }
func inv(u *rat) *rat{ // invert a rat func inv(u *rat) *rat{ // invert a rat
if u.num == 0 { panic("zero divide in inv") } if u.num == 0 { panic("zero divide in inv") }
return i2tor(u.den, u.num); return i2tor(u.den, u.num)
} }
// print eval in floating point of PS at x=c to n terms // print eval in floating point of PS at x=c to n terms
func Evaln(c *rat, U PS, n int) { func Evaln(c *rat, U PS, n int) {
xn := float64(1); xn := float64(1)
x := float64(c.num)/float64(c.den); x := float64(c.num)/float64(c.den)
val := float64(0); val := float64(0)
for i:=0; i<n; i++ { for i:=0; i<n; i++ {
u := get(U); u := get(U)
if end(u) != 0 { if end(u) != 0 {
break; break
} }
val = val + x * float64(u.num)/float64(u.den); val = val + x * float64(u.num)/float64(u.den)
xn = xn*x; xn = xn*x
} }
print(val, "\n"); print(val, "\n")
} }
// Print n terms of a power series // Print n terms of a power series
func Printn(U PS, n int){ func Printn(U PS, n int){
done := false; done := false
for ; !done && n>0; n-- { for ; !done && n>0; n-- {
u := get(U); u := get(U)
if end(u) != 0 { if end(u) != 0 {
done = true done = true
} else { } else {
u.pr() u.pr()
} }
} }
print(("\n")); print(("\n"))
} }
func Print(U PS){ func Print(U PS){
Printn(U,1000000000); Printn(U,1000000000)
} }
// Evaluate n terms of power series U at x=c // Evaluate n terms of power series U at x=c
func eval(c *rat, U PS, n int) *rat{ func eval(c *rat, U PS, n int) *rat{
if n==0 { return zero } if n==0 { return zero }
y := get(U); y := get(U)
if end(y) != 0 { return zero } if end(y) != 0 { return zero }
return add(y,mul(c,eval(c,U,n-1))); return add(y,mul(c,eval(c,U,n-1)))
} }
// Power-series constructors return channels on which power // Power-series constructors return channels on which power
...@@ -324,105 +323,105 @@ func eval(c *rat, U PS, n int) *rat{ ...@@ -324,105 +323,105 @@ func eval(c *rat, U PS, n int) *rat{
// Make a pair of power series identical to a given power series // Make a pair of power series identical to a given power series
func Split(U PS) *dch2{ func Split(U PS) *dch2{
UU := mkdch2(); UU := mkdch2()
go split(U,UU); go split(U,UU)
return UU; return UU
} }
// Add two power series // Add two power series
func Add(U, V PS) PS{ func Add(U, V PS) PS{
Z := mkPS(); Z := mkPS()
go func(U, V, Z PS){ go func(U, V, Z PS){
var uv [] item; var uv [] item
for { for {
<-Z.req; <-Z.req
uv = get2(U,V); uv = get2(U,V)
switch end(uv[0].(*rat))+2*end(uv[1].(*rat)) { switch end(uv[0].(*rat))+2*end(uv[1].(*rat)) {
case 0: case 0:
Z.dat <- add(uv[0].(*rat), uv[1].(*rat)); Z.dat <- add(uv[0].(*rat), uv[1].(*rat))
case 1: case 1:
Z.dat <- uv[1]; Z.dat <- uv[1]
copy(V,Z); copy(V,Z)
case 2: case 2:
Z.dat <- uv[0]; Z.dat <- uv[0]
copy(U,Z); copy(U,Z)
case 3: case 3:
Z.dat <- finis; Z.dat <- finis
} }
} }
}(U, V, Z); }(U, V, Z)
return Z; return Z
} }
// Multiply a power series by a constant // Multiply a power series by a constant
func Cmul(c *rat,U PS) PS{ func Cmul(c *rat,U PS) PS{
Z := mkPS(); Z := mkPS()
go func(c *rat, U, Z PS){ go func(c *rat, U, Z PS){
done := false; done := false
for !done { for !done {
<-Z.req; <-Z.req
u := get(U); u := get(U)
if end(u) != 0 { if end(u) != 0 {
done = true done = true
} else { } else {
Z.dat <- mul(c,u) Z.dat <- mul(c,u)
} }
} }
Z.dat <- finis; Z.dat <- finis
}(c, U, Z); }(c, U, Z)
return Z; return Z
} }
// Subtract // Subtract
func Sub(U, V PS) PS{ func Sub(U, V PS) PS{
return Add(U, Cmul(neg(one), V)); return Add(U, Cmul(neg(one), V))
} }
// Multiply a power series by the monomial x^n // Multiply a power series by the monomial x^n
func Monmul(U PS, n int) PS{ func Monmul(U PS, n int) PS{
Z := mkPS(); Z := mkPS()
go func(n int, U PS, Z PS){ go func(n int, U PS, Z PS){
for ; n>0; n-- { put(zero,Z) } for ; n>0; n-- { put(zero,Z) }
copy(U,Z); copy(U,Z)
}(n, U, Z); }(n, U, Z)
return Z; return Z
} }
// Multiply by x // Multiply by x
func Xmul(U PS) PS{ func Xmul(U PS) PS{
return Monmul(U,1); return Monmul(U,1)
} }
func Rep(c *rat) PS{ func Rep(c *rat) PS{
Z := mkPS(); Z := mkPS()
go repeat(c,Z); go repeat(c,Z)
return Z; return Z
} }
// Monomial c*x^n // Monomial c*x^n
func Mon(c *rat, n int) PS{ func Mon(c *rat, n int) PS{
Z:=mkPS(); Z:=mkPS()
go func(c *rat, n int, Z PS){ go func(c *rat, n int, Z PS){
if(c.num!=0) { if(c.num!=0) {
for ; n>0; n=n-1 { put(zero,Z) } for ; n>0; n=n-1 { put(zero,Z) }
put(c,Z); put(c,Z)
} }
put(finis,Z); put(finis,Z)
}(c, n, Z); }(c, n, Z)
return Z; return Z
} }
func Shift(c *rat, U PS) PS{ func Shift(c *rat, U PS) PS{
Z := mkPS(); Z := mkPS()
go func(c *rat, U, Z PS){ go func(c *rat, U, Z PS){
put(c,Z); put(c,Z)
copy(U,Z); copy(U,Z)
}(c, U, Z); }(c, U, Z)
return Z; return Z
} }
// simple pole at 1: 1/(1-x) = 1 1 1 1 1 ... // simple pole at 1: 1/(1-x) = 1 1 1 1 1 ...
...@@ -432,17 +431,17 @@ func Shift(c *rat, U PS) PS{ ...@@ -432,17 +431,17 @@ func Shift(c *rat, U PS) PS{
/* /*
func Poly(a [] *rat) PS{ func Poly(a [] *rat) PS{
Z:=mkPS(); Z:=mkPS()
begin func(a [] *rat, Z PS){ begin func(a [] *rat, Z PS){
j:=0; j:=0
done:=0; done:=0
for j=len(a); !done&&j>0; j=j-1) for j=len(a); !done&&j>0; j=j-1)
if(a[j-1].num!=0) done=1; if(a[j-1].num!=0) done=1
i:=0; i:=0
for(; i<j; i=i+1) put(a[i],Z); for(; i<j; i=i+1) put(a[i],Z)
put(finis,Z); put(finis,Z)
}(); }()
return Z; return Z
} }
*/ */
...@@ -452,82 +451,82 @@ func Poly(a [] *rat) PS{ ...@@ -452,82 +451,82 @@ func Poly(a [] *rat) PS{
// then UV = u*v + x*(u*VV+v*UU) + x*x*UU*VV // then UV = u*v + x*(u*VV+v*UU) + x*x*UU*VV
func Mul(U, V PS) PS{ func Mul(U, V PS) PS{
Z:=mkPS(); Z:=mkPS()
go func(U, V, Z PS){ go func(U, V, Z PS){
<-Z.req; <-Z.req
uv := get2(U,V); uv := get2(U,V)
if end(uv[0].(*rat))!=0 || end(uv[1].(*rat)) != 0 { if end(uv[0].(*rat))!=0 || end(uv[1].(*rat)) != 0 {
Z.dat <- finis; Z.dat <- finis
} else { } else {
Z.dat <- mul(uv[0].(*rat),uv[1].(*rat)); Z.dat <- mul(uv[0].(*rat),uv[1].(*rat))
UU := Split(U); UU := Split(U)
VV := Split(V); VV := Split(V)
W := Add(Cmul(uv[0].(*rat),VV[0]),Cmul(uv[1].(*rat),UU[0])); W := Add(Cmul(uv[0].(*rat),VV[0]),Cmul(uv[1].(*rat),UU[0]))
<-Z.req; <-Z.req
Z.dat <- get(W); Z.dat <- get(W)
copy(Add(W,Mul(UU[1],VV[1])),Z); copy(Add(W,Mul(UU[1],VV[1])),Z)
} }
}(U, V, Z); }(U, V, Z)
return Z; return Z
} }
// Differentiate // Differentiate
func Diff(U PS) PS{ func Diff(U PS) PS{
Z:=mkPS(); Z:=mkPS()
go func(U, Z PS){ go func(U, Z PS){
<-Z.req; <-Z.req
u := get(U); u := get(U)
if end(u) == 0 { if end(u) == 0 {
done:=false; done:=false
for i:=1; !done; i++ { for i:=1; !done; i++ {
u = get(U); u = get(U)
if end(u) != 0 { if end(u) != 0 {
done=true done=true
} else { } else {
Z.dat <- mul(itor(int64(i)),u); Z.dat <- mul(itor(int64(i)),u)
<-Z.req; <-Z.req
} }
} }
} }
Z.dat <- finis; Z.dat <- finis
}(U, Z); }(U, Z)
return Z; return Z
} }
// Integrate, with const of integration // Integrate, with const of integration
func Integ(c *rat,U PS) PS{ func Integ(c *rat,U PS) PS{
Z:=mkPS(); Z:=mkPS()
go func(c *rat, U, Z PS){ go func(c *rat, U, Z PS){
put(c,Z); put(c,Z)
done:=false; done:=false
for i:=1; !done; i++ { for i:=1; !done; i++ {
<-Z.req; <-Z.req
u := get(U); u := get(U)
if end(u) != 0 { done= true } if end(u) != 0 { done= true }
Z.dat <- mul(i2tor(1,int64(i)),u); Z.dat <- mul(i2tor(1,int64(i)),u)
} }
Z.dat <- finis; Z.dat <- finis
}(c, U, Z); }(c, U, Z)
return Z; return Z
} }
// Binomial theorem (1+x)^c // Binomial theorem (1+x)^c
func Binom(c *rat) PS{ func Binom(c *rat) PS{
Z:=mkPS(); Z:=mkPS()
go func(c *rat, Z PS){ go func(c *rat, Z PS){
n := 1; n := 1
t := itor(1); t := itor(1)
for c.num!=0 { for c.num!=0 {
put(t,Z); put(t,Z)
t = mul(mul(t,c),i2tor(1,int64(n))); t = mul(mul(t,c),i2tor(1,int64(n)))
c = sub(c,one); c = sub(c,one)
n++; n++
} }
put(finis,Z); put(finis,Z)
}(c, Z); }(c, Z)
return Z; return Z
} }
// Reciprocal of a power series // Reciprocal of a power series
...@@ -536,19 +535,19 @@ func Binom(c *rat) PS{ ...@@ -536,19 +535,19 @@ func Binom(c *rat) PS{
// (u+x*UU)*(z+x*ZZ) = 1 // (u+x*UU)*(z+x*ZZ) = 1
// z = 1/u // z = 1/u
// u*ZZ + z*UU +x*UU*ZZ = 0 // u*ZZ + z*UU +x*UU*ZZ = 0
// ZZ = -UU*(z+x*ZZ)/u; // ZZ = -UU*(z+x*ZZ)/u
func Recip(U PS) PS{ func Recip(U PS) PS{
Z:=mkPS(); Z:=mkPS()
go func(U, Z PS){ go func(U, Z PS){
ZZ:=mkPS2(); ZZ:=mkPS2()
<-Z.req; <-Z.req
z := inv(get(U)); z := inv(get(U))
Z.dat <- z; Z.dat <- z
split(Mul(Cmul(neg(z),U),Shift(z,ZZ[0])),ZZ); split(Mul(Cmul(neg(z),U),Shift(z,ZZ[0])),ZZ)
copy(ZZ[1],Z); copy(ZZ[1],Z)
}(U, Z); }(U, Z)
return Z; return Z
} }
// Exponential of a power series with constant term 0 // Exponential of a power series with constant term 0
...@@ -559,9 +558,9 @@ func Recip(U PS) PS{ ...@@ -559,9 +558,9 @@ func Recip(U PS) PS{
// integrate to get Z // integrate to get Z
func Exp(U PS) PS{ func Exp(U PS) PS{
ZZ := mkPS2(); ZZ := mkPS2()
split(Integ(one,Mul(ZZ[0],Diff(U))),ZZ); split(Integ(one,Mul(ZZ[0],Diff(U))),ZZ)
return ZZ[1]; return ZZ[1]
} }
// Substitute V for x in U, where the leading term of V is zero // Substitute V for x in U, where the leading term of V is zero
...@@ -571,69 +570,69 @@ func Exp(U PS) PS{ ...@@ -571,69 +570,69 @@ func Exp(U PS) PS{
// bug: a nonzero constant term is ignored // bug: a nonzero constant term is ignored
func Subst(U, V PS) PS { func Subst(U, V PS) PS {
Z:= mkPS(); Z:= mkPS()
go func(U, V, Z PS) { go func(U, V, Z PS) {
VV := Split(V); VV := Split(V)
<-Z.req; <-Z.req
u := get(U); u := get(U)
Z.dat <- u; Z.dat <- u
if end(u) == 0 { if end(u) == 0 {
if end(get(VV[0])) != 0 { if end(get(VV[0])) != 0 {
put(finis,Z); put(finis,Z)
} else { } else {
copy(Mul(VV[0],Subst(U,VV[1])),Z); copy(Mul(VV[0],Subst(U,VV[1])),Z)
} }
} }
}(U, V, Z); }(U, V, Z)
return Z; return Z
} }
// Monomial Substition: U(c x^n) // Monomial Substition: U(c x^n)
// Each Ui is multiplied by c^i and followed by n-1 zeros // Each Ui is multiplied by c^i and followed by n-1 zeros
func MonSubst(U PS, c0 *rat, n int) PS { func MonSubst(U PS, c0 *rat, n int) PS {
Z:= mkPS(); Z:= mkPS()
go func(U, Z PS, c0 *rat, n int) { go func(U, Z PS, c0 *rat, n int) {
c := one; c := one
for { for {
<-Z.req; <-Z.req
u := get(U); u := get(U)
Z.dat <- mul(u, c); Z.dat <- mul(u, c)
c = mul(c, c0); c = mul(c, c0)
if end(u) != 0 { if end(u) != 0 {
Z.dat <- finis; Z.dat <- finis
break; break
} }
for i := 1; i < n; i++ { for i := 1; i < n; i++ {
<-Z.req; <-Z.req
Z.dat <- zero; Z.dat <- zero
} }
} }
}(U, Z, c0, n); }(U, Z, c0, n)
return Z; return Z
} }
func Init() { func Init() {
chnameserial = -1; chnameserial = -1
seqno = 0; seqno = 0
chnames = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; chnames = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
zero = itor(0); zero = itor(0)
one = itor(1); one = itor(1)
finis = i2tor(1,0); finis = i2tor(1,0)
Ones = Rep(one); Ones = Rep(one)
Twos = Rep(itor(2)); Twos = Rep(itor(2))
} }
func check(U PS, c *rat, count int, str string) { func check(U PS, c *rat, count int, str string) {
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
r := get(U); r := get(U)
if !r.eq(c) { if !r.eq(c) {
print("got: "); print("got: ")
r.pr(); r.pr()
print("should get "); print("should get ")
c.pr(); c.pr()
print("\n"); print("\n")
panic(str) panic(str)
} }
} }
...@@ -642,82 +641,82 @@ func check(U PS, c *rat, count int, str string) { ...@@ -642,82 +641,82 @@ func check(U PS, c *rat, count int, str string) {
const N=10 const N=10
func checka(U PS, a []*rat, str string) { func checka(U PS, a []*rat, str string) {
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
check(U, a[i], 1, str); check(U, a[i], 1, str)
} }
} }
func main() { func main() {
Init(); Init()
if len(os.Args) > 1 { // print if len(os.Args) > 1 { // print
print("Ones: "); Printn(Ones, 10); print("Ones: "); Printn(Ones, 10)
print("Twos: "); Printn(Twos, 10); print("Twos: "); Printn(Twos, 10)
print("Add: "); Printn(Add(Ones, Twos), 10); print("Add: "); Printn(Add(Ones, Twos), 10)
print("Diff: "); Printn(Diff(Ones), 10); print("Diff: "); Printn(Diff(Ones), 10)
print("Integ: "); Printn(Integ(zero, Ones), 10); print("Integ: "); Printn(Integ(zero, Ones), 10)
print("CMul: "); Printn(Cmul(neg(one), Ones), 10); print("CMul: "); Printn(Cmul(neg(one), Ones), 10)
print("Sub: "); Printn(Sub(Ones, Twos), 10); print("Sub: "); Printn(Sub(Ones, Twos), 10)
print("Mul: "); Printn(Mul(Ones, Ones), 10); print("Mul: "); Printn(Mul(Ones, Ones), 10)
print("Exp: "); Printn(Exp(Ones), 15); print("Exp: "); Printn(Exp(Ones), 15)
print("MonSubst: "); Printn(MonSubst(Ones, neg(one), 2), 10); print("MonSubst: "); Printn(MonSubst(Ones, neg(one), 2), 10)
print("ATan: "); Printn(Integ(zero, MonSubst(Ones, neg(one), 2)), 10); print("ATan: "); Printn(Integ(zero, MonSubst(Ones, neg(one), 2)), 10)
} else { // test } else { // test
check(Ones, one, 5, "Ones"); check(Ones, one, 5, "Ones")
check(Add(Ones, Ones), itor(2), 0, "Add Ones Ones"); // 1 1 1 1 1 check(Add(Ones, Ones), itor(2), 0, "Add Ones Ones") // 1 1 1 1 1
check(Add(Ones, Twos), itor(3), 0, "Add Ones Twos"); // 3 3 3 3 3 check(Add(Ones, Twos), itor(3), 0, "Add Ones Twos") // 3 3 3 3 3
a := make([]*rat, N); a := make([]*rat, N)
d := Diff(Ones); d := Diff(Ones)
for i:=0; i < N; i++ { for i:=0; i < N; i++ {
a[i] = itor(int64(i+1)) a[i] = itor(int64(i+1))
} }
checka(d, a, "Diff"); // 1 2 3 4 5 checka(d, a, "Diff") // 1 2 3 4 5
in := Integ(zero, Ones); in := Integ(zero, Ones)
a[0] = zero; // integration constant a[0] = zero // integration constant
for i:=1; i < N; i++ { for i:=1; i < N; i++ {
a[i] = i2tor(1, int64(i)) a[i] = i2tor(1, int64(i))
} }
checka(in, a, "Integ"); // 0 1 1/2 1/3 1/4 1/5 checka(in, a, "Integ") // 0 1 1/2 1/3 1/4 1/5
check(Cmul(neg(one), Twos), itor(-2), 10, "CMul"); // -1 -1 -1 -1 -1 check(Cmul(neg(one), Twos), itor(-2), 10, "CMul") // -1 -1 -1 -1 -1
check(Sub(Ones, Twos), itor(-1), 0, "Sub Ones Twos"); // -1 -1 -1 -1 -1 check(Sub(Ones, Twos), itor(-1), 0, "Sub Ones Twos") // -1 -1 -1 -1 -1
m := Mul(Ones, Ones); m := Mul(Ones, Ones)
for i:=0; i < N; i++ { for i:=0; i < N; i++ {
a[i] = itor(int64(i+1)) a[i] = itor(int64(i+1))
} }
checka(m, a, "Mul"); // 1 2 3 4 5 checka(m, a, "Mul") // 1 2 3 4 5
e := Exp(Ones); e := Exp(Ones)
a[0] = itor(1); a[0] = itor(1)
a[1] = itor(1); a[1] = itor(1)
a[2] = i2tor(3,2); a[2] = i2tor(3,2)
a[3] = i2tor(13,6); a[3] = i2tor(13,6)
a[4] = i2tor(73,24); a[4] = i2tor(73,24)
a[5] = i2tor(167,40); a[5] = i2tor(167,40)
a[6] = i2tor(4051,720); a[6] = i2tor(4051,720)
a[7] = i2tor(37633,5040); a[7] = i2tor(37633,5040)
a[8] = i2tor(43817,4480); a[8] = i2tor(43817,4480)
a[9] = i2tor(4596553,362880); a[9] = i2tor(4596553,362880)
checka(e, a, "Exp"); // 1 1 3/2 13/6 73/24 checka(e, a, "Exp") // 1 1 3/2 13/6 73/24
at := Integ(zero, MonSubst(Ones, neg(one), 2)); at := Integ(zero, MonSubst(Ones, neg(one), 2))
for c, i := 1, 0; i < N; i++ { for c, i := 1, 0; i < N; i++ {
if i%2 == 0 { if i%2 == 0 {
a[i] = zero a[i] = zero
} else { } else {
a[i] = i2tor(int64(c), int64(i)); a[i] = i2tor(int64(c), int64(i))
c *= -1 c *= -1
} }
} }
checka(at, a, "ATan"); // 0 -1 0 -1/3 0 -1/5 checka(at, a, "ATan"); // 0 -1 0 -1/3 0 -1/5
/* /*
t := Revert(Integ(zero, MonSubst(Ones, neg(one), 2))); t := Revert(Integ(zero, MonSubst(Ones, neg(one), 2)))
a[0] = zero; a[0] = zero
a[1] = itor(1); a[1] = itor(1)
a[2] = zero; a[2] = zero
a[3] = i2tor(1,3); a[3] = i2tor(1,3)
a[4] = zero; a[4] = zero
a[5] = i2tor(2,15); a[5] = i2tor(2,15)
a[6] = zero; a[6] = zero
a[7] = i2tor(17,315); a[7] = i2tor(17,315)
a[8] = zero; a[8] = zero
a[9] = i2tor(62,2835); a[9] = i2tor(62,2835)
checka(t, a, "Tan"); // 0 1 0 1/3 0 2/15 checka(t, a, "Tan") // 0 1 0 1/3 0 2/15
*/ */
} }
} }
...@@ -32,13 +32,12 @@ func main() { ...@@ -32,13 +32,12 @@ func main() {
'\ubabe' + '\ubabe' +
'\U0010FFFF' + '\U0010FFFF' +
'\U000ebabe' '\U000ebabe'
;
if '\U000ebabe' != 0x000ebabe { if '\U000ebabe' != 0x000ebabe {
print("ebabe wrong\n"); print("ebabe wrong\n")
os.Exit(1) os.Exit(1)
} }
if i != 0x20e213 { if i != 0x20e213 {
print("number is ", i, " should be ", 0x20e213, "\n"); print("number is ", i, " should be ", 0x20e213, "\n")
os.Exit(1) os.Exit(1)
} }
} }
...@@ -12,13 +12,13 @@ ...@@ -12,13 +12,13 @@
package main package main
type Chan interface { type Chan interface {
Send(int); Send(int)
Nbsend(int) bool; Nbsend(int) bool
Recv() int; Recv() int
Nbrecv() (int, bool); Nbrecv() (int, bool)
Close(); Close()
Closed() bool; Closed() bool
Impl() string; Impl() string
} }
// direct channel operations // direct channel operations
...@@ -28,7 +28,7 @@ func (c XChan) Send(x int) { ...@@ -28,7 +28,7 @@ func (c XChan) Send(x int) {
} }
func (c XChan) Nbsend(x int) bool { func (c XChan) Nbsend(x int) bool {
return c <- x; return c <- x
} }
func (c XChan) Recv() int { func (c XChan) Recv() int {
...@@ -36,8 +36,8 @@ func (c XChan) Recv() int { ...@@ -36,8 +36,8 @@ func (c XChan) Recv() int {
} }
func (c XChan) Nbrecv() (int, bool) { func (c XChan) Nbrecv() (int, bool) {
x, ok := <-c; x, ok := <-c
return x, ok; return x, ok
} }
func (c XChan) Close() { func (c XChan) Close() {
...@@ -63,29 +63,29 @@ func (c SChan) Send(x int) { ...@@ -63,29 +63,29 @@ func (c SChan) Send(x int) {
func (c SChan) Nbsend(x int) bool { func (c SChan) Nbsend(x int) bool {
select { select {
case c <- x: case c <- x:
return true; return true
default: default:
return false; return false
} }
panic("nbsend"); panic("nbsend")
} }
func (c SChan) Recv() int { func (c SChan) Recv() int {
select { select {
case x := <-c: case x := <-c:
return x; return x
} }
panic("recv"); panic("recv")
} }
func (c SChan) Nbrecv() (int, bool) { func (c SChan) Nbrecv() (int, bool) {
select { select {
case x := <-c: case x := <-c:
return x, true; return x, true
default: default:
return 0, false; return 0, false
} }
panic("nbrecv"); panic("nbrecv")
} }
func (c SChan) Close() { func (c SChan) Close() {
...@@ -97,101 +97,101 @@ func (c SChan) Closed() bool { ...@@ -97,101 +97,101 @@ func (c SChan) Closed() bool {
} }
func (c SChan) Impl() string { func (c SChan) Impl() string {
return "(select)"; return "(select)"
} }
func test1(c Chan) { func test1(c Chan) {
// not closed until the close signal (a zero value) has been received. // not closed until the close signal (a zero value) has been received.
if c.Closed() { if c.Closed() {
println("test1: Closed before Recv zero:", c.Impl()); println("test1: Closed before Recv zero:", c.Impl())
} }
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
// recv a close signal (a zero value) // recv a close signal (a zero value)
if x := c.Recv(); x != 0 { if x := c.Recv(); x != 0 {
println("test1: recv on closed got non-zero:", x, c.Impl()); println("test1: recv on closed got non-zero:", x, c.Impl())
} }
// should now be closed. // should now be closed.
if !c.Closed() { if !c.Closed() {
println("test1: not closed after recv zero", c.Impl()); println("test1: not closed after recv zero", c.Impl())
} }
// should work with ,ok: received a value without blocking, so ok == true. // should work with ,ok: received a value without blocking, so ok == true.
x, ok := c.Nbrecv(); x, ok := c.Nbrecv()
if !ok { if !ok {
println("test1: recv on closed got not ok", c.Impl()); println("test1: recv on closed got not ok", c.Impl())
} }
if x != 0 { if x != 0 {
println("test1: recv ,ok on closed got non-zero:", x, c.Impl()); println("test1: recv ,ok on closed got non-zero:", x, c.Impl())
} }
} }
// send should work with ,ok too: sent a value without blocking, so ok == true. // send should work with ,ok too: sent a value without blocking, so ok == true.
ok := c.Nbsend(1); ok := c.Nbsend(1)
if !ok { if !ok {
println("test1: send on closed got not ok", c.Impl()); println("test1: send on closed got not ok", c.Impl())
} }
// but the value should have been discarded. // but the value should have been discarded.
if x := c.Recv(); x != 0 { if x := c.Recv(); x != 0 {
println("test1: recv on closed got non-zero after send on closed:", x, c.Impl()); println("test1: recv on closed got non-zero after send on closed:", x, c.Impl())
} }
// similarly Send. // similarly Send.
c.Send(2); c.Send(2)
if x := c.Recv(); x != 0 { if x := c.Recv(); x != 0 {
println("test1: recv on closed got non-zero after send on closed:", x, c.Impl()); println("test1: recv on closed got non-zero after send on closed:", x, c.Impl())
} }
} }
func testasync1(c Chan) { func testasync1(c Chan) {
// not closed until the close signal (a zero value) has been received. // not closed until the close signal (a zero value) has been received.
if c.Closed() { if c.Closed() {
println("testasync1: Closed before Recv zero:", c.Impl()); println("testasync1: Closed before Recv zero:", c.Impl())
} }
// should be able to get the last value via Recv // should be able to get the last value via Recv
if x := c.Recv(); x != 1 { if x := c.Recv(); x != 1 {
println("testasync1: Recv did not get 1:", x, c.Impl()); println("testasync1: Recv did not get 1:", x, c.Impl())
} }
test1(c); test1(c)
} }
func testasync2(c Chan) { func testasync2(c Chan) {
// not closed until the close signal (a zero value) has been received. // not closed until the close signal (a zero value) has been received.
if c.Closed() { if c.Closed() {
println("testasync2: Closed before Recv zero:", c.Impl()); println("testasync2: Closed before Recv zero:", c.Impl())
} }
// should be able to get the last value via Nbrecv // should be able to get the last value via Nbrecv
if x, ok := c.Nbrecv(); !ok || x != 1 { if x, ok := c.Nbrecv(); !ok || x != 1 {
println("testasync2: Nbrecv did not get 1, true:", x, ok, c.Impl()); println("testasync2: Nbrecv did not get 1, true:", x, ok, c.Impl())
} }
test1(c); test1(c)
} }
func closedsync() chan int { func closedsync() chan int {
c := make(chan int); c := make(chan int)
close(c); close(c)
return c; return c
} }
func closedasync() chan int { func closedasync() chan int {
c := make(chan int, 2); c := make(chan int, 2)
c <- 1; c <- 1
close(c); close(c)
return c; return c
} }
func main() { func main() {
test1(XChan(closedsync())); test1(XChan(closedsync()))
test1(SChan(closedsync())); test1(SChan(closedsync()))
testasync1(XChan(closedasync())); testasync1(XChan(closedasync()))
testasync1(SChan(closedasync())); testasync1(SChan(closedasync()))
testasync2(XChan(closedasync())); testasync2(XChan(closedasync()))
testasync2(SChan(closedasync())); testasync2(SChan(closedasync()))
} }
...@@ -9,7 +9,7 @@ package main ...@@ -9,7 +9,7 @@ package main
func use(bool) { } func use(bool) { }
func main() { func main() {
var a []int; var a []int
var ia interface{} = a; var ia interface{} = a
use(ia == ia); use(ia == ia)
} }
...@@ -9,7 +9,7 @@ package main ...@@ -9,7 +9,7 @@ package main
func use(bool) { } func use(bool) { }
func main() { func main() {
var b []int; var b []int
var ib interface{} = b; var ib interface{} = b
use(ib == ib); use(ib == ib)
} }
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
package main package main
func main() { func main() {
var a []int; var a []int
var ia interface{} = a; var ia interface{} = a
var m = make(map[interface{}] int); var m = make(map[interface{}] int)
m[ia] = 1; m[ia] = 1
} }
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
package main package main
func main() { func main() {
var b []int; var b []int
var ib interface{} = b; var ib interface{} = b
var m = make(map[interface{}] int); var m = make(map[interface{}] int)
m[ib] = 1; m[ib] = 1
} }
...@@ -11,9 +11,9 @@ type T struct { i int; f float; s string; next *T } ...@@ -11,9 +11,9 @@ type T struct { i int; f float; s string; next *T }
type R struct { num int } type R struct { num int }
func itor(a int) *R { func itor(a int) *R {
r := new(R); r := new(R)
r.num = a; r.num = a
return r; return r
} }
func eq(a []*R) { func eq(a []*R) {
...@@ -22,49 +22,49 @@ func eq(a []*R) { ...@@ -22,49 +22,49 @@ func eq(a []*R) {
} }
} }
type P struct { a, b int }; type P struct { a, b int }
func NewP(a, b int) *P { func NewP(a, b int) *P {
return &P{a, b} return &P{a, b}
} }
func main() { func main() {
var t T; var t T
t = T{0, 7.2, "hi", &t}; t = T{0, 7.2, "hi", &t}
var tp *T; var tp *T
tp = &T{0, 7.2, "hi", &t}; tp = &T{0, 7.2, "hi", &t}
a1 := []int{1,2,3}; a1 := []int{1,2,3}
if len(a1) != 3 { panic("a1") } if len(a1) != 3 { panic("a1") }
a2 := [10]int{1,2,3}; a2 := [10]int{1,2,3}
if len(a2) != 10 || cap(a2) != 10 { panic("a2") } if len(a2) != 10 || cap(a2) != 10 { panic("a2") }
a3 := [10]int{1,2,3,}; a3 := [10]int{1,2,3,}
if len(a3) != 10 || a2[3] != 0 { panic("a3") } if len(a3) != 10 || a2[3] != 0 { panic("a3") }
var oai []int; var oai []int
oai = []int{1,2,3}; oai = []int{1,2,3}
if len(oai) != 3 { panic("oai") } if len(oai) != 3 { panic("oai") }
at := [...]*T{&t, tp, &t}; at := [...]*T{&t, tp, &t}
if len(at) != 3 { panic("at") } if len(at) != 3 { panic("at") }
c := make(chan int); c := make(chan int)
ac := []chan int{c, c, c}; ac := []chan int{c, c, c}
if len(ac) != 3 { panic("ac") } if len(ac) != 3 { panic("ac") }
aat := [][len(at)]*T{at, at}; aat := [][len(at)]*T{at, at}
if len(aat) != 2 || len(aat[1]) != 3 { panic("aat") } if len(aat) != 2 || len(aat[1]) != 3 { panic("aat") }
s := string([]byte{'h', 'e', 'l', 'l', 'o'}); s := string([]byte{'h', 'e', 'l', 'l', 'o'})
if s != "hello" { panic("s") } if s != "hello" { panic("s") }
m := map[string]float{"one":1.0, "two":2.0, "pi":22./7.}; m := map[string]float{"one":1.0, "two":2.0, "pi":22./7.}
if len(m) != 3 { panic("m") } if len(m) != 3 { panic("m") }
eq([]*R{itor(0), itor(1), itor(2), itor(3), itor(4), itor(5)}); eq([]*R{itor(0), itor(1), itor(2), itor(3), itor(4), itor(5)})
p1 := NewP(1, 2); p1 := NewP(1, 2)
p2 := NewP(1, 2); p2 := NewP(1, 2)
if p1 == p2 { panic("NewP") } if p1 == p2 { panic("NewP") }
} }
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
package main package main
type T struct { type T struct {
int; int
} }
func f() *T { func f() *T {
...@@ -15,9 +15,9 @@ func f() *T { ...@@ -15,9 +15,9 @@ func f() *T {
} }
func main() { func main() {
x := f(); x := f()
y := f(); y := f()
if x == y { if x == y {
panic("not allocating & composite literals"); panic("not allocating & composite literals")
} }
} }
...@@ -7,26 +7,26 @@ ...@@ -7,26 +7,26 @@
package main package main
const ( const (
c0 = 0; c0 = 0
cm1 = -1; cm1 = -1
chuge = 1 << 100; chuge = 1 << 100
chuge_1 = chuge - 1; chuge_1 = chuge - 1
c1 = chuge >> 100; c1 = chuge >> 100
c3div2 = 3/2; c3div2 = 3/2
c1e3 = 1e3; c1e3 = 1e3
ctrue = true; ctrue = true
cfalse = !ctrue; cfalse = !ctrue
) )
const ( const (
f0 = 0.0; f0 = 0.0
fm1 = -1.; fm1 = -1.
fhuge float64 = 1 << 100; fhuge float64 = 1 << 100
fhuge_1 float64 = chuge - 1; fhuge_1 float64 = chuge - 1
f1 float64 = chuge >> 100; f1 float64 = chuge >> 100
f3div2 = 3./2.; f3div2 = 3./2.
f1e3 float64 = 1e3; f1e3 float64 = 1e3
) )
func assert(t bool, s string) { func assert(t bool, s string) {
...@@ -36,85 +36,85 @@ func assert(t bool, s string) { ...@@ -36,85 +36,85 @@ func assert(t bool, s string) {
} }
func ints() { func ints() {
assert(c0 == 0, "c0"); assert(c0 == 0, "c0")
assert(c1 == 1, "c1"); assert(c1 == 1, "c1")
assert(chuge > chuge_1, "chuge"); assert(chuge > chuge_1, "chuge")
assert(chuge_1 + 1 == chuge, "chuge 1"); assert(chuge_1 + 1 == chuge, "chuge 1")
assert(chuge + cm1 +1 == chuge, "cm1"); assert(chuge + cm1 +1 == chuge, "cm1")
assert(c3div2 == 1, "3/2"); assert(c3div2 == 1, "3/2")
assert(c1e3 == 1000, "c1e3 int"); assert(c1e3 == 1000, "c1e3 int")
assert(c1e3 == 1e3, "c1e3 float"); assert(c1e3 == 1e3, "c1e3 float")
// verify that all (in range) are assignable as ints // verify that all (in range) are assignable as ints
var i int; var i int
i = c0; i = c0
assert(i == c0, "i == c0"); assert(i == c0, "i == c0")
i = cm1; i = cm1
assert(i == cm1, "i == cm1"); assert(i == cm1, "i == cm1")
i = c1; i = c1
assert(i == c1, "i == c1"); assert(i == c1, "i == c1")
i = c3div2; i = c3div2
assert(i == c3div2, "i == c3div2"); assert(i == c3div2, "i == c3div2")
i = c1e3; i = c1e3
assert(i == c1e3, "i == c1e3"); assert(i == c1e3, "i == c1e3")
// verify that all are assignable as floats // verify that all are assignable as floats
var f float64; var f float64
f = c0; f = c0
assert(f == c0, "f == c0"); assert(f == c0, "f == c0")
f = cm1; f = cm1
assert(f == cm1, "f == cm1"); assert(f == cm1, "f == cm1")
f = chuge; f = chuge
assert(f == chuge, "f == chuge"); assert(f == chuge, "f == chuge")
f = chuge_1; f = chuge_1
assert(f == chuge_1, "f == chuge_1"); assert(f == chuge_1, "f == chuge_1")
f = c1; f = c1
assert(f == c1, "f == c1"); assert(f == c1, "f == c1")
f = c3div2; f = c3div2
assert(f == c3div2, "f == c3div2"); assert(f == c3div2, "f == c3div2")
f = c1e3; f = c1e3
assert(f == c1e3, "f == c1e3"); assert(f == c1e3, "f == c1e3")
} }
func floats() { func floats() {
assert(f0 == c0, "f0"); assert(f0 == c0, "f0")
assert(f1 == c1, "f1"); assert(f1 == c1, "f1")
assert(fhuge == fhuge_1, "fhuge"); // float64 can't distinguish fhuge, fhuge_1. assert(fhuge == fhuge_1, "fhuge") // float64 can't distinguish fhuge, fhuge_1.
assert(fhuge_1 + 1 == fhuge, "fhuge 1"); assert(fhuge_1 + 1 == fhuge, "fhuge 1")
assert(fhuge + fm1 +1 == fhuge, "fm1"); assert(fhuge + fm1 +1 == fhuge, "fm1")
assert(f3div2 == 1.5, "3./2."); assert(f3div2 == 1.5, "3./2.")
assert(f1e3 == 1000, "f1e3 int"); assert(f1e3 == 1000, "f1e3 int")
assert(f1e3 == 1.e3, "f1e3 float"); assert(f1e3 == 1.e3, "f1e3 float")
// verify that all (in range) are assignable as ints // verify that all (in range) are assignable as ints
var i int; var i int
i = f0; i = f0
assert(i == f0, "i == f0"); assert(i == f0, "i == f0")
i = fm1; i = fm1
assert(i == fm1, "i == fm1"); assert(i == fm1, "i == fm1")
// verify that all are assignable as floats // verify that all are assignable as floats
var f float64; var f float64
f = f0; f = f0
assert(f == f0, "f == f0"); assert(f == f0, "f == f0")
f = fm1; f = fm1
assert(f == fm1, "f == fm1"); assert(f == fm1, "f == fm1")
f = fhuge; f = fhuge
assert(f == fhuge, "f == fhuge"); assert(f == fhuge, "f == fhuge")
f = fhuge_1; f = fhuge_1
assert(f == fhuge_1, "f == fhuge_1"); assert(f == fhuge_1, "f == fhuge_1")
f = f1; f = f1
assert(f == f1, "f == f1"); assert(f == f1, "f == f1")
f = f3div2; f = f3div2
assert(f == f3div2, "f == f3div2"); assert(f == f3div2, "f == f3div2")
f = f1e3; f = f1e3
assert(f == f1e3, "f == f1e3"); assert(f == f1e3, "f == f1e3")
} }
func main() { func main() {
ints(); ints()
floats(); floats()
assert(ctrue == true, "ctrue == true"); assert(ctrue == true, "ctrue == true")
assert(cfalse == false, "cfalse == false"); assert(cfalse == false, "cfalse == false")
} }
...@@ -9,71 +9,71 @@ package main ...@@ -9,71 +9,71 @@ package main
type I interface {} type I interface {}
const ( const (
// assume all types behave similarly to int8/uint8 // assume all types behave similarly to int8/uint8
Int8 int8 = 101; Int8 int8 = 101
Minus1 int8 = -1; Minus1 int8 = -1
Uint8 uint8 = 102; Uint8 uint8 = 102
Const = 103; Const = 103
Float32 float32 = 104.5; Float32 float32 = 104.5
Float float = 105.5; Float float = 105.5
ConstFloat = 106.5; ConstFloat = 106.5
Big float64 = 1e300; Big float64 = 1e300
String = "abc"; String = "abc"
Bool = true; Bool = true
) )
var ( var (
a1 = Int8 * 100; // ERROR "overflow" a1 = Int8 * 100 // ERROR "overflow"
a2 = Int8 * -1; // OK a2 = Int8 * -1 // OK
a3 = Int8 * 1000; // ERROR "overflow" a3 = Int8 * 1000 // ERROR "overflow"
a4 = Int8 * int8(1000); // ERROR "overflow" a4 = Int8 * int8(1000) // ERROR "overflow"
a5 = int8(Int8 * 1000); // ERROR "overflow" a5 = int8(Int8 * 1000) // ERROR "overflow"
a6 = int8(Int8 * int8(1000)); // ERROR "overflow" a6 = int8(Int8 * int8(1000)) // ERROR "overflow"
a7 = Int8 - 2*Int8 - 2*Int8; // ERROR "overflow" a7 = Int8 - 2*Int8 - 2*Int8 // ERROR "overflow"
a8 = Int8 * Const / 100; // ERROR "overflow" a8 = Int8 * Const / 100 // ERROR "overflow"
a9 = Int8 * (Const / 100); // OK a9 = Int8 * (Const / 100) // OK
b1 = Uint8 * Uint8; // ERROR "overflow" b1 = Uint8 * Uint8 // ERROR "overflow"
b2 = Uint8 * -1; // ERROR "overflow" b2 = Uint8 * -1 // ERROR "overflow"
b3 = Uint8 - Uint8; // OK b3 = Uint8 - Uint8 // OK
b4 = Uint8 - Uint8 - Uint8; // ERROR "overflow" b4 = Uint8 - Uint8 - Uint8 // ERROR "overflow"
b5 = uint8(^0); // ERROR "overflow" b5 = uint8(^0) // ERROR "overflow"
b6 = ^uint8(0); // OK b6 = ^uint8(0) // OK
b7 = uint8(Minus1); // ERROR "overflow" b7 = uint8(Minus1) // ERROR "overflow"
b8 = uint8(int8(-1)); // ERROR "overflow" b8 = uint8(int8(-1)) // ERROR "overflow"
b8a = uint8(-1); // ERROR "overflow" b8a = uint8(-1) // ERROR "overflow"
b9 byte = (1<<10) >> 8; // OK b9 byte = (1<<10) >> 8 // OK
b10 byte = (1<<10); // ERROR "overflow" b10 byte = (1<<10) // ERROR "overflow"
b11 byte = (byte(1)<<10) >> 8; // ERROR "overflow" b11 byte = (byte(1)<<10) >> 8 // ERROR "overflow"
b12 byte = 1000; // ERROR "overflow" b12 byte = 1000 // ERROR "overflow"
b13 byte = byte(1000); // ERROR "overflow" b13 byte = byte(1000) // ERROR "overflow"
b14 byte = byte(100) * byte(100); // ERROR "overflow" b14 byte = byte(100) * byte(100) // ERROR "overflow"
b15 byte = byte(100) * 100; // ERROR "overflow" b15 byte = byte(100) * 100 // ERROR "overflow"
b16 byte = byte(0) * 1000; // ERROR "overflow" b16 byte = byte(0) * 1000 // ERROR "overflow"
b16a byte = 0 * 1000; // OK b16a byte = 0 * 1000 // OK
b17 byte = byte(0) * byte(1000); // ERROR "overflow" b17 byte = byte(0) * byte(1000) // ERROR "overflow"
b18 byte = Uint8/0; // ERROR "division by zero" b18 byte = Uint8/0 // ERROR "division by zero"
c1 float64 = Big; c1 float64 = Big
c2 float64 = Big*Big; // ERROR "overflow" c2 float64 = Big*Big // ERROR "overflow"
c3 float64 = float64(Big)*Big; // ERROR "overflow" c3 float64 = float64(Big)*Big // ERROR "overflow"
c4 = Big*Big; // ERROR "overflow" c4 = Big*Big // ERROR "overflow"
c5 = Big/0; // ERROR "division by zero" c5 = Big/0 // ERROR "division by zero"
) )
func f(int); func f(int)
func main() { func main() {
f(Int8); // ERROR "convert|wrong type|cannot" f(Int8) // ERROR "convert|wrong type|cannot"
f(Minus1); // ERROR "convert|wrong type|cannot" f(Minus1) // ERROR "convert|wrong type|cannot"
f(Uint8); // ERROR "convert|wrong type|cannot" f(Uint8) // ERROR "convert|wrong type|cannot"
f(Const); // OK f(Const) // OK
f(Float32); // ERROR "convert|wrong type|cannot" f(Float32) // ERROR "convert|wrong type|cannot"
f(Float); // ERROR "convert|wrong type|cannot" f(Float) // ERROR "convert|wrong type|cannot"
f(ConstFloat); // ERROR "truncate" f(ConstFloat) // ERROR "truncate"
f(ConstFloat - 0.5); // OK f(ConstFloat - 0.5) // OK
f(Big); // ERROR "convert|wrong type|cannot" f(Big) // ERROR "convert|wrong type|cannot"
f(String); // ERROR "convert|wrong type|cannot|incompatible" f(String) // ERROR "convert|wrong type|cannot|incompatible"
f(Bool); // ERROR "convert|wrong type|cannot|incompatible" f(Bool) // ERROR "convert|wrong type|cannot|incompatible"
} }
...@@ -7,6 +7,6 @@ ...@@ -7,6 +7,6 @@
package main package main
const ( const (
A int = 1; A int = 1
B byte; // ERROR "type without expr|expected .=." B byte; // ERROR "type without expr|expected .=."
) )
...@@ -9,31 +9,31 @@ package main ...@@ -9,31 +9,31 @@ package main
// explicit conversion of constants is work in progress. // explicit conversion of constants is work in progress.
// the ERRORs in this block are debatable, but they're what // the ERRORs in this block are debatable, but they're what
// the language spec says for now. // the language spec says for now.
var x1 = string(1); var x1 = string(1)
var x2 string = string(1); var x2 string = string(1)
var x3 = int(1.5); // ERROR "convert|truncate" var x3 = int(1.5) // ERROR "convert|truncate"
var x4 int = int(1.5); // ERROR "convert|truncate" var x4 int = int(1.5) // ERROR "convert|truncate"
var x5 = "a" + string(1); var x5 = "a" + string(1)
var x6 = int(1e100); // ERROR "overflow" var x6 = int(1e100) // ERROR "overflow"
var x7 = float(1e1000); // ERROR "overflow" var x7 = float(1e1000) // ERROR "overflow"
// implicit conversions merit scrutiny // implicit conversions merit scrutiny
var s string; var s string
var bad1 string = 1; // ERROR "conver|incompatible|invalid|cannot" var bad1 string = 1 // ERROR "conver|incompatible|invalid|cannot"
var bad2 = s + 1; // ERROR "conver|incompatible|invalid" var bad2 = s + 1 // ERROR "conver|incompatible|invalid"
var bad3 = s + 'a'; // ERROR "conver|incompatible|invalid" var bad3 = s + 'a' // ERROR "conver|incompatible|invalid"
var bad4 = "a" + 1; // ERROR "literals|incompatible|convert|invalid" var bad4 = "a" + 1 // ERROR "literals|incompatible|convert|invalid"
var bad5 = "a" + 'a'; // ERROR "literals|incompatible|convert|invalid" var bad5 = "a" + 'a' // ERROR "literals|incompatible|convert|invalid"
var bad6 int = 1.5; // ERROR "convert|truncate" var bad6 int = 1.5 // ERROR "convert|truncate"
var bad7 int = 1e100; // ERROR "overflow" var bad7 int = 1e100 // ERROR "overflow"
var bad8 float32 = 1e200; // ERROR "overflow" var bad8 float32 = 1e200 // ERROR "overflow"
// but these implicit conversions are okay // but these implicit conversions are okay
var good1 string = "a"; var good1 string = "a"
var good2 int = 1.0; var good2 int = 1.0
var good3 int = 1e9; var good3 int = 1e9
var good4 float = 1e20; var good4 float = 1e20
// explicit conversion of string is okay // explicit conversion of string is okay
var _ = []int("abc") var _ = []int("abc")
......
...@@ -13,28 +13,28 @@ func f2() (float, int) { return 1, 2 } ...@@ -13,28 +13,28 @@ func f2() (float, int) { return 1, 2 }
func f3() (float, int, string) { return 1, 2, "3" } func f3() (float, int, string) { return 1, 2, "3" }
func x() (s string) { func x() (s string) {
a, b, s := f3(); a, b, s := f3()
_, _ = a, b; _, _ = a, b
return // tests that result var is in scope for redeclaration return // tests that result var is in scope for redeclaration
} }
func main() { func main() {
i, f, s := f3(); i, f, s := f3()
j, f := f2(); // redeclare f j, f := f2() // redeclare f
k := f1(); k := f1()
m, g, s := f3(); m, g, s := f3()
m, h, s := f3(); m, h, s := f3()
{ {
// new block should be ok. // new block should be ok.
i, f, s := f3(); i, f, s := f3()
j, f := f2(); // redeclare f j, f := f2() // redeclare f
k := f1(); k := f1()
m, g, s := f3(); m, g, s := f3()
m, h, s := f3(); m, h, s := f3()
_, _, _, _, _, _, _, _, _ = i, f, s, j, k, m, g, s, h; _, _, _, _, _, _, _, _, _ = i, f, s, j, k, m, g, s, h
} }
if x() != "3" { if x() != "3" {
println("x() failed"); println("x() failed")
} }
_, _, _, _, _, _, _, _, _ = i, f, s, j, k, m, g, s, h; _, _, _, _, _, _, _, _, _ = i, f, s, j, k, m, g, s, h
} }
...@@ -15,44 +15,44 @@ func f3() (float, int, string) { return 1, 2, "3" } ...@@ -15,44 +15,44 @@ func f3() (float, int, string) { return 1, 2, "3" }
func main() { func main() {
{ {
// simple redeclaration // simple redeclaration
i := f1(); i := f1()
i := f1(); // ERROR "redeclared|no new" i := f1() // ERROR "redeclared|no new"
_ = i; _ = i
} }
{ {
// change of type for f // change of type for f
i, f, s := f3(); i, f, s := f3()
f, g, t := f3(); // ERROR "redeclared|cannot assign|incompatible" f, g, t := f3() // ERROR "redeclared|cannot assign|incompatible"
_, _, _, _, _ = i, f, s, g, t; _, _, _, _, _ = i, f, s, g, t
} }
{ {
// change of type for i // change of type for i
i, f, s := f3(); i, f, s := f3()
j, i, t := f3(); // ERROR "redeclared|cannot assign|incompatible" j, i, t := f3() // ERROR "redeclared|cannot assign|incompatible"
_, _, _, _, _ = i, f, s, j, t; _, _, _, _, _ = i, f, s, j, t
} }
{ {
// no new variables // no new variables
i, f, s := f3(); i, f, s := f3()
i, f := f2(); // ERROR "redeclared|no new" i, f := f2() // ERROR "redeclared|no new"
_, _, _ = i, f, s; _, _, _ = i, f, s
} }
{ {
// single redeclaration // single redeclaration
i, f, s := f3(); i, f, s := f3()
i := f1(); // ERROR "redeclared|no new|incompatible" i := f1() // ERROR "redeclared|no new|incompatible"
_, _, _ = i, f, s; _, _, _ = i, f, s
} }
// double redeclaration // double redeclaration
{ {
i, f, s := f3(); i, f, s := f3()
i, f := f2(); // ERROR "redeclared|no new" i, f := f2() // ERROR "redeclared|no new"
_, _, _ = i, f, s; _, _, _ = i, f, s
} }
{ {
// triple redeclaration // triple redeclaration
i, f, s := f3(); i, f, s := f3()
i, f, s := f3(); // ERROR "redeclared|no new" i, f, s := f3() // ERROR "redeclared|no new"
_, _, _ = i, f, s; _, _, _ = i, f, s
} }
} }
...@@ -10,18 +10,18 @@ package main ...@@ -10,18 +10,18 @@ package main
import os "os" import os "os"
func main() { func main() {
ga, e0 := os.Getenverror("GOARCH"); ga, e0 := os.Getenverror("GOARCH")
if e0 != nil { if e0 != nil {
print("$GOARCH: ", e0.String(), "\n"); print("$GOARCH: ", e0.String(), "\n")
os.Exit(1); os.Exit(1)
} }
if ga != "amd64" && ga != "386" && ga != "arm" { if ga != "amd64" && ga != "386" && ga != "arm" {
print("$GOARCH=", ga, "\n"); print("$GOARCH=", ga, "\n")
os.Exit(1); os.Exit(1)
} }
xxx, e1 := os.Getenverror("DOES_NOT_EXIST"); xxx, e1 := os.Getenverror("DOES_NOT_EXIST")
if e1 != os.ENOENV { if e1 != os.ENOENV {
print("$DOES_NOT_EXIST=", xxx, "; err = ", e1.String(), "\n"); print("$DOES_NOT_EXIST=", xxx, "; err = ", e1.String(), "\n")
os.Exit(1); os.Exit(1)
} }
} }
...@@ -14,142 +14,142 @@ package main ...@@ -14,142 +14,142 @@ package main
var bad = false var bad = false
var allptr = make([]*int, 0, 100); var allptr = make([]*int, 0, 100)
func noalias(p, q *int, s string) { func noalias(p, q *int, s string) {
n := len(allptr); n := len(allptr)
*p = -(n+1); *p = -(n+1)
*q = -(n+2); *q = -(n+2)
allptr = allptr[0:n+2]; allptr = allptr[0:n+2]
allptr[n] = p; allptr[n] = p
allptr[n+1] = q; allptr[n+1] = q
n += 2; n += 2
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
if allptr[i] != nil && *allptr[i] != -(i+1) { if allptr[i] != nil && *allptr[i] != -(i+1) {
println("aliased pointers", -(i+1), *allptr[i], "after", s); println("aliased pointers", -(i+1), *allptr[i], "after", s)
allptr[i] = nil; allptr[i] = nil
bad = true; bad = true
} }
} }
} }
func val(p, q *int, v int, s string) { func val(p, q *int, v int, s string) {
if *p != v { if *p != v {
println("wrong value want", v, "got", *p, "after", s); println("wrong value want", v, "got", *p, "after", s)
bad = true; bad = true
} }
if *q != v+1 { if *q != v+1 {
println("wrong value want", v+1, "got", *q, "after", s); println("wrong value want", v+1, "got", *q, "after", s)
bad = true; bad = true
} }
} }
func chk(p, q *int, v int, s string) { func chk(p, q *int, v int, s string) {
val(p, q, v, s); val(p, q, v, s)
noalias(p, q, s); noalias(p, q, s)
} }
func chkalias(p, q *int, v int, s string) { func chkalias(p, q *int, v int, s string) {
if p != q { if p != q {
println("want aliased pointers but got different after", s); println("want aliased pointers but got different after", s)
} }
if *q != v+1 { if *q != v+1 {
println("wrong value want", v+1, "got", *q, "after", s); println("wrong value want", v+1, "got", *q, "after", s)
} }
} }
func i_escapes(x int) *int { func i_escapes(x int) *int {
var i int; var i int
i = x; i = x
return &i; return &i
} }
func j_escapes(x int) *int { func j_escapes(x int) *int {
var j int = x; var j int = x
j = x; j = x
return &j; return &j
} }
func k_escapes(x int) *int { func k_escapes(x int) *int {
k := x; k := x
return &k; return &k
} }
func in_escapes(x int) *int { func in_escapes(x int) *int {
return &x; return &x
} }
func send(c chan int, x int) { func send(c chan int, x int) {
c <- x; c <- x
} }
func select_escapes(x int) *int { func select_escapes(x int) *int {
c := make(chan int); c := make(chan int)
go send(c, x); go send(c, x)
select { select {
case req := <-c: case req := <-c:
return &req; return &req
} }
return nil; return nil
} }
func select_escapes1(x int, y int) (*int, *int) { func select_escapes1(x int, y int) (*int, *int) {
c := make(chan int); c := make(chan int)
var a [2]int; var a [2]int
var p [2]*int; var p [2]*int
a[0] = x; a[0] = x
a[1] = y; a[1] = y
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
go send(c, a[i]); go send(c, a[i])
select { select {
case req := <-c: case req := <-c:
p[i] = &req; p[i] = &req
} }
} }
return p[0], p[1] return p[0], p[1]
} }
func range_escapes(x int) *int { func range_escapes(x int) *int {
var a [1]int; var a [1]int
a[0] = x; a[0] = x
for _, v := range a { for _, v := range a {
return &v; return &v
} }
return nil; return nil
} }
// *is* aliased // *is* aliased
func range_escapes2(x, y int) (*int, *int) { func range_escapes2(x, y int) (*int, *int) {
var a [2]int; var a [2]int
var p [2]*int; var p [2]*int
a[0] = x; a[0] = x
a[1] = y; a[1] = y
for k, v := range a { for k, v := range a {
p[k] = &v; p[k] = &v
} }
return p[0], p[1] return p[0], p[1]
} }
// *is* aliased // *is* aliased
func for_escapes2(x int, y int) (*int, *int) { func for_escapes2(x int, y int) (*int, *int) {
var p [2]*int; var p [2]*int
n := 0; n := 0
for i := x; n < 2; i = y { for i := x; n < 2; i = y {
p[n] = &i; p[n] = &i
n++; n++
} }
return p[0], p[1] return p[0], p[1]
} }
func out_escapes(i int) (x int, p *int) { func out_escapes(i int) (x int, p *int) {
x = i x = i
p = &x; // ERROR "address of out parameter" p = &x // ERROR "address of out parameter"
return; return
} }
func out_escapes_2(i int) (x int, p *int) { func out_escapes_2(i int) (x int, p *int) {
x = i x = i
return x, &x; // ERROR "address of out parameter" return x, &x // ERROR "address of out parameter"
} }
func defer1(i int) (x int) { func defer1(i int) (x int) {
...@@ -160,40 +160,40 @@ func defer1(i int) (x int) { ...@@ -160,40 +160,40 @@ func defer1(i int) (x int) {
} }
func main() { func main() {
p, q := i_escapes(1), i_escapes(2); p, q := i_escapes(1), i_escapes(2)
chk(p, q, 1, "i_escapes"); chk(p, q, 1, "i_escapes")
p, q = j_escapes(3), j_escapes(4); p, q = j_escapes(3), j_escapes(4)
chk(p, q, 3, "j_escapes"); chk(p, q, 3, "j_escapes")
p, q = k_escapes(5), k_escapes(6); p, q = k_escapes(5), k_escapes(6)
chk(p, q, 5, "k_escapes"); chk(p, q, 5, "k_escapes")
p, q = in_escapes(7), in_escapes(8); p, q = in_escapes(7), in_escapes(8)
chk(p, q, 7, "in_escapes"); chk(p, q, 7, "in_escapes")
p, q = select_escapes(9), select_escapes(10); p, q = select_escapes(9), select_escapes(10)
chk(p, q, 9, "select_escapes"); chk(p, q, 9, "select_escapes")
p, q = select_escapes1(11, 12); p, q = select_escapes1(11, 12)
chk(p, q, 11, "select_escapes1"); chk(p, q, 11, "select_escapes1")
p, q = range_escapes(13), range_escapes(14); p, q = range_escapes(13), range_escapes(14)
chk(p, q, 13, "range_escapes"); chk(p, q, 13, "range_escapes")
p, q = range_escapes2(101, 102); p, q = range_escapes2(101, 102)
chkalias(p, q, 101, "range_escapes2"); chkalias(p, q, 101, "range_escapes2")
p, q = for_escapes2(103, 104); p, q = for_escapes2(103, 104)
chkalias(p, q, 103, "for_escapes2"); chkalias(p, q, 103, "for_escapes2")
_, p = out_escapes(15) _, p = out_escapes(15)
_, q = out_escapes(16); _, q = out_escapes(16)
chk(p, q, 15, "out_escapes"); chk(p, q, 15, "out_escapes")
_, p = out_escapes_2(17) _, p = out_escapes_2(17)
_, q = out_escapes_2(18); _, q = out_escapes_2(18)
chk(p, q, 17, "out_escapes_2"); chk(p, q, 17, "out_escapes_2")
x := defer1(20) x := defer1(20)
if x != 20 { if x != 20 {
...@@ -202,6 +202,6 @@ func main() { ...@@ -202,6 +202,6 @@ func main() {
} }
if bad { if bad {
panic("BUG: no escape"); panic("BUG: no escape")
} }
} }
...@@ -24,34 +24,34 @@ func ...@@ -24,34 +24,34 @@ func
pow10(pow int) float64 { pow10(pow int) float64 {
if pow < 0 { return 1/pow10(-pow); } if pow < 0 { return 1/pow10(-pow); }
if pow > 0 { return pow10(pow-1)*10; } if pow > 0 { return pow10(pow-1)*10; }
return 1; return 1
} }
func func
close(da float64, ia, ib int64, pow int) bool { close(da float64, ia, ib int64, pow int) bool {
db := float64(ia) / float64(ib); db := float64(ia) / float64(ib)
db *= pow10(pow); db *= pow10(pow)
if da == 0 || db == 0 { if da == 0 || db == 0 {
if da == 0 && db == 0 { if da == 0 && db == 0 {
return true; return true
} }
return false; return false
} }
de := (da-db) /da; de := (da-db) /da
if de < 0 { if de < 0 {
de = -de; de = -de
} }
if de < deLim { if de < deLim {
return true; return true
} }
if !bad { if !bad {
println("BUG") println("BUG")
bad = true bad = true
} }
return false; return false
} }
func func
......
...@@ -8,49 +8,49 @@ package main ...@@ -8,49 +8,49 @@ package main
func assertequal(is, shouldbe int, msg string) { func assertequal(is, shouldbe int, msg string) {
if is != shouldbe { if is != shouldbe {
print("assertion fail", msg, "\n"); print("assertion fail", msg, "\n")
panic(1); panic(1)
} }
} }
func main() { func main() {
var i, sum int; var i, sum int
i = 0; i = 0
for { for {
i = i + 1; i = i + 1
if i > 5 { if i > 5 {
break; break
} }
} }
assertequal(i, 6, "break"); assertequal(i, 6, "break")
sum = 0; sum = 0
for i := 0; i <= 10; i++ { for i := 0; i <= 10; i++ {
sum = sum + i; sum = sum + i
} }
assertequal(sum, 55, "all three"); assertequal(sum, 55, "all three")
sum = 0; sum = 0
for i := 0; i <= 10; { for i := 0; i <= 10; {
sum = sum + i; sum = sum + i
i++; i++
} }
assertequal(sum, 55, "only two"); assertequal(sum, 55, "only two")
sum = 0; sum = 0
for sum < 100 { for sum < 100 {
sum = sum + 9; sum = sum + 9
} }
assertequal(sum, 99 + 9, "only one"); assertequal(sum, 99 + 9, "only one")
sum = 0; sum = 0
for i := 0; i <= 10; i++ { for i := 0; i <= 10; i++ {
if i % 2 == 0 { if i % 2 == 0 {
continue; continue
} }
sum = sum + i; sum = sum + i
} }
assertequal(sum, 1+3+5+7+9, "continue"); assertequal(sum, 1+3+5+7+9, "continue")
} }
...@@ -9,8 +9,8 @@ package main ...@@ -9,8 +9,8 @@ package main
func assertequal(is, shouldbe int, msg string) { func assertequal(is, shouldbe int, msg string) {
if is != shouldbe { if is != shouldbe {
print("assertion fail", msg, "\n"); print("assertion fail", msg, "\n")
panic(1); panic(1)
} }
} }
...@@ -21,69 +21,69 @@ func f2(a int) { ...@@ -21,69 +21,69 @@ func f2(a int) {
} }
func f3(a, b int) int { func f3(a, b int) int {
return a+b; return a+b
} }
func f4(a, b int, c float) int { func f4(a, b int, c float) int {
return (a+b)/2 + int(c); return (a+b)/2 + int(c)
} }
func f5(a int) int { func f5(a int) int {
return 5; return 5
} }
func f6(a int) (r int) { func f6(a int) (r int) {
return 6; return 6
} }
func f7(a int) (x int, y float) { func f7(a int) (x int, y float) {
return 7, 7.0; return 7, 7.0
} }
func f8(a int) (x int, y float) { func f8(a int) (x int, y float) {
return 8, 8.0; return 8, 8.0
} }
type T struct { type T struct {
x, y int; x, y int
} }
func (t *T) m10(a int, b float) int { func (t *T) m10(a int, b float) int {
return (t.x+a) * (t.y+int(b)); return (t.x+a) * (t.y+int(b))
} }
func f9(a int) (i int, f float) { func f9(a int) (i int, f float) {
i = 9; i = 9
f = 9.0; f = 9.0
return; return
} }
func main() { func main() {
f1(); f1()
f2(1); f2(1)
r3 := f3(1, 2); r3 := f3(1, 2)
assertequal(r3, 3, "3"); assertequal(r3, 3, "3")
r4 := f4(0, 2, 3.0); r4 := f4(0, 2, 3.0)
assertequal(r4, 4, "4"); assertequal(r4, 4, "4")
r5 := f5(1); r5 := f5(1)
assertequal(r5, 5, "5"); assertequal(r5, 5, "5")
r6 := f6(1); r6 := f6(1)
assertequal(r6, 6, "6"); assertequal(r6, 6, "6")
r7, s7 := f7(1); r7, s7 := f7(1)
assertequal(r7, 7, "r7"); assertequal(r7, 7, "r7")
assertequal(int(s7), 7, "s7"); assertequal(int(s7), 7, "s7")
r8, s8 := f8(1); r8, s8 := f8(1)
assertequal(r8, 8, "r8"); assertequal(r8, 8, "r8")
assertequal(int(s8), 8, "s8"); assertequal(int(s8), 8, "s8")
r9, s9 := f9(1); r9, s9 := f9(1)
assertequal(r9, 9, "r9"); assertequal(r9, 9, "r9")
assertequal(int(s9), 9, "s9"); assertequal(int(s9), 9, "s9")
var t *T = new(T); var t *T = new(T)
t.x = 1; t.x = 1
t.y = 2; t.y = 2
r10 := t.m10(1, 3.0); r10 := t.m10(1, 3.0)
assertequal(r10, 10, "10"); assertequal(r10, 10, "10")
} }
...@@ -9,10 +9,10 @@ ...@@ -9,10 +9,10 @@
package main package main
func f1(a int) (int, float) { // BUG (not caught by compiler): multiple return values must have names func f1(a int) (int, float) { // BUG (not caught by compiler): multiple return values must have names
return 7, 7.0; return 7, 7.0
} }
func f2(a int) (a int, b float) { // ERROR "redeclared|definition" func f2(a int) (a int, b float) { // ERROR "redeclared|definition"
return 8, 8.0; return 8, 8.0
} }
...@@ -5,20 +5,20 @@ ...@@ -5,20 +5,20 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package main package main
import os "os"; import os "os"
type t1 int; type t1 int
type t2 int; type t2 int
type t3 int; type t3 int
func f1(t1, t2, t3); func f1(t1, t2, t3)
func f2(t1, t2, t3 bool); func f2(t1, t2, t3 bool)
func f3(t1, t2, x t3); func f3(t1, t2, x t3)
func f4(t1, *t3); func f4(t1, *t3)
func (x *t1) f5(y []t2) (t1, *t3); func (x *t1) f5(y []t2) (t1, *t3)
func f6() (int, *string); func f6() (int, *string)
func f7(*t2, t3); func f7(*t2, t3)
func f8(os int) int; func f8(os int) int
func f9(os int) int { func f9(os int) int {
return os return os
......
...@@ -6,12 +6,12 @@ ...@@ -6,12 +6,12 @@
package main package main
type t1 int; type t1 int
type t2 int; type t2 int
type t3 int; type t3 int
func f1(*t2, x t3); // ERROR "named" func f1(*t2, x t3) // ERROR "named"
func f2(t1, *t2, x t3); // ERROR "named" func f2(t1, *t2, x t3) // ERROR "named"
func f3() (x int, *string); // ERROR "named" func f3() (x int, *string) // ERROR "named"
func f4() (t1 t1); // legal - scope of parameter named t1 starts in body of f4. func f4() (t1 t1) // legal - scope of parameter named t1 starts in body of f4.
...@@ -9,6 +9,6 @@ package main ...@@ -9,6 +9,6 @@ package main
var notmain func() var notmain func()
func main() { func main() {
var x = &main; // ERROR "address of|invalid" var x = &main // ERROR "address of|invalid"
main = notmain; // ERROR "assign to|invalid" main = notmain // ERROR "assign to|invalid"
} }
...@@ -11,7 +11,7 @@ import "runtime" ...@@ -11,7 +11,7 @@ import "runtime"
func mk2() { func mk2() {
b := new([10000]byte) b := new([10000]byte)
_ = b _ = b
// println(b, "stored at", &b); // println(b, "stored at", &b)
} }
func mk1() { mk2() } func mk1() { mk2() }
......
...@@ -8,7 +8,7 @@ package main ...@@ -8,7 +8,7 @@ package main
func main() { func main() {
for i := 0; i < 1e5; i++ { for i := 0; i < 1e5; i++ {
x := new([100]byte); x := new([100]byte)
_ = x; _ = x
} }
} }
...@@ -11,7 +11,7 @@ package main ...@@ -11,7 +11,7 @@ package main
func ASSERT(p bool) { func ASSERT(p bool) {
if !p { if !p {
// panic 0; // panic 0
} }
} }
...@@ -20,7 +20,7 @@ func ASSERT(p bool) { ...@@ -20,7 +20,7 @@ func ASSERT(p bool) {
// Implementation of the HashMap // Implementation of the HashMap
type KeyType interface { type KeyType interface {
Hash() uint32; Hash() uint32
Match(other *KeyType) bool Match(other *KeyType) bool
} }
...@@ -31,31 +31,30 @@ type ValueType interface { ...@@ -31,31 +31,30 @@ type ValueType interface {
type Entry struct { type Entry struct {
key *KeyType; key *KeyType
value *ValueType; value *ValueType
} }
// Using the Array type below doesn't seem to work type Array [1024]Entry
//type Array array [1024] Entry;
type HashMap struct { type HashMap struct {
map_ *[1024] Entry; map_ *Array
log2_capacity_ uint32; log2_capacity_ uint32
occupancy_ uint32; occupancy_ uint32
} }
func (m *HashMap) capacity() uint32 { func (m *HashMap) capacity() uint32 {
return 1 << m.log2_capacity_; return 1 << m.log2_capacity_
} }
func (m *HashMap) Clear() { func (m *HashMap) Clear() {
// Mark all entries as empty. // Mark all entries as empty.
var i uint32 = m.capacity() - 1; var i uint32 = m.capacity() - 1
for i > 0 { for i > 0 {
m.map_[i].key = nil; m.map_[i].key = nil
i = i - 1 i = i - 1
} }
m.occupancy_ = 0 m.occupancy_ = 0
...@@ -63,72 +62,72 @@ func (m *HashMap) Clear() { ...@@ -63,72 +62,72 @@ func (m *HashMap) Clear() {
func (m *HashMap) Initialize (initial_log2_capacity uint32) { func (m *HashMap) Initialize (initial_log2_capacity uint32) {
m.log2_capacity_ = initial_log2_capacity; m.log2_capacity_ = initial_log2_capacity
m.map_ = new([1024] Entry); m.map_ = new(Array)
m.Clear(); m.Clear()
} }
func (m *HashMap) Probe (key *KeyType) *Entry { func (m *HashMap) Probe (key *KeyType) *Entry {
ASSERT(key != nil); ASSERT(key != nil)
var i uint32 = key.Hash() % m.capacity(); var i uint32 = key.Hash() % m.capacity()
ASSERT(0 <= i && i < m.capacity()); ASSERT(0 <= i && i < m.capacity())
ASSERT(m.occupancy_ < m.capacity()); // guarantees loop termination ASSERT(m.occupancy_ < m.capacity()) // guarantees loop termination
for m.map_[i].key != nil && !m.map_[i].key.Match(key) { for m.map_[i].key != nil && !m.map_[i].key.Match(key) {
i++; i++
if i >= m.capacity() { if i >= m.capacity() {
i = 0; i = 0
} }
} }
return &m.map_[i]; return &m.map_[i]
} }
func (m *HashMap) Lookup (key *KeyType, insert bool) *Entry { func (m *HashMap) Lookup (key *KeyType, insert bool) *Entry {
// Find a matching entry. // Find a matching entry.
var p *Entry = m.Probe(key); var p *Entry = m.Probe(key)
if p.key != nil { if p.key != nil {
return p; return p
} }
// No entry found; insert one if necessary. // No entry found; insert one if necessary.
if insert { if insert {
p.key = key; p.key = key
p.value = nil; p.value = nil
m.occupancy_++; m.occupancy_++
// Grow the map if we reached >= 80% occupancy. // Grow the map if we reached >= 80% occupancy.
if m.occupancy_ + m.occupancy_/4 >= m.capacity() { if m.occupancy_ + m.occupancy_/4 >= m.capacity() {
m.Resize(); m.Resize()
p = m.Probe(key); p = m.Probe(key)
} }
return p; return p
} }
// No entry found and none inserted. // No entry found and none inserted.
return nil; return nil
} }
func (m *HashMap) Resize() { func (m *HashMap) Resize() {
var hmap *[1024] Entry = m.map_; var hmap *Array = m.map_
var n uint32 = m.occupancy_; var n uint32 = m.occupancy_
// Allocate a new map of twice the current size. // Allocate a new map of twice the current size.
m.Initialize(m.log2_capacity_ << 1); m.Initialize(m.log2_capacity_ << 1)
// Rehash all current entries. // Rehash all current entries.
var i uint32 = 0; var i uint32 = 0
for n > 0 { for n > 0 {
if hmap[i].key != nil { if hmap[i].key != nil {
m.Lookup(hmap[i].key, true).value = hmap[i].value; m.Lookup(hmap[i].key, true).value = hmap[i].value
n = n - 1; n = n - 1
} }
i++; i++
} }
} }
...@@ -137,46 +136,46 @@ func (m *HashMap) Resize() { ...@@ -137,46 +136,46 @@ func (m *HashMap) Resize() {
// Test code // Test code
type Number struct { type Number struct {
x uint32; x uint32
} }
func (n *Number) Hash() uint32 { func (n *Number) Hash() uint32 {
return n.x * 23; return n.x * 23
} }
func (n *Number) Match(other *KeyType) bool { func (n *Number) Match(other *KeyType) bool {
// var y *Number = other; // var y *Number = other
// return n.x == y.x; // return n.x == y.x
return false; return false
} }
func MakeNumber (x uint32) *Number { func MakeNumber (x uint32) *Number {
var n *Number = new(Number); var n *Number = new(Number)
n.x = x; n.x = x
return n; return n
} }
func main() { func main() {
//f unc (n int) int { return n + 1; }(1); // func (n int) int { return n + 1; }(1)
//print "HashMap - gri 2/8/2008\n"; //print "HashMap - gri 2/8/2008\n"
var hmap *HashMap = new(HashMap); var hmap *HashMap = new(HashMap)
hmap.Initialize(0); hmap.Initialize(0)
var x1 *Number = MakeNumber(1001); var x1 *Number = MakeNumber(1001)
var x2 *Number = MakeNumber(2002); var x2 *Number = MakeNumber(2002)
var x3 *Number = MakeNumber(3003); var x3 *Number = MakeNumber(3003)
_, _, _ = x1, x2, x3; _, _, _ = x1, x2, x3
// this doesn't work I think... // this doesn't work I think...
//hmap.Lookup(x1, true); //hmap.Lookup(x1, true)
//hmap.Lookup(x2, true); //hmap.Lookup(x2, true)
//hmap.Lookup(x3, true); //hmap.Lookup(x3, true)
//print "done\n"; //print "done\n"
} }
...@@ -7,5 +7,5 @@ ...@@ -7,5 +7,5 @@
package main package main
func main() { func main() {
print("hello, world\n"); print("hello, world\n")
} }
...@@ -8,92 +8,92 @@ package main ...@@ -8,92 +8,92 @@ package main
func assertequal(is, shouldbe int, msg string) { func assertequal(is, shouldbe int, msg string) {
if is != shouldbe { if is != shouldbe {
print("assertion fail", msg, "\n"); print("assertion fail", msg, "\n")
panic(1); panic(1)
} }
} }
func main() { func main() {
i5 := 5; i5 := 5
i7 := 7; i7 := 7
var count int; var count int
count = 0; count = 0
if true { if true {
count = count + 1; count = count + 1
} }
assertequal(count, 1, "if true"); assertequal(count, 1, "if true")
count = 0; count = 0
if false { if false {
count = count + 1; count = count + 1
} }
assertequal(count, 0, "if false"); assertequal(count, 0, "if false")
count = 0; count = 0
if one := 1; true { if one := 1; true {
count = count + one; count = count + one
} }
assertequal(count, 1, "if true one"); assertequal(count, 1, "if true one")
count = 0; count = 0
if one := 1; false { if one := 1; false {
count = count + 1; count = count + 1
_ = one; _ = one
} }
assertequal(count, 0, "if false one"); assertequal(count, 0, "if false one")
count = 0; count = 0
if { if {
count = count + 1; count = count + 1
} }
assertequal(count, 1, "if empty"); assertequal(count, 1, "if empty")
count = 0; count = 0
if one := 1; true { if one := 1; true {
count = count + one; count = count + one
} }
assertequal(count, 1, "if empty one"); assertequal(count, 1, "if empty one")
count = 0; count = 0
if i5 < i7 { if i5 < i7 {
count = count + 1; count = count + 1
} }
assertequal(count, 1, "if cond"); assertequal(count, 1, "if cond")
count = 0; count = 0
if true { if true {
count = count + 1; count = count + 1
} else } else
count = count - 1; count = count - 1
assertequal(count, 1, "if else true"); assertequal(count, 1, "if else true")
count = 0; count = 0
if false { if false {
count = count + 1; count = count + 1
} else } else
count = count - 1; count = count - 1
assertequal(count, -1, "if else false"); assertequal(count, -1, "if else false")
count = 0; count = 0
if t:=1; false { if t:=1; false {
count = count + 1; count = count + 1
_ = t; _ = t
t := 7; t := 7
_ = t; _ = t
} else } else
count = count - t; count = count - t
assertequal(count, -1, "if else false var"); assertequal(count, -1, "if else false var")
count = 0; count = 0
t := 1; t := 1
if false { if false {
count = count + 1; count = count + 1
t := 7; t := 7
_ = t; _ = t
} else } else
count = count - t; count = count - t
_ = t; _ = t
assertequal(count, -1, "if else false var outside"); assertequal(count, -1, "if else false var outside")
} }
...@@ -9,12 +9,12 @@ package main ...@@ -9,12 +9,12 @@ package main
import "os" import "os"
func main() { func main() {
count := 7; count := 7
if one := 1; { if one := 1; {
count = count + one count = count + one
} }
if count != 8 { if count != 8 {
print(count, " should be 8\n"); print(count, " should be 8\n")
os.Exit(1) os.Exit(1)
} }
} }
...@@ -16,10 +16,10 @@ import . "os" ...@@ -16,10 +16,10 @@ import . "os"
func f(e os.Error) func f(e os.Error)
func main() { func main() {
var _e_ _os_.Error; var _e_ _os_.Error
var dot Error; var dot Error
f(_e_); f(_e_)
f(dot); f(dot)
} }
...@@ -12,6 +12,6 @@ import "bufio" // GCCGO_ERROR "previous|not used" ...@@ -12,6 +12,6 @@ import "bufio" // GCCGO_ERROR "previous|not used"
import bufio "os" // ERROR "redeclared|redefinition|incompatible" import bufio "os" // ERROR "redeclared|redefinition|incompatible"
import ( import (
"fmt"; // GCCGO_ERROR "previous|not used" "fmt" // GCCGO_ERROR "previous|not used"
fmt "math"; // ERROR "redeclared|redefinition|incompatible" fmt "math" // ERROR "redeclared|redefinition|incompatible"
) )
...@@ -64,5 +64,5 @@ func f() { ...@@ -64,5 +64,5 @@ func f() {
cap(b1)+ // ERROR "illegal|invalid|must be" cap(b1)+ // ERROR "illegal|invalid|must be"
cap(b2)+ // ERROR "illegal|invalid|must be" cap(b2)+ // ERROR "illegal|invalid|must be"
cap(b3)+ cap(b3)+
cap(b4); // ERROR "illegal|invalid|must be" cap(b4) // ERROR "illegal|invalid|must be"
} }
...@@ -10,11 +10,11 @@ import "fmt" ...@@ -10,11 +10,11 @@ import "fmt"
import "reflect" import "reflect"
type S struct { type S struct {
A, B, C, X, Y, Z int; A, B, C, X, Y, Z int
} }
type T struct { type T struct {
S; S
} }
var a1 = S { 0, 0, 0, 1, 2, 3 } var a1 = S { 0, 0, 0, 1, 2, 3 }
...@@ -49,14 +49,14 @@ var same = []Same { ...@@ -49,14 +49,14 @@ var same = []Same {
} }
func main() { func main() {
ok := true; ok := true
for _, s := range same { for _, s := range same {
if !reflect.DeepEqual(s.a, s.b) { if !reflect.DeepEqual(s.a, s.b) {
ok = false; ok = false
fmt.Printf("not same: %v and %v\n", s.a, s.b); fmt.Printf("not same: %v and %v\n", s.a, s.b)
} }
} }
if !ok { if !ok {
fmt.Println("BUG: test/initialize"); fmt.Println("BUG: test/initialize")
} }
} }
...@@ -7,17 +7,17 @@ ...@@ -7,17 +7,17 @@
package main package main
type S struct { type S struct {
A, B, C, X, Y, Z int; A, B, C, X, Y, Z int
} }
type T struct { type T struct {
S; S
} }
var x = 1 var x = 1
var a1 = S { 0, X: 1 }; // ERROR "mixture|undefined" var a1 = S { 0, X: 1 } // ERROR "mixture|undefined"
var a2 = S { Y: 3, Z: 2, Y: 3 }; // ERROR "duplicate" var a2 = S { Y: 3, Z: 2, Y: 3 } // ERROR "duplicate"
var a3 = T { 1, 2, 3, 4, 5, 6 }; // ERROR "convert|too many" var a3 = T { 1, 2, 3, 4, 5, 6 } // ERROR "convert|too many"
var a4 = [5]byte{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } // ERROR "index|too many" var a4 = [5]byte{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } // ERROR "index|too many"
var a5 = []byte { x: 2 } // ERROR "index" var a5 = []byte { x: 2 } // ERROR "index"
......
...@@ -18,8 +18,8 @@ func f() { ...@@ -18,8 +18,8 @@ func f() {
} }
func init() { func init() {
go f(); go f()
time.Nanoseconds(); time.Nanoseconds()
} }
func main() { func main() {
......
...@@ -16,9 +16,9 @@ func main() { ...@@ -16,9 +16,9 @@ func main() {
0x0 + 0x0 +
0x123 + 0x123 +
0X0 + 0X0 +
0X123; 0X123
if s != 788 { if s != 788 {
print("s is ", s, "; should be 788\n"); print("s is ", s, "; should be 788\n")
os.Exit(1); os.Exit(1)
} }
} }
...@@ -23,24 +23,24 @@ func (z *IntPtr) M() int64 { return int64(*z) } ...@@ -23,24 +23,24 @@ func (z *IntPtr) M() int64 { return int64(*z) }
var bad bool var bad bool
func test(name string, i I) { func test(name string, i I) {
m := i.M(); m := i.M()
if m != 12345 { if m != 12345 {
println(name, m); println(name, m)
bad = true; bad = true
} }
} }
func ptrs() { func ptrs() {
var bigptr BigPtr = BigPtr{ 10000, 2000, 300, 45 }; var bigptr BigPtr = BigPtr{ 10000, 2000, 300, 45 }
var smallptr SmallPtr = SmallPtr{ 12345 }; var smallptr SmallPtr = SmallPtr{ 12345 }
var intptr IntPtr = 12345; var intptr IntPtr = 12345
// test("bigptr", bigptr); // test("bigptr", bigptr)
test("&bigptr", &bigptr); test("&bigptr", &bigptr)
// test("smallptr", smallptr); // test("smallptr", smallptr)
test("&smallptr", &smallptr); test("&smallptr", &smallptr)
// test("intptr", intptr); // test("intptr", intptr)
test("&intptr", &intptr); test("&intptr", &intptr)
} }
type Big struct { a, b, c, d int64 } type Big struct { a, b, c, d int64 }
...@@ -53,23 +53,23 @@ type Int int32 ...@@ -53,23 +53,23 @@ type Int int32
func (z Int) M() int64 { return int64(z) } func (z Int) M() int64 { return int64(z) }
func nonptrs() { func nonptrs() {
var big Big = Big{ 10000, 2000, 300, 45 }; var big Big = Big{ 10000, 2000, 300, 45 }
var small Small = Small{ 12345 }; var small Small = Small{ 12345 }
var int Int = 12345; var int Int = 12345
test("big", big); test("big", big)
test("&big", &big); test("&big", &big)
test("small", small); test("small", small)
test("&small", &small); test("&small", &small)
test("int", int); test("int", int)
test("&int", &int); test("&int", &int)
} }
func main() { func main() {
ptrs(); ptrs()
nonptrs(); nonptrs()
if bad { if bad {
println("BUG: interface4"); println("BUG: interface4")
} }
} }
...@@ -9,17 +9,17 @@ ...@@ -9,17 +9,17 @@
package main package main
type R interface { R(); } type R interface { R() }
type RW interface { R(); W(); } type RW interface { R(); W() }
var e interface {} var e interface {}
var r R; var r R
var rw RW; var rw RW
func main() { func main() {
r = r; r = r
r = rw; r = rw
e = r; e = r
e = rw; e = rw
rw = rw; rw = rw
} }
...@@ -9,17 +9,17 @@ ...@@ -9,17 +9,17 @@
package main package main
type R interface { R(); } type R interface { R() }
type RW interface { R(); W(); } type RW interface { R(); W() }
var e interface {} var e interface {}
var r R; var r R
var rw RW; var rw RW
func main() { func main() {
r = r; r = r
r = rw; r = rw
e = r; e = r
e = rw; e = rw
rw = rw; rw = rw
} }
...@@ -36,47 +36,47 @@ var ok = true ...@@ -36,47 +36,47 @@ var ok = true
func check(s string, v int64) { func check(s string, v int64) {
if v != Value { if v != Value {
println(s, v); println(s, v)
ok = false; ok = false
} }
} }
func main() { func main() {
check("t.M()", t.M()); check("t.M()", t.M())
check("pt.M()", pt.M()); check("pt.M()", pt.M())
check("ti.M()", ti.M()); check("ti.M()", ti.M())
check("pti.M()", pti.M()); check("pti.M()", pti.M())
check("s.M()", s.M()); check("s.M()", s.M())
check("ps.M()", ps.M()); check("ps.M()", ps.M())
check("sp.M()", sp.M()); check("sp.M()", sp.M())
check("psp.M()", psp.M()); check("psp.M()", psp.M())
i = t; i = t
check("i = t; i.M()", i.M()); check("i = t; i.M()", i.M())
check("i = t; pi.M()", pi.M()); check("i = t; pi.M()", pi.M())
i = pt; i = pt
check("i = pt; i.M()", i.M()); check("i = pt; i.M()", i.M())
check("i = pt; pi.M()", pi.M()); check("i = pt; pi.M()", pi.M())
i = s; i = s
check("i = s; i.M()", i.M()); check("i = s; i.M()", i.M())
check("i = s; pi.M()", pi.M()); check("i = s; pi.M()", pi.M())
i = ps; i = ps
check("i = ps; i.M()", i.M()); check("i = ps; i.M()", i.M())
check("i = ps; pi.M()", pi.M()); check("i = ps; pi.M()", pi.M())
i = sp; i = sp
check("i = sp; i.M()", i.M()); check("i = sp; i.M()", i.M())
check("i = sp; pi.M()", pi.M()); check("i = sp; pi.M()", pi.M())
i = psp; i = psp
check("i = psp; i.M()", i.M()); check("i = psp; i.M()", i.M())
check("i = psp; pi.M()", pi.M()); check("i = psp; pi.M()", pi.M())
if !ok { if !ok {
println("BUG: interface10"); println("BUG: interface10")
os.Exit(1) os.Exit(1)
} }
} }
...@@ -12,18 +12,18 @@ type T int ...@@ -12,18 +12,18 @@ type T int
func (t T) m() {} func (t T) m() {}
type I interface { m() } type I interface { m() }
type J interface { I; } type J interface { I }
func main() { func main() {
var i I; var i I
var j J; var j J
var t T; var t T
i = t; i = t
j = t; j = t
_ = i; _ = i
_ = j; _ = j
i = j; i = j
_ = i; _ = i
j = i; j = i
_ = j; _ = j
} }
...@@ -14,32 +14,32 @@ type T int ...@@ -14,32 +14,32 @@ type T int
func (t T) m() {} func (t T) m() {}
type I interface { m() } type I interface { m() }
type J interface { I; } type J interface { I }
type PI interface { p.I; } type PI interface { p.I }
type PJ interface { p.J; } type PJ interface { p.J }
func main() { func main() {
var i I; var i I
var j J; var j J
var t T; var t T
i = t; i = t
j = t; j = t
_ = i; _ = i
_ = j; _ = j
i = j; i = j
_ = i; _ = i
j = i; j = i
_ = j; _ = j
var pi PI; var pi PI
var pj PJ; var pj PJ
var pt p.T; var pt p.T
pi = pt; pi = pt
pj = pt; pj = pt
_ = pi; _ = pi
_ = pj; _ = pj
pi = pj; pi = pj
_ = pi; _ = pi
pj = pi; pj = pi
_ = pj; _ = pj
} }
...@@ -13,12 +13,12 @@ type I interface { ...@@ -13,12 +13,12 @@ type I interface {
} }
func main() { func main() {
var s *S; var s *S
var i I; var i I
var e interface {}; var e interface {}
e = s; e = s
i = e.(I); i = e.(I)
_ = i; _ = i
} }
// hide S down here to avoid static warning // hide S down here to avoid static warning
......
...@@ -12,69 +12,69 @@ package main ...@@ -12,69 +12,69 @@ package main
import "reflect" import "reflect"
type T struct { type T struct {
f float32; f float32
g float32; g float32
s string; s string
t string; t string
u uint32; u uint32
v uint32; v uint32
w uint32; w uint32
x uint32; x uint32
y uint32; y uint32
z uint32; z uint32
} }
func add(s, t string) string { func add(s, t string) string {
return s + t; return s + t
} }
func assert(b bool) { func assert(b bool) {
if !b { if !b {
panic("assert"); panic("assert")
} }
} }
func main() { func main() {
var x T; var x T
x.f = 1.0; x.f = 1.0
x.g = x.f; x.g = x.f
x.s = add("abc", "def"); x.s = add("abc", "def")
x.t = add("abc", "def"); x.t = add("abc", "def")
x.u = 1; x.u = 1
x.v = 2; x.v = 2
x.w = 1<<28; x.w = 1<<28
x.x = 2<<28; x.x = 2<<28
x.y = 0x12345678; x.y = 0x12345678
x.z = x.y; x.z = x.y
// check mem and string // check mem and string
v := reflect.NewValue(x); v := reflect.NewValue(x)
i := v.(*reflect.StructValue).Field(0); i := v.(*reflect.StructValue).Field(0)
j := v.(*reflect.StructValue).Field(1); j := v.(*reflect.StructValue).Field(1)
assert(i.Interface() == j.Interface()); assert(i.Interface() == j.Interface())
s := v.(*reflect.StructValue).Field(2); s := v.(*reflect.StructValue).Field(2)
t := v.(*reflect.StructValue).Field(3); t := v.(*reflect.StructValue).Field(3)
assert(s.Interface() == t.Interface()); assert(s.Interface() == t.Interface())
// make sure different values are different. // make sure different values are different.
// make sure whole word is being compared, // make sure whole word is being compared,
// not just a single byte. // not just a single byte.
i = v.(*reflect.StructValue).Field(4); i = v.(*reflect.StructValue).Field(4)
j = v.(*reflect.StructValue).Field(5); j = v.(*reflect.StructValue).Field(5)
assert(i.Interface() != j.Interface()); assert(i.Interface() != j.Interface())
i = v.(*reflect.StructValue).Field(6); i = v.(*reflect.StructValue).Field(6)
j = v.(*reflect.StructValue).Field(7); j = v.(*reflect.StructValue).Field(7)
assert(i.Interface() != j.Interface()); assert(i.Interface() != j.Interface())
i = v.(*reflect.StructValue).Field(8); i = v.(*reflect.StructValue).Field(8)
j = v.(*reflect.StructValue).Field(9); j = v.(*reflect.StructValue).Field(9)
assert(i.Interface() == j.Interface()); assert(i.Interface() == j.Interface())
} }
/* /*
......
...@@ -64,7 +64,7 @@ func main() { ...@@ -64,7 +64,7 @@ func main() {
v = &t v = &t
v.V() v.V()
// p = t; // ERROR // p = t // ERROR
var i interface{} = t var i interface{} = t
if _, ok := i.(P); ok { if _, ok := i.(P); ok {
println("dynamic i.(P) succeeded incorrectly") println("dynamic i.(P) succeeded incorrectly")
...@@ -87,7 +87,7 @@ func main() { ...@@ -87,7 +87,7 @@ func main() {
v = &s v = &s
v.V() v.V()
// p = s; // ERROR // p = s // ERROR
var j interface{} = s var j interface{} = s
if _, ok := j.(P); ok { if _, ok := j.(P); ok {
println("dynamic j.(P) succeeded incorrectly") println("dynamic j.(P) succeeded incorrectly")
......
...@@ -18,8 +18,8 @@ type I1 interface { Name() int8 } ...@@ -18,8 +18,8 @@ type I1 interface { Name() int8 }
type I2 interface { Name() int64 } type I2 interface { Name() int64 }
func main() { func main() {
var i1 I1; var i1 I1
var s *S; var s *S
i1 = s; i1 = s
print(i1.(I2).Name()) print(i1.(I2).Name())
} }
...@@ -14,39 +14,39 @@ var fail int ...@@ -14,39 +14,39 @@ var fail int
func check(b bool, msg string) { func check(b bool, msg string) {
if (!b) { if (!b) {
println("failure in", msg); println("failure in", msg)
fail++; fail++
} }
} }
type I1 interface { Get() int; Put(int); } type I1 interface { Get() int; Put(int) }
type S1 struct { i int } type S1 struct { i int }
func (p S1) Get() int { return p.i } func (p S1) Get() int { return p.i }
func (p S1) Put(i int) { p.i = i } func (p S1) Put(i int) { p.i = i }
func f1() { func f1() {
s := S1{1}; s := S1{1}
var i I1 = s; var i I1 = s
i.Put(2); i.Put(2)
check(i.Get() == 1, "f1 i"); check(i.Get() == 1, "f1 i")
check(s.i == 1, "f1 s"); check(s.i == 1, "f1 s")
} }
func f2() { func f2() {
s := S1{1}; s := S1{1}
var i I1 = &s; var i I1 = &s
i.Put(2); i.Put(2)
check(i.Get() == 1, "f2 i"); check(i.Get() == 1, "f2 i")
check(s.i == 1, "f2 s"); check(s.i == 1, "f2 s")
} }
func f3() { func f3() {
s := &S1{1}; s := &S1{1}
var i I1 = s; var i I1 = s
i.Put(2); i.Put(2)
check(i.Get() == 1, "f3 i"); check(i.Get() == 1, "f3 i")
check(s.i == 1, "f3 s"); check(s.i == 1, "f3 s")
} }
type S2 struct { i int } type S2 struct { i int }
...@@ -55,57 +55,57 @@ func (p *S2) Put(i int) { p.i = i } ...@@ -55,57 +55,57 @@ func (p *S2) Put(i int) { p.i = i }
// Disallowed by restriction of values going to pointer receivers // Disallowed by restriction of values going to pointer receivers
// func f4() { // func f4() {
// s := S2{1}; // s := S2{1}
// var i I1 = s; // var i I1 = s
// i.Put(2); // i.Put(2)
// check(i.Get() == 2, "f4 i"); // check(i.Get() == 2, "f4 i")
// check(s.i == 1, "f4 s"); // check(s.i == 1, "f4 s")
// } // }
func f5() { func f5() {
s := S2{1}; s := S2{1}
var i I1 = &s; var i I1 = &s
i.Put(2); i.Put(2)
check(i.Get() == 2, "f5 i"); check(i.Get() == 2, "f5 i")
check(s.i == 2, "f5 s"); check(s.i == 2, "f5 s")
} }
func f6() { func f6() {
s := &S2{1}; s := &S2{1}
var i I1 = s; var i I1 = s
i.Put(2); i.Put(2)
check(i.Get() == 2, "f6 i"); check(i.Get() == 2, "f6 i")
check(s.i == 2, "f6 s"); check(s.i == 2, "f6 s")
} }
type I2 interface { Get() int64; Put(int64); } type I2 interface { Get() int64; Put(int64) }
type S3 struct { i, j, k, l int64 } type S3 struct { i, j, k, l int64 }
func (p S3) Get() int64 { return p.l } func (p S3) Get() int64 { return p.l }
func (p S3) Put(i int64) { p.l = i } func (p S3) Put(i int64) { p.l = i }
func f7() { func f7() {
s := S3{1, 2, 3, 4}; s := S3{1, 2, 3, 4}
var i I2 = s; var i I2 = s
i.Put(5); i.Put(5)
check(i.Get() == 4, "f7 i"); check(i.Get() == 4, "f7 i")
check(s.l == 4, "f7 s"); check(s.l == 4, "f7 s")
} }
func f8() { func f8() {
s := S3{1, 2, 3, 4}; s := S3{1, 2, 3, 4}
var i I2 = &s; var i I2 = &s
i.Put(5); i.Put(5)
check(i.Get() == 4, "f8 i"); check(i.Get() == 4, "f8 i")
check(s.l == 4, "f8 s"); check(s.l == 4, "f8 s")
} }
func f9() { func f9() {
s := &S3{1, 2, 3, 4}; s := &S3{1, 2, 3, 4}
var i I2 = s; var i I2 = s
i.Put(5); i.Put(5)
check(i.Get() == 4, "f9 i"); check(i.Get() == 4, "f9 i")
check(s.l == 4, "f9 s"); check(s.l == 4, "f9 s")
} }
type S4 struct { i, j, k, l int64 } type S4 struct { i, j, k, l int64 }
...@@ -114,42 +114,42 @@ func (p *S4) Put(i int64) { p.l = i } ...@@ -114,42 +114,42 @@ func (p *S4) Put(i int64) { p.l = i }
// Disallowed by restriction of values going to pointer receivers // Disallowed by restriction of values going to pointer receivers
// func f10() { // func f10() {
// s := S4{1, 2, 3, 4}; // s := S4{1, 2, 3, 4}
// var i I2 = s; // var i I2 = s
// i.Put(5); // i.Put(5)
// check(i.Get() == 5, "f10 i"); // check(i.Get() == 5, "f10 i")
// check(s.l == 4, "f10 s"); // check(s.l == 4, "f10 s")
// } // }
func f11() { func f11() {
s := S4{1, 2, 3, 4}; s := S4{1, 2, 3, 4}
var i I2 = &s; var i I2 = &s
i.Put(5); i.Put(5)
check(i.Get() == 5, "f11 i"); check(i.Get() == 5, "f11 i")
check(s.l == 5, "f11 s"); check(s.l == 5, "f11 s")
} }
func f12() { func f12() {
s := &S4{1, 2, 3, 4}; s := &S4{1, 2, 3, 4}
var i I2 = s; var i I2 = s
i.Put(5); i.Put(5)
check(i.Get() == 5, "f12 i"); check(i.Get() == 5, "f12 i")
check(s.l == 5, "f12 s"); check(s.l == 5, "f12 s")
} }
func main() { func main() {
f1(); f1()
f2(); f2()
f3(); f3()
// f4(); // f4()
f5(); f5()
f6(); f6()
f7(); f7()
f8(); f8()
f9(); f9()
// f10(); // f10()
f11(); f11()
f12(); f12()
if fail > 0 { if fail > 0 {
os.Exit(1) os.Exit(1)
} }
......
...@@ -8,113 +8,113 @@ package main ...@@ -8,113 +8,113 @@ package main
func assert(cond bool, msg string) { func assert(cond bool, msg string) {
if !cond { if !cond {
print("assertion fail: ", msg, "\n"); print("assertion fail: ", msg, "\n")
panic(1); panic(1)
} }
} }
const ( const (
x int = iota; x int = iota
y = iota; y = iota
z = 1 << iota; z = 1 << iota
f float = 2 * iota; f float = 2 * iota
g float = 4.5 * float(iota); g float = 4.5 * float(iota)
) )
const ( const (
X = 0; X = 0
Y; Y
Z; Z
) )
const ( const (
A = 1 << iota; A = 1 << iota
B; B
C; C
D; D
E = iota * iota; E = iota * iota
F; F
G; G
) )
const ( const (
a = 1; a = 1
b = iota << a; b = iota << a
c = iota << b; c = iota << b
d; d
) )
const ( const (
i = (a << iota) + (b * iota); i = (a << iota) + (b * iota)
j; j
k; k
l; l
) )
const ( const (
m = iota == 0; m = iota == 0
n; n
) )
const ( const (
p = float(iota); p = float(iota)
q; q
r; r
) )
const ( const (
s = string(iota + 'a'); s = string(iota + 'a')
t; t
) )
const ( const (
abit, amask = 1 << iota, 1 << iota - 1; abit, amask = 1 << iota, 1 << iota - 1
bbit, bmask = 1 << iota, 1 << iota - 1; bbit, bmask = 1 << iota, 1 << iota - 1
cbit, cmask = 1 << iota, 1 << iota - 1; cbit, cmask = 1 << iota, 1 << iota - 1
) )
func main() { func main() {
assert(x == 0, "x"); assert(x == 0, "x")
assert(y == 1, "y"); assert(y == 1, "y")
assert(z == 4, "z"); assert(z == 4, "z")
assert(f == 6.0, "f"); assert(f == 6.0, "f")
assert(g == 18.0, "g"); assert(g == 18.0, "g")
assert(X == 0, "X"); assert(X == 0, "X")
assert(Y == 0, "Y"); assert(Y == 0, "Y")
assert(Z == 0, "Z"); assert(Z == 0, "Z")
assert(A == 1, "A"); assert(A == 1, "A")
assert(B == 2, "B"); assert(B == 2, "B")
assert(C == 4, "C"); assert(C == 4, "C")
assert(D == 8, "D"); assert(D == 8, "D")
assert(E == 16, "E"); assert(E == 16, "E")
assert(F == 25, "F"); assert(F == 25, "F")
assert(a == 1, "a"); assert(a == 1, "a")
assert(b == 2, "b"); assert(b == 2, "b")
assert(c == 8, "c"); assert(c == 8, "c")
assert(d == 12, "d"); assert(d == 12, "d")
assert(i == 1, "i"); assert(i == 1, "i")
assert(j == 4, "j"); assert(j == 4, "j")
assert(k == 8, "k"); assert(k == 8, "k")
assert(l == 14, "l"); assert(l == 14, "l")
assert(m, "m"); assert(m, "m")
assert(!n, "n"); assert(!n, "n")
assert(p == 0.0, "p"); assert(p == 0.0, "p")
assert(q == 1.0, "q"); assert(q == 1.0, "q")
assert(r == 2.0, "r"); assert(r == 2.0, "r")
assert(s == "a", "s"); assert(s == "a", "s")
assert(t == "b", "t"); assert(t == "b", "t")
assert(abit == 1, "abit"); assert(abit == 1, "abit")
assert(amask == 0, "amask"); assert(amask == 0, "amask")
assert(bbit == 2, "bbit"); assert(bbit == 2, "bbit")
assert(bmask == 1, "bmask"); assert(bmask == 1, "bmask")
assert(cbit == 4, "cbit"); assert(cbit == 4, "cbit")
assert(cmask == 3, "cmask"); assert(cmask == 3, "cmask")
} }
...@@ -13,10 +13,10 @@ var nbad int ...@@ -13,10 +13,10 @@ var nbad int
func assert(cond bool, msg string) { func assert(cond bool, msg string) {
if !cond { if !cond {
if nbad == 0 { if nbad == 0 {
print("BUG"); print("BUG")
} }
nbad++; nbad++
print(" ", msg); print(" ", msg)
} }
} }
...@@ -35,203 +35,203 @@ func equal(a, b float) bool { ...@@ -35,203 +35,203 @@ func equal(a, b float) bool {
func main() { func main() {
// bool // bool
var t bool = true; var t bool = true
var f bool = false; var f bool = false
assert(t == !f, "bool"); assert(t == !f, "bool")
// int8 // int8
var i00 int8 = 0; var i00 int8 = 0
var i01 int8 = 1; var i01 int8 = 1
var i02 int8 = -1; var i02 int8 = -1
var i03 int8 = 127; var i03 int8 = 127
var i04 int8 = -127; var i04 int8 = -127
var i05 int8 = -128; var i05 int8 = -128
var i06 int8 = +127; var i06 int8 = +127
assert(i01 == i00 + 1, "i01"); assert(i01 == i00 + 1, "i01")
assert(i02 == -i01, "i02"); assert(i02 == -i01, "i02")
assert(i03 == -i04, "i03"); assert(i03 == -i04, "i03")
assert(-(i05+1) == i06, "i05"); assert(-(i05+1) == i06, "i05")
// int16 // int16
var i10 int16 = 0; var i10 int16 = 0
var i11 int16 = 1; var i11 int16 = 1
var i12 int16 = -1; var i12 int16 = -1
var i13 int16 = 32767; var i13 int16 = 32767
var i14 int16 = -32767; var i14 int16 = -32767
var i15 int16 = -32768; var i15 int16 = -32768
var i16 int16 = +32767; var i16 int16 = +32767
assert(i11 == i10 + 1, "i11"); assert(i11 == i10 + 1, "i11")
assert(i12 == -i11, "i12"); assert(i12 == -i11, "i12")
assert(i13 == -i14, "i13"); assert(i13 == -i14, "i13")
assert(-(i15+1) == i16, "i15"); assert(-(i15+1) == i16, "i15")
// int32 // int32
var i20 int32 = 0; var i20 int32 = 0
var i21 int32 = 1; var i21 int32 = 1
var i22 int32 = -1; var i22 int32 = -1
var i23 int32 = 2147483647; var i23 int32 = 2147483647
var i24 int32 = -2147483647; var i24 int32 = -2147483647
var i25 int32 = -2147483648; var i25 int32 = -2147483648
var i26 int32 = +2147483647; var i26 int32 = +2147483647
assert(i21 == i20 + 1, "i21"); assert(i21 == i20 + 1, "i21")
assert(i22 == -i21, "i22"); assert(i22 == -i21, "i22")
assert(i23 == -i24, "i23"); assert(i23 == -i24, "i23")
assert(-(i25+1) == i26, "i25"); assert(-(i25+1) == i26, "i25")
assert(i23 == (1 << 31) - 1, "i23 size"); assert(i23 == (1 << 31) - 1, "i23 size")
// int64 // int64
var i30 int64 = 0; var i30 int64 = 0
var i31 int64 = 1; var i31 int64 = 1
var i32 int64 = -1; var i32 int64 = -1
var i33 int64 = 9223372036854775807; var i33 int64 = 9223372036854775807
var i34 int64 = -9223372036854775807; var i34 int64 = -9223372036854775807
var i35 int64 = -9223372036854775808; var i35 int64 = -9223372036854775808
var i36 int64 = +9223372036854775807; var i36 int64 = +9223372036854775807
assert(i31 == i30 + 1, "i31"); assert(i31 == i30 + 1, "i31")
assert(i32 == -i31, "i32"); assert(i32 == -i31, "i32")
assert(i33 == -i34, "i33"); assert(i33 == -i34, "i33")
assert(-(i35+1) == i36, "i35"); assert(-(i35+1) == i36, "i35")
assert(i33 == (1<<63) - 1, "i33 size"); assert(i33 == (1<<63) - 1, "i33 size")
// uint8 // uint8
var u00 uint8 = 0; var u00 uint8 = 0
var u01 uint8 = 1; var u01 uint8 = 1
var u02 uint8 = 255; var u02 uint8 = 255
var u03 uint8 = +255; var u03 uint8 = +255
assert(u01 == u00 + 1, "u01"); assert(u01 == u00 + 1, "u01")
assert(u02 == u03, "u02"); assert(u02 == u03, "u02")
assert(u03 == (1<<8) - 1, "u03 size"); assert(u03 == (1<<8) - 1, "u03 size")
// uint16 // uint16
var u10 uint16 = 0; var u10 uint16 = 0
var u11 uint16 = 1; var u11 uint16 = 1
var u12 uint16 = 65535; var u12 uint16 = 65535
var u13 uint16 = +65535; var u13 uint16 = +65535
assert(u11 == u10 + 1, "u11"); assert(u11 == u10 + 1, "u11")
assert(u12 == u13, "u12"); assert(u12 == u13, "u12")
// uint32 // uint32
var u20 uint32 = 0; var u20 uint32 = 0
var u21 uint32 = 1; var u21 uint32 = 1
var u22 uint32 = 4294967295; var u22 uint32 = 4294967295
var u23 uint32 = +4294967295; var u23 uint32 = +4294967295
assert(u21 == u20 + 1, "u21"); assert(u21 == u20 + 1, "u21")
assert(u22 == u23, "u22"); assert(u22 == u23, "u22")
// uint64 // uint64
var u30 uint64 = 0; var u30 uint64 = 0
var u31 uint64 = 1; var u31 uint64 = 1
var u32 uint64 = 18446744073709551615; var u32 uint64 = 18446744073709551615
var u33 uint64 = +18446744073709551615; var u33 uint64 = +18446744073709551615
_, _, _, _ = u30, u31, u32, u33; _, _, _, _ = u30, u31, u32, u33
// float // float
var f00 float = 3.14159; var f00 float = 3.14159
var f01 float = -3.14159; var f01 float = -3.14159
var f02 float = +3.14159; var f02 float = +3.14159
var f03 float = 0.0; var f03 float = 0.0
var f04 float = .0; var f04 float = .0
var f05 float = 0.; var f05 float = 0.
var f06 float = -0.0; var f06 float = -0.0
var f07 float = 1e10; var f07 float = 1e10
var f08 float = -1e10; var f08 float = -1e10
var f09 float = 1e-10; var f09 float = 1e-10
var f10 float = 1e+10; var f10 float = 1e+10
var f11 float = 1.e-10; var f11 float = 1.e-10
var f12 float = 1.e+10; var f12 float = 1.e+10
var f13 float = .1e-10; var f13 float = .1e-10
var f14 float = .1e+10; var f14 float = .1e+10
var f15 float = 1.1e-10; var f15 float = 1.1e-10
var f16 float = 1.1e+10; var f16 float = 1.1e+10
assert(f01 == -f00, "f01"); assert(f01 == -f00, "f01")
assert(f02 == -f01, "f02"); assert(f02 == -f01, "f02")
assert(f03 == f04, "f03"); assert(f03 == f04, "f03")
assert(f04 == f05, "f04"); assert(f04 == f05, "f04")
assert(f05 == f06, "f05"); assert(f05 == f06, "f05")
assert(f07 == -f08, "f07"); assert(f07 == -f08, "f07")
assert(equal(f09, 1/f10), "f09"); assert(equal(f09, 1/f10), "f09")
assert(f11 == f09, "f11"); assert(f11 == f09, "f11")
assert(f12 == f10, "f12"); assert(f12 == f10, "f12")
assert(equal(f13, f09/10.0), "f13"); assert(equal(f13, f09/10.0), "f13")
assert(equal(f14, f12/10.0), "f14"); assert(equal(f14, f12/10.0), "f14")
assert(equal(f15, f16/1e20), "f15"); assert(equal(f15, f16/1e20), "f15")
// character // character
var c0 uint8 = 'a'; var c0 uint8 = 'a'
var c1 uint8 = 'ä'; var c1 uint8 = 'ä'
var c2 uint8 = '\a'; var c2 uint8 = '\a'
var c3 uint8 = '\b'; var c3 uint8 = '\b'
var c4 uint8 = '\f'; var c4 uint8 = '\f'
var c5 uint8 = '\n'; var c5 uint8 = '\n'
var c6 uint8 = '\r'; var c6 uint8 = '\r'
var c7 uint8 = '\t'; var c7 uint8 = '\t'
var c8 uint8 = '\v'; var c8 uint8 = '\v'
// var c9 uint8 = '本'; // correctly caught as error // var c9 uint8 = '本' // correctly caught as error
var c9 uint16 = '本'; var c9 uint16 = '本'
assert(c0 == 0x61, "c0"); assert(c0 == 0x61, "c0")
assert(c1 == 0xe4, "c1"); assert(c1 == 0xe4, "c1")
assert(c2 == 0x07, "c2"); assert(c2 == 0x07, "c2")
assert(c3 == 0x08, "c3"); assert(c3 == 0x08, "c3")
assert(c4 == 0x0c, "c4"); assert(c4 == 0x0c, "c4")
assert(c5 == 0x0a, "c4"); assert(c5 == 0x0a, "c4")
assert(c6 == 0x0d, "c6"); assert(c6 == 0x0d, "c6")
assert(c7 == 0x09, "c7"); assert(c7 == 0x09, "c7")
assert(c8 == 0x0b, "c8"); assert(c8 == 0x0b, "c8")
assert(c9 == 0x672c, "c9"); assert(c9 == 0x672c, "c9")
var c00 uint8 = '\000'; var c00 uint8 = '\000'
var c01 uint8 = '\007'; var c01 uint8 = '\007'
var c02 uint8 = '\177'; var c02 uint8 = '\177'
var c03 uint8 = '\377'; var c03 uint8 = '\377'
assert(c00 == 0, "c00"); assert(c00 == 0, "c00")
assert(c01 == 7, "c01"); assert(c01 == 7, "c01")
assert(c02 == 127, "c02"); assert(c02 == 127, "c02")
assert(c03 == 255, "c03"); assert(c03 == 255, "c03")
var cx0 uint8 = '\x00'; var cx0 uint8 = '\x00'
var cx1 uint8 = '\x0f'; var cx1 uint8 = '\x0f'
var cx2 uint8 = '\xff'; var cx2 uint8 = '\xff'
assert(cx0 == 0, "cx0"); assert(cx0 == 0, "cx0")
assert(cx1 == 15, "cx1"); assert(cx1 == 15, "cx1")
assert(cx2 == 255, "cx2"); assert(cx2 == 255, "cx2")
var cu0 uint16 = '\u1234'; var cu0 uint16 = '\u1234'
var cu1 uint32 = '\U00101234'; var cu1 uint32 = '\U00101234'
assert(cu0 == 0x1234, "cu0"); assert(cu0 == 0x1234, "cu0")
assert(cu1 == 0x101234, "cu1"); assert(cu1 == 0x101234, "cu1")
// string // string
var s0 string = ""; var s0 string = ""
var s1 string = "hellô"; var s1 string = "hellô"
assert(s1[0] == 'h', "s1-0"); assert(s1[0] == 'h', "s1-0")
assert(s1[4] == 0xc3, "s1-4"); assert(s1[4] == 0xc3, "s1-4")
assert(s1[5] == 0xb4, "s1-5"); assert(s1[5] == 0xb4, "s1-5")
var s2 string = "\a\b\f\n\r\t\v"; var s2 string = "\a\b\f\n\r\t\v"
_, _ = s0, s2; _, _ = s0, s2
var s00 string = "\000"; var s00 string = "\000"
var s01 string = "\007"; var s01 string = "\007"
var s02 string = "\377"; var s02 string = "\377"
assert(s00[0] == 0, "s00"); assert(s00[0] == 0, "s00")
assert(s01[0] == 7, "s01"); assert(s01[0] == 7, "s01")
assert(s02[0] == 255, "s02"); assert(s02[0] == 255, "s02")
var x00 string = "\x00"; var x00 string = "\x00"
var x01 string = "\x0f"; var x01 string = "\x0f"
var x02 string = "\xff"; var x02 string = "\xff"
assert(x00[0] == 0, "x00"); assert(x00[0] == 0, "x00")
assert(x01[0] == 15, "x01"); assert(x01[0] == 15, "x01")
assert(x02[0] == 255, "x02"); assert(x02[0] == 255, "x02")
// these are all the same string // these are all the same string
var sj0 string = "日本語"; var sj0 string = "日本語"
var sj1 string = "\u65e5\u672c\u8a9e"; var sj1 string = "\u65e5\u672c\u8a9e"
var sj2 string = "\U000065e5\U0000672c\U00008a9e"; var sj2 string = "\U000065e5\U0000672c\U00008a9e"
var sj3 string = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"; var sj3 string = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
assert(sj0 == sj1, "sj1"); assert(sj0 == sj1, "sj1")
assert(sj0 == sj2, "sj2"); assert(sj0 == sj2, "sj2")
assert(sj0 == sj3, "sj3"); assert(sj0 == sj3, "sj3")
if nbad > 0 { if nbad > 0 {
println() println()
......
...@@ -56,7 +56,7 @@ func memset(b *byte, c byte, n uintptr) { ...@@ -56,7 +56,7 @@ func memset(b *byte, c byte, n uintptr) {
func main() { func main() {
flag.Parse() flag.Parse()
// prime(); // prime()
var blocks [1]struct { var blocks [1]struct {
base *byte base *byte
siz uintptr siz uintptr
...@@ -67,7 +67,7 @@ func main() { ...@@ -67,7 +67,7 @@ func main() {
} }
b := rand.Int() % len(blocks) b := rand.Int() % len(blocks)
if blocks[b].base != nil { if blocks[b].base != nil {
// println("Free", blocks[b].siz, blocks[b].base); // println("Free", blocks[b].siz, blocks[b].base)
runtime.Free(blocks[b].base) runtime.Free(blocks[b].base)
blocks[b].base = nil blocks[b].base = nil
allocated -= uint64(blocks[b].siz) allocated -= uint64(blocks[b].siz)
...@@ -75,8 +75,8 @@ func main() { ...@@ -75,8 +75,8 @@ func main() {
} }
siz := uintptr(rand.Int() >> (11 + rand.Uint32()%20)) siz := uintptr(rand.Int() >> (11 + rand.Uint32()%20))
base := runtime.Alloc(siz) base := runtime.Alloc(siz)
// ptr := uintptr(syscall.BytePtr(base))+uintptr(siz/2); // ptr := uintptr(syscall.BytePtr(base))+uintptr(siz/2)
// obj, size, ref, ok := allocator.find(ptr); // obj, size, ref, ok := allocator.find(ptr)
// if obj != base || *ref != 0 || !ok { // if obj != base || *ref != 0 || !ok {
// println("find", siz, obj, ref, ok) // println("find", siz, obj, ref, ok)
// panic("fail") // panic("fail")
...@@ -84,7 +84,7 @@ func main() { ...@@ -84,7 +84,7 @@ func main() {
blocks[b].base = base blocks[b].base = base
blocks[b].siz = siz blocks[b].siz = siz
allocated += uint64(siz) allocated += uint64(siz)
// println("Alloc", siz, base); // println("Alloc", siz, base)
memset(base, 0xbb, siz) memset(base, 0xbb, siz)
bigger() bigger()
} }
......
...@@ -59,7 +59,7 @@ func main() { ...@@ -59,7 +59,7 @@ func main() {
if *chatty { if *chatty {
println("Primed", i) println("Primed", i)
} }
// runtime.frozen = true; // runtime.frozen = true
} }
} }
} }
...@@ -7,318 +7,318 @@ ...@@ -7,318 +7,318 @@
package main package main
import ( import (
"fmt"; "fmt"
"strconv"; "strconv"
) )
const count = 100; const count = 100
func P(a []string) string { func P(a []string) string {
s := "{"; s := "{"
for i := 0; i < len(a); i++ { for i := 0; i < len(a); i++ {
if i > 0 { if i > 0 {
s += "," s += ","
} }
s += `"` + a[i] + `"`; s += `"` + a[i] + `"`
} }
s +="}"; s +="}"
return s; return s
} }
func main() { func main() {
// Test a map literal. // Test a map literal.
mlit := map[string] int { "0":0, "1":1, "2":2, "3":3, "4":4 }; mlit := map[string] int { "0":0, "1":1, "2":2, "3":3, "4":4 }
for i := 0; i < len(mlit); i++ { for i := 0; i < len(mlit); i++ {
s := string([]byte{byte(i)+'0'}); s := string([]byte{byte(i)+'0'})
if mlit[s] != i { if mlit[s] != i {
fmt.Printf("mlit[%s] = %d\n", s, mlit[s]) fmt.Printf("mlit[%s] = %d\n", s, mlit[s])
} }
} }
mib := make(map[int] bool); mib := make(map[int] bool)
mii := make(map[int] int); mii := make(map[int] int)
mfi := make(map[float] int); mfi := make(map[float] int)
mif := make(map[int] float); mif := make(map[int] float)
msi := make(map[string] int); msi := make(map[string] int)
mis := make(map[int] string); mis := make(map[int] string)
mss := make(map[string] string); mss := make(map[string] string)
mspa := make(map[string] []string); mspa := make(map[string] []string)
// BUG need an interface map both ways too // BUG need an interface map both ways too
type T struct { type T struct {
i int64; // can't use string here; struct values are only compared at the top level i int64 // can't use string here; struct values are only compared at the top level
f float; f float
}; }
mipT := make(map[int] *T); mipT := make(map[int] *T)
mpTi := make(map[*T] int); mpTi := make(map[*T] int)
mit := make(map[int] T); mit := make(map[int] T)
// mti := make(map[T] int); // mti := make(map[T] int)
type M map[int] int; type M map[int] int
mipM := make(map[int] M); mipM := make(map[int] M)
var apT [2*count]*T; var apT [2*count]*T
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
s := strconv.Itoa(i); s := strconv.Itoa(i)
s10 := strconv.Itoa(i*10); s10 := strconv.Itoa(i*10)
f := float(i); f := float(i)
t := T{int64(i),f}; t := T{int64(i),f}
apT[i] = new(T); apT[i] = new(T)
apT[i].i = int64(i); apT[i].i = int64(i)
apT[i].f = f; apT[i].f = f
apT[2*i] = new(T); // need twice as many entries as we use, for the nonexistence check apT[2*i] = new(T) // need twice as many entries as we use, for the nonexistence check
apT[2*i].i = int64(i); apT[2*i].i = int64(i)
apT[2*i].f = f; apT[2*i].f = f
m := M{i: i+1}; m := M{i: i+1}
mib[i] = (i != 0); mib[i] = (i != 0)
mii[i] = 10*i; mii[i] = 10*i
mfi[float(i)] = 10*i; mfi[float(i)] = 10*i
mif[i] = 10.0*f; mif[i] = 10.0*f
mis[i] = s; mis[i] = s
msi[s] = i; msi[s] = i
mss[s] = s10; mss[s] = s10
mss[s] = s10; mss[s] = s10
as := make([]string, 2); as := make([]string, 2)
as[0] = s10; as[0] = s10
as[1] = s10; as[1] = s10
mspa[s] = as; mspa[s] = as
mipT[i] = apT[i]; mipT[i] = apT[i]
mpTi[apT[i]] = i; mpTi[apT[i]] = i
mipM[i] = m; mipM[i] = m
mit[i] = t; mit[i] = t
// mti[t] = i; // mti[t] = i
} }
// test len // test len
if len(mib) != count { if len(mib) != count {
fmt.Printf("len(mib) = %d\n", len(mib)); fmt.Printf("len(mib) = %d\n", len(mib))
} }
if len(mii) != count { if len(mii) != count {
fmt.Printf("len(mii) = %d\n", len(mii)); fmt.Printf("len(mii) = %d\n", len(mii))
} }
if len(mfi) != count { if len(mfi) != count {
fmt.Printf("len(mfi) = %d\n", len(mfi)); fmt.Printf("len(mfi) = %d\n", len(mfi))
} }
if len(mif) != count { if len(mif) != count {
fmt.Printf("len(mif) = %d\n", len(mif)); fmt.Printf("len(mif) = %d\n", len(mif))
} }
if len(msi) != count { if len(msi) != count {
fmt.Printf("len(msi) = %d\n", len(msi)); fmt.Printf("len(msi) = %d\n", len(msi))
} }
if len(mis) != count { if len(mis) != count {
fmt.Printf("len(mis) = %d\n", len(mis)); fmt.Printf("len(mis) = %d\n", len(mis))
} }
if len(mss) != count { if len(mss) != count {
fmt.Printf("len(mss) = %d\n", len(mss)); fmt.Printf("len(mss) = %d\n", len(mss))
} }
if len(mspa) != count { if len(mspa) != count {
fmt.Printf("len(mspa) = %d\n", len(mspa)); fmt.Printf("len(mspa) = %d\n", len(mspa))
} }
if len(mipT) != count { if len(mipT) != count {
fmt.Printf("len(mipT) = %d\n", len(mipT)); fmt.Printf("len(mipT) = %d\n", len(mipT))
} }
if len(mpTi) != count { if len(mpTi) != count {
fmt.Printf("len(mpTi) = %d\n", len(mpTi)); fmt.Printf("len(mpTi) = %d\n", len(mpTi))
} }
// if len(mti) != count { // if len(mti) != count {
// fmt.Printf("len(mti) = %d\n", len(mti)); // fmt.Printf("len(mti) = %d\n", len(mti))
// } // }
if len(mipM) != count { if len(mipM) != count {
fmt.Printf("len(mipM) = %d\n", len(mipM)); fmt.Printf("len(mipM) = %d\n", len(mipM))
} }
// if len(mti) != count { // if len(mti) != count {
// fmt.Printf("len(mti) = %d\n", len(mti)); // fmt.Printf("len(mti) = %d\n", len(mti))
// } // }
if len(mit) != count { if len(mit) != count {
fmt.Printf("len(mit) = %d\n", len(mit)); fmt.Printf("len(mit) = %d\n", len(mit))
} }
// test construction directly // test construction directly
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
s := strconv.Itoa(i); s := strconv.Itoa(i)
s10 := strconv.Itoa(i*10); s10 := strconv.Itoa(i*10)
f := float(i); f := float(i)
// BUG m := M(i, i+1); // BUG m := M(i, i+1)
if mib[i] != (i != 0) { if mib[i] != (i != 0) {
fmt.Printf("mib[%d] = %t\n", i, mib[i]); fmt.Printf("mib[%d] = %t\n", i, mib[i])
} }
if(mii[i] != 10*i) { if(mii[i] != 10*i) {
fmt.Printf("mii[%d] = %d\n", i, mii[i]); fmt.Printf("mii[%d] = %d\n", i, mii[i])
} }
if(mfi[f] != 10*i) { if(mfi[f] != 10*i) {
fmt.Printf("mfi[%d] = %d\n", i, mfi[f]); fmt.Printf("mfi[%d] = %d\n", i, mfi[f])
} }
if(mif[i] != 10.0*f) { if(mif[i] != 10.0*f) {
fmt.Printf("mif[%d] = %g\n", i, mif[i]); fmt.Printf("mif[%d] = %g\n", i, mif[i])
} }
if(mis[i] != s) { if(mis[i] != s) {
fmt.Printf("mis[%d] = %s\n", i, mis[i]); fmt.Printf("mis[%d] = %s\n", i, mis[i])
} }
if(msi[s] != i) { if(msi[s] != i) {
fmt.Printf("msi[%s] = %d\n", s, msi[s]); fmt.Printf("msi[%s] = %d\n", s, msi[s])
} }
if mss[s] != s10 { if mss[s] != s10 {
fmt.Printf("mss[%s] = %g\n", s, mss[s]); fmt.Printf("mss[%s] = %g\n", s, mss[s])
} }
for j := 0; j < len(mspa[s]); j++ { for j := 0; j < len(mspa[s]); j++ {
if mspa[s][j] != s10 { if mspa[s][j] != s10 {
fmt.Printf("mspa[%s][%d] = %s\n", s, j, mspa[s][j]); fmt.Printf("mspa[%s][%d] = %s\n", s, j, mspa[s][j])
} }
} }
if(mipT[i].i != int64(i) || mipT[i].f != f) { if(mipT[i].i != int64(i) || mipT[i].f != f) {
fmt.Printf("mipT[%d] = %v\n", i, mipT[i]); fmt.Printf("mipT[%d] = %v\n", i, mipT[i])
} }
if(mpTi[apT[i]] != i) { if(mpTi[apT[i]] != i) {
fmt.Printf("mpTi[apT[%d]] = %d\n", i, mpTi[apT[i]]); fmt.Printf("mpTi[apT[%d]] = %d\n", i, mpTi[apT[i]])
} }
// if(mti[t] != i) { // if(mti[t] != i) {
// fmt.Printf("mti[%s] = %s\n", s, mti[t]); // fmt.Printf("mti[%s] = %s\n", s, mti[t])
// } // }
if (mipM[i][i] != i + 1) { if (mipM[i][i] != i + 1) {
fmt.Printf("mipM[%d][%d] = %d\n", i, i, mipM[i][i]); fmt.Printf("mipM[%d][%d] = %d\n", i, i, mipM[i][i])
} }
// if(mti[t] != i) { // if(mti[t] != i) {
// fmt.Printf("mti[%v] = %d\n", t, mti[t]); // fmt.Printf("mti[%v] = %d\n", t, mti[t])
// } // }
if(mit[i].i != int64(i) || mit[i].f != f) { if(mit[i].i != int64(i) || mit[i].f != f) {
fmt.Printf("mit[%d] = {%d %g}\n", i, mit[i].i, mit[i].f); fmt.Printf("mit[%d] = {%d %g}\n", i, mit[i].i, mit[i].f)
} }
} }
// test existence with tuple check // test existence with tuple check
// failed lookups yield a false value for the boolean. // failed lookups yield a false value for the boolean.
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
s := strconv.Itoa(i); s := strconv.Itoa(i)
f := float(i); f := float(i)
{ {
_, b := mib[i]; _, b := mib[i]
if !b { if !b {
fmt.Printf("tuple existence decl: mib[%d]\n", i); fmt.Printf("tuple existence decl: mib[%d]\n", i)
} }
_, b = mib[i]; _, b = mib[i]
if !b { if !b {
fmt.Printf("tuple existence assign: mib[%d]\n", i); fmt.Printf("tuple existence assign: mib[%d]\n", i)
} }
} }
{ {
_, b := mii[i]; _, b := mii[i]
if !b { if !b {
fmt.Printf("tuple existence decl: mii[%d]\n", i); fmt.Printf("tuple existence decl: mii[%d]\n", i)
} }
_, b = mii[i]; _, b = mii[i]
if !b { if !b {
fmt.Printf("tuple existence assign: mii[%d]\n", i); fmt.Printf("tuple existence assign: mii[%d]\n", i)
} }
} }
{ {
_, b := mfi[f]; _, b := mfi[f]
if !b { if !b {
fmt.Printf("tuple existence decl: mfi[%d]\n", i); fmt.Printf("tuple existence decl: mfi[%d]\n", i)
} }
_, b = mfi[f]; _, b = mfi[f]
if !b { if !b {
fmt.Printf("tuple existence assign: mfi[%d]\n", i); fmt.Printf("tuple existence assign: mfi[%d]\n", i)
} }
} }
{ {
_, b := mif[i]; _, b := mif[i]
if !b { if !b {
fmt.Printf("tuple existence decl: mif[%d]\n", i); fmt.Printf("tuple existence decl: mif[%d]\n", i)
} }
_, b = mif[i]; _, b = mif[i]
if !b { if !b {
fmt.Printf("tuple existence assign: mif[%d]\n", i); fmt.Printf("tuple existence assign: mif[%d]\n", i)
} }
} }
{ {
_, b := mis[i]; _, b := mis[i]
if !b { if !b {
fmt.Printf("tuple existence decl: mis[%d]\n", i); fmt.Printf("tuple existence decl: mis[%d]\n", i)
} }
_, b = mis[i]; _, b = mis[i]
if !b { if !b {
fmt.Printf("tuple existence assign: mis[%d]\n", i); fmt.Printf("tuple existence assign: mis[%d]\n", i)
} }
} }
{ {
_, b := msi[s]; _, b := msi[s]
if !b { if !b {
fmt.Printf("tuple existence decl: msi[%d]\n", i); fmt.Printf("tuple existence decl: msi[%d]\n", i)
} }
_, b = msi[s]; _, b = msi[s]
if !b { if !b {
fmt.Printf("tuple existence assign: msi[%d]\n", i); fmt.Printf("tuple existence assign: msi[%d]\n", i)
} }
} }
{ {
_, b := mss[s]; _, b := mss[s]
if !b { if !b {
fmt.Printf("tuple existence decl: mss[%d]\n", i); fmt.Printf("tuple existence decl: mss[%d]\n", i)
} }
_, b = mss[s]; _, b = mss[s]
if !b { if !b {
fmt.Printf("tuple existence assign: mss[%d]\n", i); fmt.Printf("tuple existence assign: mss[%d]\n", i)
} }
} }
{ {
_, b := mspa[s]; _, b := mspa[s]
if !b { if !b {
fmt.Printf("tuple existence decl: mspa[%d]\n", i); fmt.Printf("tuple existence decl: mspa[%d]\n", i)
} }
_, b = mspa[s]; _, b = mspa[s]
if !b { if !b {
fmt.Printf("tuple existence assign: mspa[%d]\n", i); fmt.Printf("tuple existence assign: mspa[%d]\n", i)
} }
} }
{ {
_, b := mipT[i]; _, b := mipT[i]
if !b { if !b {
fmt.Printf("tuple existence decl: mipT[%d]\n", i); fmt.Printf("tuple existence decl: mipT[%d]\n", i)
} }
_, b = mipT[i]; _, b = mipT[i]
if !b { if !b {
fmt.Printf("tuple existence assign: mipT[%d]\n", i); fmt.Printf("tuple existence assign: mipT[%d]\n", i)
} }
} }
{ {
_, b := mpTi[apT[i]]; _, b := mpTi[apT[i]]
if !b { if !b {
fmt.Printf("tuple existence decl: mpTi[apT[%d]]\n", i); fmt.Printf("tuple existence decl: mpTi[apT[%d]]\n", i)
} }
_, b = mpTi[apT[i]]; _, b = mpTi[apT[i]]
if !b { if !b {
fmt.Printf("tuple existence assign: mpTi[apT[%d]]\n", i); fmt.Printf("tuple existence assign: mpTi[apT[%d]]\n", i)
} }
} }
{ {
_, b := mipM[i]; _, b := mipM[i]
if !b { if !b {
fmt.Printf("tuple existence decl: mipM[%d]\n", i); fmt.Printf("tuple existence decl: mipM[%d]\n", i)
} }
_, b = mipM[i]; _, b = mipM[i]
if !b { if !b {
fmt.Printf("tuple existence assign: mipM[%d]\n", i); fmt.Printf("tuple existence assign: mipM[%d]\n", i)
} }
} }
{ {
_, b := mit[i]; _, b := mit[i]
if !b { if !b {
fmt.Printf("tuple existence decl: mit[%d]\n", i); fmt.Printf("tuple existence decl: mit[%d]\n", i)
} }
_, b = mit[i]; _, b = mit[i]
if !b { if !b {
fmt.Printf("tuple existence assign: mit[%d]\n", i); fmt.Printf("tuple existence assign: mit[%d]\n", i)
} }
} }
// { // {
// _, b := mti[t]; // _, b := mti[t]
// if !b { // if !b {
// fmt.Printf("tuple existence decl: mti[%d]\n", i); // fmt.Printf("tuple existence decl: mti[%d]\n", i)
// } // }
// _, b = mti[t]; // _, b = mti[t]
// if !b { // if !b {
// fmt.Printf("tuple existence assign: mti[%d]\n", i); // fmt.Printf("tuple existence assign: mti[%d]\n", i)
// } // }
// } // }
} }
...@@ -326,136 +326,136 @@ func main() { ...@@ -326,136 +326,136 @@ func main() {
// test nonexistence with tuple check // test nonexistence with tuple check
// failed lookups yield a false value for the boolean. // failed lookups yield a false value for the boolean.
for i := count; i < 2*count; i++ { for i := count; i < 2*count; i++ {
s := strconv.Itoa(i); s := strconv.Itoa(i)
f := float(i); f := float(i)
{ {
_, b := mib[i]; _, b := mib[i]
if b { if b {
fmt.Printf("tuple nonexistence decl: mib[%d]", i); fmt.Printf("tuple nonexistence decl: mib[%d]", i)
} }
_, b = mib[i]; _, b = mib[i]
if b { if b {
fmt.Printf("tuple nonexistence assign: mib[%d]", i); fmt.Printf("tuple nonexistence assign: mib[%d]", i)
} }
} }
{ {
_, b := mii[i]; _, b := mii[i]
if b { if b {
fmt.Printf("tuple nonexistence decl: mii[%d]", i); fmt.Printf("tuple nonexistence decl: mii[%d]", i)
} }
_, b = mii[i]; _, b = mii[i]
if b { if b {
fmt.Printf("tuple nonexistence assign: mii[%d]", i); fmt.Printf("tuple nonexistence assign: mii[%d]", i)
} }
} }
{ {
_, b := mfi[f]; _, b := mfi[f]
if b { if b {
fmt.Printf("tuple nonexistence decl: mfi[%d]", i); fmt.Printf("tuple nonexistence decl: mfi[%d]", i)
} }
_, b = mfi[f]; _, b = mfi[f]
if b { if b {
fmt.Printf("tuple nonexistence assign: mfi[%d]", i); fmt.Printf("tuple nonexistence assign: mfi[%d]", i)
} }
} }
{ {
_, b := mif[i]; _, b := mif[i]
if b { if b {
fmt.Printf("tuple nonexistence decl: mif[%d]", i); fmt.Printf("tuple nonexistence decl: mif[%d]", i)
} }
_, b = mif[i]; _, b = mif[i]
if b { if b {
fmt.Printf("tuple nonexistence assign: mif[%d]", i); fmt.Printf("tuple nonexistence assign: mif[%d]", i)
} }
} }
{ {
_, b := mis[i]; _, b := mis[i]
if b { if b {
fmt.Printf("tuple nonexistence decl: mis[%d]", i); fmt.Printf("tuple nonexistence decl: mis[%d]", i)
} }
_, b = mis[i]; _, b = mis[i]
if b { if b {
fmt.Printf("tuple nonexistence assign: mis[%d]", i); fmt.Printf("tuple nonexistence assign: mis[%d]", i)
} }
} }
{ {
_, b := msi[s]; _, b := msi[s]
if b { if b {
fmt.Printf("tuple nonexistence decl: msi[%d]", i); fmt.Printf("tuple nonexistence decl: msi[%d]", i)
} }
_, b = msi[s]; _, b = msi[s]
if b { if b {
fmt.Printf("tuple nonexistence assign: msi[%d]", i); fmt.Printf("tuple nonexistence assign: msi[%d]", i)
} }
} }
{ {
_, b := mss[s]; _, b := mss[s]
if b { if b {
fmt.Printf("tuple nonexistence decl: mss[%d]", i); fmt.Printf("tuple nonexistence decl: mss[%d]", i)
} }
_, b = mss[s]; _, b = mss[s]
if b { if b {
fmt.Printf("tuple nonexistence assign: mss[%d]", i); fmt.Printf("tuple nonexistence assign: mss[%d]", i)
} }
} }
{ {
_, b := mspa[s]; _, b := mspa[s]
if b { if b {
fmt.Printf("tuple nonexistence decl: mspa[%d]", i); fmt.Printf("tuple nonexistence decl: mspa[%d]", i)
} }
_, b = mspa[s]; _, b = mspa[s]
if b { if b {
fmt.Printf("tuple nonexistence assign: mspa[%d]", i); fmt.Printf("tuple nonexistence assign: mspa[%d]", i)
} }
} }
{ {
_, b := mipT[i]; _, b := mipT[i]
if b { if b {
fmt.Printf("tuple nonexistence decl: mipT[%d]", i); fmt.Printf("tuple nonexistence decl: mipT[%d]", i)
} }
_, b = mipT[i]; _, b = mipT[i]
if b { if b {
fmt.Printf("tuple nonexistence assign: mipT[%d]", i); fmt.Printf("tuple nonexistence assign: mipT[%d]", i)
} }
} }
{ {
_, b := mpTi[apT[i]]; _, b := mpTi[apT[i]]
if b { if b {
fmt.Printf("tuple nonexistence decl: mpTi[apt[%d]]", i); fmt.Printf("tuple nonexistence decl: mpTi[apt[%d]]", i)
} }
_, b = mpTi[apT[i]]; _, b = mpTi[apT[i]]
if b { if b {
fmt.Printf("tuple nonexistence assign: mpTi[apT[%d]]", i); fmt.Printf("tuple nonexistence assign: mpTi[apT[%d]]", i)
} }
} }
{ {
_, b := mipM[i]; _, b := mipM[i]
if b { if b {
fmt.Printf("tuple nonexistence decl: mipM[%d]", i); fmt.Printf("tuple nonexistence decl: mipM[%d]", i)
} }
_, b = mipM[i]; _, b = mipM[i]
if b { if b {
fmt.Printf("tuple nonexistence assign: mipM[%d]", i); fmt.Printf("tuple nonexistence assign: mipM[%d]", i)
} }
} }
// { // {
// _, b := mti[t]; // _, b := mti[t]
// if b { // if b {
// fmt.Printf("tuple nonexistence decl: mti[%d]", i); // fmt.Printf("tuple nonexistence decl: mti[%d]", i)
// } // }
// _, b = mti[t]; // _, b = mti[t]
// if b { // if b {
// fmt.Printf("tuple nonexistence assign: mti[%d]", i); // fmt.Printf("tuple nonexistence assign: mti[%d]", i)
// } // }
// } // }
{ {
_, b := mit[i]; _, b := mit[i]
if b { if b {
fmt.Printf("tuple nonexistence decl: mit[%d]", i); fmt.Printf("tuple nonexistence decl: mit[%d]", i)
} }
_, b = mit[i]; _, b = mit[i]
if b { if b {
fmt.Printf("tuple nonexistence assign: mit[%d]", i); fmt.Printf("tuple nonexistence assign: mit[%d]", i)
} }
} }
} }
...@@ -463,30 +463,30 @@ func main() { ...@@ -463,30 +463,30 @@ func main() {
// tests for structured map element updates // tests for structured map element updates
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
s := strconv.Itoa(i); s := strconv.Itoa(i)
mspa[s][i % 2] = "deleted"; mspa[s][i % 2] = "deleted"
if mspa[s][i % 2] != "deleted" { if mspa[s][i % 2] != "deleted" {
fmt.Printf("update mspa[%s][%d] = %s\n", s, i %2, mspa[s][i % 2]); fmt.Printf("update mspa[%s][%d] = %s\n", s, i %2, mspa[s][i % 2])
} }
mipT[i].i += 1; mipT[i].i += 1
if mipT[i].i != int64(i)+1 { if mipT[i].i != int64(i)+1 {
fmt.Printf("update mipT[%d].i = %d\n", i, mipT[i].i); fmt.Printf("update mipT[%d].i = %d\n", i, mipT[i].i)
} }
mipT[i].f = float(i + 1); mipT[i].f = float(i + 1)
if (mipT[i].f != float(i + 1)) { if (mipT[i].f != float(i + 1)) {
fmt.Printf("update mipT[%d].f = %g\n", i, mipT[i].f); fmt.Printf("update mipT[%d].f = %g\n", i, mipT[i].f)
} }
mipM[i][i]++; mipM[i][i]++
if mipM[i][i] != (i + 1) + 1 { if mipM[i][i] != (i + 1) + 1 {
fmt.Printf("update mipM[%d][%d] = %i\n", i, i, mipM[i][i]); fmt.Printf("update mipM[%d][%d] = %i\n", i, i, mipM[i][i])
} }
} }
// test range on nil map // test range on nil map
var mnil map[string] int; var mnil map[string] int
for _, _ = range mnil { for _, _ = range mnil {
panic("range mnil"); panic("range mnil")
} }
} }
...@@ -7,11 +7,11 @@ ...@@ -7,11 +7,11 @@
package main package main
type T struct { } type T struct { }
func (t *T) M(int, string); // GCCGO_ERROR "previous" func (t *T) M(int, string) // GCCGO_ERROR "previous"
func (t *T) M(int, float) { } // ERROR "redeclared|redefinition" func (t *T) M(int, float) { } // ERROR "redeclared|redefinition"
func f(int, string); // GCCGO_ERROR "previous" func f(int, string) // GCCGO_ERROR "previous"
func f(int, float) { } // ERROR "redeclared|redefinition" func f(int, float) { } // ERROR "redeclared|redefinition"
func g(a int, b string); // GCCGO_ERROR "previous" func g(a int, b string) // GCCGO_ERROR "previous"
func g(a int, c string); // ERROR "redeclared|redefinition" func g(a int, c string) // ERROR "redeclared|redefinition"
...@@ -14,24 +14,24 @@ type IN interface { ...@@ -14,24 +14,24 @@ type IN interface {
} }
func main() { func main() {
var i *int; var i *int
var f *float; var f *float
var s *string; var s *string
var m map[float] *int; var m map[float] *int
var c chan int; var c chan int
var t *T; var t *T
var in IN; var in IN
var ta []IN; var ta []IN
i = nil; i = nil
f = nil; f = nil
s = nil; s = nil
m = nil; m = nil
c = nil; c = nil
t = nil; t = nil
i = nil; i = nil
ta = make([]IN, 1); ta = make([]IN, 1)
ta[0] = nil; ta[0] = nil
_, _, _, _, _, _, _, _ = i, f, s, m, c, t, in, ta; _, _, _, _, _, _, _, _ = i, f, s, m, c, t, in, ta
} }
...@@ -13,8 +13,8 @@ import "unsafe" ...@@ -13,8 +13,8 @@ import "unsafe"
var x byte var x byte
func main() { func main() {
var p *[1<<30]byte = nil; var p *[1<<30]byte = nil
x = 123; x = 123
// The problem here is not the use of unsafe: // The problem here is not the use of unsafe:
// it is that indexing into p[] with a large // it is that indexing into p[] with a large
...@@ -23,5 +23,5 @@ func main() { ...@@ -23,5 +23,5 @@ func main() {
// Pointer offsets and array indices, if they are // Pointer offsets and array indices, if they are
// very large, need to dereference the base pointer // very large, need to dereference the base pointer
// to trigger a trap. // to trigger a trap.
println(p[uintptr(unsafe.Pointer(&x))]); // should crash println(p[uintptr(unsafe.Pointer(&x))]) // should crash
} }
...@@ -10,7 +10,7 @@ package main ...@@ -10,7 +10,7 @@ package main
import "unsafe" import "unsafe"
var dummy [512<<20]byte; // give us a big address space var dummy [512<<20]byte // give us a big address space
func main() { func main() {
// the test only tests what we intend to test // the test only tests what we intend to test
// if dummy starts in the first 256 MB of memory. // if dummy starts in the first 256 MB of memory.
...@@ -18,7 +18,7 @@ func main() { ...@@ -18,7 +18,7 @@ func main() {
// at the address that might be accidentally // at the address that might be accidentally
// dereferenced below. // dereferenced below.
if uintptr(unsafe.Pointer(&dummy)) > 256<<20 { if uintptr(unsafe.Pointer(&dummy)) > 256<<20 {
panic("dummy too far out"); panic("dummy too far out")
} }
// The problem here is that indexing into p[] with a large // The problem here is that indexing into p[] with a large
...@@ -27,6 +27,6 @@ func main() { ...@@ -27,6 +27,6 @@ func main() {
// Pointer offsets and array indices, if they are // Pointer offsets and array indices, if they are
// very large, need to dereference the base pointer // very large, need to dereference the base pointer
// to trigger a trap. // to trigger a trap.
var p *[1<<30]byte = nil; var p *[1<<30]byte = nil
println(p[256<<20]); // very likely to be inside dummy, but should crash println(p[256<<20]) // very likely to be inside dummy, but should crash
} }
...@@ -11,10 +11,10 @@ package main ...@@ -11,10 +11,10 @@ package main
import "unsafe" import "unsafe"
func f([]byte) { func f([]byte) {
panic("unreachable"); panic("unreachable")
} }
var dummy [512<<20]byte; // give us a big address space var dummy [512<<20]byte // give us a big address space
func main() { func main() {
// the test only tests what we intend to test // the test only tests what we intend to test
// if dummy starts in the first 256 MB of memory. // if dummy starts in the first 256 MB of memory.
...@@ -22,7 +22,7 @@ func main() { ...@@ -22,7 +22,7 @@ func main() {
// at the address that might be accidentally // at the address that might be accidentally
// dereferenced below. // dereferenced below.
if uintptr(unsafe.Pointer(&dummy)) > 256<<20 { if uintptr(unsafe.Pointer(&dummy)) > 256<<20 {
panic("dummy too far out"); panic("dummy too far out")
} }
// The problem here is that indexing into p[] with a large // The problem here is that indexing into p[] with a large
...@@ -32,6 +32,6 @@ func main() { ...@@ -32,6 +32,6 @@ func main() {
// To avoid needing a check on every slice beyond the // To avoid needing a check on every slice beyond the
// usual len and cap, we require the *array -> slice // usual len and cap, we require the *array -> slice
// conversion to do the check. // conversion to do the check.
var p *[1<<30]byte = nil; var p *[1<<30]byte = nil
f(p[0:]); // should crash f(p[0:]) // should crash
} }
...@@ -10,7 +10,7 @@ package main ...@@ -10,7 +10,7 @@ package main
import "unsafe" import "unsafe"
var dummy [512<<20]byte; // give us a big address space var dummy [512<<20]byte // give us a big address space
func main() { func main() {
// the test only tests what we intend to test // the test only tests what we intend to test
// if dummy starts in the first 256 MB of memory. // if dummy starts in the first 256 MB of memory.
...@@ -18,7 +18,7 @@ func main() { ...@@ -18,7 +18,7 @@ func main() {
// at the address that might be accidentally // at the address that might be accidentally
// dereferenced below. // dereferenced below.
if uintptr(unsafe.Pointer(&dummy)) > 256<<20 { if uintptr(unsafe.Pointer(&dummy)) > 256<<20 {
panic("dummy too far out"); panic("dummy too far out")
} }
// The problem here is that indexing into p[] with a large // The problem here is that indexing into p[] with a large
...@@ -28,7 +28,7 @@ func main() { ...@@ -28,7 +28,7 @@ func main() {
// To avoid needing a check on every slice beyond the // To avoid needing a check on every slice beyond the
// usual len and cap, we require the *array -> slice // usual len and cap, we require the *array -> slice
// conversion to do the check. // conversion to do the check.
var p *[1<<30]byte = nil; var p *[1<<30]byte = nil
var x []byte = p[0:]; // should crash var x []byte = p[0:] // should crash
_ = x; _ = x
} }
...@@ -10,8 +10,8 @@ package main ...@@ -10,8 +10,8 @@ package main
import "unsafe" import "unsafe"
var dummy [512<<20]byte; // give us a big address space var dummy [512<<20]byte // give us a big address space
var q *[1<<30]byte; var q *[1<<30]byte
func main() { func main() {
// the test only tests what we intend to test // the test only tests what we intend to test
// if dummy starts in the first 256 MB of memory. // if dummy starts in the first 256 MB of memory.
...@@ -19,7 +19,7 @@ func main() { ...@@ -19,7 +19,7 @@ func main() {
// at the address that might be accidentally // at the address that might be accidentally
// dereferenced below. // dereferenced below.
if uintptr(unsafe.Pointer(&dummy)) > 256<<20 { if uintptr(unsafe.Pointer(&dummy)) > 256<<20 {
panic("dummy too far out"); panic("dummy too far out")
} }
// The problem here is that indexing into p[] with a large // The problem here is that indexing into p[] with a large
...@@ -29,7 +29,7 @@ func main() { ...@@ -29,7 +29,7 @@ func main() {
// To avoid needing a check on every slice beyond the // To avoid needing a check on every slice beyond the
// usual len and cap, we require the *array -> slice // usual len and cap, we require the *array -> slice
// conversion to do the check. // conversion to do the check.
var x []byte; var x []byte
var y = &x; var y = &x
*y = q[0:]; // should crash (uses arraytoslice runtime routine) *y = q[0:] // should crash (uses arraytoslice runtime routine)
} }
...@@ -10,7 +10,7 @@ package main ...@@ -10,7 +10,7 @@ package main
import "unsafe" import "unsafe"
var dummy [512<<20]byte; // give us a big address space var dummy [512<<20]byte // give us a big address space
func main() { func main() {
// the test only tests what we intend to test // the test only tests what we intend to test
// if dummy starts in the first 256 MB of memory. // if dummy starts in the first 256 MB of memory.
...@@ -18,7 +18,7 @@ func main() { ...@@ -18,7 +18,7 @@ func main() {
// at the address that might be accidentally // at the address that might be accidentally
// dereferenced below. // dereferenced below.
if uintptr(unsafe.Pointer(&dummy)) > 256<<20 { if uintptr(unsafe.Pointer(&dummy)) > 256<<20 {
panic("dummy too far out"); panic("dummy too far out")
} }
// The problem here is that indexing into p[] with a large // The problem here is that indexing into p[] with a large
...@@ -28,6 +28,6 @@ func main() { ...@@ -28,6 +28,6 @@ func main() {
// To avoid needing a check on every slice beyond the // To avoid needing a check on every slice beyond the
// usual len and cap, we require the slice operation // usual len and cap, we require the slice operation
// to do the check. // to do the check.
var p *[1<<30]byte = nil; var p *[1<<30]byte = nil
var _ []byte = p[10:len(p)-10]; // should crash var _ []byte = p[10:len(p)-10] // should crash
} }
...@@ -10,10 +10,10 @@ package main ...@@ -10,10 +10,10 @@ package main
import "unsafe" import "unsafe"
var dummy [512<<20]byte; // give us a big address space var dummy [512<<20]byte // give us a big address space
type T struct { type T struct {
x [256<<20] byte; x [256<<20] byte
i int; i int
} }
func main() { func main() {
...@@ -23,13 +23,13 @@ func main() { ...@@ -23,13 +23,13 @@ func main() {
// at the address that might be accidentally // at the address that might be accidentally
// dereferenced below. // dereferenced below.
if uintptr(unsafe.Pointer(&dummy)) > 256<<20 { if uintptr(unsafe.Pointer(&dummy)) > 256<<20 {
panic("dummy too far out"); panic("dummy too far out")
} }
// The problem here is that indexing into t with a large // The problem here is that indexing into t with a large
// enough index can jump out of the unmapped section // enough index can jump out of the unmapped section
// at the beginning of memory and into valid memory. // at the beginning of memory and into valid memory.
// We require the pointer dereference to check. // We require the pointer dereference to check.
var t *T; var t *T
println(t.i); // should crash println(t.i) // should crash
} }
...@@ -10,14 +10,14 @@ package main ...@@ -10,14 +10,14 @@ package main
import "unsafe" import "unsafe"
var dummy [512<<20]byte; // give us a big address space var dummy [512<<20]byte // give us a big address space
type T struct { type T struct {
x [256<<20] byte; x [256<<20] byte
i int; i int
} }
func f() *T { func f() *T {
return nil; return nil
} }
func main() { func main() {
...@@ -27,12 +27,12 @@ func main() { ...@@ -27,12 +27,12 @@ func main() {
// at the address that might be accidentally // at the address that might be accidentally
// dereferenced below. // dereferenced below.
if uintptr(unsafe.Pointer(&dummy)) > 256<<20 { if uintptr(unsafe.Pointer(&dummy)) > 256<<20 {
panic("dummy too far out"); panic("dummy too far out")
} }
// The problem here is that indexing into t with a large // The problem here is that indexing into t with a large
// enough index can jump out of the unmapped section // enough index can jump out of the unmapped section
// at the beginning of memory and into valid memory. // at the beginning of memory and into valid memory.
// We require the pointer dereference to check. // We require the pointer dereference to check.
println(f().i); // should crash println(f().i) // should crash
} }
...@@ -10,14 +10,14 @@ package main ...@@ -10,14 +10,14 @@ package main
import "unsafe" import "unsafe"
var dummy [512<<20]byte; // give us a big address space var dummy [512<<20]byte // give us a big address space
type T struct { type T struct {
x [256<<20] byte; x [256<<20] byte
i int; i int
} }
var y *T; var y *T
var x = &y; var x = &y
func main() { func main() {
// the test only tests what we intend to test // the test only tests what we intend to test
...@@ -26,12 +26,12 @@ func main() { ...@@ -26,12 +26,12 @@ func main() {
// at the address that might be accidentally // at the address that might be accidentally
// dereferenced below. // dereferenced below.
if uintptr(unsafe.Pointer(&dummy)) > 256<<20 { if uintptr(unsafe.Pointer(&dummy)) > 256<<20 {
panic("dummy too far out"); panic("dummy too far out")
} }
// The problem here is that indexing into t with a large // The problem here is that indexing into t with a large
// enough index can jump out of the unmapped section // enough index can jump out of the unmapped section
// at the beginning of memory and into valid memory. // at the beginning of memory and into valid memory.
// We require the pointer dereference to check. // We require the pointer dereference to check.
println((*x).i); // should crash println((*x).i) // should crash
} }
...@@ -10,10 +10,10 @@ package main ...@@ -10,10 +10,10 @@ package main
import "unsafe" import "unsafe"
var dummy [512<<20]byte; // give us a big address space var dummy [512<<20]byte // give us a big address space
type T struct { type T struct {
x [256<<20] byte; x [256<<20] byte
i int; i int
} }
func main() { func main() {
...@@ -23,13 +23,13 @@ func main() { ...@@ -23,13 +23,13 @@ func main() {
// at the address that might be accidentally // at the address that might be accidentally
// dereferenced below. // dereferenced below.
if uintptr(unsafe.Pointer(&dummy)) > 256<<20 { if uintptr(unsafe.Pointer(&dummy)) > 256<<20 {
panic("dummy too far out"); panic("dummy too far out")
} }
// The problem here is that indexing into t with a large // The problem here is that indexing into t with a large
// enough index can jump out of the unmapped section // enough index can jump out of the unmapped section
// at the beginning of memory and into valid memory. // at the beginning of memory and into valid memory.
// We require the address calculation to check. // We require the address calculation to check.
var t *T; var t *T
println(&t.i); // should crash println(&t.i) // should crash
} }
...@@ -25,7 +25,7 @@ func main() { ...@@ -25,7 +25,7 @@ func main() {
if len(s) != 2 || s[0] != 0xc2 || s[1] != 0xff || if len(s) != 2 || s[0] != 0xc2 || s[1] != 0xff ||
len(t) != 2 || t[0] != 0xd0 || t[1] != 0xfe || len(t) != 2 || t[0] != 0xd0 || t[1] != 0xfe ||
len(u) != 3 || u[0] != 0xab || u[1] != 0x00 || u[2] != 0xfc { len(u) != 3 || u[0] != 0xab || u[1] != 0x00 || u[2] != 0xfc {
println("BUG: non-UTF-8 string mangled"); println("BUG: non-UTF-8 string mangled")
os.Exit(2) os.Exit(2)
} }
......
...@@ -9,9 +9,9 @@ package main ...@@ -9,9 +9,9 @@ package main
func f(interface{}) func f(interface{})
func g() {} func g() {}
func main() { func main() {
f(map[string]string{"a":"b","c":"d"}); f(map[string]string{"a":"b","c":"d"})
f([...]int{1,2,3}); f([...]int{1,2,3})
f(map[string]func(){"a":g,"c":g}); f(map[string]func(){"a":g,"c":g})
f(make(chan(<-chan int))); f(make(chan(<-chan int)))
f(make(chan<-(chan int))); f(make(chan<-(chan int)))
} }
...@@ -7,6 +7,6 @@ ...@@ -7,6 +7,6 @@
package main package main
func main() { func main() {
print(-(1<<63), "\n"); print(-(1<<63), "\n")
print((1<<63)-1, "\n") print((1<<63)-1, "\n")
} }
...@@ -16,5 +16,5 @@ package main ...@@ -16,5 +16,5 @@ package main
import "runtime" import "runtime"
func main() { func main() {
runtime.printbool(true); // ERROR "unexported" runtime.printbool(true) // ERROR "unexported"
} }
...@@ -17,8 +17,8 @@ func Generate(ch chan<- int) { ...@@ -17,8 +17,8 @@ func Generate(ch chan<- int) {
// removing those divisible by 'prime'. // removing those divisible by 'prime'.
func Filter(in <-chan int, out chan<- int, prime int) { func Filter(in <-chan int, out chan<- int, prime int) {
for { for {
i := <-in; // Receive value of new variable 'i' from 'in'. i := <-in // Receive value of new variable 'i' from 'in'.
if i % prime != 0 { if i%prime != 0 {
out <- i // Send 'i' to channel 'out'. out <- i // Send 'i' to channel 'out'.
} }
} }
...@@ -26,13 +26,13 @@ func Filter(in <-chan int, out chan<- int, prime int) { ...@@ -26,13 +26,13 @@ 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() { func Sieve() {
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 {
prime := <-ch; prime := <-ch
print(prime, "\n"); print(prime, "\n")
ch1 := make(chan int); ch1 := make(chan int)
go Filter(ch, ch1, prime); go Filter(ch, ch1, prime)
ch = ch1 ch = ch1
} }
} }
......
...@@ -10,6 +10,6 @@ package main ...@@ -10,6 +10,6 @@ package main
import "syscall" import "syscall"
func main() { func main() {
syscall.Kill(syscall.Getpid(), syscall.SIGCHLD); syscall.Kill(syscall.Getpid(), syscall.SIGCHLD)
println("survived SIGCHLD"); println("survived SIGCHLD")
} }
...@@ -9,10 +9,10 @@ package p ...@@ -9,10 +9,10 @@ package p
// Should be no init func in the assembly. // Should be no init func in the assembly.
// All these initializations should be done at link time. // All these initializations should be done at link time.
type S struct{ a,b,c int }; type S struct{ a,b,c int }
type SS struct{ aa,bb,cc S }; type SS struct{ aa,bb,cc S }
type SA struct{ a,b,c [3]int }; type SA struct{ a,b,c [3]int }
type SC struct{ a,b,c []int }; type SC struct{ a,b,c []int }
var ( var (
zero = 2 zero = 2
......
...@@ -12,15 +12,15 @@ var ecode int ...@@ -12,15 +12,15 @@ var ecode int
func assert(a, b, c string) { func assert(a, b, c string) {
if a != b { if a != b {
ecode = 1; ecode = 1
print("FAIL: ", c, ": ", a, "!=", b, "\n"); print("FAIL: ", c, ": ", a, "!=", b, "\n")
var max int = len(a); var max int = len(a)
if len(b) > max { if len(b) > max {
max = len(b) max = len(b)
} }
for i := 0; i < max; i++ { for i := 0; i < max; i++ {
ac := 0; ac := 0
bc := 0; bc := 0
if i < len(a) { if i < len(a) {
ac = int(a[i]) ac = int(a[i])
} }
...@@ -48,7 +48,7 @@ var ( ...@@ -48,7 +48,7 @@ var (
) )
func main() { func main() {
ecode = 0; ecode = 0
s := s :=
"" + "" +
" " + " " +
...@@ -67,38 +67,38 @@ func main() { ...@@ -67,38 +67,38 @@ func main() {
`本` + `本` +
`\a\b\f\n\r\t\v\\\'` + `\a\b\f\n\r\t\v\\\'` +
`\000\123\x00\xca\xFE\u0123\ubabe\U0000babe` + `\000\123\x00\xca\xFE\u0123\ubabe\U0000babe` +
`\x\u\U\`; `\x\u\U\`
assert("", ``, "empty"); assert("", ``, "empty")
assert(" ", " ", "blank"); assert(" ", " ", "blank")
assert("\x61", "a", "lowercase a"); assert("\x61", "a", "lowercase a")
assert("\x61", `a`, "lowercase a (backquote)"); assert("\x61", `a`, "lowercase a (backquote)")
assert("\u00e4", "ä", "a umlaut"); assert("\u00e4", "ä", "a umlaut")
assert("\u00e4", `ä`, "a umlaut (backquote)"); assert("\u00e4", `ä`, "a umlaut (backquote)")
assert("\u672c", "本", "nihon"); assert("\u672c", "本", "nihon")
assert("\u672c", `本`, "nihon (backquote)"); assert("\u672c", `本`, "nihon (backquote)")
assert("\x07\x08\x0c\x0a\x0d\x09\x0b\x5c\x22", assert("\x07\x08\x0c\x0a\x0d\x09\x0b\x5c\x22",
"\a\b\f\n\r\t\v\\\"", "\a\b\f\n\r\t\v\\\"",
"backslashes"); "backslashes")
assert("\\a\\b\\f\\n\\r\\t\\v\\\\\\\"", assert("\\a\\b\\f\\n\\r\\t\\v\\\\\\\"",
`\a\b\f\n\r\t\v\\\"`, `\a\b\f\n\r\t\v\\\"`,
"backslashes (backquote)"); "backslashes (backquote)")
assert("\x00\x53\000\xca\376S몾몾", assert("\x00\x53\000\xca\376S몾몾",
"\000\123\x00\312\xFE\u0053\ubabe\U0000babe", "\000\123\x00\312\xFE\u0053\ubabe\U0000babe",
"backslashes 2"); "backslashes 2")
assert("\\000\\123\\x00\\312\\xFE\\u0123\\ubabe\\U0000babe", assert("\\000\\123\\x00\\312\\xFE\\u0123\\ubabe\\U0000babe",
`\000\123\x00\312\xFE\u0123\ubabe\U0000babe`, `\000\123\x00\312\xFE\u0123\ubabe\U0000babe`,
"backslashes 2 (backquote)"); "backslashes 2 (backquote)")
assert("\\x\\u\\U\\", `\x\u\U\`, "backslash 3 (backquote)"); assert("\\x\\u\\U\\", `\x\u\U\`, "backslash 3 (backquote)")
// test large runes. perhaps not the most logical place for this test. // test large runes. perhaps not the most logical place for this test.
var r int32; var r int32
r = 0x10ffff; // largest rune value r = 0x10ffff; // largest rune value
s = string(r); s = string(r)
assert(s, "\xf4\x8f\xbf\xbf", "largest rune"); assert(s, "\xf4\x8f\xbf\xbf", "largest rune")
r = 0x10ffff + 1; r = 0x10ffff + 1
s = string(r); s = string(r)
assert(s, "\xef\xbf\xbd", "too-large rune"); assert(s, "\xef\xbf\xbd", "too-large rune")
assert(string(gr1), gx1, "global ->[]int") assert(string(gr1), gx1, "global ->[]int")
assert(string(gr2), gx2fix, "global invalid ->[]int") assert(string(gr2), gx2fix, "global invalid ->[]int")
...@@ -116,5 +116,5 @@ func main() { ...@@ -116,5 +116,5 @@ func main() {
assert(string(b1), gx1, "->[]byte") assert(string(b1), gx1, "->[]byte")
assert(string(b2), gx2, "invalid ->[]byte") assert(string(b2), gx2, "invalid ->[]byte")
os.Exit(ecode); os.Exit(ecode)
} }
...@@ -7,55 +7,55 @@ ...@@ -7,55 +7,55 @@
package main package main
import ( import (
"fmt"; "fmt"
"os"; "os"
"utf8"; "utf8"
) )
func main() { func main() {
s := "\000\123\x00\xca\xFE\u0123\ubabe\U0000babe\U0010FFFFx"; s := "\000\123\x00\xca\xFE\u0123\ubabe\U0000babe\U0010FFFFx"
expect := []int{ 0, 0123, 0, 0xFFFD, 0xFFFD, 0x123, 0xbabe, 0xbabe, 0x10FFFF, 'x' }; expect := []int{ 0, 0123, 0, 0xFFFD, 0xFFFD, 0x123, 0xbabe, 0xbabe, 0x10FFFF, 'x' }
offset := 0; offset := 0
var i, c int; var i, c int
ok := true; ok := true
cnum := 0; cnum := 0
for i, c = range s { for i, c = range s {
rune, size := utf8.DecodeRuneInString(s[i:len(s)]); // check it another way rune, size := utf8.DecodeRuneInString(s[i:len(s)]) // check it another way
if i != offset { if i != offset {
fmt.Printf("unexpected offset %d not %d\n", i, offset); fmt.Printf("unexpected offset %d not %d\n", i, offset)
ok = false; ok = false
} }
if rune != expect[cnum] { if rune != expect[cnum] {
fmt.Printf("unexpected rune %d from DecodeRuneInString: %x not %x\n", i, rune, expect[cnum]); fmt.Printf("unexpected rune %d from DecodeRuneInString: %x not %x\n", i, rune, expect[cnum])
ok = false; ok = false
} }
if c != expect[cnum] { if c != expect[cnum] {
fmt.Printf("unexpected rune %d from range: %x not %x\n", i, rune, expect[cnum]); fmt.Printf("unexpected rune %d from range: %x not %x\n", i, rune, expect[cnum])
ok = false; ok = false
} }
offset += size; offset += size
cnum++; cnum++
} }
if i != len(s)-1 { if i != len(s)-1 {
fmt.Println("after loop i is", i, "not", len(s)-1); fmt.Println("after loop i is", i, "not", len(s)-1)
ok = false; ok = false
} }
i = 12345; i = 12345
c = 23456; c = 23456
for i, c = range "" { for i, c = range "" {
} }
if i != 12345 { if i != 12345 {
fmt.Println("range empty string assigned to index:", i); fmt.Println("range empty string assigned to index:", i)
ok = false; ok = false
} }
if c != 23456 { if c != 23456 {
fmt.Println("range empty string assigned to value:", c); fmt.Println("range empty string assigned to value:", c)
ok = false; ok = false
} }
if !ok { if !ok {
fmt.Println("BUG: stringrange"); fmt.Println("BUG: stringrange")
os.Exit(1) os.Exit(1)
} }
} }
...@@ -8,59 +8,59 @@ package main ...@@ -8,59 +8,59 @@ package main
func assert(cond bool, msg string) { func assert(cond bool, msg string) {
if !cond { if !cond {
print("assertion fail: ", msg, "\n"); print("assertion fail: ", msg, "\n")
panic(1); panic(1)
} }
} }
func main() { func main() {
i5 := 5; i5 := 5
i7 := 7; i7 := 7
hello := "hello"; hello := "hello"
switch true { switch true {
case i5 < 5: assert(false, "<"); case i5 < 5: assert(false, "<")
case i5 == 5: assert(true, "!"); case i5 == 5: assert(true, "!")
case i5 > 5: assert(false, ">"); case i5 > 5: assert(false, ">")
} }
switch { switch {
case i5 < 5: assert(false, "<"); case i5 < 5: assert(false, "<")
case i5 == 5: assert(true, "!"); case i5 == 5: assert(true, "!")
case i5 > 5: assert(false, ">"); case i5 > 5: assert(false, ">")
} }
switch x := 5; true { switch x := 5; true {
case i5 < x: assert(false, "<"); case i5 < x: assert(false, "<")
case i5 == x: assert(true, "!"); case i5 == x: assert(true, "!")
case i5 > x: assert(false, ">"); case i5 > x: assert(false, ">")
} }
switch x := 5; true { switch x := 5; true {
case i5 < x: assert(false, "<"); case i5 < x: assert(false, "<")
case i5 == x: assert(true, "!"); case i5 == x: assert(true, "!")
case i5 > x: assert(false, ">"); case i5 > x: assert(false, ">")
} }
switch i5 { switch i5 {
case 0: assert(false, "0"); case 0: assert(false, "0")
case 1: assert(false, "1"); case 1: assert(false, "1")
case 2: assert(false, "2"); case 2: assert(false, "2")
case 3: assert(false, "3"); case 3: assert(false, "3")
case 4: assert(false, "4"); case 4: assert(false, "4")
case 5: assert(true, "5"); case 5: assert(true, "5")
case 6: assert(false, "6"); case 6: assert(false, "6")
case 7: assert(false, "7"); case 7: assert(false, "7")
case 8: assert(false, "8"); case 8: assert(false, "8")
case 9: assert(false, "9"); case 9: assert(false, "9")
default: assert(false, "default"); default: assert(false, "default")
} }
switch i5 { switch i5 {
case 0,1,2,3,4: assert(false, "4"); case 0,1,2,3,4: assert(false, "4")
case 5: assert(true, "5"); case 5: assert(true, "5")
case 6,7,8,9: assert(false, "9"); case 6,7,8,9: assert(false, "9")
default: assert(false, "default"); default: assert(false, "default")
} }
switch i5 { switch i5 {
...@@ -68,72 +68,72 @@ func main() { ...@@ -68,72 +68,72 @@ func main() {
case 1: case 1:
case 2: case 2:
case 3: case 3:
case 4: assert(false, "4"); case 4: assert(false, "4")
case 5: assert(true, "5"); case 5: assert(true, "5")
case 6: case 6:
case 7: case 7:
case 8: case 8:
case 9: case 9:
default: assert(i5 == 5, "good"); default: assert(i5 == 5, "good")
} }
switch i5 { switch i5 {
case 0: dummy := 0; _ = dummy; fallthrough; case 0: dummy := 0; _ = dummy; fallthrough
case 1: dummy := 0; _ = dummy; fallthrough; case 1: dummy := 0; _ = dummy; fallthrough
case 2: dummy := 0; _ = dummy; fallthrough; case 2: dummy := 0; _ = dummy; fallthrough
case 3: dummy := 0; _ = dummy; fallthrough; case 3: dummy := 0; _ = dummy; fallthrough
case 4: dummy := 0; _ = dummy; assert(false, "4"); case 4: dummy := 0; _ = dummy; assert(false, "4")
case 5: dummy := 0; _ = dummy; fallthrough; case 5: dummy := 0; _ = dummy; fallthrough
case 6: dummy := 0; _ = dummy; fallthrough; case 6: dummy := 0; _ = dummy; fallthrough
case 7: dummy := 0; _ = dummy; fallthrough; case 7: dummy := 0; _ = dummy; fallthrough
case 8: dummy := 0; _ = dummy; fallthrough; case 8: dummy := 0; _ = dummy; fallthrough
case 9: dummy := 0; _ = dummy; fallthrough; case 9: dummy := 0; _ = dummy; fallthrough
default: dummy := 0; _ = dummy; assert(i5 == 5, "good"); default: dummy := 0; _ = dummy; assert(i5 == 5, "good")
} }
fired := false; fired := false
switch i5 { switch i5 {
case 0: dummy := 0; _ = dummy; fallthrough; // tests scoping of cases case 0: dummy := 0; _ = dummy; fallthrough; // tests scoping of cases
case 1: dummy := 0; _ = dummy; fallthrough; case 1: dummy := 0; _ = dummy; fallthrough
case 2: dummy := 0; _ = dummy; fallthrough; case 2: dummy := 0; _ = dummy; fallthrough
case 3: dummy := 0; _ = dummy; fallthrough; case 3: dummy := 0; _ = dummy; fallthrough
case 4: dummy := 0; _ = dummy; assert(false, "4"); case 4: dummy := 0; _ = dummy; assert(false, "4")
case 5: dummy := 0; _ = dummy; fallthrough; case 5: dummy := 0; _ = dummy; fallthrough
case 6: dummy := 0; _ = dummy; fallthrough; case 6: dummy := 0; _ = dummy; fallthrough
case 7: dummy := 0; _ = dummy; fallthrough; case 7: dummy := 0; _ = dummy; fallthrough
case 8: dummy := 0; _ = dummy; fallthrough; case 8: dummy := 0; _ = dummy; fallthrough
case 9: dummy := 0; _ = dummy; fallthrough; case 9: dummy := 0; _ = dummy; fallthrough
default: dummy := 0; _ = dummy; fired = !fired; assert(i5 == 5, "good"); default: dummy := 0; _ = dummy; fired = !fired; assert(i5 == 5, "good")
} }
assert(fired, "fired"); assert(fired, "fired")
count := 0; count := 0
switch i5 { switch i5 {
case 0: count = count + 1; fallthrough; case 0: count = count + 1; fallthrough
case 1: count = count + 1; fallthrough; case 1: count = count + 1; fallthrough
case 2: count = count + 1; fallthrough; case 2: count = count + 1; fallthrough
case 3: count = count + 1; fallthrough; case 3: count = count + 1; fallthrough
case 4: count = count + 1; assert(false, "4"); case 4: count = count + 1; assert(false, "4")
case 5: count = count + 1; fallthrough; case 5: count = count + 1; fallthrough
case 6: count = count + 1; fallthrough; case 6: count = count + 1; fallthrough
case 7: count = count + 1; fallthrough; case 7: count = count + 1; fallthrough
case 8: count = count + 1; fallthrough; case 8: count = count + 1; fallthrough
case 9: count = count + 1; fallthrough; case 9: count = count + 1; fallthrough
default: assert(i5 == count, "good"); default: assert(i5 == count, "good")
} }
assert(fired, "fired"); assert(fired, "fired")
switch hello { switch hello {
case "wowie": assert(false, "wowie"); case "wowie": assert(false, "wowie")
case "hello": assert(true, "hello"); case "hello": assert(true, "hello")
case "jumpn": assert(false, "jumpn"); case "jumpn": assert(false, "jumpn")
default: assert(false, "default"); default: assert(false, "default")
} }
fired = false; fired = false
switch i := i5 + 2; i { switch i := i5 + 2; i {
case i7: fired = true; case i7: fired = true
default: assert(false, "fail"); default: assert(false, "fail")
} }
assert(fired, "var"); assert(fired, "var")
} }
...@@ -9,12 +9,12 @@ package main ...@@ -9,12 +9,12 @@ package main
import "os" import "os"
func main() { func main() {
i := 0; i := 0
switch x := 5; { switch x := 5; {
case i < x: case i < x:
os.Exit(0); os.Exit(0)
case i == x: case i == x:
case i > x: case i > x:
os.Exit(1); os.Exit(1)
} }
} }
...@@ -10,34 +10,34 @@ const ...@@ -10,34 +10,34 @@ const
a_const = 0 a_const = 0
const ( const (
pi = /* the usual */ 3.14159265358979323; pi = /* the usual */ 3.14159265358979323
e = 2.718281828; e = 2.718281828
mask1 int = 1 << iota; mask1 int = 1 << iota
mask2 = 1 << iota; mask2 = 1 << iota
mask3 = 1 << iota; mask3 = 1 << iota
mask4 = 1 << iota; mask4 = 1 << iota
) )
type ( type (
Empty interface {}; Empty interface {}
Point struct { Point struct {
x, y int; x, y int
}; }
Point2 Point Point2 Point
) )
func (p *Point) Initialize(x, y int) *Point { func (p *Point) Initialize(x, y int) *Point {
p.x, p.y = x, y; p.x, p.y = x, y
return p; return p
} }
func (p *Point) Distance() int { func (p *Point) Distance() int {
return p.x * p.x + p.y * p.y; return p.x * p.x + p.y * p.y
} }
var ( var (
x1 int; x1 int
x2 int; x2 int
u, v, w float u, v, w float
) )
...@@ -45,40 +45,40 @@ func foo() {} ...@@ -45,40 +45,40 @@ func foo() {}
func min(x, y int) int { func min(x, y int) int {
if x < y { return x; } if x < y { return x; }
return y; return y
} }
func swap(x, y int) (u, v int) { func swap(x, y int) (u, v int) {
u = y; u = y
v = x; v = x
return; return
} }
func control_structs() { func control_structs() {
var p *Point = new(Point).Initialize(2, 3); var p *Point = new(Point).Initialize(2, 3)
i := p.Distance(); i := p.Distance()
var f float = 0.3; var f float = 0.3
_ = f; _ = f
for {}
for {} for {}
for {};
for j := 0; j < i; j++ { for j := 0; j < i; j++ {
if i == 0 { if i == 0 {
} else i = 0; } else i = 0
var x float; var x float
_ = x; _ = x
} }
foo: // a label foo: // a label
var j int; var j int
switch y := 0; true { switch y := 0; true {
case i < y: case i < y:
fallthrough; fallthrough
case i < j: case i < j:
case i == 0, i == 1, i == j: case i == 0, i == 1, i == j:
i++; i++; i++; i++
goto foo; goto foo
default: default:
i = -+-+i; i = -+-+i
break; break
} }
} }
......
...@@ -9,22 +9,22 @@ package main ...@@ -9,22 +9,22 @@ package main
import "os" import "os"
const ( const (
Bool = iota; Bool = iota
Int; Int
Float; Float
String; String
Struct; Struct
Chan; Chan
Array; Array
Map; Map
Func; Func
Last; Last
) )
type S struct { a int } type S struct { a int }
var s S = S{1234} var s S = S{1234}
var c = make(chan int); var c = make(chan int)
var a = []int{0,1,2,3} var a = []int{0,1,2,3}
...@@ -32,81 +32,81 @@ var m = make(map[string]int) ...@@ -32,81 +32,81 @@ var m = make(map[string]int)
func assert(b bool, s string) { func assert(b bool, s string) {
if !b { if !b {
println(s); println(s)
os.Exit(1); os.Exit(1)
} }
} }
func f(i int) interface{} { func f(i int) interface{} {
switch i { switch i {
case Bool: case Bool:
return true; return true
case Int: case Int:
return 7; return 7
case Float: case Float:
return 7.4; return 7.4
case String: case String:
return "hello"; return "hello"
case Struct: case Struct:
return s; return s
case Chan: case Chan:
return c; return c
case Array: case Array:
return a; return a
case Map: case Map:
return m; return m
case Func: case Func:
return f; return f
} }
panic("bad type number"); panic("bad type number")
} }
func main() { func main() {
for i := Bool; i < Last; i++ { for i := Bool; i < Last; i++ {
switch x := f(i).(type) { switch x := f(i).(type) {
case bool: case bool:
assert(x == true && i == Bool, "bool"); assert(x == true && i == Bool, "bool")
case int: case int:
assert(x == 7 && i == Int, "int"); assert(x == 7 && i == Int, "int")
case float: case float:
assert(x == 7.4 && i == Float, "float"); assert(x == 7.4 && i == Float, "float")
case string: case string:
assert(x == "hello"&& i == String, "string"); assert(x == "hello"&& i == String, "string")
case S: case S:
assert(x.a == 1234 && i == Struct, "struct"); assert(x.a == 1234 && i == Struct, "struct")
case chan int: case chan int:
assert(x == c && i == Chan, "chan"); assert(x == c && i == Chan, "chan")
case []int: case []int:
assert(x[3] == 3 && i == Array, "array"); assert(x[3] == 3 && i == Array, "array")
case map[string]int: case map[string]int:
assert(x == m && i == Map, "map"); assert(x == m && i == Map, "map")
case func(i int) interface{}: case func(i int) interface{}:
assert(x == f && i == Func, "fun"); assert(x == f && i == Func, "fun")
default: default:
assert(false, "unknown"); assert(false, "unknown")
} }
} }
// boolean switch (has had bugs in past; worth writing down) // boolean switch (has had bugs in past; worth writing down)
switch { switch {
case true: case true:
assert(true, "switch 2 bool"); assert(true, "switch 2 bool")
default: default:
assert(false, "switch 2 unknown"); assert(false, "switch 2 unknown")
} }
switch true { switch true {
case true: case true:
assert(true, "switch 3 bool"); assert(true, "switch 3 bool")
default: default:
assert(false, "switch 3 unknown"); assert(false, "switch 3 unknown")
} }
switch false { switch false {
case false: case false:
assert(true, "switch 4 bool"); assert(true, "switch 4 bool")
default: default:
assert(false, "switch 4 unknown"); assert(false, "switch 4 unknown")
} }
} }
...@@ -9,46 +9,46 @@ package main ...@@ -9,46 +9,46 @@ package main
import "utf8" import "utf8"
func main() { func main() {
var chars [6] int; var chars [6] int
chars[0] = 'a'; chars[0] = 'a'
chars[1] = 'b'; chars[1] = 'b'
chars[2] = 'c'; chars[2] = 'c'
chars[3] = '\u65e5'; chars[3] = '\u65e5'
chars[4] = '\u672c'; chars[4] = '\u672c'
chars[5] = '\u8a9e'; chars[5] = '\u8a9e'
s := ""; s := ""
for i := 0; i < 6; i++ { for i := 0; i < 6; i++ {
s += string(chars[i]); s += string(chars[i])
} }
var l = len(s); var l = len(s)
for w, i, j := 0,0,0; i < l; i += w { for w, i, j := 0,0,0; i < l; i += w {
var r int; var r int
r, w = utf8.DecodeRuneInString(s[i:len(s)]); r, w = utf8.DecodeRuneInString(s[i:len(s)])
if w == 0 { panic("zero width in string") } if w == 0 { panic("zero width in string") }
if r != chars[j] { panic("wrong value from string") } if r != chars[j] { panic("wrong value from string") }
j++; j++
} }
// encoded as bytes: 'a' 'b' 'c' e6 97 a5 e6 9c ac e8 aa 9e // encoded as bytes: 'a' 'b' 'c' e6 97 a5 e6 9c ac e8 aa 9e
const L = 12; const L = 12
if L != l { panic("wrong length constructing array") } if L != l { panic("wrong length constructing array") }
a := make([]byte, L); a := make([]byte, L)
a[0] = 'a'; a[0] = 'a'
a[1] = 'b'; a[1] = 'b'
a[2] = 'c'; a[2] = 'c'
a[3] = 0xe6; a[3] = 0xe6
a[4] = 0x97; a[4] = 0x97
a[5] = 0xa5; a[5] = 0xa5
a[6] = 0xe6; a[6] = 0xe6
a[7] = 0x9c; a[7] = 0x9c
a[8] = 0xac; a[8] = 0xac
a[9] = 0xe8; a[9] = 0xe8
a[10] = 0xaa; a[10] = 0xaa
a[11] = 0x9e; a[11] = 0x9e
for w, i, j := 0,0,0; i < L; i += w { for w, i, j := 0,0,0; i < L; i += w {
var r int; var r int
r, w = utf8.DecodeRune(a[i:L]); r, w = utf8.DecodeRune(a[i:L])
if w == 0 { panic("zero width in bytes") } if w == 0 { panic("zero width in bytes") }
if r != chars[j] { panic("wrong value from bytes") } if r != chars[j] { panic("wrong value from bytes") }
j++; j++
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment