1
+ /*
2
+ * Copyright (c) 2023, 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
+ * @run testng/othervm --enable-native-access=ALL-UNNAMED TestScope
27
+ */
28
+
29
+ import org .testng .annotations .*;
30
+
31
+ import java .lang .foreign .Arena ;
32
+ import java .lang .foreign .MemorySegment ;
33
+ import java .lang .foreign .SymbolLookup ;
34
+ import java .nio .ByteBuffer ;
35
+ import java .nio .IntBuffer ;
36
+
37
+ import static org .testng .Assert .*;
38
+
39
+ public class TestScope {
40
+
41
+ static {
42
+ System .loadLibrary ("LookupTest" );
43
+ }
44
+
45
+ @ Test
46
+ public void testDifferentArrayScope () {
47
+ MemorySegment .Scope scope1 = MemorySegment .ofArray (new byte [10 ]).scope ();
48
+ MemorySegment .Scope scope2 = MemorySegment .ofArray (new byte [10 ]).scope ();
49
+ assertNotEquals (scope1 , scope2 );
50
+ }
51
+
52
+ @ Test
53
+ public void testDifferentBufferScope () {
54
+ MemorySegment .Scope scope1 = MemorySegment .ofBuffer (ByteBuffer .allocateDirect (10 )).scope ();
55
+ MemorySegment .Scope scope2 = MemorySegment .ofBuffer (ByteBuffer .allocateDirect (10 )).scope ();
56
+ assertNotEquals (scope1 , scope2 );
57
+ }
58
+
59
+ @ Test
60
+ public void testDifferentArenaScope () {
61
+ MemorySegment .Scope scope1 = Arena .ofAuto ().allocate (10 ).scope ();
62
+ MemorySegment .Scope scope2 = Arena .ofAuto ().allocate (10 ).scope ();
63
+ assertNotEquals (scope1 , scope2 );
64
+ }
65
+
66
+ @ Test
67
+ public void testSameArrayScope () {
68
+ byte [] arr = new byte [10 ];
69
+ assertEquals (MemorySegment .ofArray (arr ).scope (), MemorySegment .ofArray (arr ).scope ());
70
+ ByteBuffer buf = ByteBuffer .wrap (arr );
71
+ assertEquals (MemorySegment .ofArray (arr ).scope (), MemorySegment .ofBuffer (buf ).scope ());
72
+ testDerivedBufferScope (MemorySegment .ofArray (arr ));
73
+ }
74
+
75
+ @ Test
76
+ public void testSameBufferScope () {
77
+ ByteBuffer buf = ByteBuffer .allocateDirect (10 );
78
+ assertEquals (MemorySegment .ofBuffer (buf ).scope (), MemorySegment .ofBuffer (buf ).scope ());
79
+ testDerivedBufferScope (MemorySegment .ofBuffer (buf ));
80
+ }
81
+
82
+ @ Test
83
+ public void testSameArenaScope () {
84
+ try (Arena arena = Arena .ofConfined ()) {
85
+ MemorySegment segment1 = arena .allocate (10 );
86
+ MemorySegment segment2 = arena .allocate (10 );
87
+ assertEquals (segment1 .scope (), segment2 .scope ());
88
+ testDerivedBufferScope (segment1 );
89
+ }
90
+ }
91
+
92
+ @ Test
93
+ public void testSameNativeScope () {
94
+ MemorySegment segment1 = MemorySegment .ofAddress (42 );
95
+ MemorySegment segment2 = MemorySegment .ofAddress (43 );
96
+ assertEquals (segment1 .scope (), segment2 .scope ());
97
+ assertEquals (segment1 .scope (), segment2 .reinterpret (10 ).scope ());
98
+ assertNotEquals (segment1 .scope (), Arena .global ().scope ());
99
+ testDerivedBufferScope (segment1 .reinterpret (10 ));
100
+ }
101
+
102
+ @ Test
103
+ public void testSameLookupScope () {
104
+ SymbolLookup loaderLookup = SymbolLookup .loaderLookup ();
105
+ MemorySegment segment1 = loaderLookup .find ("f" ).get ();
106
+ MemorySegment segment2 = loaderLookup .find ("c" ).get ();
107
+ assertEquals (segment1 .scope (), segment2 .scope ());
108
+ testDerivedBufferScope (segment1 .reinterpret (10 ));
109
+ }
110
+
111
+ void testDerivedBufferScope (MemorySegment segment ) {
112
+ ByteBuffer buffer = segment .asByteBuffer ();
113
+ MemorySegment .Scope expectedScope = segment .scope ();
114
+ assertEquals (MemorySegment .ofBuffer (buffer ).scope (), expectedScope );
115
+ // buffer slices should have same scope
116
+ ByteBuffer slice = buffer .slice (0 , 2 );
117
+ assertEquals (expectedScope , MemorySegment .ofBuffer (slice ).scope ());
118
+ // buffer views should have same scope
119
+ IntBuffer view = buffer .asIntBuffer ();
120
+ assertEquals (expectedScope , MemorySegment .ofBuffer (view ).scope ());
121
+ }
122
+ }
0 commit comments