summaryrefslogtreecommitdiff
path: root/test/640-checker-integer-valueof/src/Main.java
blob: 0837fd18ee9b0dc832e45ddbdb42cb026b33fe07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {

  /// CHECK-START: java.lang.Integer Main.foo(int) disassembly (after)
  /// CHECK: <<Integer:l\d+>>     InvokeStaticOrDirect method_name:java.lang.Integer.valueOf intrinsic:IntegerValueOf
  /// CHECK:                      pAllocObjectInitialized
  /// CHECK:                      Return [<<Integer>>]
  public static Integer foo(int a) {
    return Integer.valueOf(a);
  }

  /// CHECK-START: java.lang.Integer Main.foo2() disassembly (after)
  /// CHECK: <<Integer:l\d+>>     InvokeStaticOrDirect method_name:java.lang.Integer.valueOf intrinsic:IntegerValueOf
  /// CHECK-NOT:                  pAllocObjectInitialized
  /// CHECK:                      Return [<<Integer>>]
  public static Integer foo2() {
    return Integer.valueOf(-42);
  }

  /// CHECK-START: java.lang.Integer Main.foo3() disassembly (after)
  /// CHECK: <<Integer:l\d+>>     InvokeStaticOrDirect method_name:java.lang.Integer.valueOf intrinsic:IntegerValueOf
  /// CHECK-NOT:                  pAllocObjectInitialized
  /// CHECK:                      Return [<<Integer>>]
  public static Integer foo3() {
    return Integer.valueOf(42);
  }

  /// CHECK-START: java.lang.Integer Main.foo4() disassembly (after)
  /// CHECK: <<Integer:l\d+>>     InvokeStaticOrDirect method_name:java.lang.Integer.valueOf intrinsic:IntegerValueOf
  /// CHECK:                      pAllocObjectInitialized
  /// CHECK:                      Return [<<Integer>>]
  public static Integer foo4() {
    return Integer.valueOf(55555);
  }

  public static void main(String[] args) {
    assertEqual("42", foo(intField));
    assertEqual(foo(intField), foo(intField2));
    assertEqual("-42", foo2());
    assertEqual("42", foo3());
    assertEqual("55555", foo4());
    assertEqual("55555", foo(intField3));
    assertEqual("-129", foo(intFieldMinus129));
    assertEqual("-128", foo(intFieldMinus128));
    assertEqual(foo(intFieldMinus128), foo(intFieldMinus128));
    assertEqual("-127", foo(intFieldMinus127));
    assertEqual(foo(intFieldMinus127), foo(intFieldMinus127));
    assertEqual("126", foo(intField126));
    assertEqual(foo(intField126), foo(intField126));
    assertEqual("127", foo(intField127));
    assertEqual(foo(intField127), foo(intField127));
    assertEqual("128", foo(intField128));
  }

  static void assertEqual(String a, Integer b) {
    if (!a.equals(b.toString())) {
      throw new Error("Expected " + a + ", got " + b);
    }
  }

  static void assertEqual(Integer a, Integer b) {
    if (a != b) {
      throw new Error("Expected " + a + ", got " + b);
    }
  }

  static int intField = 42;
  static int intField2 = 42;
  static int intField3 = 55555;

  // Edge cases.
  static int intFieldMinus129 = -129;
  static int intFieldMinus128 = -128;
  static int intFieldMinus127 = -127;
  static int intField126 = 126;
  static int intField127 = 127;
  static int intField128 = 128;
}