summaryrefslogtreecommitdiff
path: root/test/ken
diff options
context:
space:
mode:
authorKen Thompson <ken@golang.org>2008-08-29 13:24:53 -0700
committerKen Thompson <ken@golang.org>2008-08-29 13:24:53 -0700
commit2a47b8de5a7f118db015b9b621dc3e8eb5f78d9f (patch)
tree04aa70c53d846411240c1d6ba0022d85a7cc7c21 /test/ken
parent60aa317be6902c6f10599d159c1f6fb9b9d0c817 (diff)
downloadgo-2a47b8de5a7f118db015b9b621dc3e8eb5f78d9f.tar.gz
fix type of (1<<x)
R=r OCL=14656 CL=14656
Diffstat (limited to 'test/ken')
-rw-r--r--test/ken/array.go156
-rw-r--r--test/ken/shift.go131
2 files changed, 287 insertions, 0 deletions
diff --git a/test/ken/array.go b/test/ken/array.go
new file mode 100644
index 000000000..bd17f645c
--- /dev/null
+++ b/test/ken/array.go
@@ -0,0 +1,156 @@
+// $G $D/$F.go && $L $F.$A && ./$A.out
+
+// 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.
+
+
+package main
+
+export 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;
+ }
+}
+
+export 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;
+}
+
+export 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;
+ }
+}
+
+export 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 := new([]int, 10, 100);
+ if len(a) != 10 && cap(a) != 100 {
+ panic("len and cap from new: ", len(a), " ", cap(a), "\n");
+ }
+
+ a = a[0:100];
+ setpd(a);
+
+ a = a[0:10];
+ res(sumpd(a), 0, 10);
+
+ a = a[5:25];
+ res(sumpd(a), 5, 25);
+}
+
+// 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);
+ res(sumpd(a), 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);
+ res(sumpd(&a), 0, 80);
+}
+
+// generate bounds error with ptr dynamic
+func
+testpdfault()
+{
+ a := new([]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");
+ a[80] = 0;
+ print("bad\n");
+}
+
+func
+main()
+{
+ print("testpdpd\n"); testpdpd();
+ print("testpfpf\n"); testpfpf();
+ print("testpdpf1\n"); testpdpf1();
+ print("testpdpf2\n"); testpdpf2();
+// print("testpdfault\n"); testpdfault();
+// print("testfdfault\n"); testfdfault();
+}
diff --git a/test/ken/shift.go b/test/ken/shift.go
new file mode 100644
index 000000000..180abbf3f
--- /dev/null
+++ b/test/ken/shift.go
@@ -0,0 +1,131 @@
+// $G $D/$F.go && $L $F.$A && ./$A.out
+
+// 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.
+
+
+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) << 1025, 0,2,0);
+ testi( int(1234) >> 1025, 0,2,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);
+ testi(int(-1234) << 1025, 1,2,0);
+ testi(int(-1234) >> 1025, 1,2,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);
+ testu(uint(5678) << 1025, 2,2,0);
+ testu(uint(5678) >> 1025, 2,2,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;
+}