1 /*
  2  * Copyright (c) 2016, 2021, 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 package runtime.valhalla.inlinetypes;
 25 
 26 import jdk.test.lib.Asserts;
 27 
 28 /*
 29  * @test
 30  * @summary withfield bytecode test
 31  * @library /test/lib
 32  * @build org.openjdk.asmtools.* org.openjdk.asmtools.jasm.*
 33  * @run driver org.openjdk.asmtools.JtregDriver jasm -strict WithFieldTestClasses.jasm
 34  * @compile -XDenablePrimitiveClasses Point.java WithFieldTest.java
 35  * @run main/othervm -XX:+EnableValhalla -XX:+EnablePrimitiveClasses runtime.valhalla.inlinetypes.WithFieldTest
 36  */
 37 
 38 public class WithFieldTest {
 39 
 40     public static void main(String[] args) {
 41         creationTest();
 42         creationTest();
 43         witherTest();
 44         witherTest();
 45         allTypesTest();
 46     }
 47 
 48     static void creationTest() {
 49         WithFieldPoint p = WithFieldPoint.make(10,20);
 50         p.checkFields(10, 20);
 51     }
 52 
 53     static void witherTest() {
 54         WithFieldPoint p1 = WithFieldPoint.make(2,12);
 55         p1.checkFields(2, 12);
 56         WithFieldPoint p2 = p1.withX(3);
 57         p2.checkFields(3, 12);
 58         WithFieldPoint p3 = p2.withY(14);
 59         p3.checkFields(3, 14);
 60         WithFieldPoint p4 = p1.withY(14);
 61         p4.checkFields(2, 14);
 62         WithFieldPoint p5 = p4.withX(3);
 63         p5.checkFields(3, 14);
 64     }
 65 
 66     static boolean[] z_values = new boolean[] { false, true, false};
 67     static byte[] b_values = new byte[] { 0, 125, -111};
 68     static short[] s_values = new short[] { 0, 32654, -31836};
 69     static char[] c_values = new char[] { 0, 1, 65528};
 70     static int[] i_values = new int[] { 0, 2137523847, -2037453241};
 71     static long[] l_values = new long[] { 0, 9123456036854775807L, -9112272036854775507L};
 72     static float[] f_values = new float[] { 0.0f, 1.52758043e7f, -7.93757e-5f};
 73     static double[] d_values = new double[] { 0.0d, 3.304786e9d, -0.7548345e-15d};
 74     static Object[] o_values = new Object[] { null, "Hello", "Duke"};
 75     static Point[] p_values = new Point[] { new Point(0, 0), new Point(-1, 1), new Point(1, -1)};
 76 
 77     static void allTypesTest() {
 78         AllTypes value = AllTypes.default;
 79         int[] map = new int[10];
 80         verifyAllTypes(value, map);
 81         for (int i = 2; i >= 0; i--) {
 82             value = value.set_z(z_values[map[0] = i]);
 83             verifyAllTypes(value, map);
 84             value = value.set_b(b_values[map[1] = i]);
 85             verifyAllTypes(value, map);
 86             value = value.set_s(s_values[map[2] = i]);
 87             verifyAllTypes(value, map);
 88             value = value.set_c(c_values[map[3] = i]);
 89             verifyAllTypes(value, map);
 90             value = value.set_i(i_values[map[4] = i]);
 91             verifyAllTypes(value, map);
 92             value = value.set_l(l_values[map[5] = i]);
 93             verifyAllTypes(value, map);
 94             value = value.set_f(f_values[map[6] = i]);
 95             verifyAllTypes(value, map);
 96             value = value.set_d(d_values[map[7] = i]);
 97             verifyAllTypes(value, map);
 98             value = value.set_o(o_values[map[8] = i]);
 99             verifyAllTypes(value, map);
100             value = value.set_p(p_values[map[9] = i]);
101             verifyAllTypes(value, map);
102         }
103     }
104 
105     static void verifyAllTypes(AllTypes x, int[] map) {
106         Asserts.assertEquals(x.z, z_values[map[0]]);
107         Asserts.assertEquals(x.b, b_values[map[1]]);
108         Asserts.assertEquals(x.s, s_values[map[2]]);
109         Asserts.assertEquals(x.c, c_values[map[3]]);
110         Asserts.assertEquals(x.i, i_values[map[4]]);
111         Asserts.assertEquals(x.l, l_values[map[5]]);
112         Asserts.assertEquals(x.f, f_values[map[6]]);
113         Asserts.assertEquals(x.d, d_values[map[7]]);
114         Asserts.assertEquals(x.o, o_values[map[8]]);
115         Asserts.assertEquals(x.p, p_values[map[9]]);
116     }
117 
118 }