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.
   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.valuetypes;
  25 
  26 import jdk.test.lib.Asserts;
  27 
  28 /*
  29  * @test VWithFieldTest
  30  * @summary vwithfield bytecode test
  31  * @library /test/lib
  32  * @compile -XDenableValueTypes --add-modules jdk.incubator.mvt Point.java
  33  * @compile -XDenableValueTypes VWithFieldTest.java
  34  * @run main/othervm -Xint -XX:+EnableValhalla runtime.valhalla.valuetypes.VWithFieldTest
  35  * @run main/othervm -Xcomp -XX:+EnableValhalla runtime.valhalla.valuetypes.VWithFieldTest
  36  */
  37 
  38 public class VWithFieldTest {
  39 
  40     static __ByValue final class Point {
  41         final private int x;
  42         final private int y;
  43         
  44         __ValueFactory static Point make(int x, int y) {
  45             Point p = __MakeDefault Point();
  46             Asserts.assertEquals(p.x, 0, "invalid x default value");
  47             Asserts.assertEquals(p.y, 0, "invalid y default value");
  48             p.x = x;
  49             Asserts.assertEquals(p.x, x, "invalid x value");
  50             Asserts.assertEquals(p.y, 0, "invalid y value");
  51             p.y = y;
  52             Asserts.assertEquals(p.x, x, "invalid x value");
  53             Asserts.assertEquals(p.y, y, "invalid y value");
  54             return p;
  55         }
  56 
  57         Point () {
  58             x = 0;
  59             y = 0;
  60         }
  61 
  62         public int getX() {
  63             return x;
  64         }
  65         
  66         __ValueFactory static Point setX(Point p, int x) {
  67             p.x = x;
  68             return p;
  69         }
  70 
  71         public int getY() {
  72             return y;
  73         }
  74         
  75         __ValueFactory static Point setY(Point p, int y) {
  76             p.y = y;
  77             return p;
  78         }
  79     }
  80 
  81     public static void main(String[] args) {
  82         creationTest();
  83         creationTest();
  84         witherTest();
  85         witherTest();
  86     }
  87 
  88     static void creationTest() {
  89         Point p = Point.make(10,20);
  90         Asserts.assertEquals(p.x, 10, "invalid x value");
  91         Asserts.assertEquals(p.y, 20, "invalid y value");
  92     }
  93 
  94     static void witherTest() {
  95         Point p1 = Point.make(2,12);
  96         Asserts.assertEquals(p1.x, 2, "invalid x value");
  97         Asserts.assertEquals(p1.y, 12, "invalid y value");
  98         Point p2 = Point.setX(p1,3);
  99         Asserts.assertEquals(p2.x, 3, "invalid x value");
 100         Asserts.assertEquals(p2.y, 12, "invalid y value");
 101         Point p3 = Point.setY(p2, 14);
 102         Asserts.assertEquals(p3.x, 3, "invalid x value");
 103         Asserts.assertEquals(p3.y, 14, "invalid y value");
 104     }
 105     
 106 }