1 /*
   2  * Copyright (c) 2018, 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 import sun.hotspot.WhiteBox;
  26 
  27 //
  28 // Help test archived integer cache consistency.
  29 //
  30 // Takes two arguments:
  31 // 0: the expected AutoBoxCacheMax setting
  32 // 1: if the values are expected to be retrieved from the archive or not
  33 //
  34 public class CheckIntegerCacheApp {
  35     static WhiteBox wb;
  36 
  37     public static void main(String[] args) throws Exception {
  38         wb = WhiteBox.getWhiteBox();
  39 
  40         if (!wb.areOpenArchiveHeapObjectsMapped()) {
  41             System.out.println("This may happen during normal operation. Test Skipped.");
  42             return;
  43         }
  44 
  45         if (args.length != 2) {
  46             throw new RuntimeException(
  47                     "FAILED. Incorrect argument length: " + args.length);
  48         }
  49 
  50         boolean archivedExpected = Boolean.parseBoolean(args[1]);
  51 
  52         // Base JLS compliance check
  53         for (int i = -128; i <= 127; i++) {
  54             if (Integer.valueOf(i) != Integer.valueOf(i)) {
  55                 throw new RuntimeException(
  56                         "FAILED. All values in range [-128, 127] should be interned in cache: " + i);
  57             }
  58             checkArchivedAsExpected(archivedExpected, i);
  59         }
  60 
  61         int high = Integer.parseInt(args[0]);
  62         if (Integer.valueOf(high) != Integer.valueOf(high)) {
  63             throw new RuntimeException(
  64                     "FAILED. Value expected to be retrieved from cache: " + high);
  65         }
  66         checkArchivedAsExpected(archivedExpected, Integer.valueOf(high));
  67 
  68         if (Integer.valueOf(high + 1) == Integer.valueOf(high + 1)) {
  69             throw new RuntimeException(
  70                     "FAILED. Value not expected to be retrieved from cache: " + high);
  71         }
  72         checkArchivedAsExpected(false, Integer.valueOf(high + 1));
  73     }
  74 
  75     private static void checkArchivedAsExpected(boolean archivedExpected, Integer value) {
  76         if (archivedExpected) {
  77             if (!wb.isShared(Integer.valueOf(value))) {
  78                 throw new RuntimeException(
  79                         "FAILED. Value expected to be archived: " + value);
  80             }
  81         } else {
  82             if (wb.isShared(Integer.valueOf(value))) {
  83                 throw new RuntimeException(
  84                         "FAILED. Value not expected to be archived: " + value);
  85             }
  86         }
  87     }
  88 }