1 /*
   2  * Copyright (c) 2018, 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 
  25 /*
  26  * @test
  27  * @summary test value bootstrap methods
  28  * @compile -XDallowWithFieldOperator ValueBootstrapMethodsTest.java
  29  * @run main/othervm -XX:+EnableValhalla -Dvalue.bsm.salt=1 ValueBootstrapMethodsTest
  30  */
  31 
  32 import java.util.List;
  33 import java.util.Objects;
  34 
  35 public class ValueBootstrapMethodsTest {
  36 
  37     public static final value class Value {
  38         private final int i;
  39         private final double d;
  40         private final String s;
  41         private final List<String> l;
  42         Value() {
  43             this.i = 0;
  44             this.d = 0;
  45             this.s = "default";
  46             this.l = List.of();
  47         }
  48         public static Value make(int i, double d, String s, String... items) {
  49             Value v = Value.default;
  50             v = __WithField(v.i, i);
  51             v = __WithField(v.d, d);
  52             v = __WithField(v.s, s);
  53             v = __WithField(v.l, List.of(items));
  54             return v;
  55         }
  56 
  57         private List<Object> values() {
  58             return List.of(Value.class, i, d, s, l);
  59         }
  60 
  61         public int localHashCode() {
  62             return values().hashCode();
  63         }
  64 
  65         public String localToString() {
  66             System.out.println(l);
  67             return String.format("[%s i=%s d=%s s=%s l=%s]", Value.class.getName(),
  68                                  i, String.valueOf(d), s, l.toString());
  69         }
  70     }
  71 
  72     private static void assertEquals(Object o1, Object expected) {
  73         if (!Objects.equals(o1, expected)) {
  74             throw new RuntimeException(o1 + " expected: " + expected);
  75         }
  76     }
  77 
  78     public static void main(String... args) throws Throwable {
  79 
  80         Value value = Value.make(10, 5.03, "foo", "bar", "goo");
  81 
  82         assertEquals(value.localHashCode(), value.hashCode());
  83         assertEquals(value.localToString(), value.toString());
  84 
  85         if (!value.equals(value)) {
  86             throw new RuntimeException("expected equals");
  87         }
  88 
  89         Value v2 = Value.make(20, 5.03, "foo", "bar", "goo");
  90         if (value.equals(v2)) {
  91             throw new RuntimeException("expected unequals");
  92         }
  93 
  94         Value v3 = Value.make(20, 5.03, "foo", "bar", "goo");
  95         if (!v2.equals(v3)) {
  96             throw new RuntimeException("expected equals");
  97         }
  98     }
  99 }