1 /*
   2  * Copyright (c) 2017, 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 package runtime.valhalla.valuetypes;
  24 
  25 import jdk.test.lib.Asserts;
  26 
  27 /*
  28  * @test ValueTypeCreation
  29  * @summary Value Type creation test
  30  * @library /test/lib
  31  * @compile  -XDenableValueTypes ValueTypeCreation.java Point.java Long8Value.java Person.java
  32  * @run main/othervm -Xint -XX:+EnableValhalla runtime.valhalla.valuetypes.ValueTypeCreation
  33  * @run main/othervm -Xcomp -XX:+EnableValhalla runtime.valhalla.valuetypes.ValueTypeCreation
  34  */
  35 public class ValueTypeCreation {
  36     public static void main(String[] args) {
  37         ValueTypeCreation valueTypeCreation = new ValueTypeCreation();
  38         valueTypeCreation.run();
  39     }
  40 
  41     public void run() {
  42         testPoint();
  43         testLong8();
  44         // Embedded oops not yet supported
  45         //testPerson();
  46         StaticSelf.test();
  47     }
  48 
  49     void testPoint() {
  50         Point p = Point.createPoint(1, 2);
  51         Asserts.assertEquals(p.x, 1, "invalid point x value");
  52         Asserts.assertEquals(p.y, 2, "invalid point y value");
  53         Point p2 = clonePoint(p);
  54         Asserts.assertEquals(p2.x, 1, "invalid point clone x value");
  55         Asserts.assertEquals(p2.y, 2, "invalid point clone y value");
  56     }
  57 
  58     static Point clonePoint(Point p) {
  59         Point q = p;
  60         return q;
  61     }
  62 
  63     void testLong8() {
  64         Long8Value long8Value = Long8Value.create(1, 2, 3, 4, 5, 6, 7, 8);
  65         Asserts.assertEquals(long8Value.getLongField1(), 1L, "Field 1 incorrect");
  66         Asserts.assertEquals(long8Value.getLongField8(), 8L, "Field 8 incorrect");
  67         Long8Value.check(long8Value, 1, 2, 3, 4, 5, 6, 7, 8);
  68     }
  69 
  70     void testPerson() {
  71         Person person = Person.create(1, "John", "Smith");
  72         Asserts.assertEquals(person.getId(), 1L, "Id field incorrect");
  73         Asserts.assertEquals(person.getFirstName(), "John", "First name incorrect");
  74         Asserts.assertEquals(person.getLastName(), "Smith", "Last name incorrect");
  75     }
  76 
  77     static final __ByValue class StaticSelf {
  78 
  79         static final StaticSelf DEFAULT = create(0);
  80         final int f1;
  81 
  82         private StaticSelf() { f1 = 0; }
  83         public String toString() { return "StaticSelf f1=" + f1; }
  84 
  85         __ValueFactory static StaticSelf create(int f1) {
  86             StaticSelf s = __MakeDefault StaticSelf();
  87             s.f1 = f1;
  88             return s;
  89         }
  90 
  91         public static void test() {
  92             String s = DEFAULT.toString();
  93         }
  94 
  95     }
  96 }