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