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