Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2017 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 public class Main {
     18 
     19   /// CHECK-START: java.lang.Integer Main.foo(int) disassembly (after)
     20   /// CHECK: <<Integer:l\d+>>     InvokeStaticOrDirect method_name:java.lang.Integer.valueOf intrinsic:IntegerValueOf
     21   /// CHECK:                      pAllocObjectInitialized
     22   /// CHECK:                      Return [<<Integer>>]
     23   public static Integer foo(int a) {
     24     return Integer.valueOf(a);
     25   }
     26 
     27   /// CHECK-START: java.lang.Integer Main.foo2() disassembly (after)
     28   /// CHECK: <<Integer:l\d+>>     InvokeStaticOrDirect method_name:java.lang.Integer.valueOf intrinsic:IntegerValueOf
     29   /// CHECK-NOT:                  pAllocObjectInitialized
     30   /// CHECK:                      Return [<<Integer>>]
     31   public static Integer foo2() {
     32     return Integer.valueOf(-42);
     33   }
     34 
     35   /// CHECK-START: java.lang.Integer Main.foo3() disassembly (after)
     36   /// CHECK: <<Integer:l\d+>>     InvokeStaticOrDirect method_name:java.lang.Integer.valueOf intrinsic:IntegerValueOf
     37   /// CHECK-NOT:                  pAllocObjectInitialized
     38   /// CHECK:                      Return [<<Integer>>]
     39   public static Integer foo3() {
     40     return Integer.valueOf(42);
     41   }
     42 
     43   /// CHECK-START: java.lang.Integer Main.foo4() disassembly (after)
     44   /// CHECK: <<Integer:l\d+>>     InvokeStaticOrDirect method_name:java.lang.Integer.valueOf intrinsic:IntegerValueOf
     45   /// CHECK:                      pAllocObjectInitialized
     46   /// CHECK:                      Return [<<Integer>>]
     47   public static Integer foo4() {
     48     return Integer.valueOf(55555);
     49   }
     50 
     51   public static void main(String[] args) {
     52     assertEqual("42", foo(intField));
     53     assertEqual(foo(intField), foo(intField2));
     54     assertEqual("-42", foo2());
     55     assertEqual("42", foo3());
     56     assertEqual("55555", foo4());
     57     assertEqual("55555", foo(intField3));
     58     assertEqual("-129", foo(intFieldMinus129));
     59     assertEqual("-128", foo(intFieldMinus128));
     60     assertEqual(foo(intFieldMinus128), foo(intFieldMinus128));
     61     assertEqual("-127", foo(intFieldMinus127));
     62     assertEqual(foo(intFieldMinus127), foo(intFieldMinus127));
     63     assertEqual("126", foo(intField126));
     64     assertEqual(foo(intField126), foo(intField126));
     65     assertEqual("127", foo(intField127));
     66     assertEqual(foo(intField127), foo(intField127));
     67     assertEqual("128", foo(intField128));
     68   }
     69 
     70   static void assertEqual(String a, Integer b) {
     71     if (!a.equals(b.toString())) {
     72       throw new Error("Expected " + a + ", got " + b);
     73     }
     74   }
     75 
     76   static void assertEqual(Integer a, Integer b) {
     77     if (a != b) {
     78       throw new Error("Expected " + a + ", got " + b);
     79     }
     80   }
     81 
     82   static int intField = 42;
     83   static int intField2 = 42;
     84   static int intField3 = 55555;
     85 
     86   // Edge cases.
     87   static int intFieldMinus129 = -129;
     88   static int intFieldMinus128 = -128;
     89   static int intFieldMinus127 = -127;
     90   static int intField126 = 126;
     91   static int intField127 = 127;
     92   static int intField128 = 128;
     93 }
     94