1
0
mirror of https://github.com/golang/go synced 2024-11-22 05:04:40 -07:00

test: gofmt a few tests

I'm planning to change these tests, but the gofmt changes are
fairly extensive, so I'm separating the gofmt changes from the
substantive changes.

R=golang-dev, rsc, r
CC=golang-dev
https://golang.org/cl/5557052
This commit is contained in:
Ian Lance Taylor 2012-01-18 13:20:55 -08:00
parent 20812c4907
commit 6b3462820f
8 changed files with 242 additions and 248 deletions

View File

@ -10,49 +10,55 @@ type Element interface {
} }
type Vector struct { type Vector struct {
nelem int; nelem int
elem []Element; elem []Element
} }
func New() *Vector { func New() *Vector {
v := new(Vector); v := new(Vector)
v.nelem = 0; v.nelem = 0
v.elem = make([]Element, 10); v.elem = make([]Element, 10)
return v; return v
} }
func (v *Vector) At(i int) Element { func (v *Vector) At(i int) Element {
return v.elem[i]; return v.elem[i]
} }
func (v *Vector) Insert(e Element) { func (v *Vector) Insert(e Element) {
v.elem[v.nelem] = e; v.elem[v.nelem] = e
v.nelem++; v.nelem++
} }
func main() { func main() {
type I struct { val int; }; type I struct{ val int }
i0 := new(I); i0.val = 0; i0 := new(I)
i1 := new(I); i1.val = 11; i0.val = 0
i2 := new(I); i2.val = 222; i1 := new(I)
i3 := new(I); i3.val = 3333; i1.val = 11
i4 := new(I); i4.val = 44444; i2 := new(I)
v := New(); i2.val = 222
print("hi\n"); i3 := new(I)
v.Insert(i4); i3.val = 3333
v.Insert(i3); i4 := new(I)
v.Insert(i2); i4.val = 44444
v.Insert(i1); v := New()
v.Insert(i0); print("hi\n")
v.Insert(i4)
v.Insert(i3)
v.Insert(i2)
v.Insert(i1)
v.Insert(i0)
for i := 0; i < v.nelem; i++ { for i := 0; i < v.nelem; i++ {
var x *I; var x *I
x = v.At(i).(*I); x = v.At(i).(*I)
print(i, " ", x.val, "\n"); // prints correct list print(i, " ", x.val, "\n") // prints correct list
} }
for i := 0; i < v.nelem; i++ { for i := 0; i < v.nelem; i++ {
print(i, " ", v.At(i).(*I).val, "\n"); print(i, " ", v.At(i).(*I).val, "\n")
} }
} }
/* /*
bug027.go:50: illegal types for operand bug027.go:50: illegal types for operand
(<Element>I{}) CONV (<I>{}) (<Element>I{}) CONV (<I>{})

View File

@ -7,19 +7,23 @@
package main package main
func main() { func main() {
var i, k int; var i, k int
outer: outer:
for k=0; k<2; k++ { for k = 0; k < 2; k++ {
print("outer loop top k ", k, "\n"); print("outer loop top k ", k, "\n")
if k != 0 { panic("k not zero") } // inner loop breaks this one every time if k != 0 {
for i=0; i<2; i++ { panic("k not zero")
if i != 0 { panic("i not zero") } // loop breaks every time } // inner loop breaks this one every time
print("inner loop top i ", i, "\n"); for i = 0; i < 2; i++ {
if i != 0 {
panic("i not zero")
} // loop breaks every time
print("inner loop top i ", i, "\n")
if true { if true {
print("do break\n"); print("do break\n")
break outer; break outer
} }
} }
} }
print("broke\n"); print("broke\n")
} }

View File

@ -4,33 +4,31 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package main package main
func func main() {
main() { i := 0
i := 0;
if false { if false {
goto gogoloop; goto gogoloop
} }
if false { if false {
goto gogoloop; goto gogoloop
} }
if false { if false {
goto gogoloop; goto gogoloop
} }
goto gogoloop; goto gogoloop
// backward declared // backward declared
loop: loop:
i = i+1; i = i + 1
if i < 100 { if i < 100 {
goto loop; goto loop
} }
print(i); print(i)
print("\n"); print("\n")
return; return
gogoloop: gogoloop:
goto loop; goto loop
} }

View File

@ -7,61 +7,60 @@
package main package main
type Item interface { type Item interface {
Print(); Print()
} }
type ListItem struct { type ListItem struct {
item Item; item Item
next *ListItem; next *ListItem
} }
type List struct { type List struct {
head *ListItem; head *ListItem
} }
func (list *List) Init() { func (list *List) Init() {
list.head = nil; list.head = nil
} }
func (list *List) Insert(i Item) { func (list *List) Insert(i Item) {
item := new(ListItem); item := new(ListItem)
item.item = i; item.item = i
item.next = list.head; item.next = list.head
list.head = item; list.head = item
} }
func (list *List) Print() { func (list *List) Print() {
i := list.head; i := list.head
for i != nil { for i != nil {
i.item.Print(); i.item.Print()
i = i.next; i = i.next
} }
} }
// Something to put in a list // Something to put in a list
type Integer struct { type Integer struct {
val int; val int
} }
func (this *Integer) Init(i int) *Integer { func (this *Integer) Init(i int) *Integer {
this.val = i; this.val = i
return this; return this
} }
func (this *Integer) Print() { func (this *Integer) Print() {
print(this.val); print(this.val)
} }
func func main() {
main() { list := new(List)
list := new(List); list.Init()
list.Init();
for i := 0; i < 10; i = i + 1 { for i := 0; i < 10; i = i + 1 {
integer := new(Integer); integer := new(Integer)
integer.Init(i); integer.Init(i)
list.Insert(integer); list.Insert(integer)
} }
list.Print(); list.Print()
print("\n"); print("\n")
} }

View File

@ -4,269 +4,268 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package main package main
const nilchar = 0; const nilchar = 0
type Atom struct { type Atom struct {
str string; str string
integer int; integer int
next *Slist; /* in hash bucket */ next *Slist /* in hash bucket */
} }
type List struct { type List struct {
car *Slist; car *Slist
cdr*Slist; cdr *Slist
} }
type Slist struct { type Slist struct {
isatom bool; isatom bool
isstring bool; isstring bool
//union { //union {
atom Atom; atom Atom
list List; list List
//} u; //} u;
} }
func (this *Slist) Car() *Slist { func (this *Slist) Car() *Slist {
return this.list.car; return this.list.car
} }
func (this *Slist) Cdr() *Slist { func (this *Slist) Cdr() *Slist {
return this.list.cdr; return this.list.cdr
} }
func (this *Slist) String() string { func (this *Slist) String() string {
return this.atom.str; return this.atom.str
} }
func (this *Slist) Integer() int { func (this *Slist) Integer() int {
return this.atom.integer; return this.atom.integer
} }
func (slist *Slist) Free() { func (slist *Slist) Free() {
if slist == nil { if slist == nil {
return; return
} }
if slist.isatom { if slist.isatom {
// free(slist.String()); // free(slist.String());
} else { } else {
slist.Car().Free(); slist.Car().Free()
slist.Cdr().Free(); slist.Cdr().Free()
} }
// free(slist); // free(slist);
} }
//Slist* atom(byte *s, int i); //Slist* atom(byte *s, int i);
var token int; var token int
var peekc int = -1; var peekc int = -1
var lineno int32 = 1; var lineno int32 = 1
var input string; var input string
var inputindex int = 0; var inputindex int = 0
var tokenbuf [100]byte; var tokenbuf [100]byte
var tokenlen int = 0; var tokenlen int = 0
const EOF int = -1; const EOF int = -1
func main() { func main() {
var list *Slist; var list *Slist
OpenFile(); OpenFile()
for ;; { for {
list = Parse(); list = Parse()
if list == nil { if list == nil {
break; break
} }
list.Print(); list.Print()
list.Free(); list.Free()
break; break
} }
} }
func (slist *Slist) PrintOne(doparen bool) { func (slist *Slist) PrintOne(doparen bool) {
if slist == nil { if slist == nil {
return; return
} }
if slist.isatom { if slist.isatom {
if slist.isstring { if slist.isstring {
print(slist.String()); print(slist.String())
} else { } else {
print(slist.Integer()); print(slist.Integer())
} }
} else { } else {
if doparen { if doparen {
print("(" ); print("(")
} }
slist.Car().PrintOne(true); slist.Car().PrintOne(true)
if slist.Cdr() != nil { if slist.Cdr() != nil {
print(" "); print(" ")
slist.Cdr().PrintOne(false); slist.Cdr().PrintOne(false)
} }
if doparen { if doparen {
print(")"); print(")")
} }
} }
} }
func (slist *Slist) Print() { func (slist *Slist) Print() {
slist.PrintOne(true); slist.PrintOne(true)
print("\n"); print("\n")
} }
func Get() int { func Get() int {
var c int; var c int
if peekc >= 0 { if peekc >= 0 {
c = peekc; c = peekc
peekc = -1; peekc = -1
} else { } else {
c = int(input[inputindex]); c = int(input[inputindex])
inputindex++; inputindex++
if c == '\n' { if c == '\n' {
lineno = lineno + 1; lineno = lineno + 1
} }
if c == nilchar { if c == nilchar {
inputindex = inputindex - 1; inputindex = inputindex - 1
c = EOF; c = EOF
} }
} }
return c; return c
} }
func WhiteSpace(c int) bool { func WhiteSpace(c int) bool {
return c == ' ' || c == '\t' || c == '\r' || c == '\n'; return c == ' ' || c == '\t' || c == '\r' || c == '\n'
} }
func NextToken() { func NextToken() {
var i, c int; var i, c int
tokenbuf[0] = nilchar; // clear previous token tokenbuf[0] = nilchar // clear previous token
c = Get(); c = Get()
for WhiteSpace(c) { for WhiteSpace(c) {
c = Get(); c = Get()
} }
switch c { switch c {
case EOF: case EOF:
token = EOF; token = EOF
case '(', ')': case '(', ')':
token = c; token = c
break; break
default: default:
for i = 0; i < 100 - 1; { // sizeof tokenbuf - 1 for i = 0; i < 100-1; { // sizeof tokenbuf - 1
tokenbuf[i] = byte(c); tokenbuf[i] = byte(c)
i = i + 1; i = i + 1
c = Get(); c = Get()
if c == EOF { if c == EOF {
break; break
} }
if WhiteSpace(c) || c == ')' { if WhiteSpace(c) || c == ')' {
peekc = c; peekc = c
break; break
} }
} }
if i >= 100 - 1 { // sizeof tokenbuf - 1 if i >= 100-1 { // sizeof tokenbuf - 1
panic("atom too long\n"); panic("atom too long\n")
} }
tokenlen = i; tokenlen = i
tokenbuf[i] = nilchar; tokenbuf[i] = nilchar
if '0' <= tokenbuf[0] && tokenbuf[0] <= '9' { if '0' <= tokenbuf[0] && tokenbuf[0] <= '9' {
token = '0'; token = '0'
} else { } else {
token = 'A'; token = 'A'
} }
} }
} }
func Expect(c int) { func Expect(c int) {
if token != c { if token != c {
print("parse error: expected ", c, "\n"); print("parse error: expected ", c, "\n")
panic("parse"); panic("parse")
} }
NextToken(); NextToken()
} }
// Parse a non-parenthesized list up to a closing paren or EOF // Parse a non-parenthesized list up to a closing paren or EOF
func ParseList() *Slist { func ParseList() *Slist {
var slist, retval *Slist; var slist, retval *Slist
slist = new(Slist); slist = new(Slist)
slist.list.car = nil; slist.list.car = nil
slist.list.cdr = nil; slist.list.cdr = nil
slist.isatom = false; slist.isatom = false
slist.isstring = false; slist.isstring = false
retval = slist; retval = slist
for ;; { for {
slist.list.car = Parse(); slist.list.car = Parse()
if token == ')' || token == EOF { // empty cdr if token == ')' || token == EOF { // empty cdr
break; break
} }
slist.list.cdr = new(Slist); slist.list.cdr = new(Slist)
slist = slist.list.cdr; slist = slist.list.cdr
} }
return retval; return retval
} }
func atom(i int) *Slist { // BUG: uses tokenbuf; should take argument) func atom(i int) *Slist { // BUG: uses tokenbuf; should take argument)
var slist *Slist; var slist *Slist
slist = new(Slist); slist = new(Slist)
if token == '0' { if token == '0' {
slist.atom.integer = i; slist.atom.integer = i
slist.isstring = false; slist.isstring = false
} else { } else {
slist.atom.str = string(tokenbuf[0:tokenlen]); slist.atom.str = string(tokenbuf[0:tokenlen])
slist.isstring = true; slist.isstring = true
} }
slist.isatom = true; slist.isatom = true
return slist; return slist
} }
func atoi() int { // BUG: uses tokenbuf; should take argument) func atoi() int { // BUG: uses tokenbuf; should take argument)
var v int = 0; var v int = 0
for i := 0; i < tokenlen && '0' <= tokenbuf[i] && tokenbuf[i] <= '9'; i = i + 1 { for i := 0; i < tokenlen && '0' <= tokenbuf[i] && tokenbuf[i] <= '9'; i = i + 1 {
v = 10 * v + int(tokenbuf[i] - '0'); v = 10*v + int(tokenbuf[i]-'0')
} }
return v; return v
} }
func Parse() *Slist { func Parse() *Slist {
var slist *Slist; var slist *Slist
if token == EOF || token == ')' { if token == EOF || token == ')' {
return nil; return nil
} }
if token == '(' { if token == '(' {
NextToken(); NextToken()
slist = ParseList(); slist = ParseList()
Expect(')'); Expect(')')
return slist; return slist
} else { } else {
// Atom // Atom
switch token { switch token {
case EOF: case EOF:
return nil; return nil
case '0': case '0':
slist = atom(atoi()); slist = atom(atoi())
case '"', 'A': case '"', 'A':
slist = atom(0); slist = atom(0)
default: default:
slist = nil; slist = nil
print("unknown token: ", token, "\n"); print("unknown token: ", token, "\n")
} }
NextToken(); NextToken()
return slist; return slist
} }
return nil; return nil
} }
func OpenFile() { func OpenFile() {
input = "(defn foo (add 12 34))\n\x00"; input = "(defn foo (add 12 34))\n\x00"
inputindex = 0; inputindex = 0
peekc = -1; // BUG peekc = -1 // BUG
NextToken(); NextToken()
} }

View File

@ -6,19 +6,18 @@
package main package main
func func main() {
main() { a := 3
a := 3; for i := 0; i < 10; i = i + 1 {
for i:=0; i<10; i=i+1 { switch i {
switch(i) {
case 5: case 5:
print("five"); print("five")
case a,7: case a, 7:
print("a"); print("a")
default: default:
print(i); print(i)
} }
print("out", i); print("out", i)
} }
print("\n"); print("\n")
} }

View File

@ -8,7 +8,6 @@ package main
type Number *Number type Number *Number
// ------------------------------------- // -------------------------------------
// Peano primitives // Peano primitives
@ -16,24 +15,20 @@ func zero() *Number {
return nil return nil
} }
func is_zero(x *Number) bool { func is_zero(x *Number) bool {
return x == nil return x == nil
} }
func add1(x *Number) *Number { func add1(x *Number) *Number {
e := new(Number) e := new(Number)
*e = x *e = x
return e return e
} }
func sub1(x *Number) *Number { func sub1(x *Number) *Number {
return *x return *x
} }
func add(x, y *Number) *Number { func add(x, y *Number) *Number {
if is_zero(y) { if is_zero(y) {
return x return x
@ -42,7 +37,6 @@ func add(x, y *Number) *Number {
return add(add1(x), sub1(y)) return add(add1(x), sub1(y))
} }
func mul(x, y *Number) *Number { func mul(x, y *Number) *Number {
if is_zero(x) || is_zero(y) { if is_zero(x) || is_zero(y) {
return zero() return zero()
@ -51,7 +45,6 @@ func mul(x, y *Number) *Number {
return add(mul(x, sub1(y)), x) return add(mul(x, sub1(y)), x)
} }
func fact(n *Number) *Number { func fact(n *Number) *Number {
if is_zero(n) { if is_zero(n) {
return add1(zero()) return add1(zero())
@ -60,7 +53,6 @@ func fact(n *Number) *Number {
return mul(fact(sub1(n)), n) return mul(fact(sub1(n)), n)
} }
// ------------------------------------- // -------------------------------------
// Helpers to generate/count Peano integers // Helpers to generate/count Peano integers
@ -72,7 +64,6 @@ func gen(n int) *Number {
return zero() return zero()
} }
func count(x *Number) int { func count(x *Number) int {
if is_zero(x) { if is_zero(x) {
return 0 return 0
@ -81,7 +72,6 @@ func count(x *Number) int {
return count(sub1(x)) + 1 return count(sub1(x)) + 1
} }
func check(x *Number, expected int) { func check(x *Number, expected int) {
var c = count(x) var c = count(x)
if c != expected { if c != expected {
@ -90,7 +80,6 @@ func check(x *Number, expected int) {
} }
} }
// ------------------------------------- // -------------------------------------
// Test basic functionality // Test basic functionality
@ -115,7 +104,6 @@ func init() {
check(fact(gen(5)), 120) check(fact(gen(5)), 120)
} }
// ------------------------------------- // -------------------------------------
// Factorial // Factorial

View File

@ -10,15 +10,16 @@ package main
var p, pc int var p, pc int
var a [30000]byte var a [30000]byte
const prog = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.!" const prog = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.!"
func scan(dir int) { func scan(dir int) {
for nest := dir; dir*nest > 0; pc += dir { for nest := dir; dir*nest > 0; pc += dir {
switch prog[pc+dir] { switch prog[pc+dir] {
case ']': case ']':
nest-- nest--
case '[': case '[':
nest++ nest++
} }
} }
} }
@ -26,26 +27,26 @@ func scan(dir int) {
func main() { func main() {
for { for {
switch prog[pc] { switch prog[pc] {
case '>': case '>':
p++ p++
case '<': case '<':
p-- p--
case '+': case '+':
a[p]++ a[p]++
case '-': case '-':
a[p]-- a[p]--
case '.': case '.':
print(string(a[p])) print(string(a[p]))
case '[': case '[':
if a[p] == 0 { if a[p] == 0 {
scan(1) scan(1)
} }
case ']': case ']':
if a[p] != 0 { if a[p] != 0 {
scan(-1) scan(-1)
} }
default: default:
return return
} }
pc++ pc++
} }