Path: blob/master/test/hotspot/jtreg/compiler/stable/TestStableBoolean.java
41152 views
/*1* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test TestStableBoolean25* @summary tests on stable fields and arrays26* @library /test/lib /27* @modules java.base/jdk.internal.misc28* @modules java.base/jdk.internal.vm.annotation29* @build sun.hotspot.WhiteBox30*31* @run main/bootclasspath/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp32* -XX:CompileOnly=::get,::get1,::get2,::get3,::get433* -XX:-TieredCompilation34* -XX:+FoldStableValues35* compiler.stable.TestStableBoolean36* @run main/bootclasspath/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp37* -XX:CompileOnly=::get,::get1,::get2,::get3,::get438* -XX:-TieredCompilation39* -XX:-FoldStableValues40* compiler.stable.TestStableBoolean41*42* @run main/bootclasspath/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp43* -XX:CompileOnly=::get,::get1,::get2,::get3,::get444* -XX:+TieredCompilation -XX:TieredStopAtLevel=145* -XX:+FoldStableValues46* compiler.stable.TestStableBoolean47* @run main/bootclasspath/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp48* -XX:CompileOnly=::get,::get1,::get2,::get3,::get449* -XX:+TieredCompilation -XX:TieredStopAtLevel=150* -XX:-FoldStableValues51* compiler.stable.TestStableBoolean52*/5354package compiler.stable;5556import jdk.internal.vm.annotation.Stable;5758import java.lang.reflect.InvocationTargetException;5960public class TestStableBoolean {61static final boolean isStableEnabled = StableConfiguration.isStableEnabled;6263public static void main(String[] args) throws Exception {64run(DefaultValue.class);65run(BooleanStable.class);66run(DefaultStaticValue.class);67run(StaticBooleanStable.class);68run(VolatileBooleanStable.class);6970// @Stable arrays: Dim 1-471run(BooleanArrayDim1.class);72run(BooleanArrayDim2.class);73run(BooleanArrayDim3.class);74run(BooleanArrayDim4.class);7576// @Stable Object field: dynamic arrays77run(ObjectArrayLowerDim0.class);78run(ObjectArrayLowerDim1.class);79run(ObjectArrayLowerDim2.class);8081// Nested @Stable fields82run(NestedStableField.class);83run(NestedStableField1.class);84run(NestedStableField2.class);85run(NestedStableField3.class);8687if (failed) {88throw new Error("TEST FAILED");89}90}9192/* ==================================================== */9394static class DefaultValue {95public @Stable boolean v;9697public static final DefaultValue c = new DefaultValue();98public static boolean get() { return c.v; }99public static void test() throws Exception {100boolean val1 = get();101c.v = true; boolean val2 = get();102assertEquals(val1, false);103assertEquals(val2, true);104}105}106107/* ==================================================== */108109static class BooleanStable {110public @Stable boolean v;111112public static final BooleanStable c = new BooleanStable();113public static boolean get() { return c.v; }114public static void test() throws Exception {115c.v = true; boolean val1 = get();116c.v = false; boolean val2 = get();117assertEquals(val1, true);118assertEquals(val2, (isStableEnabled ? true : false));119}120}121122/* ==================================================== */123124static class DefaultStaticValue {125public static @Stable boolean v;126127public static final DefaultStaticValue c = new DefaultStaticValue();128public static boolean get() { return c.v; }129public static void test() throws Exception {130boolean val1 = get();131c.v = true; boolean val2 = get();132assertEquals(val1, false);133assertEquals(val2, true);134}135}136137/* ==================================================== */138139static class StaticBooleanStable {140public static @Stable boolean v;141142public static final StaticBooleanStable c = new StaticBooleanStable();143public static boolean get() { return c.v; }144public static void test() throws Exception {145c.v = true; boolean val1 = get();146c.v = false; boolean val2 = get();147assertEquals(val1, true);148assertEquals(val2, (isStableEnabled ? true : false));149}150}151152/* ==================================================== */153154static class VolatileBooleanStable {155public @Stable volatile boolean v;156157public static final VolatileBooleanStable c = new VolatileBooleanStable();158public static boolean get() { return c.v; }159public static void test() throws Exception {160c.v = true; boolean val1 = get();161c.v = false; boolean val2 = get();162assertEquals(val1, true);163assertEquals(val2, (isStableEnabled ? true : false));164}165}166167/* ==================================================== */168// @Stable array == field && all components are stable169170static class BooleanArrayDim1 {171public @Stable boolean[] v;172173public static final BooleanArrayDim1 c = new BooleanArrayDim1();174public static boolean get() { return c.v[0]; }175public static boolean get1() { return c.v[10]; }176public static boolean[] get2() { return c.v; }177public static void test() throws Exception {178{179c.v = new boolean[1]; c.v[0] = true; boolean val1 = get();180c.v[0] = false; boolean val2 = get();181assertEquals(val1, true);182assertEquals(val2, (isStableEnabled ? true : false));183}184185{186c.v = new boolean[20]; c.v[10] = true; boolean val1 = get1();187c.v[10] = false; boolean val2 = get1();188assertEquals(val1, true);189assertEquals(val2, (isStableEnabled ? true : false));190}191192{193c.v = new boolean[1]; boolean[] val1 = get2();194c.v = new boolean[1]; boolean[] val2 = get2();195assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));196}197}198}199200/* ==================================================== */201202static class BooleanArrayDim2 {203public @Stable boolean[][] v;204205public static final BooleanArrayDim2 c = new BooleanArrayDim2();206public static boolean get() { return c.v[0][0]; }207public static boolean[] get1() { return c.v[0]; }208public static boolean[][] get2() { return c.v; }209public static void test() throws Exception {210{211c.v = new boolean[1][1]; c.v[0][0] = true; boolean val1 = get();212c.v[0][0] = false; boolean val2 = get();213assertEquals(val1, true);214assertEquals(val2, (isStableEnabled ? true : false));215216c.v = new boolean[1][1]; c.v[0][0] = false; boolean val3 = get();217assertEquals(val3, (isStableEnabled ? true : false));218219c.v[0] = new boolean[1]; c.v[0][0] = false; boolean val4 = get();220assertEquals(val4, (isStableEnabled ? true : false));221}222223{224c.v = new boolean[1][1]; boolean[] val1 = get1();225c.v[0] = new boolean[1]; boolean[] val2 = get1();226assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));227}228229{230c.v = new boolean[1][1]; boolean[][] val1 = get2();231c.v = new boolean[1][1]; boolean[][] val2 = get2();232assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));233}234}235}236237/* ==================================================== */238239static class BooleanArrayDim3 {240public @Stable boolean[][][] v;241242public static final BooleanArrayDim3 c = new BooleanArrayDim3();243public static boolean get() { return c.v[0][0][0]; }244public static boolean[] get1() { return c.v[0][0]; }245public static boolean[][] get2() { return c.v[0]; }246public static boolean[][][] get3() { return c.v; }247public static void test() throws Exception {248{249c.v = new boolean[1][1][1]; c.v[0][0][0] = true; boolean val1 = get();250c.v[0][0][0] = false; boolean val2 = get();251assertEquals(val1, true);252assertEquals(val2, (isStableEnabled ? true : false));253254c.v = new boolean[1][1][1]; c.v[0][0][0] = false; boolean val3 = get();255assertEquals(val3, (isStableEnabled ? true : false));256257c.v[0] = new boolean[1][1]; c.v[0][0][0] = false; boolean val4 = get();258assertEquals(val4, (isStableEnabled ? true : false));259260c.v[0][0] = new boolean[1]; c.v[0][0][0] = false; boolean val5 = get();261assertEquals(val5, (isStableEnabled ? true : false));262}263264{265c.v = new boolean[1][1][1]; boolean[] val1 = get1();266c.v[0][0] = new boolean[1]; boolean[] val2 = get1();267assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));268}269270{271c.v = new boolean[1][1][1]; boolean[][] val1 = get2();272c.v[0] = new boolean[1][1]; boolean[][] val2 = get2();273assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));274}275276{277c.v = new boolean[1][1][1]; boolean[][][] val1 = get3();278c.v = new boolean[1][1][1]; boolean[][][] val2 = get3();279assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));280}281}282}283284/* ==================================================== */285286static class BooleanArrayDim4 {287public @Stable boolean[][][][] v;288289public static final BooleanArrayDim4 c = new BooleanArrayDim4();290public static boolean get() { return c.v[0][0][0][0]; }291public static boolean[] get1() { return c.v[0][0][0]; }292public static boolean[][] get2() { return c.v[0][0]; }293public static boolean[][][] get3() { return c.v[0]; }294public static boolean[][][][] get4() { return c.v; }295public static void test() throws Exception {296{297c.v = new boolean[1][1][1][1]; c.v[0][0][0][0] = true; boolean val1 = get();298c.v[0][0][0][0] = false; boolean val2 = get();299assertEquals(val1, true);300assertEquals(val2, (isStableEnabled ? true : false));301302c.v = new boolean[1][1][1][1]; c.v[0][0][0][0] = false; boolean val3 = get();303assertEquals(val3, (isStableEnabled ? true : false));304305c.v[0] = new boolean[1][1][1]; c.v[0][0][0][0] = false; boolean val4 = get();306assertEquals(val4, (isStableEnabled ? true : false));307308c.v[0][0] = new boolean[1][1]; c.v[0][0][0][0] = false; boolean val5 = get();309assertEquals(val5, (isStableEnabled ? true : false));310311c.v[0][0][0] = new boolean[1]; c.v[0][0][0][0] = false; boolean val6 = get();312assertEquals(val6, (isStableEnabled ? true : false));313}314315{316c.v = new boolean[1][1][1][1]; boolean[] val1 = get1();317c.v[0][0][0] = new boolean[1]; boolean[] val2 = get1();318assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));319}320321{322c.v = new boolean[1][1][1][1]; boolean[][] val1 = get2();323c.v[0][0] = new boolean[1][1]; boolean[][] val2 = get2();324assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));325}326327{328c.v = new boolean[1][1][1][1]; boolean[][][] val1 = get3();329c.v[0] = new boolean[1][1][1]; boolean[][][] val2 = get3();330assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));331}332333{334c.v = new boolean[1][1][1][1]; boolean[][][][] val1 = get4();335c.v = new boolean[1][1][1][1]; boolean[][][][] val2 = get4();336assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));337}338339}340}341342/* ==================================================== */343// Dynamic Dim is higher than static344345static class ObjectArrayLowerDim0 {346public @Stable Object v;347348public static final ObjectArrayLowerDim0 c = new ObjectArrayLowerDim0();349public static boolean get() { return ((boolean[])c.v)[0]; }350public static boolean[] get1() { return (boolean[])c.v; }351public static boolean[] get2() { return (boolean[])c.v; }352353public static void test() throws Exception {354{355c.v = new boolean[1]; ((boolean[])c.v)[0] = true; boolean val1 = get();356((boolean[])c.v)[0] = false; boolean val2 = get();357358assertEquals(val1, true);359assertEquals(val2, false);360}361362{363c.v = new boolean[1]; boolean[] val1 = get1();364c.v = new boolean[1]; boolean[] val2 = get1();365assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));366}367}368}369370/* ==================================================== */371372static class ObjectArrayLowerDim1 {373public @Stable Object[] v;374375public static final ObjectArrayLowerDim1 c = new ObjectArrayLowerDim1();376public static boolean get() { return ((boolean[][])c.v)[0][0]; }377public static boolean[] get1() { return (boolean[])(c.v[0]); }378public static Object[] get2() { return c.v; }379380public static void test() throws Exception {381{382c.v = new boolean[1][1]; ((boolean[][])c.v)[0][0] = true; boolean val1 = get();383((boolean[][])c.v)[0][0] = false; boolean val2 = get();384385assertEquals(val1, true);386assertEquals(val2, false);387}388389{390c.v = new boolean[1][1]; c.v[0] = new boolean[0]; boolean[] val1 = get1();391c.v[0] = new boolean[0]; boolean[] val2 = get1();392393assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));394}395396{397c.v = new boolean[0][0]; Object[] val1 = get2();398c.v = new boolean[0][0]; Object[] val2 = get2();399400assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));401}402}403}404405/* ==================================================== */406407static class ObjectArrayLowerDim2 {408public @Stable Object[][] v;409410public static final ObjectArrayLowerDim2 c = new ObjectArrayLowerDim2();411public static boolean get() { return ((boolean[][][])c.v)[0][0][0]; }412public static boolean[] get1() { return (boolean[])(c.v[0][0]); }413public static boolean[][] get2() { return (boolean[][])(c.v[0]); }414public static Object[][] get3() { return c.v; }415416public static void test() throws Exception {417{418c.v = new boolean[1][1][1]; ((boolean[][][])c.v)[0][0][0] = true; boolean val1 = get();419((boolean[][][])c.v)[0][0][0] = false; boolean val2 = get();420421assertEquals(val1, true);422assertEquals(val2, false);423}424425{426c.v = new boolean[1][1][1]; c.v[0][0] = new boolean[0]; boolean[] val1 = get1();427c.v[0][0] = new boolean[0]; boolean[] val2 = get1();428429assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));430}431432{433c.v = new boolean[1][1][1]; c.v[0] = new boolean[0][0]; boolean[][] val1 = get2();434c.v[0] = new boolean[0][0]; boolean[][] val2 = get2();435436assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));437}438439{440c.v = new boolean[0][0][0]; Object[][] val1 = get3();441c.v = new boolean[0][0][0]; Object[][] val2 = get3();442443assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));444}445}446}447448/* ==================================================== */449450static class NestedStableField {451static class A {452public @Stable boolean a;453454}455public @Stable A v;456457public static final NestedStableField c = new NestedStableField();458public static A get() { return c.v; }459public static boolean get1() { return get().a; }460461public static void test() throws Exception {462{463c.v = new A(); c.v.a = true; A val1 = get();464c.v.a = false; A val2 = get();465466assertEquals(val1.a, false);467assertEquals(val2.a, false);468}469470{471c.v = new A(); c.v.a = true; boolean val1 = get1();472c.v.a = false; boolean val2 = get1();473c.v = new A(); c.v.a = false; boolean val3 = get1();474475assertEquals(val1, true);476assertEquals(val2, (isStableEnabled ? true : false));477assertEquals(val3, (isStableEnabled ? true : false));478}479}480}481482/* ==================================================== */483484static class NestedStableField1 {485static class A {486public @Stable boolean a;487public @Stable A next;488}489public @Stable A v;490491public static final NestedStableField1 c = new NestedStableField1();492public static A get() { return c.v.next.next.next.next.next.next.next; }493public static boolean get1() { return get().a; }494495public static void test() throws Exception {496{497c.v = new A(); c.v.next = new A(); c.v.next.next = c.v;498c.v.a = true; c.v.next.a = true; A val1 = get();499c.v.a = false; c.v.next.a = false; A val2 = get();500501assertEquals(val1.a, false);502assertEquals(val2.a, false);503}504505{506c.v = new A(); c.v.next = c.v;507c.v.a = true; boolean val1 = get1();508c.v.a = false; boolean val2 = get1();509c.v = new A(); c.v.next = c.v;510c.v.a = false; boolean val3 = get1();511512assertEquals(val1, true);513assertEquals(val2, (isStableEnabled ? true : false));514assertEquals(val3, (isStableEnabled ? true : false));515}516}517}518/* ==================================================== */519520static class NestedStableField2 {521static class A {522public @Stable boolean a;523public @Stable A left;524public A right;525}526527public @Stable A v;528529public static final NestedStableField2 c = new NestedStableField2();530public static boolean get() { return c.v.left.left.left.a; }531public static boolean get1() { return c.v.left.left.right.left.a; }532533public static void test() throws Exception {534{535c.v = new A(); c.v.left = c.v.right = c.v;536c.v.a = true; boolean val1 = get(); boolean val2 = get1();537c.v.a = false; boolean val3 = get(); boolean val4 = get1();538539assertEquals(val1, true);540assertEquals(val3, (isStableEnabled ? true : false));541542assertEquals(val2, true);543assertEquals(val4, false);544}545}546}547548/* ==================================================== */549550static class NestedStableField3 {551static class A {552public @Stable boolean a;553public @Stable A[] left;554public A[] right;555}556557public @Stable A[] v;558559public static final NestedStableField3 c = new NestedStableField3();560public static boolean get() { return c.v[0].left[1].left[0].left[1].a; }561public static boolean get1() { return c.v[1].left[0].left[1].right[0].left[1].a; }562563public static void test() throws Exception {564{565A elem = new A();566c.v = new A[] { elem, elem }; c.v[0].left = c.v[0].right = c.v;567elem.a = true; boolean val1 = get(); boolean val2 = get1();568elem.a = false; boolean val3 = get(); boolean val4 = get1();569570assertEquals(val1, true);571assertEquals(val3, (isStableEnabled ? true : false));572573assertEquals(val2, true);574assertEquals(val4, false);575}576}577}578579/* ==================================================== */580// Auxiliary methods581static void assertEquals(boolean i, boolean j) { if (i != j) throw new AssertionError(i + " != " + j); }582static void assertTrue(boolean b) { if (!b) throw new AssertionError(); }583584static boolean failed = false;585586public static void run(Class<?> test) {587Throwable ex = null;588System.out.print(test.getName()+": ");589try {590test.getMethod("test").invoke(null);591} catch (InvocationTargetException e) {592ex = e.getCause();593} catch (Throwable e) {594ex = e;595} finally {596if (ex == null) {597System.out.println("PASSED");598} else {599failed = true;600System.out.println("FAILED");601ex.printStackTrace(System.out);602}603}604}605}606607608