1 /*
   2  * Copyright (c) 2016, 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  * @test TestStableUShort
  26  * @summary tests on stable fields and arrays
  27  * @library /test/lib /
  28  * @modules java.base/jdk.internal.misc
  29  * @modules java.base/jdk.internal.vm.annotation
  30  * @build sun.hotspot.WhiteBox
  31  *
  32  * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+AlwaysIncrementalInline
  33  *                                 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp
  34  *                                 -XX:-TieredCompilation
  35  *                                 -XX:+FoldStableValues
  36  *                                 -XX:CompileOnly=::get,::get1
  37  *                                 compiler.stable.TestStableUShort
  38  * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+AlwaysIncrementalInline
  39  *                                 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp
  40  *                                 -XX:-TieredCompilation
  41  *                                 -XX:-FoldStableValues
  42  *                                 -XX:CompileOnly=::get,::get1
  43  *                                 compiler.stable.TestStableUShort
  44  *
  45  * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+AlwaysIncrementalInline
  46  *                                 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp
  47  *                                 -XX:+TieredCompilation -XX:TieredStopAtLevel=1
  48  *                                 -XX:+FoldStableValues
  49  *                                 -XX:CompileOnly=::get,::get1
  50  *                                 compiler.stable.TestStableUShort
  51  * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+AlwaysIncrementalInline
  52  *                                 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp
  53  *                                 -XX:+TieredCompilation -XX:TieredStopAtLevel=1
  54  *                                 -XX:-FoldStableValues
  55  *                                 -XX:CompileOnly=::get,::get1
  56  *                                 compiler.stable.TestStableUShort
  57  */
  58 
  59 package compiler.stable;
  60 
  61 import jdk.internal.vm.annotation.Stable;
  62 
  63 import java.lang.reflect.InvocationTargetException;
  64 
  65 public class TestStableUShort {
  66     static final boolean isStableEnabled = StableConfiguration.isStableEnabled;
  67 
  68     public static void main(String[] args) throws Exception {
  69         run(UShortStable.class);
  70         run(UShortArrayDim1.class);
  71 
  72         if (failed) {
  73             throw new Error("TEST FAILED");
  74         }
  75     }
  76 
  77     /* ==================================================== */
  78 
  79     static class UShortStable {
  80         public @Stable short v;
  81 
  82         public static final UShortStable c = new UShortStable();
  83 
  84         public static int get() { return c.v & 0xFFFF; }
  85 
  86         public static void test() throws Exception {
  87             short v1 = -1, v2 = 1;
  88 
  89             c.v = v1; int r1 = get();
  90             c.v = v2; int r2 = get();
  91 
  92             assertEquals(r1, v1 & 0xFFFF);
  93             assertEquals(r2, (isStableEnabled ? v1 : v2) & 0xFFFF);
  94         }
  95     }
  96 
  97     /* ==================================================== */
  98 
  99     static class UShortArrayDim1 {
 100         public @Stable short[] v;
 101 
 102         public static final UShortArrayDim1 c = new UShortArrayDim1();
 103 
 104         public static short[] get()  { return c.v; }
 105         public static int    get1() { return get()[0] & 0xFFFF; }
 106 
 107         public static void test() throws Exception {
 108             short v1 = -1, v2 = 1;
 109 
 110             c.v = new short[1];
 111             c.v[0] = v1; int r1 = get1();
 112             c.v[0] = v2; int r2 = get1();
 113 
 114             assertEquals(r1, v1 & 0xFFFF);
 115             assertEquals(r2, (isStableEnabled ? v1 : v2) & 0xFFFF);
 116         }
 117     }
 118 
 119     /* ==================================================== */
 120     // Auxiliary methods
 121     static void assertEquals(int i, int j) { if (i != j)  throw new AssertionError(i + " != " + j); }
 122     static void assertTrue(boolean b) { if (!b)  throw new AssertionError(); }
 123 
 124     static boolean failed = false;
 125 
 126     public static void run(Class<?> test) {
 127         Throwable ex = null;
 128         System.out.print(test.getName()+": ");
 129         try {
 130             test.getMethod("test").invoke(null);
 131         } catch (InvocationTargetException e) {
 132             ex = e.getCause();
 133         } catch (Throwable e) {
 134             ex = e;
 135         } finally {
 136             if (ex == null) {
 137                 System.out.println("PASSED");
 138             } else {
 139                 failed = true;
 140                 System.out.println("FAILED");
 141                 ex.printStackTrace(System.out);
 142             }
 143         }
 144     }
 145 }