1 /*
 2  * Copyright (c) 2020, 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 import jdk.internal.vm.annotation.ImplicitlyConstructible;
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  * @requires (os.simpleArch == "x64" | os.simpleArch == "aarch64")
36  * @requires (os.family == "linux" | os.family == "mac")
37  * @enablePreview
38  * @compile TestJNIIsSameObject.java
39  * @run main/othervm/native TestJNIIsSameObject
40  */
41 
42 public class TestJNIIsSameObject {
43   @ImplicitlyConstructible
44   @LooselyConsistentValue
45   static value class Value {
46     int i;
47 
48     public Value(int i) {
49       this.i = i;
50     }
51   }
52   native static boolean isSameObject(Object o0, Object o1);
53 
54   static {
55     System.loadLibrary("JNIIsSameObject");
56   }
57 
58   public static void main(String[] args) {
59     // Same value in different instances
60     Value v0 = new Value(3);
61     Value v1 = new Value(3);
62     Asserts.assertTrue(isSameObject(v0, v1));
63 
64     // Different values
65     Value v2 = new Value(4);
66     Asserts.assertFalse(isSameObject(v0, v2));
67 
68     // Same object
69     TestJNIIsSameObject t0 = new TestJNIIsSameObject();
70     Object o = t0;
71     Asserts.assertTrue(isSameObject(t0, o));
72 
73     // Different objects
74     TestJNIIsSameObject t1 = new TestJNIIsSameObject();
75     Asserts.assertFalse(isSameObject(t0, t1));
76 
77     // Comparing against null
78     Asserts.assertFalse(isSameObject(v0, null));
79     Asserts.assertFalse(isSameObject(null, v0));
80     Asserts.assertFalse(isSameObject(t0, null));
81     Asserts.assertFalse(isSameObject(null, t0));
82 
83     // Object vs inline
84     Asserts.assertFalse(isSameObject(v0, t0));
85     Asserts.assertFalse(isSameObject(t0, v0));
86 
87   }
88 }