blob: af8789615ee5ddc2197c7e845aad16631c88c3f3 [file] [log] [blame]
Russ Coxd2cc9882012-02-16 23:50:37 -05001// run
Ken Thompson944ad622008-08-29 13:24:53 -07002
3// Copyright 2009 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
Rob Pikeeb37b5b2012-02-24 16:24:24 +11007// Test shift.
8
Ken Thompson944ad622008-08-29 13:24:53 -07009package main
10
11var ians [18]int;
12var uans [18]uint;
13var pass string;
14
15func
Robert Griesemer581530e2009-12-10 12:53:23 -080016testi(i int, t1,t2,t3 int) {
Ken Thompson944ad622008-08-29 13:24:53 -070017 n := ((t1*3) + t2)*2 + t3;
18 if i != ians[n] {
19 print("itest ", t1,t2,t3,pass,
20 " is ", i, " sb ", ians[n], "\n");
21 }
22}
23
24func
Robert Griesemer581530e2009-12-10 12:53:23 -080025index(t1,t2,t3 int) int {
Ken Thompson944ad622008-08-29 13:24:53 -070026 return ((t1*3) + t2)*2 + t3;
27}
28
29func
Robert Griesemer581530e2009-12-10 12:53:23 -080030testu(u uint, t1,t2,t3 int) {
Ken Thompson944ad622008-08-29 13:24:53 -070031 n := index(t1,t2,t3);
32 if u != uans[n] {
33 print("utest ", t1,t2,t3,pass,
34 " is ", u, " sb ", uans[n], "\n");
35 }
36}
37
38func
Robert Griesemer581530e2009-12-10 12:53:23 -080039main() {
Ken Thompson944ad622008-08-29 13:24:53 -070040 var i int;
41 var u,c uint;
42
43 /*
44 * test constant evaluations
45 */
46 pass = "con"; // constant part
47
48 testi( int(1234) << 0, 0,0,0);
49 testi( int(1234) >> 0, 0,0,1);
50 testi( int(1234) << 5, 0,1,0);
51 testi( int(1234) >> 5, 0,1,1);
Ken Thompson944ad622008-08-29 13:24:53 -070052
53 testi(int(-1234) << 0, 1,0,0);
54 testi(int(-1234) >> 0, 1,0,1);
55 testi(int(-1234) << 5, 1,1,0);
56 testi(int(-1234) >> 5, 1,1,1);
Ken Thompson944ad622008-08-29 13:24:53 -070057
58 testu(uint(5678) << 0, 2,0,0);
59 testu(uint(5678) >> 0, 2,0,1);
60 testu(uint(5678) << 5, 2,1,0);
61 testu(uint(5678) >> 5, 2,1,1);
Ken Thompson944ad622008-08-29 13:24:53 -070062
63 /*
64 * test variable evaluations
65 */
66 pass = "var"; // variable part
67
68 for t1:=0; t1<3; t1++ { // +int, -int, uint
69 for t2:=0; t2<3; t2++ { // 0, +small, +large
70 for t3:=0; t3<2; t3++ { // <<, >>
71 switch t1 {
72 case 0: i = 1234;
73 case 1: i = -1234;
74 case 2: u = 5678;
75 }
76 switch t2 {
77 case 0: c = 0;
78 case 1: c = 5;
79 case 2: c = 1025;
80 }
81 switch t3 {
82 case 0: i <<= c; u <<= c;
83 case 1: i >>= c; u >>= c;
84 }
85 switch t1 {
86 case 0: testi(i,t1,t2,t3);
87 case 1: testi(i,t1,t2,t3);
88 case 2: testu(u,t1,t2,t3);
89 }
90 }
91 }
92 }
93}
94
95func
Robert Griesemer581530e2009-12-10 12:53:23 -080096init() {
Ken Thompson944ad622008-08-29 13:24:53 -070097 /*
98 * set the 'correct' answer
99 */
100
101 ians[index(0,0,0)] = 1234;
102 ians[index(0,0,1)] = 1234;
103 ians[index(0,1,0)] = 39488;
104 ians[index(0,1,1)] = 38;
105 ians[index(0,2,0)] = 0;
106 ians[index(0,2,1)] = 0;
107
108 ians[index(1,0,0)] = -1234;
109 ians[index(1,0,1)] = -1234;
110 ians[index(1,1,0)] = -39488;
111 ians[index(1,1,1)] = -39;
112 ians[index(1,2,0)] = 0;
113 ians[index(1,2,1)] = -1;
114
115 uans[index(2,0,0)] = 5678;
116 uans[index(2,0,1)] = 5678;
117 uans[index(2,1,0)] = 181696;
118 uans[index(2,1,1)] = 177;
119 uans[index(2,2,0)] = 0;
120 uans[index(2,2,1)] = 0;
121}