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