1 /*
 2  * Copyright (c) 2018, 2024, 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 /*
32  * test
33  * @run main CheckIntegerCacheApp
34  * @run main/othervm --enable-preview CheckIntegerCacheApp
35  */
36 public class CheckIntegerCacheApp {
37     public static void main(String[] args) throws Exception {
38         if (args.length != 1) {
39             throw new RuntimeException(
40                     "FAILED. Incorrect argument length: " + args.length);
41         }
42 
43         // Base JLS compliance check
44         for (int i = -128; i <= 127; i++) {
45             if (Integer.valueOf(i) != Integer.valueOf(i)) {
46                 throw new RuntimeException(
47                         "FAILED. All values in range [-128, 127] should be interned in cache: " + i);
48             }
49             if (Byte.valueOf((byte)i) != Byte.valueOf((byte)i)) {
50                 throw new RuntimeException(
51                         "FAILED. All Byte values in range [-128, 127] should be interned in cache: " + (byte)i);
52             }
53             if (Short.valueOf((short)i) != Short.valueOf((short)i)) {
54                 throw new RuntimeException(
55                         "FAILED. All Short values in range [-128, 127] should be interned in cache: " + (byte)i);
56             }
57             if (Long.valueOf(i) != Long.valueOf(i)) {
58                 throw new RuntimeException(
59                         "FAILED. All Long values in range [-128, 127] should be interned in cache: " + i);
60             }
61 
62             // Character cache only values 0 through 127
63             if (i >= 0) {
64                 if (Character.valueOf((char)i) != Character.valueOf((char)i)) {
65                     throw new RuntimeException(
66                             "FAILED. All Character values in range [0, 127] should be interned in cache: " + i);
67                 }
68             }
69         }
70 
71         // Check that archived integer cache agrees with runtime integer cache.
72         for (int i = -128; i <= 127; i++) {
73             if (ArchivedIntegerHolder.archivedObjects[i + 128] != Integer.valueOf(i)) {
74                 throw new RuntimeException(
75                         "FAILED. Archived and runtime caches disagree for " + i);
76             }
77         }
78 
79         int high = Integer.parseInt(args[0]);
80         if (Integer.valueOf(high) != Integer.valueOf(high)) {
81             throw new RuntimeException(
82                     "FAILED. Value expected to be retrieved from cache: " + high);
83         }
84 
85         if (Integer.valueOf(high + 1) == Integer.valueOf(high + 1)) {
86             throw new RuntimeException(
87                     "FAILED. Value not expected to be retrieved from cache: " + high);
88         }
89     }
90 }