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