1 /*
 2  * Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 //
26 // Help test archived box cache consistency.
27 //
28 // args[0]: the expected maximum value expected to be archived
29 //
30 
31 import jdk.internal.misc.PreviewFeatures;
32 
33 public class CheckIntegerCacheApp {
34     public static void main(String[] args) throws Exception {
35         if (args.length != 1) {
36             throw new RuntimeException(
37                     "FAILED. Incorrect argument length: " + args.length);
38         }
39 
40         // Base JLS compliance check
41         for (int i = -128; i <= 127; i++) {
42             if (Integer.valueOf(i) != Integer.valueOf(i)) {
43                 throw new RuntimeException(
44                         "FAILED. All values in range [-128, 127] should be interned in cache: " + i);
45             }
46             if (Byte.valueOf((byte)i) != Byte.valueOf((byte)i)) {
47                 throw new RuntimeException(
48                         "FAILED. All Byte values in range [-128, 127] should be interned in cache: " + (byte)i);
49             }
50             if (Short.valueOf((short)i) != Short.valueOf((short)i)) {
51                 throw new RuntimeException(
52                         "FAILED. All Short values in range [-128, 127] should be interned in cache: " + (byte)i);
53             }
54             if (Long.valueOf(i) != Long.valueOf(i)) {
55                 throw new RuntimeException(
56                         "FAILED. All Long values in range [-128, 127] should be interned in cache: " + i);
57             }
58 
59             // Character cache only values 0 through 127
60             if (i >= 0) {
61                 if (Character.valueOf((char)i) != Character.valueOf((char)i)) {
62                     throw new RuntimeException(
63                             "FAILED. All Character values in range [0, 127] should be interned in cache: " + i);
64                 }
65             }
66         }
67 
68         // Check that archived integer cache agrees with runtime integer cache.
69         for (int i = -128; i <= 127; i++) {
70             if (ArchivedIntegerHolder.archivedObjects[i + 128] != Integer.valueOf(i)) {
71                 throw new RuntimeException(
72                         "FAILED. Archived and runtime caches disagree for " + i);
73             }
74         }
75 
76         int high = Integer.parseInt(args[0]);
77         if (Integer.valueOf(high) != Integer.valueOf(high)) {
78             throw new RuntimeException(
79                     "FAILED. Value expected to be retrieved from cache: " + high);
80         }
81 
82         // In preview mode the Integer is a value class and the Integer cache is disabled
83         if (!PreviewFeatures.isEnabled()) {
84             if (Integer.valueOf(high + 1) == Integer.valueOf(high + 1)) {
85                 throw new RuntimeException(
86                         "FAILED. Value not expected to be retrieved from cache: " + high);
87             }
88         } else {
89             if (Integer.valueOf(high + 1) != Integer.valueOf(high + 1)) {
90                 throw new RuntimeException(
91                         "FAILED. Values must be equal in preview mode: " + high);
92             }
93         }
94     }
95 }