diff options
Diffstat (limited to '')
42 files changed, 5542 insertions, 0 deletions
diff --git a/test/ken/array.go b/test/ken/array.go new file mode 100644 index 0000000..9412e35 --- /dev/null +++ b/test/ken/array.go @@ -0,0 +1,138 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test arrays and slices. + +package main + +func setpd(a []int) { + // print("setpd a=", a, " len=", len(a), " cap=", cap(a), "\n"); + for i := 0; i < len(a); i++ { + a[i] = i + } +} + +func sumpd(a []int) int { + // print("sumpd a=", a, " len=", len(a), " cap=", cap(a), "\n"); + t := 0 + for i := 0; i < len(a); i++ { + t += a[i] + } + // print("sumpd t=", t, "\n"); + return t +} + +func setpf(a *[20]int) { + // print("setpf a=", a, " len=", len(a), " cap=", cap(a), "\n"); + for i := 0; i < len(a); i++ { + a[i] = i + } +} + +func sumpf(a *[20]int) int { + // print("sumpf a=", a, " len=", len(a), " cap=", cap(a), "\n"); + t := 0 + for i := 0; i < len(a); i++ { + t += a[i] + } + // print("sumpf t=", t, "\n"); + return t +} + +func res(t int, lb, hb int) { + sb := (hb - lb) * (hb + lb - 1) / 2 + if t != sb { + print("lb=", lb, + "; hb=", hb, + "; t=", t, + "; sb=", sb, + "\n") + panic("res") + } +} + +// call ptr dynamic with ptr dynamic +func testpdpd() { + a := make([]int, 10, 100) + if len(a) != 10 && cap(a) != 100 { + print("len and cap from new: ", len(a), " ", cap(a), "\n") + panic("fail") + } + + a = a[0:100] + setpd(a) + + a = a[0:10] + res(sumpd(a), 0, 10) + + a = a[5:25] + res(sumpd(a), 5, 25) + + a = a[30:95] + res(sumpd(a), 35, 100) +} + +// call ptr fixed with ptr fixed +func testpfpf() { + var a [20]int + + setpf(&a) + res(sumpf(&a), 0, 20) +} + +// call ptr dynamic with ptr fixed from new +func testpdpf1() { + a := new([40]int) + setpd(a[0:]) + res(sumpd(a[0:]), 0, 40) + + b := (*a)[5:30] + res(sumpd(b), 5, 30) +} + +// call ptr dynamic with ptr fixed from var +func testpdpf2() { + var a [80]int + + setpd(a[0:]) + res(sumpd(a[0:]), 0, 80) +} + +// generate bounds error with ptr dynamic +func testpdfault() { + a := make([]int, 100) + + print("good\n") + for i := 0; i < 100; i++ { + a[i] = 0 + } + print("should fault\n") + a[100] = 0 + print("bad\n") +} + +// generate bounds error with ptr fixed +func testfdfault() { + var a [80]int + + print("good\n") + for i := 0; i < 80; i++ { + a[i] = 0 + } + print("should fault\n") + x := 80 + a[x] = 0 + print("bad\n") +} + +func main() { + testpdpd() + testpfpf() + testpdpf1() + testpdpf2() + // print("testpdfault\n"); testpdfault(); + // print("testfdfault\n"); testfdfault(); +} diff --git a/test/ken/chan.go b/test/ken/chan.go new file mode 100644 index 0000000..36b18f8 --- /dev/null +++ b/test/ken/chan.go @@ -0,0 +1,331 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test communication operations including select. + +package main + +import "os" +import "runtime" +import "sync" + +var randx int + +func nrand(n int) int { + randx += 10007 + if randx >= 1000000 { + randx -= 1000000 + } + return randx % n +} + +type Chan struct { + sc, rc chan int // send and recv chan + sv, rv int // send and recv seq +} + +var ( + nproc int + nprocLock sync.Mutex + cval int + end int = 10000 + totr, tots int + totLock sync.Mutex + nc *Chan +) + +func init() { + nc = new(Chan) +} + +func changeNproc(adjust int) int { + nprocLock.Lock() + nproc += adjust + ret := nproc + nprocLock.Unlock() + return ret +} + +func mkchan(c, n int) []*Chan { + ca := make([]*Chan, n) + for i := 0; i < n; i++ { + cval = cval + 100 + ch := new(Chan) + ch.sc = make(chan int, c) + ch.rc = ch.sc + ch.sv = cval + ch.rv = cval + ca[i] = ch + } + return ca +} + +func expect(v, v0 int) (newv int) { + if v == v0 { + if v%100 == 75 { + return end + } + return v + 1 + } + print("got ", v, " expected ", v0+1, "\n") + panic("fail") +} + +func (c *Chan) send() bool { + // print("send ", c.sv, "\n"); + totLock.Lock() + tots++ + totLock.Unlock() + c.sv = expect(c.sv, c.sv) + if c.sv == end { + c.sc = nil + return true + } + return false +} + +func send(c *Chan) { + for { + for r := nrand(10); r >= 0; r-- { + runtime.Gosched() + } + c.sc <- c.sv + if c.send() { + break + } + } + changeNproc(-1) +} + +func (c *Chan) recv(v int) bool { + // print("recv ", v, "\n"); + totLock.Lock() + totr++ + totLock.Unlock() + c.rv = expect(c.rv, v) + if c.rv == end { + c.rc = nil + return true + } + return false +} + +func recv(c *Chan) { + var v int + + for { + for r := nrand(10); r >= 0; r-- { + runtime.Gosched() + } + v = <-c.rc + if c.recv(v) { + break + } + } + changeNproc(-1) +} + +func sel(r0, r1, r2, r3, s0, s1, s2, s3 *Chan) { + var v int + + a := 0 // local chans running + + if r0.rc != nil { + a++ + } + if r1.rc != nil { + a++ + } + if r2.rc != nil { + a++ + } + if r3.rc != nil { + a++ + } + if s0.sc != nil { + a++ + } + if s1.sc != nil { + a++ + } + if s2.sc != nil { + a++ + } + if s3.sc != nil { + a++ + } + + for { + for r := nrand(5); r >= 0; r-- { + runtime.Gosched() + } + + select { + case v = <-r0.rc: + if r0.recv(v) { + a-- + } + case v = <-r1.rc: + if r1.recv(v) { + a-- + } + case v = <-r2.rc: + if r2.recv(v) { + a-- + } + case v = <-r3.rc: + if r3.recv(v) { + a-- + } + case s0.sc <- s0.sv: + if s0.send() { + a-- + } + case s1.sc <- s1.sv: + if s1.send() { + a-- + } + case s2.sc <- s2.sv: + if s2.send() { + a-- + } + case s3.sc <- s3.sv: + if s3.send() { + a-- + } + } + if a == 0 { + break + } + } + changeNproc(-1) +} + +// direct send to direct recv +func test1(c *Chan) { + changeNproc(2) + go send(c) + go recv(c) +} + +// direct send to select recv +func test2(c int) { + ca := mkchan(c, 4) + + changeNproc(4) + go send(ca[0]) + go send(ca[1]) + go send(ca[2]) + go send(ca[3]) + + changeNproc(1) + go sel(ca[0], ca[1], ca[2], ca[3], nc, nc, nc, nc) +} + +// select send to direct recv +func test3(c int) { + ca := mkchan(c, 4) + + changeNproc(4) + go recv(ca[0]) + go recv(ca[1]) + go recv(ca[2]) + go recv(ca[3]) + + changeNproc(1) + go sel(nc, nc, nc, nc, ca[0], ca[1], ca[2], ca[3]) +} + +// select send to select recv +func test4(c int) { + ca := mkchan(c, 4) + + changeNproc(2) + go sel(nc, nc, nc, nc, ca[0], ca[1], ca[2], ca[3]) + go sel(ca[0], ca[1], ca[2], ca[3], nc, nc, nc, nc) +} + +func test5(c int) { + ca := mkchan(c, 8) + + changeNproc(2) + go sel(ca[4], ca[5], ca[6], ca[7], ca[0], ca[1], ca[2], ca[3]) + go sel(ca[0], ca[1], ca[2], ca[3], ca[4], ca[5], ca[6], ca[7]) +} + +func test6(c int) { + ca := mkchan(c, 12) + + changeNproc(4) + go send(ca[4]) + go send(ca[5]) + go send(ca[6]) + go send(ca[7]) + + changeNproc(4) + go recv(ca[8]) + go recv(ca[9]) + go recv(ca[10]) + go recv(ca[11]) + + changeNproc(2) + go sel(ca[4], ca[5], ca[6], ca[7], ca[0], ca[1], ca[2], ca[3]) + go sel(ca[0], ca[1], ca[2], ca[3], ca[8], ca[9], ca[10], ca[11]) +} + +// wait for outstanding tests to finish +func wait() { + runtime.Gosched() + for changeNproc(0) != 0 { + runtime.Gosched() + } +} + +// run all tests with specified buffer size +func tests(c int) { + ca := mkchan(c, 4) + test1(ca[0]) + test1(ca[1]) + test1(ca[2]) + test1(ca[3]) + wait() + + test2(c) + wait() + + test3(c) + wait() + + test4(c) + wait() + + test5(c) + wait() + + test6(c) + wait() +} + +// run all test with 4 buffser sizes +func main() { + + tests(0) + tests(1) + tests(10) + tests(100) + + t := 4 * // buffer sizes + (4*4 + // tests 1,2,3,4 channels + 8 + // test 5 channels + 12) * // test 6 channels + 76 // sends/recvs on a channel + + if tots != t || totr != t { + print("tots=", tots, " totr=", totr, " sb=", t, "\n") + os.Exit(1) + } + os.Exit(0) +} diff --git a/test/ken/chan1.go b/test/ken/chan1.go new file mode 100644 index 0000000..cbd21a3 --- /dev/null +++ b/test/ken/chan1.go @@ -0,0 +1,55 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test communication with multiple simultaneous goroutines. + +package main + +import "runtime" + +const N = 1000 // sent messages +const M = 10 // receiving goroutines +const W = 2 // channel buffering +var h [N]int // marking of send/recv + +func r(c chan int, m int) { + for { + select { + case r := <-c: + if h[r] != 1 { + println("r", + "m=", m, + "r=", r, + "h=", h[r]) + panic("fail") + } + h[r] = 2 + } + } +} + +func s(c chan int) { + for n := 0; n < N; n++ { + r := n + if h[r] != 0 { + println("s") + panic("fail") + } + h[r] = 1 + c <- r + } +} + +func main() { + c := make(chan int, W) + for m := 0; m < M; m++ { + go r(c, m) + runtime.Gosched() + } + runtime.Gosched() + runtime.Gosched() + s(c) +} diff --git a/test/ken/complit.go b/test/ken/complit.go new file mode 100644 index 0000000..bc50bbe --- /dev/null +++ b/test/ken/complit.go @@ -0,0 +1,172 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test composite literals. + +package main + +type M map[int]int +type S struct{ a,b,c int }; +type SS struct{ aa,bb,cc S }; +type SA struct{ a,b,c [3]int }; +type SC struct{ a,b,c []int }; +type SM struct{ a,b,c M }; + +func +main() { + test("s.a", s.a); + test("s.b", s.b); + test("s.c", s.c); + + test("ss.aa.a", ss.aa.a); + test("ss.aa.b", ss.aa.b); + test("ss.aa.c", ss.aa.c); + + test("ss.bb.a", ss.bb.a); + test("ss.bb.b", ss.bb.b); + test("ss.bb.c", ss.bb.c); + + test("ss.cc.a", ss.cc.a); + test("ss.cc.b", ss.cc.b); + test("ss.cc.c", ss.cc.c); + + for i:=0; i<3; i++ { + test("a[i]", a[i]); + test("c[i]", c[i]); + test("m[i]", m[i]); + + test("as[i].a", as[i].a); + test("as[i].b", as[i].b); + test("as[i].c", as[i].c); + + test("cs[i].a", cs[i].a); + test("cs[i].b", cs[i].b); + test("cs[i].c", cs[i].c); + + test("ms[i].a", ms[i].a); + test("ms[i].b", ms[i].b); + test("ms[i].c", ms[i].c); + + test("sa.a[i]", sa.a[i]); + test("sa.b[i]", sa.b[i]); + test("sa.c[i]", sa.c[i]); + + test("sc.a[i]", sc.a[i]); + test("sc.b[i]", sc.b[i]); + test("sc.c[i]", sc.c[i]); + + test("sm.a[i]", sm.a[i]); + test("sm.b[i]", sm.b[i]); + test("sm.c[i]", sm.c[i]); + + for j:=0; j<3; j++ { + test("aa[i][j]", aa[i][j]); + test("ac[i][j]", ac[i][j]); + test("am[i][j]", am[i][j]); + test("ca[i][j]", ca[i][j]); + test("cc[i][j]", cc[i][j]); + test("cm[i][j]", cm[i][j]); + test("ma[i][j]", ma[i][j]); + test("mc[i][j]", mc[i][j]); + test("mm[i][j]", mm[i][j]); + } + } + +} + +var ref = 0; + +func +test(xs string, x int) { + + if ref >= len(answers) { + println(xs, x); + return; + } + + if x != answers[ref] { + println(xs, "is", x, "should be", answers[ref]) + } + ref++; +} + + +var a = [3]int{1001, 1002, 1003} +var s = S{1101, 1102, 1103} +var c = []int{1201, 1202, 1203} +var m = M{0:1301, 1:1302, 2:1303} + +var aa = [3][3]int{[3]int{2001,2002,2003}, [3]int{2004,2005,2006}, [3]int{2007,2008,2009}} +var as = [3]S{S{2101,2102,2103},S{2104,2105,2106},S{2107,2108,2109}} +var ac = [3][]int{[]int{2201,2202,2203}, []int{2204,2205,2206}, []int{2207,2208,2209}} +var am = [3]M{M{0:2301,1:2302,2:2303}, M{0:2304,1:2305,2:2306}, M{0:2307,1:2308,2:2309}} + +var sa = SA{[3]int{3001,3002,3003},[3]int{3004,3005,3006},[3]int{3007,3008,3009}} +var ss = SS{S{3101,3102,3103},S{3104,3105,3106},S{3107,3108,3109}} +var sc = SC{[]int{3201,3202,3203},[]int{3204,3205,3206},[]int{3207,3208,3209}} +var sm = SM{M{0:3301,1:3302,2:3303}, M{0:3304,1:3305,2:3306}, M{0:3307,1:3308,2:3309}} + +var ca = [][3]int{[3]int{4001,4002,4003}, [3]int{4004,4005,4006}, [3]int{4007,4008,4009}} +var cs = []S{S{4101,4102,4103},S{4104,4105,4106},S{4107,4108,4109}} +var cc = [][]int{[]int{4201,4202,4203}, []int{4204,4205,4206}, []int{4207,4208,4209}} +var cm = []M{M{0:4301,1:4302,2:4303}, M{0:4304,1:4305,2:4306}, M{0:4307,1:4308,2:4309}} + +var ma = map[int][3]int{0:[3]int{5001,5002,5003}, 1:[3]int{5004,5005,5006}, 2:[3]int{5007,5008,5009}} +var ms = map[int]S{0:S{5101,5102,5103},1:S{5104,5105,5106},2:S{5107,5108,5109}} +var mc = map[int][]int{0:[]int{5201,5202,5203}, 1:[]int{5204,5205,5206}, 2:[]int{5207,5208,5209}} +var mm = map[int]M{0:M{0:5301,1:5302,2:5303}, 1:M{0:5304,1:5305,2:5306}, 2:M{0:5307,1:5308,2:5309}} + +var answers = [...]int { + // s + 1101, 1102, 1103, + + // ss + 3101, 3102, 3103, + 3104, 3105, 3106, + 3107, 3108, 3109, + + // [0] + 1001, 1201, 1301, + 2101, 2102, 2103, + 4101, 4102, 4103, + 5101, 5102, 5103, + 3001, 3004, 3007, + 3201, 3204, 3207, + 3301, 3304, 3307, + + // [0][j] + 2001, 2201, 2301, 4001, 4201, 4301, 5001, 5201, 5301, + 2002, 2202, 2302, 4002, 4202, 4302, 5002, 5202, 5302, + 2003, 2203, 2303, 4003, 4203, 4303, 5003, 5203, 5303, + + // [1] + 1002, 1202, 1302, + 2104, 2105, 2106, + 4104, 4105, 4106, + 5104, 5105, 5106, + 3002, 3005, 3008, + 3202, 3205, 3208, + 3302, 3305, 3308, + + // [1][j] + 2004, 2204, 2304, 4004, 4204, 4304, 5004, 5204, 5304, + 2005, 2205, 2305, 4005, 4205, 4305, 5005, 5205, 5305, + 2006, 2206, 2306, 4006, 4206, 4306, 5006, 5206, 5306, + + // [2] + 1003, 1203, 1303, + 2107, 2108, 2109, + 4107, 4108, 4109, + 5107, 5108, 5109, + 3003, 3006, 3009, + 3203, 3206, 3209, + 3303, 3306, 3309, + + // [2][j] + 2007, 2207, 2307, 4007, 4207, 4307, 5007, 5207, 5307, + 2008, 2208, 2308, 4008, 4208, 4308, 5008, 5208, 5308, + 2009, 2209, 2309, 4009, 4209, 4309, 5009, 5209, 5309, +} diff --git a/test/ken/convert.go b/test/ken/convert.go new file mode 100644 index 0000000..33acbd8 --- /dev/null +++ b/test/ken/convert.go @@ -0,0 +1,432 @@ +// run + +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test, near-exhaustive, of converting numbers between types. +// No complex numbers though. + +package main + +var i8 int8; +var u8 uint8; +var i16 int16; +var u16 uint16; +var i32 int32; +var u32 uint32; +var i64 int64; +var u64 uint64; +var f32 float32; +var f64 float64; + +type big float64 + +type t struct { + from, to int + val big +} + +const ( + ti8 = iota+1 + tu8 + ti16 + tu16 + ti32 + tu32 + ti64 + tu64 + tf32 + tf64 +) + +var x = []t{ + + /* value good in all types (10) */ + { ti8, ti8, 10 }, { ti8, tu8, 10 }, { ti8, ti16, 10 }, { ti8, tu16, 10 }, + { ti8, ti32, 10 }, { ti8, tu32, 10 }, { ti8, ti64, 10 }, { ti8, tu64, 10 }, + { ti8, tf32, 10 }, { ti8, tf64, 10 }, + + { tu8, ti8, 10 }, { tu8, tu8, 10 }, { tu8, ti16, 10 }, { tu8, tu16, 10 }, + { tu8, ti32, 10 }, { tu8, tu32, 10 }, { tu8, ti64, 10 }, { tu8, tu64, 10 }, + { tu8, tf32, 10 }, { tu8, tf64, 10 }, + + { ti16, ti8, 10 }, { ti16, tu8, 10 }, { ti16, ti16, 10 }, { ti16, tu16, 10 }, + { ti16, ti32, 10 }, { ti16, tu32, 10 }, { ti16, ti64, 10 }, { ti16, tu64, 10 }, + { ti16, tf32, 10 }, { ti16, tf64, 10 }, + + { tu16, ti8, 10 }, { tu16, tu8, 10 }, { tu16, ti16, 10 }, { tu16, tu16, 10 }, + { tu16, ti32, 10 }, { tu16, tu32, 10 }, { tu16, ti64, 10 }, { tu16, tu64, 10 }, + { tu16, tf32, 10 }, { tu16, tf64, 10 }, + + { ti32, ti8, 10 }, { ti32, tu8, 10 }, { ti32, ti16, 10 }, { ti32, tu16, 10 }, + { ti32, ti32, 10 }, { ti32, tu32, 10 }, { ti32, ti64, 10 }, { ti32, tu64, 10 }, + { ti32, tf32, 10 }, { ti32, tf64, 10 }, + + { tu32, ti8, 10 }, { tu32, tu8, 10 }, { tu32, ti16, 10 }, { tu32, tu16, 10 }, + { tu32, ti32, 10 }, { tu32, tu32, 10 }, { tu32, ti64, 10 }, { tu32, tu64, 10 }, + { tu32, tf32, 10 }, { tu32, tf64, 10 }, + + { ti64, ti8, 10 }, { ti64, tu8, 10 }, { ti64, ti16, 10 }, { ti64, tu16, 10 }, + { ti64, ti32, 10 }, { ti64, tu32, 10 }, { ti64, ti64, 10 }, { ti64, tu64, 10 }, + { ti64, tf32, 10 }, { ti64, tf64, 10 }, + + { tu64, ti8, 10 }, { tu64, tu8, 10 }, { tu64, ti16, 10 }, { tu64, tu16, 10 }, + { tu64, ti32, 10 }, { tu64, tu32, 10 }, { tu64, ti64, 10 }, { tu64, tu64, 10 }, + { tu64, tf32, 10 }, { tu64, tf64, 10 }, + + { tf32, ti8, 10 }, { tf32, tu8, 10 }, { tf32, ti16, 10 }, { tf32, tu16, 10 }, + { tf32, ti32, 10 }, { tf32, tu32, 10 }, { tf32, ti64, 10 }, { tf32, tu64, 10 }, + { tf32, tf32, 10 }, { tf32, tf64, 10 }, + + { tf64, ti8, 10 }, { tf64, tu8, 10 }, { tf64, ti16, 10 }, { tf64, tu16, 10 }, + { tf64, ti32, 10 }, { tf64, tu32, 10 }, { tf64, ti64, 10 }, { tf64, tu64, 10 }, + { tf64, tf32, 10 }, { tf64, tf64, 10 }, + + /* value good in all signed types (-4) */ + { ti8, ti8, -4 }, { ti8, ti16, -4 }, + { ti8, ti32, -4 }, { ti8, ti64, -4 }, + { ti8, tf32, -4 }, { ti8, tf64, -4 }, + + { ti16, ti8, -4 }, { ti16, ti16, -4 }, + { ti16, ti32, -4 }, { ti16, ti64, -4 }, + { ti16, tf32, -4 }, + + { ti32, ti8, -4 }, { ti32, ti16, -4 }, + { ti32, ti32, -4 }, { ti32, ti64, -4 }, + { ti32, tf32, -4 }, { ti32, tf64, -4 }, + + { ti64, ti8, -4 }, { ti64, ti16, -4 }, + { ti64, ti32, -4 }, { ti64, ti64, -4 }, + { ti64, tf32, -4 }, + + { tf32, ti8, -4 }, { tf32, ti16, -4 }, + { tf32, ti32, -4 }, { tf32, ti64, -4 }, + { tf32, tf32, -4 }, + + { tf64, ti8, -4 }, { tf64, ti16, -4 }, + { tf64, ti32, -4 }, { tf64, ti64, -4 }, + { tf64, tf32, -4 }, { tf64, tf64, -4 }, + + /* value good in u8 and up (175) */ + { tu8, tu8, 175 }, { tu8, ti16, 175 }, { tu8, tu16, 175 }, + { tu8, ti32, 175 }, { tu8, tu32, 175 }, { tu8, ti64, 175 }, { tu8, tu64, 175 }, + { tu8, tf32, 175 }, { tu8, tf64, 175 }, + + { ti16, tu8, 175 }, { ti16, ti16, 175 }, { ti16, tu16, 175 }, + { ti16, ti32, 175 }, { ti16, tu32, 175 }, { ti16, ti64, 175 }, { ti16, tu64, 175 }, + { ti16, tf32, 175 }, { ti16, tf64, 175 }, + + { tu16, tu8, 175 }, { tu16, ti16, 175 }, { tu16, tu16, 175 }, + { tu16, ti32, 175 }, { tu16, tu32, 175 }, { tu16, ti64, 175 }, { tu16, tu64, 175 }, + { tu16, tf32, 175 }, { tu16, tf64, 175 }, + + { ti32, tu8, 175 }, { ti32, ti16, 175 }, { ti32, tu16, 175 }, + { ti32, ti32, 175 }, { ti32, tu32, 175 }, { ti32, ti64, 175 }, { ti32, tu64, 175 }, + { ti32, tf32, 175 }, { ti32, tf64, 175 }, + + { tu32, tu8, 175 }, { tu32, ti16, 175 }, { tu32, tu16, 175 }, + { tu32, ti32, 175 }, { tu32, tu32, 175 }, { tu32, ti64, 175 }, { tu32, tu64, 175 }, + { tu32, tf32, 175 }, { tu32, tf64, 175 }, + + { ti64, tu8, 175 }, { ti64, ti16, 175 }, { ti64, tu16, 175 }, + { ti64, ti32, 175 }, { ti64, tu32, 175 }, { ti64, ti64, 175 }, { ti64, tu64, 175 }, + { ti64, tf32, 175 }, { ti64, tf64, 175 }, + + { tu64, tu8, 175 }, { tu64, ti16, 175 }, { tu64, tu16, 175 }, + { tu64, ti32, 175 }, { tu64, tu32, 175 }, { tu64, ti64, 175 }, { tu64, tu64, 175 }, + { tu64, tf32, 175 }, { tu64, tf64, 175 }, + + { tf32, tu8, 175 }, { tf32, ti16, 175 }, { tf32, tu16, 175 }, + { tf32, ti32, 175 }, { tf32, tu32, 175 }, { tf32, ti64, 175 }, { tf32, tu64, 175 }, + { tf32, tf32, 175 }, { tf32, tf64, 175 }, + + { tf64, tu8, 175 }, { tf64, ti16, 175 }, { tf64, tu16, 175 }, + { tf64, ti32, 175 }, { tf64, tu32, 175 }, { tf64, ti64, 175 }, { tf64, tu64, 175 }, + { tf64, tf32, 175 }, { tf64, tf64, 175 }, + + /* value good in u16 and up (41259) */ + { tu16, tu16, 41259 }, + { tu16, ti32, 41259 }, { tu16, ti64, 41259 }, { tu16, tu64, 41259 }, + { tu16, tf32, 41259 }, { tu16, tf64, 41259 }, + + { ti32, tu16, 41259 }, + { ti32, ti32, 41259 }, { ti32, tu32, 41259 }, { ti32, ti64, 41259 }, { ti32, tu64, 41259 }, + { ti32, tf32, 41259 }, { ti32, tf64, 41259 }, + + { tu32, tu16, 41259 }, + { tu32, ti32, 41259 }, { tu32, tu32, 41259 }, { tu32, ti64, 41259 }, { tu32, tu64, 41259 }, + { tu32, tf32, 41259 }, { tu32, tf64, 41259 }, + + { ti64, tu16, 41259 }, + { ti64, ti32, 41259 }, { ti64, tu32, 41259 }, { ti64, ti64, 41259 }, { ti64, tu64, 41259 }, + { ti64, tf32, 41259 }, { ti64, tf64, 41259 }, + + { tu64, tu16, 41259 }, + { tu64, ti32, 41259 }, { tu64, tu32, 41259 }, { tu64, ti64, 41259 }, { tu64, tu64, 41259 }, + { tu64, tf32, 41259 }, { tu64, tf64, 41259 }, + + { tf32, tu16, 41259 }, + { tf32, ti32, 41259 }, { tf32, tu32, 41259 }, { tf32, ti64, 41259 }, { tf32, tu64, 41259 }, + { tf32, tf32, 41259 }, { tf32, tf64, 41259 }, + + { tf64, tu16, 41259 }, + { tf64, ti32, 41259 }, { tf64, tu32, 41259 }, { tf64, ti64, 41259 }, { tf64, tu64, 41259 }, + { tf64, tf32, 41259 }, { tf64, tf64, 41259 }, + + /* value good in u32 and up (3758096384) */ + { tu32, tu32, 3758096384 }, { tu32, ti64, 3758096384 }, { tu32, tu64, 3758096384 }, + { tu32, tf32, 3758096384 }, { tu32, tf64, 3758096384 }, + + { ti64, tu32, 3758096384 }, { ti64, ti64, 3758096384 }, { ti64, tu64, 3758096384 }, + { ti64, tf32, 3758096384 }, { ti64, tf64, 3758096384 }, + + { tu64, tu32, 3758096384 }, { tu64, ti64, 3758096384 }, { tu64, tu64, 3758096384 }, + { tu64, tf32, 3758096384 }, { tu64, tf64, 3758096384 }, + + { tf32, tu32, 3758096384 }, { tf32, ti64, 3758096384 }, { tf32, tu64, 3758096384 }, + { tf32, tf32, 3758096384 }, { tf32, tf64, 3758096384 }, + + { tf64, tu32, 3758096384 }, { tf64, ti64, 3758096384 }, { tf64, tu64, 3758096384 }, + { tf64, tf32, 3758096384 }, { tf64, tf64, 3758096384 }, + + /* value good in u64 and up (16717361816799281152) */ + { tu64, tu64, 16717361816799281152 }, + { tu64, tf32, 16717361816799281152 }, { tu64, tf64, 16717361816799281152 }, + + { tf32, tu64, 16717361816799281152 }, + { tf32, tf32, 16717361816799281152 }, { tf32, tf64, 16717361816799281152 }, + + { tf64, tu64, 16717361816799281152 }, + { tf64, tf32, 16717361816799281152 }, { tf64, tf64, 16717361816799281152 }, +} + +func main() { + for i:=0; i<len(x); i++ { + v := x[i].val // input value + w := big(0) // output value + f := x[i].from // input type + t := x[i].to // output type + + i8 = 0; u8 = 0; i16 = 0; u16 = 0 + i32 = 0; u32 = 0; i64 = 0; u64 = 0 + f32 = 0; f64 = 0 + + switch f*100 + t { + default: + println("missing case", i, v, f, t) + w = v + + case ti8*100 + ti8: + i8 = int8(v); i8 = int8(i8); w = big(i8) + case ti8*100 + tu8: + i8 = int8(v); u8 = uint8(i8); w = big(u8) + case ti8*100 + ti16: + i8 = int8(v); i16 = int16(i8); w = big(i16) + case ti8*100 + tu16: + i8 = int8(v); u16 = uint16(i8); w = big(u16) + case ti8*100 + ti32: + i8 = int8(v); i32 = int32(i8); w = big(i32) + case ti8*100 + tu32: + i8 = int8(v); u32 = uint32(i8); w = big(u32) + case ti8*100 + ti64: + i8 = int8(v); i64 = int64(i8); w = big(i64) + case ti8*100 + tu64: + i8 = int8(v); u64 = uint64(i8); w = big(u64) + case ti8*100 + tf32: + i8 = int8(v); f32 = float32(i8); w = big(f32) + case ti8*100 + tf64: + i8 = int8(v); f64 = float64(i8); w = big(f64) + + case tu8*100 + ti8: + u8 = uint8(v); i8 = int8(u8); w = big(i8) + case tu8*100 + tu8: + u8 = uint8(v); u8 = uint8(u8); w = big(u8) + case tu8*100 + ti16: + u8 = uint8(v); i16 = int16(u8); w = big(i16) + case tu8*100 + tu16: + u8 = uint8(v); u16 = uint16(u8); w = big(u16) + case tu8*100 + ti32: + u8 = uint8(v); i32 = int32(u8); w = big(i32) + case tu8*100 + tu32: + u8 = uint8(v); u32 = uint32(u8); w = big(u32) + case tu8*100 + ti64: + u8 = uint8(v); i64 = int64(u8); w = big(i64) + case tu8*100 + tu64: + u8 = uint8(v); u64 = uint64(u8); w = big(u64) + case tu8*100 + tf32: + u8 = uint8(v); f32 = float32(u8); w = big(f32) + case tu8*100 + tf64: + u8 = uint8(v); f64 = float64(u8); w = big(f64) + + case ti16*100 + ti8: + i16 = int16(v); i8 = int8(i16); w = big(i8) + case ti16*100 + tu8: + i16 = int16(v); u8 = uint8(i16); w = big(u8) + case ti16*100 + ti16: + i16 = int16(v); i16 = int16(i16); w = big(i16) + case ti16*100 + tu16: + i16 = int16(v); u16 = uint16(i16); w = big(u16) + case ti16*100 + ti32: + i16 = int16(v); i32 = int32(i16); w = big(i32) + case ti16*100 + tu32: + i16 = int16(v); u32 = uint32(i16); w = big(u32) + case ti16*100 + ti64: + i16 = int16(v); i64 = int64(i16); w = big(i64) + case ti16*100 + tu64: + i16 = int16(v); u64 = uint64(i16); w = big(u64) + case ti16*100 + tf32: + i16 = int16(v); f32 = float32(i16); w = big(f32) + case ti16*100 + tf64: + i16 = int16(v); f64 = float64(i16); w = big(f64) + + case tu16*100 + ti8: + u16 = uint16(v); i8 = int8(u16); w = big(i8) + case tu16*100 + tu8: + u16 = uint16(v); u8 = uint8(u16); w = big(u8) + case tu16*100 + ti16: + u16 = uint16(v); i16 = int16(u16); w = big(i16) + case tu16*100 + tu16: + u16 = uint16(v); u16 = uint16(u16); w = big(u16) + case tu16*100 + ti32: + u16 = uint16(v); i32 = int32(u16); w = big(i32) + case tu16*100 + tu32: + u16 = uint16(v); u32 = uint32(u16); w = big(u32) + case tu16*100 + ti64: + u16 = uint16(v); i64 = int64(u16); w = big(i64) + case tu16*100 + tu64: + u16 = uint16(v); u64 = uint64(u16); w = big(u64) + case tu16*100 + tf32: + u16 = uint16(v); f32 = float32(u16); w = big(f32) + case tu16*100 + tf64: + u16 = uint16(v); f64 = float64(u16); w = big(f64) + + case ti32*100 + ti8: + i32 = int32(v); i8 = int8(i32); w = big(i8) + case ti32*100 + tu8: + i32 = int32(v); u8 = uint8(i32); w = big(u8) + case ti32*100 + ti16: + i32 = int32(v); i16 = int16(i32); w = big(i16) + case ti32*100 + tu16: + i32 = int32(v); u16 = uint16(i32); w = big(u16) + case ti32*100 + ti32: + i32 = int32(v); i32 = int32(i32); w = big(i32) + case ti32*100 + tu32: + i32 = int32(v); u32 = uint32(i32); w = big(u32) + case ti32*100 + ti64: + i32 = int32(v); i64 = int64(i32); w = big(i64) + case ti32*100 + tu64: + i32 = int32(v); u64 = uint64(i32); w = big(u64) + case ti32*100 + tf32: + i32 = int32(v); f32 = float32(i32); w = big(f32) + case ti32*100 + tf64: + i32 = int32(v); f64 = float64(i32); w = big(f64) + + case tu32*100 + ti8: + u32 = uint32(v); i8 = int8(u32); w = big(i8) + case tu32*100 + tu8: + u32 = uint32(v); u8 = uint8(u32); w = big(u8) + case tu32*100 + ti16: + u32 = uint32(v); i16 = int16(u32); w = big(i16) + case tu32*100 + tu16: + u32 = uint32(v); u16 = uint16(u32); w = big(u16) + case tu32*100 + ti32: + u32 = uint32(v); i32 = int32(u32); w = big(i32) + case tu32*100 + tu32: + u32 = uint32(v); u32 = uint32(u32); w = big(u32) + case tu32*100 + ti64: + u32 = uint32(v); i64 = int64(u32); w = big(i64) + case tu32*100 + tu64: + u32 = uint32(v); u64 = uint64(u32); w = big(u64) + case tu32*100 + tf32: + u32 = uint32(v); f32 = float32(u32); w = big(f32) + case tu32*100 + tf64: + u32 = uint32(v); f64 = float64(u32); w = big(f64) + + case ti64*100 + ti8: + i64 = int64(v); i8 = int8(i64); w = big(i8) + case ti64*100 + tu8: + i64 = int64(v); u8 = uint8(i64); w = big(u8) + case ti64*100 + ti16: + i64 = int64(v); i16 = int16(i64); w = big(i16) + case ti64*100 + tu16: + i64 = int64(v); u16 = uint16(i64); w = big(u16) + case ti64*100 + ti32: + i64 = int64(v); i32 = int32(i64); w = big(i32) + case ti64*100 + tu32: + i64 = int64(v); u32 = uint32(i64); w = big(u32) + case ti64*100 + ti64: + i64 = int64(v); i64 = int64(i64); w = big(i64) + case ti64*100 + tu64: + i64 = int64(v); u64 = uint64(i64); w = big(u64) + case ti64*100 + tf32: + i64 = int64(v); f32 = float32(i64); w = big(f32) + case ti64*100 + tf64: + i64 = int64(v); f64 = float64(i64); w = big(f64) + + case tu64*100 + ti8: + u64 = uint64(v); i8 = int8(u64); w = big(i8) + case tu64*100 + tu8: + u64 = uint64(v); u8 = uint8(u64); w = big(u8) + case tu64*100 + ti16: + u64 = uint64(v); i16 = int16(u64); w = big(i16) + case tu64*100 + tu16: + u64 = uint64(v); u16 = uint16(u64); w = big(u16) + case tu64*100 + ti32: + u64 = uint64(v); i32 = int32(u64); w = big(i32) + case tu64*100 + tu32: + u64 = uint64(v); u32 = uint32(u64); w = big(u32) + case tu64*100 + ti64: + u64 = uint64(v); i64 = int64(u64); w = big(i64) + case tu64*100 + tu64: + u64 = uint64(v); u64 = uint64(u64); w = big(u64) + case tu64*100 + tf32: + u64 = uint64(v); f32 = float32(u64); w = big(f32) + case tu64*100 + tf64: + u64 = uint64(v); f64 = float64(u64); w = big(f64) + + case tf32*100 + ti8: + f32 = float32(v); i8 = int8(f32); w = big(i8) + case tf32*100 + tu8: + f32 = float32(v); u8 = uint8(f32); w = big(u8) + case tf32*100 + ti16: + f32 = float32(v); i16 = int16(f32); w = big(i16) + case tf32*100 + tu16: + f32 = float32(v); u16 = uint16(f32); w = big(u16) + case tf32*100 + ti32: + f32 = float32(v); i32 = int32(f32); w = big(i32) + case tf32*100 + tu32: + f32 = float32(v); u32 = uint32(f32); w = big(u32) + case tf32*100 + ti64: + f32 = float32(v); i64 = int64(f32); w = big(i64) + case tf32*100 + tu64: + f32 = float32(v); u64 = uint64(f32); w = big(u64) + case tf32*100 + tf32: + f32 = float32(v); f32 = float32(f32); w = big(f32) + case tf32*100 + tf64: + f32 = float32(v); f64 = float64(f32); w = big(f64) + + case tf64*100 + ti8: + f64 = float64(v); i8 = int8(f64); w = big(i8) + case tf64*100 + tu8: + f64 = float64(v); u8 = uint8(f64); w = big(u8) + case tf64*100 + ti16: + f64 = float64(v); i16 = int16(f64); w = big(i16) + case tf64*100 + tu16: + f64 = float64(v); u16 = uint16(f64); w = big(u16) + case tf64*100 + ti32: + f64 = float64(v); i32 = int32(f64); w = big(i32) + case tf64*100 + tu32: + f64 = float64(v); u32 = uint32(f64); w = big(u32) + case tf64*100 + ti64: + f64 = float64(v); i64 = int64(f64); w = big(i64) + case tf64*100 + tu64: + f64 = float64(v); u64 = uint64(f64); w = big(u64) + case tf64*100 + tf32: + f64 = float64(v); f32 = float32(f64); w = big(f32) + case tf64*100 + tf64: + f64 = float64(v); f64 = float64(f64); w = big(f64) + } + if v != w { println(i, v, w, f, t) } + } +} diff --git a/test/ken/cplx0.go b/test/ken/cplx0.go new file mode 100644 index 0000000..5d78dc0 --- /dev/null +++ b/test/ken/cplx0.go @@ -0,0 +1,30 @@ +// run + +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test trivial, bootstrap-level complex numbers, including printing. + +package main + +const ( + R = 5 + I = 6i + + C1 = R + I // ADD(5,6) +) + +func doprint(c complex128) { println(c) } + +func main() { + + // constants + println(C1) + doprint(C1) + + // variables + c1 := C1 + println(c1) + doprint(c1) +} diff --git a/test/ken/cplx0.out b/test/ken/cplx0.out new file mode 100644 index 0000000..7627c28 --- /dev/null +++ b/test/ken/cplx0.out @@ -0,0 +1,4 @@ +(+5.000000e+000+6.000000e+000i) +(+5.000000e+000+6.000000e+000i) +(+5.000000e+000+6.000000e+000i) +(+5.000000e+000+6.000000e+000i) diff --git a/test/ken/cplx1.go b/test/ken/cplx1.go new file mode 100644 index 0000000..78240a5 --- /dev/null +++ b/test/ken/cplx1.go @@ -0,0 +1,99 @@ +// run + +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test simple arithmetic and assignment for complex numbers. + +package main + +const ( + R = 5 + I = 6i + + C1 = R + I // ADD(5,6) +) + +func main() { + var b bool + + // constants + b = (5 + 6i) == C1 + if !b { + println("const bool 1", b) + panic("fail") + } + + b = (5 + 6i) != C1 + if b { + println("const bool 2", b) + panic("fail") + } + + b = C1 == (5 + 6i) + if !b { + println("const bool 3", b) + panic("fail") + } + + b = C1 != (5 + 6i) + if b { + println("const bool 4", b) + panic("fail") + } + + // vars passed through parameters + booltest(5+6i, true) + booltest(5+7i, false) + booltest(6+6i, false) + booltest(6+9i, false) +} + +func booltest(a complex64, r bool) { + var b bool + + b = a == C1 + if b != r { + println("param bool 1", a, b, r) + panic("fail") + } + + b = a != C1 + if b == r { + println("param bool 2", a, b, r) + panic("fail") + } + + b = C1 == a + if b != r { + println("param bool 3", a, b, r) + panic("fail") + } + + b = C1 != a + if b == r { + println("param bool 4", a, b, r) + panic("fail") + } + + if r { + if a != C1 { + println("param bool 5", a, b, r) + panic("fail") + } + if C1 != a { + println("param bool 6", a, b, r) + panic("fail") + } + } else { + if a == C1 { + println("param bool 6", a, b, r) + panic("fail") + } + if C1 == a { + println("param bool 7", a, b, r) + panic("fail") + } + } +} diff --git a/test/ken/cplx2.go b/test/ken/cplx2.go new file mode 100644 index 0000000..eb1da7b --- /dev/null +++ b/test/ken/cplx2.go @@ -0,0 +1,122 @@ +// run + +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test arithmetic on complex numbers, including multiplication and division. + +package main + +const ( + R = 5 + I = 6i + + C1 = R + I // ADD(5,6) + C2 = R - I // SUB(5,-6) + C3 = -(R + I) // ADD(5,6) NEG(-5,-6) + C4 = -(R - I) // SUB(5,-6) NEG(-5,6) + + C5 = C1 + R // ADD(10,6) + C6 = C1 + I // ADD(5,12) + + Ca = C5 + C6 // ADD(15,18) + Cb = C5 - C6 // SUB(5,-6) + + Cc = C5 * C6 // MUL(-22,-150) + Cd = C5 / C6 // DIV(0.721893,-0.532544) + Ce = Cd * C6 // MUL(10,6) sb C5 +) + +func main() { + + var r complex64 = 5 + 0i + if r != R { + println("opcode 1", r, R) + panic("fail") + } + + var i complex64 = 6i + if i != I { + println("opcode 2", i, I) + panic("fail") + } + + c1 := r + i + if c1 != C1 { + println("opcode x", c1, C1) + panic("fail") + } + + c2 := r - i + if c2 != C2 { + println("opcode x", c2, C2) + panic("fail") + } + + c3 := -(r + i) + if c3 != C3 { + println("opcode x", c3, C3) + panic("fail") + } + + c4 := -(r - i) + if c4 != C4 { + println("opcode x", c4, C4) + panic("fail") + } + + c5 := c1 + r + if c5 != C5 { + println("opcode x", c5, C5) + panic("fail") + } + + c6 := c1 + i + if c6 != C6 { + println("opcode x", c6, C6) + panic("fail") + } + + ca := c5 + c6 + if ca != Ca { + println("opcode x", ca, Ca) + panic("fail") + } + + cb := c5 - c6 + if cb != Cb { + println("opcode x", cb, Cb) + panic("fail") + } + + cc := c5 * c6 + if cc != Cc { + println("opcode x", cc, Cc) + panic("fail") + } + + cd := c5 / c6 + if cd != Cd { + println("opcode x", cd, Cd) + panic("fail") + } + + ce := cd * c6 + if ce != Ce { + println("opcode x", ce, Ce) + panic("fail") + } + + r32 := real(complex64(ce)) + if r32 != float32(real(Ce)) { + println("real(complex64(ce))", r32, real(Ce)) + panic("fail") + } + + r64 := real(complex128(ce)) + if r64 != real(Ce) { + println("real(complex128(ce))", r64, real(Ce)) + panic("fail") + } +} diff --git a/test/ken/cplx3.go b/test/ken/cplx3.go new file mode 100644 index 0000000..be0b864 --- /dev/null +++ b/test/ken/cplx3.go @@ -0,0 +1,54 @@ +// run + +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test composition, decomposition, and reflection on complex numbers. + +package main + +import "unsafe" +import "reflect" + +const ( + R = 5 + I = 6i + + C1 = R + I // ADD(5,6) +) + +func main() { + c0 := C1 + c0 = (c0 + c0 + c0) / (c0 + c0 + 3i) + r, i := real(c0), imag(c0) + d := r - 1.292308 + if d < 0 { + d = - d + } + if d > 1e-6 { + println(r, "!= 1.292308") + panic(0) + } + d = i + 0.1384615 + if d < 0 { + d = - d + } + if d > 1e-6 { + println(i, "!= -0.1384615") + panic(0) + } + + c := *(*complex128)(unsafe.Pointer(&c0)) + if c != c0 { + println(c, "!=", c) + panic(0) + } + + var a interface{} + switch c := reflect.ValueOf(a); c.Kind() { + case reflect.Complex64, reflect.Complex128: + v := c.Complex() + _, _ = complex128(v), true + } +} diff --git a/test/ken/cplx4.go b/test/ken/cplx4.go new file mode 100644 index 0000000..97d5d16 --- /dev/null +++ b/test/ken/cplx4.go @@ -0,0 +1,61 @@ +// run + +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test complex numbers,including fmt support. +// Used to crash. + +package main + +import "fmt" + +const ( + R = 5 + I = 6i + + C1 = R + I // ADD(5,6) +) + +func want(s, w string) { + if s != w { + panic(s + " != " + w) + } +} + +func doprint(c complex128, w string) { + s := fmt.Sprintf("%f", c) + want(s, w) +} + +func main() { + + // constants + s := fmt.Sprintf("%f", -C1) + want(s, "(-5.000000-6.000000i)") + doprint(C1, "(5.000000+6.000000i)") + + // variables + c1 := C1 + s = fmt.Sprintf("%f", c1) + want(s, "(5.000000+6.000000i)") + doprint(c1, "(5.000000+6.000000i)") + + // 128 + c2 := complex128(C1) + s = fmt.Sprintf("%G", c2) + want(s, "(5+6i)") + + // real, imag, complex + c3 := complex(real(c2)+3, imag(c2)-5) + c2 + s = fmt.Sprintf("%G", c3) + want(s, "(13+7i)") + + // compiler used to crash on nested divide + c4 := complex(real(c3/2), imag(c3/2)) + if c4 != c3/2 { + fmt.Printf("BUG: c3 = %G != c4 = %G\n", c3, c4) + panic(0) + } +} diff --git a/test/ken/cplx5.go b/test/ken/cplx5.go new file mode 100644 index 0000000..4e8f443 --- /dev/null +++ b/test/ken/cplx5.go @@ -0,0 +1,72 @@ +// run + +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test compound types made of complex numbers. + +package main + +var a [12]complex128 +var s []complex128 +var c chan complex128 +var f struct { + c complex128 +} +var m map[complex128]complex128 + +func main() { + // array of complex128 + for i := 0; i < len(a); i++ { + a[i] = complex(float64(i), float64(-i)) + } + if a[5] != 5-5i { + panic(a[5]) + } + + // slice of complex128 + s = make([]complex128, len(a)) + for i := 0; i < len(s); i++ { + s[i] = a[i] + } + if s[5] != 5-5i { + panic(s[5]) + } + + // chan + c = make(chan complex128) + go chantest(c) + vc := <-c + if vc != 5-5i { + panic(vc) + } + + // pointer of complex128 + v := a[5] + pv := &v + if *pv != 5-5i { + panic(*pv) + } + + // field of complex128 + f.c = a[5] + if f.c != 5-5i { + panic(f.c) + } + + // map of complex128 + m = make(map[complex128]complex128) + for i := 0; i < len(s); i++ { + m[-a[i]] = a[i] + } + if m[5i-5] != 5-5i { + panic(m[5i-5]) + } + vm := m[complex(-5, 5)] + if vm != 5-5i { + panic(vm) + } +} + +func chantest(c chan complex128) { c <- a[5] } diff --git a/test/ken/divconst.go b/test/ken/divconst.go new file mode 100644 index 0000000..670e074 --- /dev/null +++ b/test/ken/divconst.go @@ -0,0 +1,634 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test integer division by constants. + +package main + +import "math/rand" + +const Count = 1e5 + +func i64rand() int64 { + for { + a := int64(rand.Uint32()) + a = (a << 32) | int64(rand.Uint32()) + a >>= uint(rand.Intn(64)) + if -a != a { + return a + } + } + return 0 // impossible +} + +func i64test(a, b, c int64) { + d := a / c + if d != b { + println("i64", a, b, c, d) + panic("fail") + } +} + +func i64run() { + var a, b int64 + + for i := 0; i < Count; i++ { + a = i64rand() + + b = a / 1 + i64test(a, b, 1) + b = a / 2 + i64test(a, b, 2) + b = a / 3 + i64test(a, b, 3) + b = a / 4 + i64test(a, b, 4) + b = a / 5 + i64test(a, b, 5) + b = a / 6 + i64test(a, b, 6) + b = a / 7 + i64test(a, b, 7) + b = a / 8 + i64test(a, b, 8) + b = a / 10 + i64test(a, b, 10) + b = a / 16 + i64test(a, b, 16) + b = a / 20 + i64test(a, b, 20) + b = a / 32 + i64test(a, b, 32) + b = a / 60 + i64test(a, b, 60) + b = a / 64 + i64test(a, b, 64) + b = a / 128 + i64test(a, b, 128) + b = a / 256 + i64test(a, b, 256) + b = a / 16384 + i64test(a, b, 16384) + + b = a / -1 + i64test(a, b, -1) + b = a / -2 + i64test(a, b, -2) + b = a / -3 + i64test(a, b, -3) + b = a / -4 + i64test(a, b, -4) + b = a / -5 + i64test(a, b, -5) + b = a / -6 + i64test(a, b, -6) + b = a / -7 + i64test(a, b, -7) + b = a / -8 + i64test(a, b, -8) + b = a / -10 + i64test(a, b, -10) + b = a / -16 + i64test(a, b, -16) + b = a / -20 + i64test(a, b, -20) + b = a / -32 + i64test(a, b, -32) + b = a / -60 + i64test(a, b, -60) + b = a / -64 + i64test(a, b, -64) + b = a / -128 + i64test(a, b, -128) + b = a / -256 + i64test(a, b, -256) + b = a / -16384 + i64test(a, b, -16384) + } +} + +func u64rand() uint64 { + a := uint64(rand.Uint32()) + a = (a << 32) | uint64(rand.Uint32()) + a >>= uint(rand.Intn(64)) + return a +} + +func u64test(a, b, c uint64) { + d := a / c + if d != b { + println("u64", a, b, c, d) + panic("fail") + } +} + +func u64run() { + var a, b uint64 + + for i := 0; i < Count; i++ { + a = u64rand() + + b = a / 1 + u64test(a, b, 1) + b = a / 2 + u64test(a, b, 2) + b = a / 3 + u64test(a, b, 3) + b = a / 4 + u64test(a, b, 4) + b = a / 5 + u64test(a, b, 5) + b = a / 6 + u64test(a, b, 6) + b = a / 7 + u64test(a, b, 7) + b = a / 8 + u64test(a, b, 8) + b = a / 10 + u64test(a, b, 10) + b = a / 16 + u64test(a, b, 16) + b = a / 20 + u64test(a, b, 20) + b = a / 32 + u64test(a, b, 32) + b = a / 60 + u64test(a, b, 60) + b = a / 64 + u64test(a, b, 64) + b = a / 128 + u64test(a, b, 128) + b = a / 256 + u64test(a, b, 256) + b = a / 16384 + u64test(a, b, 16384) + } +} + +func i32rand() int32 { + for { + a := int32(rand.Uint32()) + a >>= uint(rand.Intn(32)) + if -a != a { + return a + } + } + return 0 // impossible +} + +func i32test(a, b, c int32) { + d := a / c + if d != b { + println("i32", a, b, c, d) + panic("fail") + } +} + +func i32run() { + var a, b int32 + + for i := 0; i < Count; i++ { + a = i32rand() + + b = a / 1 + i32test(a, b, 1) + b = a / 2 + i32test(a, b, 2) + b = a / 3 + i32test(a, b, 3) + b = a / 4 + i32test(a, b, 4) + b = a / 5 + i32test(a, b, 5) + b = a / 6 + i32test(a, b, 6) + b = a / 7 + i32test(a, b, 7) + b = a / 8 + i32test(a, b, 8) + b = a / 10 + i32test(a, b, 10) + b = a / 16 + i32test(a, b, 16) + b = a / 20 + i32test(a, b, 20) + b = a / 32 + i32test(a, b, 32) + b = a / 60 + i32test(a, b, 60) + b = a / 64 + i32test(a, b, 64) + b = a / 128 + i32test(a, b, 128) + b = a / 256 + i32test(a, b, 256) + b = a / 16384 + i32test(a, b, 16384) + + b = a / -1 + i32test(a, b, -1) + b = a / -2 + i32test(a, b, -2) + b = a / -3 + i32test(a, b, -3) + b = a / -4 + i32test(a, b, -4) + b = a / -5 + i32test(a, b, -5) + b = a / -6 + i32test(a, b, -6) + b = a / -7 + i32test(a, b, -7) + b = a / -8 + i32test(a, b, -8) + b = a / -10 + i32test(a, b, -10) + b = a / -16 + i32test(a, b, -16) + b = a / -20 + i32test(a, b, -20) + b = a / -32 + i32test(a, b, -32) + b = a / -60 + i32test(a, b, -60) + b = a / -64 + i32test(a, b, -64) + b = a / -128 + i32test(a, b, -128) + b = a / -256 + i32test(a, b, -256) + } +} + +func u32rand() uint32 { + a := uint32(rand.Uint32()) + a >>= uint(rand.Intn(32)) + return a +} + +func u32test(a, b, c uint32) { + d := a / c + if d != b { + println("u32", a, b, c, d) + panic("fail") + } +} + +func u32run() { + var a, b uint32 + + for i := 0; i < Count; i++ { + a = u32rand() + + b = a / 1 + u32test(a, b, 1) + b = a / 2 + u32test(a, b, 2) + b = a / 3 + u32test(a, b, 3) + b = a / 4 + u32test(a, b, 4) + b = a / 5 + u32test(a, b, 5) + b = a / 6 + u32test(a, b, 6) + b = a / 7 + u32test(a, b, 7) + b = a / 8 + u32test(a, b, 8) + b = a / 10 + u32test(a, b, 10) + b = a / 16 + u32test(a, b, 16) + b = a / 20 + u32test(a, b, 20) + b = a / 32 + u32test(a, b, 32) + b = a / 60 + u32test(a, b, 60) + b = a / 64 + u32test(a, b, 64) + b = a / 128 + u32test(a, b, 128) + b = a / 256 + u32test(a, b, 256) + b = a / 16384 + u32test(a, b, 16384) + } +} + +func i16rand() int16 { + for { + a := int16(rand.Uint32()) + a >>= uint(rand.Intn(16)) + if -a != a { + return a + } + } + return 0 // impossible +} + +func i16test(a, b, c int16) { + d := a / c + if d != b { + println("i16", a, b, c, d) + panic("fail") + } +} + +func i16run() { + var a, b int16 + + for i := 0; i < Count; i++ { + a = i16rand() + + b = a / 1 + i16test(a, b, 1) + b = a / 2 + i16test(a, b, 2) + b = a / 3 + i16test(a, b, 3) + b = a / 4 + i16test(a, b, 4) + b = a / 5 + i16test(a, b, 5) + b = a / 6 + i16test(a, b, 6) + b = a / 7 + i16test(a, b, 7) + b = a / 8 + i16test(a, b, 8) + b = a / 10 + i16test(a, b, 10) + b = a / 16 + i16test(a, b, 16) + b = a / 20 + i16test(a, b, 20) + b = a / 32 + i16test(a, b, 32) + b = a / 60 + i16test(a, b, 60) + b = a / 64 + i16test(a, b, 64) + b = a / 128 + i16test(a, b, 128) + b = a / 256 + i16test(a, b, 256) + b = a / 16384 + i16test(a, b, 16384) + + b = a / -1 + i16test(a, b, -1) + b = a / -2 + i16test(a, b, -2) + b = a / -3 + i16test(a, b, -3) + b = a / -4 + i16test(a, b, -4) + b = a / -5 + i16test(a, b, -5) + b = a / -6 + i16test(a, b, -6) + b = a / -7 + i16test(a, b, -7) + b = a / -8 + i16test(a, b, -8) + b = a / -10 + i16test(a, b, -10) + b = a / -16 + i16test(a, b, -16) + b = a / -20 + i16test(a, b, -20) + b = a / -32 + i16test(a, b, -32) + b = a / -60 + i16test(a, b, -60) + b = a / -64 + i16test(a, b, -64) + b = a / -128 + i16test(a, b, -128) + b = a / -256 + i16test(a, b, -256) + b = a / -16384 + i16test(a, b, -16384) + } +} + +func u16rand() uint16 { + a := uint16(rand.Uint32()) + a >>= uint(rand.Intn(16)) + return a +} + +func u16test(a, b, c uint16) { + d := a / c + if d != b { + println("u16", a, b, c, d) + panic("fail") + } +} + +func u16run() { + var a, b uint16 + + for i := 0; i < Count; i++ { + a = u16rand() + + b = a / 1 + u16test(a, b, 1) + b = a / 2 + u16test(a, b, 2) + b = a / 3 + u16test(a, b, 3) + b = a / 4 + u16test(a, b, 4) + b = a / 5 + u16test(a, b, 5) + b = a / 6 + u16test(a, b, 6) + b = a / 7 + u16test(a, b, 7) + b = a / 8 + u16test(a, b, 8) + b = a / 10 + u16test(a, b, 10) + b = a / 16 + u16test(a, b, 16) + b = a / 20 + u16test(a, b, 20) + b = a / 32 + u16test(a, b, 32) + b = a / 60 + u16test(a, b, 60) + b = a / 64 + u16test(a, b, 64) + b = a / 128 + u16test(a, b, 128) + b = a / 256 + u16test(a, b, 256) + b = a / 16384 + u16test(a, b, 16384) + } +} + +func i8rand() int8 { + for { + a := int8(rand.Uint32()) + a >>= uint(rand.Intn(8)) + if -a != a { + return a + } + } + return 0 // impossible +} + +func i8test(a, b, c int8) { + d := a / c + if d != b { + println("i8", a, b, c, d) + panic("fail") + } +} + +func i8run() { + var a, b int8 + + for i := 0; i < Count; i++ { + a = i8rand() + + b = a / 1 + i8test(a, b, 1) + b = a / 2 + i8test(a, b, 2) + b = a / 3 + i8test(a, b, 3) + b = a / 4 + i8test(a, b, 4) + b = a / 5 + i8test(a, b, 5) + b = a / 6 + i8test(a, b, 6) + b = a / 7 + i8test(a, b, 7) + b = a / 8 + i8test(a, b, 8) + b = a / 10 + i8test(a, b, 10) + b = a / 8 + i8test(a, b, 8) + b = a / 20 + i8test(a, b, 20) + b = a / 32 + i8test(a, b, 32) + b = a / 60 + i8test(a, b, 60) + b = a / 64 + i8test(a, b, 64) + b = a / 127 + i8test(a, b, 127) + + b = a / -1 + i8test(a, b, -1) + b = a / -2 + i8test(a, b, -2) + b = a / -3 + i8test(a, b, -3) + b = a / -4 + i8test(a, b, -4) + b = a / -5 + i8test(a, b, -5) + b = a / -6 + i8test(a, b, -6) + b = a / -7 + i8test(a, b, -7) + b = a / -8 + i8test(a, b, -8) + b = a / -10 + i8test(a, b, -10) + b = a / -8 + i8test(a, b, -8) + b = a / -20 + i8test(a, b, -20) + b = a / -32 + i8test(a, b, -32) + b = a / -60 + i8test(a, b, -60) + b = a / -64 + i8test(a, b, -64) + b = a / -128 + i8test(a, b, -128) + } +} + +func u8rand() uint8 { + a := uint8(rand.Uint32()) + a >>= uint(rand.Intn(8)) + return a +} + +func u8test(a, b, c uint8) { + d := a / c + if d != b { + println("u8", a, b, c, d) + panic("fail") + } +} + +func u8run() { + var a, b uint8 + + for i := 0; i < Count; i++ { + a = u8rand() + + b = a / 1 + u8test(a, b, 1) + b = a / 2 + u8test(a, b, 2) + b = a / 3 + u8test(a, b, 3) + b = a / 4 + u8test(a, b, 4) + b = a / 5 + u8test(a, b, 5) + b = a / 6 + u8test(a, b, 6) + b = a / 7 + u8test(a, b, 7) + b = a / 8 + u8test(a, b, 8) + b = a / 10 + u8test(a, b, 10) + b = a / 8 + u8test(a, b, 8) + b = a / 20 + u8test(a, b, 20) + b = a / 32 + u8test(a, b, 32) + b = a / 60 + u8test(a, b, 60) + b = a / 64 + u8test(a, b, 64) + b = a / 128 + u8test(a, b, 128) + b = a / 184 + u8test(a, b, 184) + } +} + +func main() { + xtest() + i64run() + u64run() + i32run() + u32run() + i16run() + u16run() + i8run() + u8run() +} + +func xtest() { +} diff --git a/test/ken/divmod.go b/test/ken/divmod.go new file mode 100644 index 0000000..f1bd56e --- /dev/null +++ b/test/ken/divmod.go @@ -0,0 +1,249 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test integer division and modulus. + +package main + +const ( + // example from the spec + n1 = +5 + n2 = -5 + d1 = +3 + d2 = -3 + + q1 = +1 + q2 = -1 + q3 = -1 + q4 = +1 + + r1 = +2 + r2 = -2 + r3 = +2 + r4 = -2 +) + +func main() { + /* ideals */ + if n1/d1 != q1 || n1%d1 != r1 { + println("ideal-1", n1, d1, n1/d1, n1%d1) + panic("fail") + } + if n2/d1 != q2 || n2%d1 != r2 { + println("ideal-2", n2, d1, n2/d1, n2%d1) + panic("fail") + } + if n1/d2 != q3 || n1%d2 != r3 { + println("ideal-3", n1, d2, n1/d2, n1%d2) + panic("fail") + } + if n2/d2 != q4 || n2%d2 != r4 { + println("ideal-4", n2, d2, n2/d2, n2%d2) + panic("fail") + } + + /* int */ + var in1 int = +5 + var in2 int = -5 + var id1 int = +3 + var id2 int = -3 + + if in1/id1 != q1 || in1%id1 != r1 { + println("int-1", in1, id1, in1/id1, in1%id1) + panic("fail") + } + if in2/id1 != q2 || in2%id1 != r2 { + println("int-2", in2, id1, in2/id1, in2%id1) + panic("fail") + } + if in1/id2 != q3 || in1%id2 != r3 { + println("int-3", in1, id2, in1/id2, in1%id2) + panic("fail") + } + if in2/id2 != q4 || in2%id2 != r4 { + println("int-4", in2, id2, in2/id2, in2%id2) + panic("fail") + } + + /* int8 */ + var bn1 int8 = +5 + var bn2 int8 = -5 + var bd1 int8 = +3 + var bd2 int8 = -3 + + if bn1/bd1 != q1 || bn1%bd1 != r1 { + println("int8-1", bn1, bd1, bn1/bd1, bn1%bd1) + panic("fail") + } + if bn2/bd1 != q2 || bn2%bd1 != r2 { + println("int8-2", bn2, bd1, bn2/bd1, bn2%bd1) + panic("fail") + } + if bn1/bd2 != q3 || bn1%bd2 != r3 { + println("int8-3", bn1, bd2, bn1/bd2, bn1%bd2) + panic("fail") + } + if bn2/bd2 != q4 || bn2%bd2 != r4 { + println("int8-4", bn2, bd2, bn2/bd2, bn2%bd2) + panic("fail") + } + + /* int16 */ + var sn1 int16 = +5 + var sn2 int16 = -5 + var sd1 int16 = +3 + var sd2 int16 = -3 + + if sn1/sd1 != q1 || sn1%sd1 != r1 { + println("int16-1", sn1, sd1, sn1/sd1, sn1%sd1) + panic("fail") + } + if sn2/sd1 != q2 || sn2%sd1 != r2 { + println("int16-2", sn2, sd1, sn2/sd1, sn2%sd1) + panic("fail") + } + if sn1/sd2 != q3 || sn1%sd2 != r3 { + println("int16-3", sn1, sd2, sn1/sd2, sn1%sd2) + panic("fail") + } + if sn2/sd2 != q4 || sn2%sd2 != r4 { + println("int16-4", sn2, sd2, sn2/sd2, sn2%sd2) + panic("fail") + } + + /* int32 */ + var ln1 int32 = +5 + var ln2 int32 = -5 + var ld1 int32 = +3 + var ld2 int32 = -3 + + if ln1/ld1 != q1 || ln1%ld1 != r1 { + println("int32-1", ln1, ld1, ln1/ld1, ln1%ld1) + panic("fail") + } + if ln2/ld1 != q2 || ln2%ld1 != r2 { + println("int32-2", ln2, ld1, ln2/ld1, ln2%ld1) + panic("fail") + } + if ln1/ld2 != q3 || ln1%ld2 != r3 { + println("int32-3", ln1, ld2, ln1/ld2, ln1%ld2) + panic("fail") + } + if ln2/ld2 != q4 || ln2%ld2 != r4 { + println("int32-4", ln2, ld2, ln2/ld2, ln2%ld2) + panic("fail") + } + + /* int64 */ + var qn1 int64 = +5 + var qn2 int64 = -5 + var qd1 int64 = +3 + var qd2 int64 = -3 + + if qn1/qd1 != q1 || qn1%qd1 != r1 { + println("int64-1", qn1, qd1, qn1/qd1, qn1%qd1) + panic("fail") + } + if qn2/qd1 != q2 || qn2%qd1 != r2 { + println("int64-2", qn2, qd1, qn2/qd1, qn2%qd1) + panic("fail") + } + if qn1/qd2 != q3 || qn1%qd2 != r3 { + println("int64-3", qn1, qd2, qn1/qd2, qn1%qd2) + panic("fail") + } + if qn2/qd2 != q4 || qn2%qd2 != r4 { + println("int64-4", qn2, qd2, qn2/qd2, qn2%qd2) + panic("fail") + } + + if n1/qd1 != q1 || n1%qd1 != r1 { + println("mixed int64-1", n1, qd1, n1/qd1, n1%qd1) + panic("fail") + } + if n2/qd1 != q2 || n2%qd1 != r2 { + println("mixed int64-2", n2, qd1, n2/qd1, n2%qd1) + panic("fail") + } + if n1/qd2 != q3 || n1%qd2 != r3 { + println("mixed int64-3", n1, qd2, n1/qd2, n1%qd2) + panic("fail") + } + if n2/qd2 != q4 || n2%qd2 != r4 { + println("mixed int64-4", n2, qd2, n2/qd2, n2%qd2) + panic("fail") + } + + if qn1/d1 != q1 || qn1%d1 != r1 { + println("mixed int64-5", qn1, d1, qn1/d1, qn1%d1) + panic("fail") + } + if qn2/d1 != q2 || qn2%d1 != r2 { + println("mixed int64-6", qn2, d1, qn2/d1, qn2%d1) + panic("fail") + } + if qn1/d2 != q3 || qn1%d2 != r3 { + println("mixed int64-7", qn1, d2, qn1/d2, qn1%d2) + panic("fail") + } + if qn2/d2 != q4 || qn2%d2 != r4 { + println("mixed int64-8", qn2, d2, qn2/d2, qn2%d2) + panic("fail") + } + + /* uint */ + var uin1 uint = +5 + var uid1 uint = +3 + + if uin1/uid1 != q1 || uin1%uid1 != r1 { + println("uint", uin1, uid1, uin1/uid1, uin1%uid1) + panic("fail") + } + + /* uint8 */ + var ubn1 uint8 = +5 + var ubd1 uint8 = +3 + + if ubn1/ubd1 != q1 || ubn1%ubd1 != r1 { + println("uint8", ubn1, ubd1, ubn1/ubd1, ubn1%ubd1) + panic("fail") + } + + /* uint16 */ + var usn1 uint16 = +5 + var usd1 uint16 = +3 + + if usn1/usd1 != q1 || usn1%usd1 != r1 { + println("uint16", usn1, usd1, usn1/usd1, usn1%usd1) + panic("fail") + } + + /* uint32 */ + var uln1 uint32 = +5 + var uld1 uint32 = +3 + + if uln1/uld1 != q1 || uln1%uld1 != r1 { + println("uint32", uln1, uld1, uln1/uld1, uln1%uld1) + panic("fail") + } + + /* uint64 */ + var uqn1 uint64 = +5 + var uqd1 uint64 = +3 + + if uqn1/uqd1 != q1 || uqn1%uqd1 != r1 { + println("uint64", uqn1, uqd1, uqn1/uqd1, uqn1%uqd1) + panic("fail") + } + if n1/uqd1 != q1 || n1%uqd1 != r1 { + println("mixed uint64-1", n1, uqd1, n1/uqd1, n1%uqd1) + panic("fail") + } + if uqn1/d1 != q1 || uqn1%d1 != r1 { + println("mixed uint64-2", uqn1, d1, uqn1/d1, uqn1%d1) + panic("fail") + } +} diff --git a/test/ken/embed.go b/test/ken/embed.go new file mode 100644 index 0000000..f7ca066 --- /dev/null +++ b/test/ken/embed.go @@ -0,0 +1,319 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test embedded fields of structs, including methods. + +package main + + +type I interface { + test1() int + test2() int + test3() int + test4() int + test5() int + test6() int + test7() int +} + +/****** + ****** + ******/ + +type SubpSubp struct { + a7 int + a int +} + +func (p *SubpSubp) test7() int { + if p.a != p.a7 { + println("SubpSubp", p, p.a7) + panic("fail") + } + return p.a +} +func (p *SubpSubp) testx() { println("SubpSubp", p, p.a7) } + +/****** + ****** + ******/ + +type SubpSub struct { + a6 int + SubpSubp + a int +} + +func (p *SubpSub) test6() int { + if p.a != p.a6 { + println("SubpSub", p, p.a6) + panic("fail") + } + return p.a +} +func (p *SubpSub) testx() { println("SubpSub", p, p.a6) } + +/****** + ****** + ******/ + +type SubSubp struct { + a5 int + a int +} + +func (p *SubSubp) test5() int { + if p.a != p.a5 { + println("SubpSub", p, p.a5) + panic("fail") + } + return p.a +} + +/****** + ****** + ******/ + +type SubSub struct { + a4 int + a int +} + +func (p *SubSub) test4() int { + if p.a != p.a4 { + println("SubpSub", p, p.a4) + panic("fail") + } + return p.a +} + +/****** + ****** + ******/ + +type Subp struct { + a3 int + *SubpSubp + SubpSub + a int +} + +func (p *Subp) test3() int { + if p.a != p.a3 { + println("SubpSub", p, p.a3) + panic("fail") + } + return p.a +} + +/****** + ****** + ******/ + +type Sub struct { + a2 int + *SubSubp + SubSub + a int +} + +func (p *Sub) test2() int { + if p.a != p.a2 { + println("SubpSub", p, p.a2) + panic("fail") + } + return p.a +} + +/****** + ****** + ******/ + +type S struct { + a1 int + Sub + *Subp + a int +} + +func (p *S) test1() int { + if p.a != p.a1 { + println("SubpSub", p, p.a1) + panic("fail") + } + return p.a +} + +/****** + ****** + ******/ + +func main() { + var i I + var s *S + + // allocate + s = new(S) + s.Subp = new(Subp) + s.Sub.SubSubp = new(SubSubp) + s.Subp.SubpSubp = new(SubpSubp) + + // explicit assignment + s.a = 1 + s.Sub.a = 2 + s.Subp.a = 3 + s.Sub.SubSub.a = 4 + s.Sub.SubSubp.a = 5 + s.Subp.SubpSub.a = 6 + s.Subp.SubpSubp.a = 7 + + // embedded (unique) assignment + s.a1 = 1 + s.a2 = 2 + s.a3 = 3 + s.a4 = 4 + s.a5 = 5 + s.a6 = 6 + s.a7 = 7 + + // unique calls with explicit & + if s.test1() != 1 { + println("t1", 1) + panic("fail") + } + if (&s.Sub).test2() != 2 { + println("t1", 2) + panic("fail") + } + if s.Subp.test3() != 3 { + println("t1", 3) + panic("fail") + } + if (&s.Sub.SubSub).test4() != 4 { + println("t1", 4) + panic("fail") + } + if s.Sub.SubSubp.test5() != 5 { + println("t1", 5) + panic("fail") + } + if (&s.Subp.SubpSub).test6() != 6 { + println("t1", 6) + panic("fail") + } + if s.Subp.SubpSubp.test7() != 7 { + println("t1", 7) + panic("fail") + } + + // automatic & + if s.Sub.test2() != 2 { + println("t2", 2) + panic("fail") + } + if s.Sub.SubSub.test4() != 4 { + println("t2", 4) + panic("fail") + } + if s.Subp.SubpSub.test6() != 6 { + println("t2", 6) + panic("fail") + } + + // embedded calls + if s.test1() != s.a1 { + println("t3", 1) + panic("fail") + } + if s.test2() != s.a2 { + println("t3", 2) + panic("fail") + } + if s.test3() != s.a3 { + println("t3", 3) + panic("fail") + } + if s.test4() != s.a4 { + println("t3", 4) + panic("fail") + } + if s.test5() != s.a5 { + println("t3", 5) + panic("fail") + } + if s.test6() != s.a6 { + println("t3", 6) + panic("fail") + } + if s.test7() != s.a7 { + println("t3", 7) + panic("fail") + } + + // run it through an interface + i = s + s = i.(*S) + + // same as t3 + if s.test1() != s.a1 { + println("t4", 1) + panic("fail") + } + if s.test2() != s.a2 { + println("t4", 2) + panic("fail") + } + if s.test3() != s.a3 { + println("t4", 3) + panic("fail") + } + if s.test4() != s.a4 { + println("t4", 4) + panic("fail") + } + if s.test5() != s.a5 { + println("t4", 5) + panic("fail") + } + if s.test6() != s.a6 { + println("t4", 6) + panic("fail") + } + if s.test7() != s.a7 { + println("t4", 7) + panic("fail") + } + + // call interface + if i.test1() != s.test1() { + println("t5", 1) + panic("fail") + } + if i.test2() != s.test2() { + println("t5", 2) + panic("fail") + } + if i.test3() != s.test3() { + println("t5", 3) + panic("fail") + } + if i.test4() != s.test4() { + println("t5", 4) + panic("fail") + } + if i.test5() != s.test5() { + println("t5", 5) + panic("fail") + } + if i.test6() != s.test6() { + println("t5", 6) + panic("fail") + } + if i.test7() != s.test7() { + println("t5", 7) + panic("fail") + } +} diff --git a/test/ken/for.go b/test/ken/for.go new file mode 100644 index 0000000..db35548 --- /dev/null +++ b/test/ken/for.go @@ -0,0 +1,19 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test simple for loop. + +package main + +func +main() { + var t,i int; + + for i=0; i<100; i=i+1 { + t = t+i; + } + if t != 50*99 { panic(t); } +} diff --git a/test/ken/interbasic.go b/test/ken/interbasic.go new file mode 100644 index 0000000..d8fbb95 --- /dev/null +++ b/test/ken/interbasic.go @@ -0,0 +1,184 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test interfaces on basic types. + +package main + +type myint int +type mystring string +type I0 interface{} + +func f() { + var ia, ib I0 + var i myint + var s mystring + + if ia != ib { + panic("1") + } + + i = 1 + ia = i + ib = i + if ia != ib { + panic("2") + } + if ia == nil { + panic("3") + } + + i = 2 + ia = i + if ia == ib { + panic("4") + } + + ia = nil + if ia == ib { + panic("5") + } + + ib = nil + if ia != ib { + panic("6") + } + + if ia != nil { + panic("7") + } + + s = "abc" + ia = s + ib = nil + if ia == ib { + panic("8") + } + + s = "def" + ib = s + if ia == ib { + panic("9") + } + + s = "abc" + ib = s + if ia != ib { + panic("a") + } +} + +func main() { + var ia [20]I0 + var b bool + var s string + var i8 int8 + var i16 int16 + var i32 int32 + var i64 int64 + var u8 uint8 + var u16 uint16 + var u32 uint32 + var u64 uint64 + + f() + + ia[0] = "xxx" + ia[1] = 12345 + ia[2] = true + + s = "now is" + ia[3] = s + b = false + ia[4] = b + + i8 = 29 + ia[5] = i8 + i16 = 994 + ia[6] = i16 + i32 = 3434 + ia[7] = i32 + i64 = 1234567 + ia[8] = i64 + + u8 = 12 + ia[9] = u8 + u16 = 799 + ia[10] = u16 + u32 = 4455 + ia[11] = u32 + u64 = 765432 + ia[12] = u64 + + s = ia[0].(string) + if s != "xxx" { + println(0, s) + panic("fail") + } + i32 = int32(ia[1].(int)) + if i32 != 12345 { + println(1, i32) + panic("fail") + } + b = ia[2].(bool) + if b != true { + println(2, b) + panic("fail") + } + + s = ia[3].(string) + if s != "now is" { + println(3, s) + panic("fail") + } + b = ia[4].(bool) + if b != false { + println(4, b) + panic("fail") + } + + i8 = ia[5].(int8) + if i8 != 29 { + println(5, i8) + panic("fail") + } + i16 = ia[6].(int16) + if i16 != 994 { + println(6, i16) + panic("fail") + } + i32 = ia[7].(int32) + if i32 != 3434 { + println(7, i32) + panic("fail") + } + i64 = ia[8].(int64) + if i64 != 1234567 { + println(8, i64) + panic("fail") + } + + u8 = ia[9].(uint8) + if u8 != 12 { + println(5, u8) + panic("fail") + } + u16 = ia[10].(uint16) + if u16 != 799 { + println(6, u16) + panic("fail") + } + u32 = ia[11].(uint32) + if u32 != 4455 { + println(7, u32) + panic("fail") + } + u64 = ia[12].(uint64) + if u64 != 765432 { + println(8, u64) + panic("fail") + } +} diff --git a/test/ken/interfun.go b/test/ken/interfun.go new file mode 100644 index 0000000..9432181 --- /dev/null +++ b/test/ken/interfun.go @@ -0,0 +1,59 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test interfaces and methods. + +package main + +type S struct { + a,b int; +} + +type I1 interface { + f ()int; +} + +type I2 interface { + g() int; + f() int; +} + +func (this *S) f()int { + return this.a; +} + +func (this *S) g()int { + return this.b; +} + +func +main() { + var i1 I1; + var i2 I2; + var g *S; + + s := new(S); + s.a = 5; + s.b = 6; + + // call structure + if s.f() != 5 { panic(11); } + if s.g() != 6 { panic(12); } + + i1 = s; // convert S to I1 + i2 = i1.(I2); // convert I1 to I2 + + // call interface + if i1.f() != 5 { panic(21); } + if i2.f() != 5 { panic(22); } + if i2.g() != 6 { panic(23); } + + g = i1.(*S); // convert I1 to S + if g != s { panic(31); } + + g = i2.(*S); // convert I2 to S + if g != s { panic(32); } +} diff --git a/test/ken/intervar.go b/test/ken/intervar.go new file mode 100644 index 0000000..8a2fca0 --- /dev/null +++ b/test/ken/intervar.go @@ -0,0 +1,69 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test interface assignment. + +package main + +type Iputs interface { + puts (s string) string; +} + +// --------- + +type Print struct { + whoami int; + put Iputs; +} + +func (p *Print) dop() string { + r := " print " + string(p.whoami + '0') + return r + p.put.puts("abc"); +} + +// --------- + +type Bio struct { + whoami int; + put Iputs; +} + +func (b *Bio) puts(s string) string { + r := " bio " + string(b.whoami + '0') + return r + b.put.puts(s); +} + +// --------- + +type File struct { + whoami int; + put Iputs; +} + +func (f *File) puts(s string) string { + return " file " + string(f.whoami + '0') + " -- " + s +} + +func +main() { + p := new(Print); + b := new(Bio); + f := new(File); + + p.whoami = 1; + p.put = b; + + b.whoami = 2; + b.put = f; + + f.whoami = 3; + + r := p.dop(); + expected := " print 1 bio 2 file 3 -- abc" + if r != expected { + panic(r + " != " + expected) + } +} diff --git a/test/ken/label.go b/test/ken/label.go new file mode 100644 index 0000000..fcb3e61 --- /dev/null +++ b/test/ken/label.go @@ -0,0 +1,34 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test goto and labels. + +package main + +func main() { + i := 0 + if false { + goto gogoloop + } + if false { + goto gogoloop + } + if false { + goto gogoloop + } + goto gogoloop + + // backward declared +loop: + i = i + 1 + if i < 100 { + goto loop + } + return + +gogoloop: + goto loop +} diff --git a/test/ken/litfun.go b/test/ken/litfun.go new file mode 100644 index 0000000..e241d4e --- /dev/null +++ b/test/ken/litfun.go @@ -0,0 +1,23 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test simple function literals. + +package main + +func +main() { + x := func(a int)int { + x := func(a int)int { + x := func(a int)int { + return a+5; + }; + return x(a)+7; + }; + return x(a)+11; + }; + if x(3) != 3+5+7+11 { panic(x(3)); } +} diff --git a/test/ken/mfunc.go b/test/ken/mfunc.go new file mode 100644 index 0000000..ef24991 --- /dev/null +++ b/test/ken/mfunc.go @@ -0,0 +1,22 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test simple multi-argument multi-valued function. + +package main + +func +main() { + var x,y int; + + x,y = simple(10,20,30); + if x+y != 65 { panic(x+y); } +} + +func +simple(ia,ib,ic int) (oa,ob int) { + return ia+5, ib+ic; +} diff --git a/test/ken/modconst.go b/test/ken/modconst.go new file mode 100644 index 0000000..c27bf64 --- /dev/null +++ b/test/ken/modconst.go @@ -0,0 +1,634 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test integer modulus by constants. + +package main + +import "math/rand" + +const Count = 1e5 + +func i64rand() int64 { + for { + a := int64(rand.Uint32()) + a = (a << 32) | int64(rand.Uint32()) + a >>= uint(rand.Intn(64)) + if -a != a { + return a + } + } + return 0 // impossible +} + +func i64test(a, b, c int64) { + d := a % c + if d != b { + println("i64", a, b, c, d) + panic("fail") + } +} + +func i64run() { + var a, b int64 + + for i := 0; i < Count; i++ { + a = i64rand() + + b = a % 1 + i64test(a, b, 1) + b = a % 2 + i64test(a, b, 2) + b = a % 3 + i64test(a, b, 3) + b = a % 4 + i64test(a, b, 4) + b = a % 5 + i64test(a, b, 5) + b = a % 6 + i64test(a, b, 6) + b = a % 7 + i64test(a, b, 7) + b = a % 8 + i64test(a, b, 8) + b = a % 10 + i64test(a, b, 10) + b = a % 16 + i64test(a, b, 16) + b = a % 20 + i64test(a, b, 20) + b = a % 32 + i64test(a, b, 32) + b = a % 60 + i64test(a, b, 60) + b = a % 64 + i64test(a, b, 64) + b = a % 128 + i64test(a, b, 128) + b = a % 256 + i64test(a, b, 256) + b = a % 16384 + i64test(a, b, 16384) + + b = a % -1 + i64test(a, b, -1) + b = a % -2 + i64test(a, b, -2) + b = a % -3 + i64test(a, b, -3) + b = a % -4 + i64test(a, b, -4) + b = a % -5 + i64test(a, b, -5) + b = a % -6 + i64test(a, b, -6) + b = a % -7 + i64test(a, b, -7) + b = a % -8 + i64test(a, b, -8) + b = a % -10 + i64test(a, b, -10) + b = a % -16 + i64test(a, b, -16) + b = a % -20 + i64test(a, b, -20) + b = a % -32 + i64test(a, b, -32) + b = a % -60 + i64test(a, b, -60) + b = a % -64 + i64test(a, b, -64) + b = a % -128 + i64test(a, b, -128) + b = a % -256 + i64test(a, b, -256) + b = a % -16384 + i64test(a, b, -16384) + } +} + +func u64rand() uint64 { + a := uint64(rand.Uint32()) + a = (a << 32) | uint64(rand.Uint32()) + a >>= uint(rand.Intn(64)) + return a +} + +func u64test(a, b, c uint64) { + d := a % c + if d != b { + println("u64", a, b, c, d) + panic("fail") + } +} + +func u64run() { + var a, b uint64 + + for i := 0; i < Count; i++ { + a = u64rand() + + b = a % 1 + u64test(a, b, 1) + b = a % 2 + u64test(a, b, 2) + b = a % 3 + u64test(a, b, 3) + b = a % 4 + u64test(a, b, 4) + b = a % 5 + u64test(a, b, 5) + b = a % 6 + u64test(a, b, 6) + b = a % 7 + u64test(a, b, 7) + b = a % 8 + u64test(a, b, 8) + b = a % 10 + u64test(a, b, 10) + b = a % 16 + u64test(a, b, 16) + b = a % 20 + u64test(a, b, 20) + b = a % 32 + u64test(a, b, 32) + b = a % 60 + u64test(a, b, 60) + b = a % 64 + u64test(a, b, 64) + b = a % 128 + u64test(a, b, 128) + b = a % 256 + u64test(a, b, 256) + b = a % 16384 + u64test(a, b, 16384) + } +} + +func i32rand() int32 { + for { + a := int32(rand.Uint32()) + a >>= uint(rand.Intn(32)) + if -a != a { + return a + } + } + return 0 // impossible +} + +func i32test(a, b, c int32) { + d := a % c + if d != b { + println("i32", a, b, c, d) + panic("fail") + } +} + +func i32run() { + var a, b int32 + + for i := 0; i < Count; i++ { + a = i32rand() + + b = a % 1 + i32test(a, b, 1) + b = a % 2 + i32test(a, b, 2) + b = a % 3 + i32test(a, b, 3) + b = a % 4 + i32test(a, b, 4) + b = a % 5 + i32test(a, b, 5) + b = a % 6 + i32test(a, b, 6) + b = a % 7 + i32test(a, b, 7) + b = a % 8 + i32test(a, b, 8) + b = a % 10 + i32test(a, b, 10) + b = a % 16 + i32test(a, b, 16) + b = a % 20 + i32test(a, b, 20) + b = a % 32 + i32test(a, b, 32) + b = a % 60 + i32test(a, b, 60) + b = a % 64 + i32test(a, b, 64) + b = a % 128 + i32test(a, b, 128) + b = a % 256 + i32test(a, b, 256) + b = a % 16384 + i32test(a, b, 16384) + + b = a % -1 + i32test(a, b, -1) + b = a % -2 + i32test(a, b, -2) + b = a % -3 + i32test(a, b, -3) + b = a % -4 + i32test(a, b, -4) + b = a % -5 + i32test(a, b, -5) + b = a % -6 + i32test(a, b, -6) + b = a % -7 + i32test(a, b, -7) + b = a % -8 + i32test(a, b, -8) + b = a % -10 + i32test(a, b, -10) + b = a % -16 + i32test(a, b, -16) + b = a % -20 + i32test(a, b, -20) + b = a % -32 + i32test(a, b, -32) + b = a % -60 + i32test(a, b, -60) + b = a % -64 + i32test(a, b, -64) + b = a % -128 + i32test(a, b, -128) + b = a % -256 + i32test(a, b, -256) + } +} + +func u32rand() uint32 { + a := uint32(rand.Uint32()) + a >>= uint(rand.Intn(32)) + return a +} + +func u32test(a, b, c uint32) { + d := a % c + if d != b { + println("u32", a, b, c, d) + panic("fail") + } +} + +func u32run() { + var a, b uint32 + + for i := 0; i < Count; i++ { + a = u32rand() + + b = a % 1 + u32test(a, b, 1) + b = a % 2 + u32test(a, b, 2) + b = a % 3 + u32test(a, b, 3) + b = a % 4 + u32test(a, b, 4) + b = a % 5 + u32test(a, b, 5) + b = a % 6 + u32test(a, b, 6) + b = a % 7 + u32test(a, b, 7) + b = a % 8 + u32test(a, b, 8) + b = a % 10 + u32test(a, b, 10) + b = a % 16 + u32test(a, b, 16) + b = a % 20 + u32test(a, b, 20) + b = a % 32 + u32test(a, b, 32) + b = a % 60 + u32test(a, b, 60) + b = a % 64 + u32test(a, b, 64) + b = a % 128 + u32test(a, b, 128) + b = a % 256 + u32test(a, b, 256) + b = a % 16384 + u32test(a, b, 16384) + } +} + +func i16rand() int16 { + for { + a := int16(rand.Uint32()) + a >>= uint(rand.Intn(16)) + if -a != a { + return a + } + } + return 0 // impossible +} + +func i16test(a, b, c int16) { + d := a % c + if d != b { + println("i16", a, b, c, d) + panic("fail") + } +} + +func i16run() { + var a, b int16 + + for i := 0; i < Count; i++ { + a = i16rand() + + b = a % 1 + i16test(a, b, 1) + b = a % 2 + i16test(a, b, 2) + b = a % 3 + i16test(a, b, 3) + b = a % 4 + i16test(a, b, 4) + b = a % 5 + i16test(a, b, 5) + b = a % 6 + i16test(a, b, 6) + b = a % 7 + i16test(a, b, 7) + b = a % 8 + i16test(a, b, 8) + b = a % 10 + i16test(a, b, 10) + b = a % 16 + i16test(a, b, 16) + b = a % 20 + i16test(a, b, 20) + b = a % 32 + i16test(a, b, 32) + b = a % 60 + i16test(a, b, 60) + b = a % 64 + i16test(a, b, 64) + b = a % 128 + i16test(a, b, 128) + b = a % 256 + i16test(a, b, 256) + b = a % 16384 + i16test(a, b, 16384) + + b = a % -1 + i16test(a, b, -1) + b = a % -2 + i16test(a, b, -2) + b = a % -3 + i16test(a, b, -3) + b = a % -4 + i16test(a, b, -4) + b = a % -5 + i16test(a, b, -5) + b = a % -6 + i16test(a, b, -6) + b = a % -7 + i16test(a, b, -7) + b = a % -8 + i16test(a, b, -8) + b = a % -10 + i16test(a, b, -10) + b = a % -16 + i16test(a, b, -16) + b = a % -20 + i16test(a, b, -20) + b = a % -32 + i16test(a, b, -32) + b = a % -60 + i16test(a, b, -60) + b = a % -64 + i16test(a, b, -64) + b = a % -128 + i16test(a, b, -128) + b = a % -256 + i16test(a, b, -256) + b = a % -16384 + i16test(a, b, -16384) + } +} + +func u16rand() uint16 { + a := uint16(rand.Uint32()) + a >>= uint(rand.Intn(16)) + return a +} + +func u16test(a, b, c uint16) { + d := a % c + if d != b { + println("u16", a, b, c, d) + panic("fail") + } +} + +func u16run() { + var a, b uint16 + + for i := 0; i < Count; i++ { + a = u16rand() + + b = a % 1 + u16test(a, b, 1) + b = a % 2 + u16test(a, b, 2) + b = a % 3 + u16test(a, b, 3) + b = a % 4 + u16test(a, b, 4) + b = a % 5 + u16test(a, b, 5) + b = a % 6 + u16test(a, b, 6) + b = a % 7 + u16test(a, b, 7) + b = a % 8 + u16test(a, b, 8) + b = a % 10 + u16test(a, b, 10) + b = a % 16 + u16test(a, b, 16) + b = a % 20 + u16test(a, b, 20) + b = a % 32 + u16test(a, b, 32) + b = a % 60 + u16test(a, b, 60) + b = a % 64 + u16test(a, b, 64) + b = a % 128 + u16test(a, b, 128) + b = a % 256 + u16test(a, b, 256) + b = a % 16384 + u16test(a, b, 16384) + } +} + +func i8rand() int8 { + for { + a := int8(rand.Uint32()) + a >>= uint(rand.Intn(8)) + if -a != a { + return a + } + } + return 0 // impossible +} + +func i8test(a, b, c int8) { + d := a % c + if d != b { + println("i8", a, b, c, d) + panic("fail") + } +} + +func i8run() { + var a, b int8 + + for i := 0; i < Count; i++ { + a = i8rand() + + b = a % 1 + i8test(a, b, 1) + b = a % 2 + i8test(a, b, 2) + b = a % 3 + i8test(a, b, 3) + b = a % 4 + i8test(a, b, 4) + b = a % 5 + i8test(a, b, 5) + b = a % 6 + i8test(a, b, 6) + b = a % 7 + i8test(a, b, 7) + b = a % 8 + i8test(a, b, 8) + b = a % 10 + i8test(a, b, 10) + b = a % 8 + i8test(a, b, 8) + b = a % 20 + i8test(a, b, 20) + b = a % 32 + i8test(a, b, 32) + b = a % 60 + i8test(a, b, 60) + b = a % 64 + i8test(a, b, 64) + b = a % 127 + i8test(a, b, 127) + + b = a % -1 + i8test(a, b, -1) + b = a % -2 + i8test(a, b, -2) + b = a % -3 + i8test(a, b, -3) + b = a % -4 + i8test(a, b, -4) + b = a % -5 + i8test(a, b, -5) + b = a % -6 + i8test(a, b, -6) + b = a % -7 + i8test(a, b, -7) + b = a % -8 + i8test(a, b, -8) + b = a % -10 + i8test(a, b, -10) + b = a % -8 + i8test(a, b, -8) + b = a % -20 + i8test(a, b, -20) + b = a % -32 + i8test(a, b, -32) + b = a % -60 + i8test(a, b, -60) + b = a % -64 + i8test(a, b, -64) + b = a % -128 + i8test(a, b, -128) + b = a % -101 + i8test(a, b, -101) + } +} + +func u8rand() uint8 { + a := uint8(rand.Uint32()) + a >>= uint(rand.Intn(8)) + return a +} + +func u8test(a, b, c uint8) { + d := a % c + if d != b { + println("u8", a, b, c, d) + panic("fail") + } +} + +func u8run() { + var a, b uint8 + + for i := 0; i < Count; i++ { + a = u8rand() + + b = a % 1 + u8test(a, b, 1) + b = a % 2 + u8test(a, b, 2) + b = a % 3 + u8test(a, b, 3) + b = a % 4 + u8test(a, b, 4) + b = a % 5 + u8test(a, b, 5) + b = a % 6 + u8test(a, b, 6) + b = a % 7 + u8test(a, b, 7) + b = a % 8 + u8test(a, b, 8) + b = a % 10 + u8test(a, b, 10) + b = a % 8 + u8test(a, b, 8) + b = a % 20 + u8test(a, b, 20) + b = a % 32 + u8test(a, b, 32) + b = a % 60 + u8test(a, b, 60) + b = a % 64 + u8test(a, b, 64) + b = a % 127 + u8test(a, b, 127) + } +} + +func main() { + xtest() + i64run() + u64run() + i32run() + u32run() + i16run() + u16run() + i8run() + u8run() +} + +func xtest() { +} diff --git a/test/ken/ptrfun.go b/test/ken/ptrfun.go new file mode 100644 index 0000000..af806cf --- /dev/null +++ b/test/ken/ptrfun.go @@ -0,0 +1,45 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test method invocation with pointer receivers and function-valued fields. + +package main + +type C struct { + a int; + x func(p *C)int; +} + +func (this *C) f()int { + return this.a; +} + +func +main() { + var v int; + var c *C; + + c = new(C); + c.a = 6; + c.x = g; + + v = g(c); + if v != 6 { panic(v); } + + v = c.x(c); + if v != 6 { panic(v); } + + v = c.f(); + if v != 6 { panic(v); } +} + +func g(p *C)int { + var v int; + + v = p.a; + if v != 6 { panic(v); } + return p.a; +} diff --git a/test/ken/ptrvar.go b/test/ken/ptrvar.go new file mode 100644 index 0000000..d78170c --- /dev/null +++ b/test/ken/ptrvar.go @@ -0,0 +1,54 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test pointers and the . (selector) operator on structs. + +package main + +type x2 struct { a,b,c int; d int; }; +var g1 x2; +var g2 struct { a,b,c int; d x2; }; + +func +main() { + var x int; + var s1 *x2; + var s2 *struct { a,b,c int; d x2; }; + + s1 = &g1; + s2 = &g2; + + s1.a = 1; + s1.b = 2; + s1.c = 3; + s1.d = 5; + + s2.a = 7; + s2.b = 11; + s2.c = 13; + s2.d.a = 17; + s2.d.b = 19; + s2.d.c = 23; + s2.d.d = 20; + + if(s2.d.c != 23) { panic(1); } + if(g2.d.c != 23) { panic(2); } + + x = s1.a + + s1.b + + s1.c + + s1.d + + + s2.a + + s2.b + + s2.c + + s2.d.a + + s2.d.b + + s2.d.c + + s2.d.d; + + if(x != 121) { panic(x); } +} diff --git a/test/ken/range.go b/test/ken/range.go new file mode 100644 index 0000000..89c14e5 --- /dev/null +++ b/test/ken/range.go @@ -0,0 +1,121 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test 'for range' on arrays, slices, and maps. + +package main + +const size = 16 + +var a [size]byte +var p []byte +var m map[int]byte + +func f(k int) byte { + return byte(k * 10007 % size) +} + +func init() { + p = make([]byte, size) + m = make(map[int]byte) + for k := 0; k < size; k++ { + v := f(k) + a[k] = v + p[k] = v + m[k] = v + } +} + +func main() { + var i int + + /* + * key only + */ + i = 0 + for k := range a { + v := a[k] + if v != f(k) { + println("key array range", k, v, a[k]) + panic("fail") + } + i++ + } + if i != size { + println("key array size", i) + panic("fail") + } + + i = 0 + for k := range p { + v := p[k] + if v != f(k) { + println("key pointer range", k, v, p[k]) + panic("fail") + } + i++ + } + if i != size { + println("key pointer size", i) + panic("fail") + } + + i = 0 + for k := range m { + v := m[k] + if v != f(k) { + println("key map range", k, v, m[k]) + panic("fail") + } + i++ + } + if i != size { + println("key map size", i) + panic("fail") + } + + /* + * key,value + */ + i = 0 + for k, v := range a { + if v != f(k) { + println("key:value array range", k, v, a[k]) + panic("fail") + } + i++ + } + if i != size { + println("key:value array size", i) + panic("fail") + } + + i = 0 + for k, v := range p { + if v != f(k) { + println("key:value pointer range", k, v, p[k]) + panic("fail") + } + i++ + } + if i != size { + println("key:value pointer size", i) + panic("fail") + } + + i = 0 + for k, v := range m { + if v != f(k) { + println("key:value map range", k, v, m[k]) + panic("fail") + } + i++ + } + if i != size { + println("key:value map size", i) + panic("fail") + } +} diff --git a/test/ken/rob1.go b/test/ken/rob1.go new file mode 100644 index 0000000..3042a67 --- /dev/null +++ b/test/ken/rob1.go @@ -0,0 +1,72 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test general operation using a list implementation. + +package main + +type Item interface { + Print() string +} + +type ListItem struct { + item Item + next *ListItem +} + +type List struct { + head *ListItem +} + +func (list *List) Init() { + list.head = nil +} + +func (list *List) Insert(i Item) { + item := new(ListItem) + item.item = i + item.next = list.head + list.head = item +} + +func (list *List) Print() string { + r := "" + i := list.head + for i != nil { + r += i.item.Print() + i = i.next + } + return r +} + +// Something to put in a list +type Integer struct { + val int +} + +func (this *Integer) Init(i int) *Integer { + this.val = i + return this +} + +func (this *Integer) Print() string { + return string(this.val + '0') +} + +func main() { + list := new(List) + list.Init() + for i := 0; i < 10; i = i + 1 { + integer := new(Integer) + integer.Init(i) + list.Insert(integer) + } + + r := list.Print() + if r != "9876543210" { + panic(r) + } +} diff --git a/test/ken/rob2.go b/test/ken/rob2.go new file mode 100644 index 0000000..4b4410e --- /dev/null +++ b/test/ken/rob2.go @@ -0,0 +1,280 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test general operation using s-list. +// First Go program ever run (although not in this exact form). + +package main + +import "fmt" + +const nilchar = 0 + +type Atom struct { + str string + integer int + next *Slist /* in hash bucket */ +} + +type List struct { + car *Slist + cdr *Slist +} + +type Slist struct { + isatom bool + isstring bool + //union { + atom Atom + list List + //} u; + +} + +func (this *Slist) Car() *Slist { + return this.list.car +} + +func (this *Slist) Cdr() *Slist { + return this.list.cdr +} + +func (this *Slist) String() string { + return this.atom.str +} + +func (this *Slist) Integer() int { + return this.atom.integer +} + +func (slist *Slist) Free() { + if slist == nil { + return + } + if slist.isatom { + // free(slist.String()); + } else { + slist.Car().Free() + slist.Cdr().Free() + } + // free(slist); +} + +//Slist* atom(byte *s, int i); + +var token int +var peekc int = -1 +var lineno int32 = 1 + +var input string +var inputindex int = 0 +var tokenbuf [100]byte +var tokenlen int = 0 + +const EOF int = -1 + +func main() { + var list *Slist + + OpenFile() + for { + list = Parse() + if list == nil { + break + } + r := list.Print() + list.Free() + if r != "(defn foo (add 12 34))" { + panic(r) + } + break + } +} + +func (slist *Slist) PrintOne(doparen bool) string { + if slist == nil { + return "" + } + var r string + if slist.isatom { + if slist.isstring { + r = slist.String() + } else { + r = fmt.Sprintf("%v", slist.Integer()) + } + } else { + if doparen { + r += "(" + } + r += slist.Car().PrintOne(true) + if slist.Cdr() != nil { + r += " " + r += slist.Cdr().PrintOne(false) + } + if doparen { + r += ")" + } + } + return r +} + +func (slist *Slist) Print() string { + return slist.PrintOne(true) +} + +func Get() int { + var c int + + if peekc >= 0 { + c = peekc + peekc = -1 + } else { + c = int(input[inputindex]) + inputindex++ + if c == '\n' { + lineno = lineno + 1 + } + if c == nilchar { + inputindex = inputindex - 1 + c = EOF + } + } + return c +} + +func WhiteSpace(c int) bool { + return c == ' ' || c == '\t' || c == '\r' || c == '\n' +} + +func NextToken() { + var i, c int + + tokenbuf[0] = nilchar // clear previous token + c = Get() + for WhiteSpace(c) { + c = Get() + } + switch c { + case EOF: + token = EOF + case '(', ')': + token = c + break + default: + for i = 0; i < 100-1; { // sizeof tokenbuf - 1 + tokenbuf[i] = byte(c) + i = i + 1 + c = Get() + if c == EOF { + break + } + if WhiteSpace(c) || c == ')' { + peekc = c + break + } + } + if i >= 100-1 { // sizeof tokenbuf - 1 + panic("atom too long\n") + } + tokenlen = i + tokenbuf[i] = nilchar + if '0' <= tokenbuf[0] && tokenbuf[0] <= '9' { + token = '0' + } else { + token = 'A' + } + } +} + +func Expect(c int) { + if token != c { + print("parse error: expected ", c, "\n") + panic("parse") + } + NextToken() +} + +// Parse a non-parenthesized list up to a closing paren or EOF +func ParseList() *Slist { + var slist, retval *Slist + + slist = new(Slist) + slist.list.car = nil + slist.list.cdr = nil + slist.isatom = false + slist.isstring = false + + retval = slist + for { + slist.list.car = Parse() + if token == ')' || token == EOF { // empty cdr + break + } + slist.list.cdr = new(Slist) + slist = slist.list.cdr + } + return retval +} + +func atom(i int) *Slist { // BUG: uses tokenbuf; should take argument) + var slist *Slist + + slist = new(Slist) + if token == '0' { + slist.atom.integer = i + slist.isstring = false + } else { + slist.atom.str = string(tokenbuf[0:tokenlen]) + slist.isstring = true + } + slist.isatom = true + return slist +} + +func atoi() int { // BUG: uses tokenbuf; should take argument) + var v int = 0 + for i := 0; i < tokenlen && '0' <= tokenbuf[i] && tokenbuf[i] <= '9'; i = i + 1 { + v = 10*v + int(tokenbuf[i]-'0') + } + return v +} + +func Parse() *Slist { + var slist *Slist + + if token == EOF || token == ')' { + return nil + } + if token == '(' { + NextToken() + slist = ParseList() + Expect(')') + return slist + } else { + // Atom + switch token { + case EOF: + return nil + case '0': + slist = atom(atoi()) + case '"', 'A': + slist = atom(0) + default: + slist = nil + print("unknown token: ", token, "\n") + } + NextToken() + return slist + } + return nil +} + +func OpenFile() { + input = "(defn foo (add 12 34))\n\x00" + inputindex = 0 + peekc = -1 // BUG + NextToken() +} diff --git a/test/ken/robfor.go b/test/ken/robfor.go new file mode 100644 index 0000000..c6a420b --- /dev/null +++ b/test/ken/robfor.go @@ -0,0 +1,58 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test for loops of many forms. + +package main + +func assertequal(is, shouldbe int, msg string) { + if is != shouldbe { + print("assertion fail" + msg + "\n"); + panic(1); + } +} + +func main() { + var i, sum int; + + i = 0; + for { + i = i + 1; + if i > 5 { + break; + } + } + assertequal(i, 6, "break"); + + sum = 0; + for i := 0; i <= 10; i++ { + sum = sum + i; + } + assertequal(sum, 55, "all three"); + + sum = 0; + for i := 0; i <= 10; { + sum = sum + i; + i++; + } + assertequal(sum, 55, "only two"); + + sum = 0; + for sum < 100 { + sum = sum + 9; + } + assertequal(sum, 99 + 9, "only one"); + + sum = 0; + for i := 0; i <= 10; i++ { + if i % 2 == 0 { + continue; + } + sum = sum + i; + } + assertequal(sum, 1+3+5+7+9, "continue"); + +} diff --git a/test/ken/robfunc.go b/test/ken/robfunc.go new file mode 100644 index 0000000..885267e --- /dev/null +++ b/test/ken/robfunc.go @@ -0,0 +1,96 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test functions of many signatures. + +package main + +func assertequal(is, shouldbe int, msg string) { + if is != shouldbe { + print("assertion fail" + msg + "\n") + panic(1) + } +} + +func f1() { +} + +func f2(a int) { +} + +func f3(a, b int) int { + return a + b +} + +func f4(a, b int, c float64) int { + return (a+b)/2 + int(c) +} + +func f5(a int) int { + return 5 +} + +func f6(a int) (r int) { + return 6 +} + +func f7(a int) (x int, y float64) { + return 7, 7.0 +} + + +func f8(a int) (x int, y float64) { + return 8, 8.0 +} + +type T struct { + x, y int +} + +func (t *T) m10(a int, b float64) int { + return (t.x + a) * (t.y + int(b)) +} + + +func f9(a int) (in int, fl float64) { + i := 9 + f := float64(9) + return i, f +} + + +func main() { + f1() + f2(1) + r3 := f3(1, 2) + assertequal(r3, 3, "3") + r4 := f4(0, 2, 3.0) + assertequal(r4, 4, "4") + r5 := f5(1) + assertequal(r5, 5, "5") + r6 := f6(1) + assertequal(r6, 6, "6") + var r7 int + var s7 float64 + r7, s7 = f7(1) + assertequal(r7, 7, "r7") + assertequal(int(s7), 7, "s7") + var r8 int + var s8 float64 + r8, s8 = f8(1) + assertequal(r8, 8, "r8") + assertequal(int(s8), 8, "s8") + var r9 int + var s9 float64 + r9, s9 = f9(1) + assertequal(r9, 9, "r9") + assertequal(int(s9), 9, "s9") + var t *T = new(T) + t.x = 1 + t.y = 2 + r10 := t.m10(1, 3.0) + assertequal(r10, 10, "10") +} diff --git a/test/ken/shift.go b/test/ken/shift.go new file mode 100644 index 0000000..af87896 --- /dev/null +++ b/test/ken/shift.go @@ -0,0 +1,121 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test shift. + +package main + +var ians [18]int; +var uans [18]uint; +var pass string; + +func +testi(i int, t1,t2,t3 int) { + n := ((t1*3) + t2)*2 + t3; + if i != ians[n] { + print("itest ", t1,t2,t3,pass, + " is ", i, " sb ", ians[n], "\n"); + } +} + +func +index(t1,t2,t3 int) int { + return ((t1*3) + t2)*2 + t3; +} + +func +testu(u uint, t1,t2,t3 int) { + n := index(t1,t2,t3); + if u != uans[n] { + print("utest ", t1,t2,t3,pass, + " is ", u, " sb ", uans[n], "\n"); + } +} + +func +main() { + var i int; + var u,c uint; + + /* + * test constant evaluations + */ + pass = "con"; // constant part + + testi( int(1234) << 0, 0,0,0); + testi( int(1234) >> 0, 0,0,1); + testi( int(1234) << 5, 0,1,0); + testi( int(1234) >> 5, 0,1,1); + + testi(int(-1234) << 0, 1,0,0); + testi(int(-1234) >> 0, 1,0,1); + testi(int(-1234) << 5, 1,1,0); + testi(int(-1234) >> 5, 1,1,1); + + testu(uint(5678) << 0, 2,0,0); + testu(uint(5678) >> 0, 2,0,1); + testu(uint(5678) << 5, 2,1,0); + testu(uint(5678) >> 5, 2,1,1); + + /* + * test variable evaluations + */ + pass = "var"; // variable part + + for t1:=0; t1<3; t1++ { // +int, -int, uint + for t2:=0; t2<3; t2++ { // 0, +small, +large + for t3:=0; t3<2; t3++ { // <<, >> + switch t1 { + case 0: i = 1234; + case 1: i = -1234; + case 2: u = 5678; + } + switch t2 { + case 0: c = 0; + case 1: c = 5; + case 2: c = 1025; + } + switch t3 { + case 0: i <<= c; u <<= c; + case 1: i >>= c; u >>= c; + } + switch t1 { + case 0: testi(i,t1,t2,t3); + case 1: testi(i,t1,t2,t3); + case 2: testu(u,t1,t2,t3); + } + } + } + } +} + +func +init() { + /* + * set the 'correct' answer + */ + + ians[index(0,0,0)] = 1234; + ians[index(0,0,1)] = 1234; + ians[index(0,1,0)] = 39488; + ians[index(0,1,1)] = 38; + ians[index(0,2,0)] = 0; + ians[index(0,2,1)] = 0; + + ians[index(1,0,0)] = -1234; + ians[index(1,0,1)] = -1234; + ians[index(1,1,0)] = -39488; + ians[index(1,1,1)] = -39; + ians[index(1,2,0)] = 0; + ians[index(1,2,1)] = -1; + + uans[index(2,0,0)] = 5678; + uans[index(2,0,1)] = 5678; + uans[index(2,1,0)] = 181696; + uans[index(2,1,1)] = 177; + uans[index(2,2,0)] = 0; + uans[index(2,2,1)] = 0; +} diff --git a/test/ken/simparray.go b/test/ken/simparray.go new file mode 100644 index 0000000..0e81a34 --- /dev/null +++ b/test/ken/simparray.go @@ -0,0 +1,50 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test simple operations on arrays. + +package main + +var b[10] float32; + +func +main() { + var a[10] float32; + + for i:=int16(5); i<10; i=i+1 { + a[i] = float32(i); + } + + s1 := float32(0); + for i:=5; i<10; i=i+1 { + s1 = s1 + a[i]; + } + + if s1 != 35 { panic(s1); } + + for i:=int16(5); i<10; i=i+1 { + b[i] = float32(i); + } + + s2 := float32(0); + for i:=5; i<10; i=i+1 { + s2 = s2 + b[i]; + } + + if s2 != 35 { panic(s2); } + + b := new([100]int); + for i:=0; i<100; i=i+1 { + b[i] = i; + } + + s3 := 0; + for i:=0; i<100; i=i+1 { + s3 = s3+b[i]; + } + + if s3 != 4950 { panic(s3); } +} diff --git a/test/ken/simpbool.go b/test/ken/simpbool.go new file mode 100644 index 0000000..ab2ecc2 --- /dev/null +++ b/test/ken/simpbool.go @@ -0,0 +1,107 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test basic operations on bool. + +package main + +type s struct { + a bool; + b bool; +} + +func +main() { + var a,b bool; + + a = true; + b = false; + + if !a { panic(1); } + if b { panic(2); } + if !!!a { panic(3); } + if !!b { panic(4); } + + a = !b; + if !a { panic(5); } + if !!!a { panic(6); } + + var x *s; + x = new(s); + x.a = true; + x.b = false; + + if !x.a { panic(7); } + if x.b { panic(8); } + if !!!x.a { panic(9); } + if !!x.b { panic(10); } + + x.a = !x.b; + if !x.a { panic(11); } + if !!!x.a { panic(12); } + + /* + * test && + */ + a = true; + b = true; + if !(a && b) { panic(21); } + if a && !b { panic(22); } + if !a && b { panic(23); } + if !a && !b { panic(24); } + + a = false; + b = true; + if !(!a && b) { panic(31); } + if !a && !b { panic(32); } + if a && b { panic(33); } + if a && !b { panic(34); } + + a = true; + b = false; + if !(a && !b) { panic(41); } + if a && b { panic(41); } + if !a && !b { panic(41); } + if !a && b { panic(44); } + + a = false; + b = false; + if !(!a && !b) { panic(51); } + if !a && b { panic(52); } + if a && !b { panic(53); } + if a && b { panic(54); } + + /* + * test || + */ + a = true; + b = true; + if !(a || b) { panic(61); } + if !(a || !b) { panic(62); } + if !(!a || b) { panic(63); } + if !a || !b { panic(64); } + + a = false; + b = true; + if !(!a || b) { panic(71); } + if !(!a || !b) { panic(72); } + if !(a || b) { panic(73); } + if a || !b { panic(74); } + + a = true; + b = false; + if !(a || !b) { panic(81); } + if !(a || b) { panic(82); } + if !(!a || !b) { panic(83); } + if !a || b { panic(84); } + + a = false; + b = false; + if !(!a || !b) { panic(91); } + if !(!a || b) { panic(92); } + if !(a || !b) { panic(93); } + if a || b { panic(94); } +} diff --git a/test/ken/simpconv.go b/test/ken/simpconv.go new file mode 100644 index 0000000..22cad2a --- /dev/null +++ b/test/ken/simpconv.go @@ -0,0 +1,30 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test simple arithmetic conversion. + +package main + +type vlong int64 +type short int16 + +func main() { + s1 := vlong(0) + for i := short(0); i < 10; i = i + 1 { + s1 = s1 + vlong(i) + } + if s1 != 45 { + panic(s1) + } + + s2 := float64(0) + for i := 0; i < 10; i = i + 1 { + s2 = s2 + float64(i) + } + if s2 != 45 { + panic(s2) + } +} diff --git a/test/ken/simpfun.go b/test/ken/simpfun.go new file mode 100644 index 0000000..e5dc2b2 --- /dev/null +++ b/test/ken/simpfun.go @@ -0,0 +1,26 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test simple functions. + +package main + +func +main() { + var x int; + + x = fun(10,20,30); + if x != 60 { panic(x); } +} + +func +fun(ia,ib,ic int)int { + var o int; + + o = ia+ib+ic; + if o != 60 { panic(o); } + return o; +} diff --git a/test/ken/simpswitch.go b/test/ken/simpswitch.go new file mode 100644 index 0000000..b28250b --- /dev/null +++ b/test/ken/simpswitch.go @@ -0,0 +1,28 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test simple switch. + +package main + +func main() { + r := "" + a := 3 + for i := 0; i < 10; i = i + 1 { + switch i { + case 5: + r += "five" + case a, 7: + r += "a" + default: + r += string(i + '0') + } + r += "out" + string(i+'0') + } + if r != "0out01out12out2aout34out4fiveout56out6aout78out89out9" { + panic(r) + } +} diff --git a/test/ken/simpvar.go b/test/ken/simpvar.go new file mode 100644 index 0000000..c6eefbb --- /dev/null +++ b/test/ken/simpvar.go @@ -0,0 +1,27 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test scoping of variables. + + +package main + +var x,y int; + +func +main() { + + x = 15; + y = 20; + { + var x int; + x = 25; + y = 25; + _ = x; + } + x = x+y; + if(x != 40) { panic(x); } +} diff --git a/test/ken/slicearray.go b/test/ken/slicearray.go new file mode 100644 index 0000000..6cf676c --- /dev/null +++ b/test/ken/slicearray.go @@ -0,0 +1,212 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test basic operations of slices and arrays. + +package main + +var bx [10]byte +var by []byte +var fx [10]float64 +var fy []float64 +var lb, hb int +var t int + +func main() { + lb = 0 + hb = 10 + by = bx[0:] + tstb() + + lb = 0 + hb = 10 + fy = fx[0:] + tstf() + + // width 1 (byte) + lb = 0 + hb = 10 + by = bx[lb:hb] + tstb() + by = bx[lb:10] + tstb() + by = bx[lb:] + tstb() + by = bx[:hb] + tstb() + by = bx[0:hb] + tstb() + by = bx[0:10] + tstb() + by = bx[0:] + tstb() + by = bx[:10] + tstb() + by = bx[:] + tstb() + + lb = 2 + hb = 10 + by = bx[lb:hb] + tstb() + by = bx[lb:10] + tstb() + by = bx[lb:] + tstb() + by = bx[2:hb] + tstb() + by = bx[2:10] + tstb() + by = bx[2:] + tstb() + + lb = 0 + hb = 8 + by = bx[lb:hb] + tstb() + by = bx[lb:8] + tstb() + by = bx[0:hb] + tstb() + by = bx[0:8] + tstb() + by = bx[:8] + tstb() + by = bx[:hb] + tstb() + + lb = 2 + hb = 8 + by = bx[lb:hb] + tstb() + by = bx[lb:8] + tstb() + by = bx[2:hb] + tstb() + by = bx[2:8] + tstb() + + // width 4 (float64) + lb = 0 + hb = 10 + fy = fx[lb:hb] + tstf() + fy = fx[lb:10] + tstf() + fy = fx[lb:] + tstf() + fy = fx[:hb] + tstf() + fy = fx[0:hb] + tstf() + fy = fx[0:10] + tstf() + fy = fx[0:] + tstf() + fy = fx[:10] + tstf() + fy = fx[:] + tstf() + + lb = 2 + hb = 10 + fy = fx[lb:hb] + tstf() + fy = fx[lb:10] + tstf() + fy = fx[lb:] + tstf() + fy = fx[2:hb] + tstf() + fy = fx[2:10] + tstf() + fy = fx[2:] + tstf() + + lb = 0 + hb = 8 + fy = fx[lb:hb] + tstf() + fy = fx[lb:8] + tstf() + fy = fx[:hb] + tstf() + fy = fx[0:hb] + tstf() + fy = fx[0:8] + tstf() + fy = fx[:8] + tstf() + + lb = 2 + hb = 8 + fy = fx[lb:hb] + tstf() + fy = fx[lb:8] + tstf() + fy = fx[2:hb] + tstf() + fy = fx[2:8] + tstf() +} + +func tstb() { + t++ + if len(by) != hb-lb { + println("t=", t, "lb=", lb, "hb=", hb, + "len=", len(by), "hb-lb=", hb-lb) + panic("fail") + } + if cap(by) != len(bx)-lb { + println("t=", t, "lb=", lb, "hb=", hb, + "cap=", cap(by), "len(bx)-lb=", len(bx)-lb) + panic("fail") + } + for i := lb; i < hb; i++ { + if bx[i] != by[i-lb] { + println("t=", t, "lb=", lb, "hb=", hb, + "bx[", i, "]=", bx[i], + "by[", i-lb, "]=", by[i-lb]) + panic("fail") + } + } + by = nil +} + +func tstf() { + t++ + if len(fy) != hb-lb { + println("t=", t, "lb=", lb, "hb=", hb, + "len=", len(fy), "hb-lb=", hb-lb) + panic("fail") + } + if cap(fy) != len(fx)-lb { + println("t=", t, "lb=", lb, "hb=", hb, + "cap=", cap(fy), "len(fx)-lb=", len(fx)-lb) + panic("fail") + } + for i := lb; i < hb; i++ { + if fx[i] != fy[i-lb] { + println("t=", t, "lb=", lb, "hb=", hb, + "fx[", i, "]=", fx[i], + "fy[", i-lb, "]=", fy[i-lb]) + panic("fail") + } + } + fy = nil +} + +func init() { + for i := 0; i < len(bx); i++ { + bx[i] = byte(i + 20) + } + by = nil + + for i := 0; i < len(fx); i++ { + fx[i] = float64(i + 20) + } + fy = nil +} diff --git a/test/ken/sliceslice.go b/test/ken/sliceslice.go new file mode 100644 index 0000000..c07c591 --- /dev/null +++ b/test/ken/sliceslice.go @@ -0,0 +1,205 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test slicing and re-slicing. + +package main + +var bx []byte +var by []byte +var fx []float64 +var fy []float64 +var lb, hb int +var t int + +func main() { + + // width 1 (byte) + lb = 0 + hb = 10 + by = bx[lb:hb] + tstb() + by = bx[lb:10] + tstb() + by = bx[lb:] + tstb() + by = bx[:hb] + tstb() + by = bx[0:hb] + tstb() + by = bx[0:10] + tstb() + by = bx[0:] + tstb() + by = bx[:10] + tstb() + by = bx[:] + tstb() + + lb = 2 + hb = 10 + by = bx[lb:hb] + tstb() + by = bx[lb:10] + tstb() + by = bx[lb:] + tstb() + by = bx[2:hb] + tstb() + by = bx[2:10] + tstb() + by = bx[2:] + tstb() + + lb = 0 + hb = 8 + by = bx[lb:hb] + tstb() + by = bx[lb:8] + tstb() + by = bx[0:hb] + tstb() + by = bx[0:8] + tstb() + by = bx[:8] + tstb() + by = bx[:hb] + tstb() + + lb = 2 + hb = 8 + by = bx[lb:hb] + tstb() + by = bx[lb:8] + tstb() + by = bx[2:hb] + tstb() + by = bx[2:8] + tstb() + + // width 4 (float64) + lb = 0 + hb = 10 + fy = fx[lb:hb] + tstf() + fy = fx[lb:10] + tstf() + fy = fx[lb:] + tstf() + fy = fx[:hb] + tstf() + fy = fx[0:hb] + tstf() + fy = fx[0:10] + tstf() + fy = fx[0:] + tstf() + fy = fx[:10] + tstf() + fy = fx[:] + tstf() + + lb = 2 + hb = 10 + fy = fx[lb:hb] + tstf() + fy = fx[lb:10] + tstf() + fy = fx[lb:] + tstf() + fy = fx[2:hb] + tstf() + fy = fx[2:10] + tstf() + fy = fx[2:] + tstf() + + lb = 0 + hb = 8 + fy = fx[lb:hb] + tstf() + fy = fx[lb:8] + tstf() + fy = fx[:hb] + tstf() + fy = fx[0:hb] + tstf() + fy = fx[0:8] + tstf() + fy = fx[:8] + tstf() + + lb = 2 + hb = 8 + fy = fx[lb:hb] + tstf() + fy = fx[lb:8] + tstf() + fy = fx[2:hb] + tstf() + fy = fx[2:8] + tstf() +} + +func tstb() { + t++ + if len(by) != hb-lb { + println("t=", t, "lb=", lb, "hb=", hb, + "len=", len(by), "hb-lb=", hb-lb) + panic("fail") + } + if cap(by) != len(bx)-lb { + println("t=", t, "lb=", lb, "hb=", hb, + "cap=", cap(by), "len(bx)-lb=", len(bx)-lb) + panic("fail") + } + for i := lb; i < hb; i++ { + if bx[i] != by[i-lb] { + println("t=", t, "lb=", lb, "hb=", hb, + "bx[", i, "]=", bx[i], + "by[", i-lb, "]=", by[i-lb]) + panic("fail") + } + } + by = nil +} + +func tstf() { + t++ + if len(fy) != hb-lb { + println("t=", t, "lb=", lb, "hb=", hb, + "len=", len(fy), "hb-lb=", hb-lb) + panic("fail") + } + if cap(fy) != len(fx)-lb { + println("t=", t, "lb=", lb, "hb=", hb, + "cap=", cap(fy), "len(fx)-lb=", len(fx)-lb) + panic("fail") + } + for i := lb; i < hb; i++ { + if fx[i] != fy[i-lb] { + println("t=", t, "lb=", lb, "hb=", hb, + "fx[", i, "]=", fx[i], + "fy[", i-lb, "]=", fy[i-lb]) + panic("fail") + } + } + fy = nil +} + +func init() { + bx = make([]byte, 10) + for i := 0; i < len(bx); i++ { + bx[i] = byte(i + 20) + } + by = nil + + fx = make([]float64, 10) + for i := 0; i < len(fx); i++ { + fx[i] = float64(i + 20) + } + fy = nil +} diff --git a/test/ken/string.go b/test/ken/string.go new file mode 100644 index 0000000..7bb3cab --- /dev/null +++ b/test/ken/string.go @@ -0,0 +1,114 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test string operations including printing. + +package main + +func main() { + var c string + + a := `abc` + b := `xyz` + + /* print a literal */ + print(`abc`) + + /* print a variable */ + print(b, "-") + + /* catenate literals */ + print(`abc`+`xyz`, "-") + + /* catenate variables */ + print(a+b, "-") + + /* compare literals */ + if `abc` == `xyz` || `abc` != "abc" || `abc` > `xyz` { + panic("compare literals") + } + + /* compare variables */ + if a == b || a != a || a > b { + panic("compare variables") + } + + /* cat */ + c = a + b + print(c, "-") + + /* catequal */ + c = a + c += b + print(c, "-") + + /* clumsy evaluation */ + c = b + c = a + c + print(c, "-") + + /* len */ + if len(c) != 6 { + print("len ", len(c)) + panic("fail") + } + + /* index strings */ + for i := 0; i < len(c); i = i + 1 { + if c[i] != (a + b)[i] { + print("index ", i, " ", c[i], " ", (a + b)[i]) + panic("fail") + } + } + + /* slice strings */ + print(c[0:3], c[3:]) + + print("\n") + + /* create string with integer constant */ + c = string('x') + if c != "x" { + panic("create int " + c) + } + + /* create string with integer variable */ + v := 'x' + c = string(v) + if c != "x" { + panic("create int " + c) + } + + /* create string with byte array */ + var z1 [3]byte + z1[0] = 'a' + z1[1] = 'b' + z1[2] = 'c' + c = string(z1[0:]) + if c != "abc" { + panic("create byte array " + c) + } + + /* create string with int array */ + var z2 [3]rune + z2[0] = 'a' + z2[1] = '\u1234' + z2[2] = 'c' + c = string(z2[0:]) + if c != "a\u1234c" { + panic("create int array " + c) + } + + /* create string with byte array pointer */ + z3 := new([3]byte) + z3[0] = 'a' + z3[1] = 'b' + z3[2] = 'c' + c = string(z3[0:]) + if c != "abc" { + panic("create array pointer " + c) + } +} diff --git a/test/ken/string.out b/test/ken/string.out new file mode 100644 index 0000000..8bc36bc --- /dev/null +++ b/test/ken/string.out @@ -0,0 +1 @@ +abcxyz-abcxyz-abcxyz-abcxyz-abcxyz-abcxyz-abcxyz diff --git a/test/ken/strvar.go b/test/ken/strvar.go new file mode 100644 index 0000000..4d511fe --- /dev/null +++ b/test/ken/strvar.go @@ -0,0 +1,79 @@ +// run + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Test struct-valued variables (not pointers). + +package main + +type x2 struct { a,b,c int; d int; }; +var g1 x2; +var g2 struct { a,b,c int; d x2; }; + +func +main() { + var x int; + var s1 *x2; + var s2 *struct { a,b,c int; d x2; }; + var s3 struct { a,b,c int; d x2; }; + + s1 = &g1; + s2 = &g2; + + s1.a = 1; + s1.b = 2; + s1.c = 3; + s1.d = 5; + + if(s1.c != 3) { panic(s1.c); } + if(g1.c != 3) { panic(g1.c); } + + s2.a = 7; + s2.b = 11; + s2.c = 13; + s2.d.a = 17; + s2.d.b = 19; + s2.d.c = 23; + s2.d.d = 29; + + if(s2.d.c != 23) { panic(s2.d.c); } + if(g2.d.c != 23) { panic(g2.d.c); } + + x = s1.a + + s1.b + + s1.c + + s1.d + + + s2.a + + s2.b + + s2.c + + s2.d.a + + s2.d.b + + s2.d.c + + s2.d.d; + + if(x != 130) { panic(x); } + + // test an automatic struct + s3.a = 7; + s3.b = 11; + s3.c = 13; + s3.d.a = 17; + s3.d.b = 19; + s3.d.c = 23; + s3.d.d = 29; + + if(s3.d.c != 23) { panic(s3.d.c); } + + x = s3.a + + s3.b + + s3.c + + s3.d.a + + s3.d.b + + s3.d.c + + s3.d.d; + + if(x != 119) { panic(x); } +} |