1 /* 2 * Copyright (c) 2019, 2024, 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 * @test 26 * @bug 8209687 27 * @summary Verify that Parse::optimize_cmp_with_klass() works with value classes. 28 * @library /test/lib 29 * @enablePreview 30 * @run main/othervm -Xbatch compiler.valhalla.inlinetypes.TestOptimizeKlassCmp 31 */ 32 33 package compiler.valhalla.inlinetypes; 34 35 import jdk.test.lib.Asserts; 36 37 value class MyValue { 38 public int x; 39 40 public MyValue(int x) { 41 this.x = x; 42 } 43 } 44 45 public class TestOptimizeKlassCmp { 46 47 public static boolean test1(MyValue v1, MyValue v2) { 48 return v1.equals(v2); 49 } 50 51 public static boolean test2(MyValue v1, MyValue v2) { 52 return v1.getClass().equals(v2.getClass()); 53 } 54 55 public static boolean test3(Object o1, Object o2) { 56 return o1.getClass().equals(o2.getClass()); 57 } 58 59 public static void main(String[] args) { 60 MyValue v1 = new MyValue(0); 61 MyValue v2 = new MyValue(1); 62 for (int i = 0; i < 10_000; ++i) { 63 Asserts.assertFalse(test1(v1, v2)); 64 Asserts.assertTrue(test1(v1, v1)); 65 Asserts.assertTrue(test2(v1, v2)); 66 Asserts.assertTrue(test2(v1, v1)); 67 Asserts.assertTrue(test3(v1, v2)); 68 Asserts.assertTrue(test3(v1, v1)); 69 } 70 } 71 }