1 /*
   2  * Copyright (c) 2016, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @test TestStableUShort
  28  * @summary tests on stable fields and arrays
  29  * @library /testlibrary /test/lib
  30  * @build TestStableUShort StableConfiguration sun.hotspot.WhiteBox
  31  * @run main ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  32  * @run main ClassFileInstaller
  33  *           java/lang/invoke/StableConfiguration
  34  *           java/lang/invoke/TestStableUShort
  35  *           java/lang/invoke/TestStableUShort$UShortStable
  36  *           java/lang/invoke/TestStableUShort$UShortArrayDim1
  37  *
  38  * @run main/othervm -Xbootclasspath/a:.
  39  *                   -XX:+IgnoreUnrecognizedVMOptions -XX:+AlwaysIncrementalInline
  40  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp
  41  *                   -XX:-TieredCompilation
  42  *                   -XX:+FoldStableValues
  43  *                   -XX:CompileOnly=::get,::get1
  44  *                   java.lang.invoke.TestStableUShort
  45  * @run main/othervm -Xbootclasspath/a:.
  46  *                   -XX:+IgnoreUnrecognizedVMOptions -XX:+AlwaysIncrementalInline
  47  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp
  48  *                   -XX:-TieredCompilation
  49  *                   -XX:-FoldStableValues
  50  *                   -XX:CompileOnly=::get,::get1
  51  *                   java.lang.invoke.TestStableUShort
  52  *
  53  * @run main/othervm -Xbootclasspath/a:.
  54  *                   -XX:+IgnoreUnrecognizedVMOptions -XX:+AlwaysIncrementalInline
  55  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp
  56  *                   -XX:+TieredCompilation -XX:TieredStopAtLevel=1
  57  *                   -XX:+FoldStableValues
  58  *                   -XX:CompileOnly=::get,::get1
  59  *                   java.lang.invoke.TestStableUShort
  60  * @run main/othervm -Xbootclasspath/a:.
  61  *                   -XX:+IgnoreUnrecognizedVMOptions -XX:+AlwaysIncrementalInline
  62  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp
  63  *                   -XX:+TieredCompilation -XX:TieredStopAtLevel=1
  64  *                   -XX:-FoldStableValues
  65  *                   -XX:CompileOnly=::get,::get1
  66  *                   java.lang.invoke.TestStableUShort
  67  *
  68  */
  69 package java.lang.invoke;
  70 
  71 import jdk.internal.vm.annotation.Stable;
  72 
  73 import java.lang.reflect.InvocationTargetException;
  74 
  75 public class TestStableUShort {
  76     static final boolean isStableEnabled = StableConfiguration.isStableEnabled;
  77 
  78     public static void main(String[] args) throws Exception {
  79         run(UShortStable.class);
  80         run(UShortArrayDim1.class);
  81 
  82         if (failed) {
  83             throw new Error("TEST FAILED");
  84         }
  85     }
  86 
  87     /* ==================================================== */
  88 
  89     static class UShortStable {
  90         public @Stable short v;
  91 
  92         public static final UShortStable c = new UShortStable();
  93 
  94         public static int get() { return c.v & 0xFFFF; }
  95 
  96         public static void test() throws Exception {
  97             short v1 = -1, v2 = 1;
  98 
  99             c.v = v1; int r1 = get();
 100             c.v = v2; int r2 = get();
 101 
 102             assertEquals(r1, v1 & 0xFFFF);
 103             assertEquals(r2, (isStableEnabled ? v1 : v2) & 0xFFFF);
 104         }
 105     }
 106 
 107     /* ==================================================== */
 108 
 109     static class UShortArrayDim1 {
 110         public @Stable short[] v;
 111 
 112         public static final UShortArrayDim1 c = new UShortArrayDim1();
 113 
 114         public static short[] get()  { return c.v; }
 115         public static int    get1() { return get()[0] & 0xFFFF; }
 116 
 117         public static void test() throws Exception {
 118             short v1 = -1, v2 = 1;
 119 
 120             c.v = new short[1];
 121             c.v[0] = v1; int r1 = get1();
 122             c.v[0] = v2; int r2 = get1();
 123 
 124             assertEquals(r1, v1 & 0xFFFF);
 125             assertEquals(r2, (isStableEnabled ? v1 : v2) & 0xFFFF);
 126         }
 127     }
 128 
 129     /* ==================================================== */
 130     // Auxiliary methods
 131     static void assertEquals(int i, int j) { if (i != j)  throw new AssertionError(i + " != " + j); }
 132     static void assertTrue(boolean b) { if (!b)  throw new AssertionError(); }
 133 
 134     static boolean failed = false;
 135 
 136     public static void run(Class<?> test) {
 137         Throwable ex = null;
 138         System.out.print(test.getName()+": ");
 139         try {
 140             test.getMethod("test").invoke(null);
 141         } catch (InvocationTargetException e) {
 142             ex = e.getCause();
 143         } catch (Throwable e) {
 144             ex = e;
 145         } finally {
 146             if (ex == null) {
 147                 System.out.println("PASSED");
 148             } else {
 149                 failed = true;
 150                 System.out.println("FAILED");
 151                 ex.printStackTrace(System.out);
 152             }
 153         }
 154     }
 155 }