1 /*
2  * Copyright (C) 2018 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 import java.lang.reflect.Method;
18 
19 public class Main {
20   static long smallLong = 42L;
21   static long smallLongShlOne = 84L;
22   static long smallLongShrOne = 21L;
23   static long smallLongUShrOne = 21L;
24   static long longLong = 123456789123456789L;
25   static long longLongShlOne = 246913578246913578L;
26   static long longLongShrOne = 61728394561728394L;
27   static long longLongUShrOne = 61728394561728394L;
28 
29   static long negativeSmallLong = -42L;
30   static long negativeSmallLongShlOne = -84L;
31   static long negativeSmallLongShrOne = -21L;
32   static long negativeSmallLongUShrOne = 9223372036854775787L;
33   static long negativeLongLong = -123456789123456789L;
34   static long negativeLongLongShlOne = -246913578246913578L;
35   static long negativeLongLongShrOne = -61728394561728395L;
36   static long negativeLongLongUShrOne = 9161643642293047413L;
37 
assertEquals(long expected, long actual)38   private static void assertEquals(long expected, long actual) {
39     if (expected != actual) {
40       throw new Error("Expected " + expected + ", got " + actual);
41     }
42   }
43 
main(String[] args)44   public static void main(String[] args) throws Exception {
45     Class<?> c = Class.forName("Test");
46     Method m = c.getMethod("shlZero", long.class);
47     assertEquals(smallLong, (Long)m.invoke(null, smallLong));
48     assertEquals(longLong, (Long)m.invoke(null, longLong));
49 
50     m = c.getMethod("shrZero", long.class);
51     assertEquals(smallLong, (Long)m.invoke(null, smallLong));
52     assertEquals(longLong, (Long)m.invoke(null, longLong));
53 
54     m = c.getMethod("ushrZero", long.class);
55     assertEquals(smallLong, (Long)m.invoke(null, smallLong));
56     assertEquals(longLong, (Long)m.invoke(null, longLong));
57 
58     m = c.getMethod("shlOne", long.class);
59     assertEquals(smallLongShlOne, (Long)m.invoke(null, smallLong));
60     assertEquals(longLongShlOne, (Long)m.invoke(null, longLong));
61 
62     m = c.getMethod("shrOne", long.class);
63     assertEquals(smallLongShrOne, (Long)m.invoke(null, smallLong));
64     assertEquals(longLongShrOne, (Long)m.invoke(null, longLong));
65 
66     m = c.getMethod("ushrOne", long.class);
67     assertEquals(smallLongUShrOne, (Long)m.invoke(null, smallLong));
68     assertEquals(longLongUShrOne, (Long)m.invoke(null, longLong));
69 
70     // Test with negative numbers.
71 
72     m = c.getMethod("shlZero", long.class);
73     assertEquals(negativeSmallLong, (Long)m.invoke(null, negativeSmallLong));
74     assertEquals(negativeLongLong, (Long)m.invoke(null, negativeLongLong));
75 
76     m = c.getMethod("shrZero", long.class);
77     assertEquals(negativeSmallLong, (Long)m.invoke(null, negativeSmallLong));
78     assertEquals(negativeLongLong, (Long)m.invoke(null, negativeLongLong));
79 
80     m = c.getMethod("ushrZero", long.class);
81     assertEquals(negativeSmallLong, (Long)m.invoke(null, negativeSmallLong));
82     assertEquals(negativeLongLong, (Long)m.invoke(null, negativeLongLong));
83 
84     m = c.getMethod("shlOne", long.class);
85     assertEquals(negativeSmallLongShlOne, (Long)m.invoke(null, negativeSmallLong));
86     assertEquals(negativeLongLongShlOne, (Long)m.invoke(null, negativeLongLong));
87 
88     m = c.getMethod("shrOne", long.class);
89     assertEquals(negativeSmallLongShrOne, (Long)m.invoke(null, negativeSmallLong));
90     assertEquals(negativeLongLongShrOne, (Long)m.invoke(null, negativeLongLong));
91 
92     m = c.getMethod("ushrOne", long.class);
93     assertEquals(negativeSmallLongUShrOne, (Long)m.invoke(null, negativeSmallLong));
94     assertEquals(negativeLongLongUShrOne, (Long)m.invoke(null, negativeLongLong));
95   }
96 }
97