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