1 /*
2 * Copyright (c) 2020, 2026, 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.inlinetypes;
25
26 import jdk.internal.vm.annotation.LooselyConsistentValue;
27 import jdk.test.lib.Asserts;
28
29
30 /*
31 * @test
32 * @summary Test JNI IsSameObject semantic with inline types
33 * @library /testlibrary /test/lib
34 * @modules java.base/jdk.internal.vm.annotation
35 * @enablePreview
36 * @compile TestJNIIsSameObject.java
37 * @run main/othervm/native runtime.valhalla.inlinetypes.TestJNIIsSameObject
38 */
39 public class TestJNIIsSameObject {
40 @LooselyConsistentValue
41 static value class Value {
42 int i;
43
44 public Value(int i) {
45 this.i = i;
46 }
47 }
48 native static boolean isSameObject(Object o0, Object o1);
49
50 static {
51 System.loadLibrary("JNIIsSameObject");
52 }
53
54 public static void main(String[] args) {
55 // Same value in different instances
56 Value v0 = new Value(3);
57 Value v1 = new Value(3);
58 Asserts.assertTrue(isSameObject(v0, v1));
59
60 // Different values
61 Value v2 = new Value(4);
62 Asserts.assertFalse(isSameObject(v0, v2));
63
64 // Same object
65 TestJNIIsSameObject t0 = new TestJNIIsSameObject();
66 Object o = t0;
67 Asserts.assertTrue(isSameObject(t0, o));
68
69 // Different objects
70 TestJNIIsSameObject t1 = new TestJNIIsSameObject();
71 Asserts.assertFalse(isSameObject(t0, t1));
72
73 // Comparing against null
74 Asserts.assertFalse(isSameObject(v0, null));
75 Asserts.assertFalse(isSameObject(null, v0));
76 Asserts.assertFalse(isSameObject(t0, null));
77 Asserts.assertFalse(isSameObject(null, t0));
78
79 // Object vs inline
80 Asserts.assertFalse(isSameObject(v0, t0));
81 Asserts.assertFalse(isSameObject(t0, v0));
82
83 }
84 }