Russ Cox | d2cc988 | 2012-02-16 23:50:37 -0500 | [diff] [blame] | 1 | // run |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 2 | |
| 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 Pike | eb37b5b | 2012-02-24 16:24:24 +1100 | [diff] [blame] | 7 | // Test shift. |
| 8 | |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 9 | package main |
| 10 | |
| 11 | var ians [18]int; |
| 12 | var uans [18]uint; |
| 13 | var pass string; |
| 14 | |
| 15 | func |
Robert Griesemer | 581530e | 2009-12-10 12:53:23 -0800 | [diff] [blame] | 16 | testi(i int, t1,t2,t3 int) { |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 17 | 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 | |
| 24 | func |
Robert Griesemer | 581530e | 2009-12-10 12:53:23 -0800 | [diff] [blame] | 25 | index(t1,t2,t3 int) int { |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 26 | return ((t1*3) + t2)*2 + t3; |
| 27 | } |
| 28 | |
| 29 | func |
Robert Griesemer | 581530e | 2009-12-10 12:53:23 -0800 | [diff] [blame] | 30 | testu(u uint, t1,t2,t3 int) { |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 31 | 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 | |
| 38 | func |
Robert Griesemer | 581530e | 2009-12-10 12:53:23 -0800 | [diff] [blame] | 39 | main() { |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 40 | 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 Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 52 | |
| 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 Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 57 | |
| 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 Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 62 | |
| 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 | |
| 95 | func |
Robert Griesemer | 581530e | 2009-12-10 12:53:23 -0800 | [diff] [blame] | 96 | init() { |
Ken Thompson | 944ad62 | 2008-08-29 13:24:53 -0700 | [diff] [blame] | 97 | /* |
| 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 | } |