1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 class Main {
main(String[] args)18   public static void main(String[] args) {
19     testIntShiftRight();
20     testIntShiftLeft();
21     testIntUnsignedShiftRight();
22     testLongShiftRight();
23     testLongShiftLeft();
24     testLongUnsignedShiftRight();
25   }
26 
testIntShiftLeft()27   public static void testIntShiftLeft() {
28     int a = myField;
29     int b = myOtherField << a;
30     if (b != -2147483648) {
31       throw new Error("Expected -2147483648, got " + b);
32     }
33     if (a != 0xFFF) {
34       throw new Error("Expected 0xFFF, got " + a);
35     }
36   }
37 
testIntShiftRight()38   public static void testIntShiftRight() {
39     int a = myField;
40     int b = myOtherField >> a;
41     if (b != 0) {
42       throw new Error("Expected 0, got " + b);
43     }
44     if (a != 0xFFF) {
45       throw new Error("Expected 0xFFF, got " + a);
46     }
47   }
48 
testIntUnsignedShiftRight()49   public static void testIntUnsignedShiftRight() {
50     int a = myField;
51     int b = myOtherField >>> a;
52     if (b != 0) {
53       throw new Error("Expected 0, got " + b);
54     }
55     if (a != 0xFFF) {
56       throw new Error("Expected 0xFFF, got " + a);
57     }
58   }
59 
testLongShiftLeft()60   public static void testLongShiftLeft() {
61     long a = myLongField;
62     long b = myOtherLongField << a;
63     if (b != 0x2468ACF13579BDEL) {
64       throw new Error("Expected 0x2468ACF13579BDEL, got " + b);
65     }
66     // The int conversion will be GVN'ed with the one required
67     // by Java specification of long shift left.
68     if ((int)a != 0x41) {
69       throw new Error("Expected 0x41, got " + a);
70     }
71   }
72 
testLongShiftRight()73   public static void testLongShiftRight() {
74     long a = myLongField;
75     long b = myOtherLongField >> a;
76     if (b != 0x91A2B3C4D5E6F7L) {
77       throw new Error("Expected 0x91A2B3C4D5E6F7L, got " + b);
78     }
79     // The int conversion will be GVN'ed with the one required
80     // by Java specification of long shift right.
81     if ((int)a != 0x41) {
82       throw new Error("Expected 0x41, got " + a);
83     }
84   }
85 
testLongUnsignedShiftRight()86   public static void testLongUnsignedShiftRight() {
87     long a = myLongField;
88     long b = myOtherLongField >>> a;
89     if (b != 0x91A2B3C4D5E6F7L) {
90       throw new Error("Expected 0x91A2B3C4D5E6F7L, got " + b);
91     }
92     // The int conversion will be GVN'ed with the one required
93     // by Java specification of long shift right.
94     if ((int)a != 0x41) {
95       throw new Error("Expected 0x41, got " + a);
96     }
97   }
98 
99   static int myField = 0xFFF;
100   static int myOtherField = 0x1;
101 
102   // Use a value that will need to be masked before doing the shift.
103   // The maximum shift is 0x3F.
104   static long myLongField = 0x41;
105   static long myOtherLongField = 0x123456789abcdefL;
106 }
107