< prev index next >

test/jdk/java/foreign/TestAdaptVarHandles.java

Print this page

 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 /*
 26  * @test
 27  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false -Xverify:all TestAdaptVarHandles
 28  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true -Xverify:all TestAdaptVarHandles
 29  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false -Xverify:all TestAdaptVarHandles
 30  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true -Xverify:all TestAdaptVarHandles
 31  */
 32 
 33 import jdk.incubator.foreign.MemoryHandles;
 34 import jdk.incubator.foreign.MemoryLayout;
 35 import jdk.incubator.foreign.MemoryLayouts;
 36 import jdk.incubator.foreign.MemorySegment;
 37 import jdk.incubator.foreign.ResourceScope;
 38 import jdk.incubator.foreign.ValueLayout;
 39 import org.testng.annotations.*;
 40 import static org.testng.Assert.*;
 41 
 42 import java.lang.invoke.MethodHandle;
 43 import java.lang.invoke.MethodHandles;
 44 import java.lang.invoke.MethodType;
 45 import java.lang.invoke.VarHandle;
 46 import java.util.List;
 47 
 48 public class TestAdaptVarHandles {
 49 
 50     static MethodHandle S2I;
 51     static MethodHandle I2S;
 52     static MethodHandle CTX_I2S;
 53     static MethodHandle O2I;
 54     static MethodHandle I2O;
 55     static MethodHandle S2L;

 69             O2I = MethodHandles.explicitCastArguments(S2I, MethodType.methodType(int.class, Object.class));
 70             I2O = MethodHandles.explicitCastArguments(I2S, MethodType.methodType(Object.class, int.class));
 71             S2L = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "stringToLong", MethodType.methodType(long.class, String.class));
 72             S2L_EX = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "stringToLongException", MethodType.methodType(long.class, String.class));
 73             BASE_ADDR = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "baseAddress", MethodType.methodType(MemorySegment.class, MemorySegment.class));
 74             SUM_OFFSETS = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "sumOffsets", MethodType.methodType(long.class, long.class, long.class));
 75             VOID_FILTER = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "void_filter", MethodType.methodType(void.class, String.class));
 76 
 77             MethodHandle s2i_ex = MethodHandles.throwException(int.class, Throwable.class);
 78             s2i_ex = MethodHandles.insertArguments(s2i_ex, 0, new Throwable());
 79             S2I_EX = MethodHandles.dropArguments(s2i_ex, 0, String.class);
 80 
 81             MethodHandle i2s_ex = MethodHandles.throwException(String.class, Throwable.class);
 82             i2s_ex = MethodHandles.insertArguments(i2s_ex, 0, new Throwable());
 83             I2S_EX = MethodHandles.dropArguments(i2s_ex, 0, int.class);
 84         } catch (Throwable ex) {
 85             throw new ExceptionInInitializerError();
 86         }
 87     }
 88 
 89     static final VarHandle intHandleIndexed = MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_INT)
 90             .varHandle(int.class, MemoryLayout.PathElement.sequenceElement());
 91 
 92     static final VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
 93 
 94     static final VarHandle floatHandle = MemoryLayouts.JAVA_FLOAT.varHandle(float.class);
 95 
 96     @Test
 97     public void testFilterValue() throws Throwable {
 98         ValueLayout layout = MemoryLayouts.JAVA_INT;
 99         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
100         VarHandle intHandle = layout.varHandle(int.class);
101         VarHandle i2SHandle = MemoryHandles.filterValue(intHandle, S2I, I2S);
102         i2SHandle.set(segment, "1");
103         String oldValue = (String)i2SHandle.getAndAdd(segment, "42");
104         assertEquals(oldValue, "1");
105         String value = (String)i2SHandle.get(segment);
106         assertEquals(value, "43");
107         boolean swapped = (boolean)i2SHandle.compareAndSet(segment, "43", "12");
108         assertTrue(swapped);
109         oldValue = (String)i2SHandle.compareAndExchange(segment, "12", "42");
110         assertEquals(oldValue, "12");
111         value = (String)i2SHandle.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment);
112         assertEquals(value, "42");
113     }
114 
115     @Test
116     public void testFilterValueComposite() throws Throwable {
117         ValueLayout layout = MemoryLayouts.JAVA_INT;
118         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
119         VarHandle intHandle = layout.varHandle(int.class);
120         MethodHandle CTX_S2I = MethodHandles.dropArguments(S2I, 0, String.class, String.class);
121         VarHandle i2SHandle = MemoryHandles.filterValue(intHandle, CTX_S2I, CTX_I2S);
122         i2SHandle = MemoryHandles.insertCoordinates(i2SHandle, 1, "a", "b");
123         i2SHandle.set(segment, "1");
124         String oldValue = (String)i2SHandle.getAndAdd(segment, "42");
125         assertEquals(oldValue, "ab1");
126         String value = (String)i2SHandle.get(segment);
127         assertEquals(value, "ab43");
128         boolean swapped = (boolean)i2SHandle.compareAndSet(segment, "43", "12");
129         assertTrue(swapped);
130         oldValue = (String)i2SHandle.compareAndExchange(segment, "12", "42");
131         assertEquals(oldValue, "ab12");
132         value = (String)i2SHandle.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment);
133         assertEquals(value, "ab42");
134     }
135 
136     @Test
137     public void testFilterValueLoose() throws Throwable {
138         ValueLayout layout = MemoryLayouts.JAVA_INT;
139         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
140         VarHandle intHandle = layout.varHandle(int.class);
141         VarHandle i2SHandle = MemoryHandles.filterValue(intHandle, O2I, I2O);
142         i2SHandle.set(segment, "1");
143         String oldValue = (String)i2SHandle.getAndAdd(segment, "42");
144         assertEquals(oldValue, "1");
145         String value = (String)i2SHandle.get(segment);
146         assertEquals(value, "43");
147         boolean swapped = (boolean)i2SHandle.compareAndSet(segment, "43", "12");
148         assertTrue(swapped);
149         oldValue = (String)i2SHandle.compareAndExchange(segment, "12", "42");
150         assertEquals(oldValue, "12");
151         value = (String)(Object)i2SHandle.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment);
152         assertEquals(value, "42");
153     }
154 
155     @Test(expectedExceptions = IllegalArgumentException.class)
156     public void testBadFilterCarrier() {
157         MemoryHandles.filterValue(floatHandle, S2I, I2S);
158     }
159 
160     @Test(expectedExceptions = IllegalArgumentException.class)
161     public void testBadFilterUnboxArity() {
162         VarHandle floatHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
163         MemoryHandles.filterValue(floatHandle, S2I.bindTo(""), I2S);
164     }
165 
166     @Test(expectedExceptions = IllegalArgumentException.class)
167     public void testBadFilterBoxArity() {
168         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
169         MemoryHandles.filterValue(intHandle, S2I, I2S.bindTo(42));
170     }
171 
172     @Test(expectedExceptions = IllegalArgumentException.class)
173     public void testBadFilterBoxPrefixCoordinates() {
174         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
175         MemoryHandles.filterValue(intHandle,
176                 MethodHandles.dropArguments(S2I, 1, int.class),
177                 MethodHandles.dropArguments(I2S, 1, long.class));
178     }
179 
180     @Test(expectedExceptions = IllegalArgumentException.class)
181     public void testBadFilterBoxException() {
182         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
183         MemoryHandles.filterValue(intHandle, I2S, S2L_EX);
184     }
185 
186     @Test(expectedExceptions = IllegalArgumentException.class)
187     public void testBadFilterUnboxException() {
188         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
189         MemoryHandles.filterValue(intHandle, S2L_EX, I2S);
190     }
191 
192     @Test(expectedExceptions = IllegalArgumentException.class)
193     public void testBadFilterBoxHandleException() {
194         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
195         MemoryHandles.filterValue(intHandle, S2I, I2S_EX);





196     }
197 
198     @Test(expectedExceptions = IllegalArgumentException.class)
199     public void testBadFilterUnboxHandleException() {
200         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
201         MemoryHandles.filterValue(intHandle, S2I_EX, I2S);




202     }
203 
204     @Test
205     public void testFilterCoordinates() throws Throwable {
206         ValueLayout layout = MemoryLayouts.JAVA_INT;
207         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
208         VarHandle intHandle_longIndex = MemoryHandles.filterCoordinates(intHandleIndexed, 0, BASE_ADDR, S2L);
209         intHandle_longIndex.set(segment, "0", 1);
210         int oldValue = (int)intHandle_longIndex.getAndAdd(segment, "0", 42);
211         assertEquals(oldValue, 1);
212         int value = (int)intHandle_longIndex.get(segment, "0");
213         assertEquals(value, 43);
214         boolean swapped = (boolean)intHandle_longIndex.compareAndSet(segment, "0", 43, 12);
215         assertTrue(swapped);
216         oldValue = (int)intHandle_longIndex.compareAndExchange(segment, "0", 12, 42);
217         assertEquals(oldValue, 12);
218         value = (int)intHandle_longIndex.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment, "0");
219         assertEquals(value, 42);
220     }
221 
222     @Test(expectedExceptions = IllegalArgumentException.class)
223     public void testBadFilterCoordinatesNegativePos() {
224         MemoryHandles.filterCoordinates(intHandle, -1, SUM_OFFSETS);
225     }
226 

229         MemoryHandles.filterCoordinates(intHandle, 1, SUM_OFFSETS);
230     }
231 
232     @Test(expectedExceptions = IllegalArgumentException.class)
233     public void testBadFilterCoordinatesWrongFilterType() {
234         MemoryHandles.filterCoordinates(intHandleIndexed, 1, S2I);
235     }
236 
237     @Test(expectedExceptions = IllegalArgumentException.class)
238     public void testBadFilterCoordinatesWrongFilterException() {
239         MemoryHandles.filterCoordinates(intHandleIndexed, 1, S2L_EX);
240     }
241 
242     @Test(expectedExceptions = IllegalArgumentException.class)
243     public void testBadFilterCoordinatesTooManyFilters() {
244         MemoryHandles.filterCoordinates(intHandleIndexed, 1, S2L, S2L);
245     }
246 
247     @Test
248     public void testInsertCoordinates() throws Throwable {
249         ValueLayout layout = MemoryLayouts.JAVA_INT;
250         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
251         VarHandle intHandle_longIndex = MemoryHandles.insertCoordinates(intHandleIndexed, 0, segment, 0L);
252         intHandle_longIndex.set(1);
253         int oldValue = (int)intHandle_longIndex.getAndAdd(42);
254         assertEquals(oldValue, 1);
255         int value = (int)intHandle_longIndex.get();
256         assertEquals(value, 43);
257         boolean swapped = (boolean)intHandle_longIndex.compareAndSet(43, 12);
258         assertTrue(swapped);
259         oldValue = (int)intHandle_longIndex.compareAndExchange(12, 42);
260         assertEquals(oldValue, 12);
261         value = (int)intHandle_longIndex.toMethodHandle(VarHandle.AccessMode.GET).invokeExact();
262         assertEquals(value, 42);
263     }
264 
265     @Test(expectedExceptions = IllegalArgumentException.class)
266     public void testBadInsertCoordinatesNegativePos() {
267         MemoryHandles.insertCoordinates(intHandle, -1, 42);
268     }
269 
270     @Test(expectedExceptions = IllegalArgumentException.class)
271     public void testBadInsertCoordinatesPosTooBig() {
272         MemoryHandles.insertCoordinates(intHandle, 1, 42);
273     }
274 
275     @Test(expectedExceptions = ClassCastException.class)
276     public void testBadInsertCoordinatesWrongCoordinateType() {
277         MemoryHandles.insertCoordinates(intHandleIndexed, 1, "Hello");
278     }
279 
280     @Test(expectedExceptions = IllegalArgumentException.class)
281     public void testBadInsertCoordinatesTooManyValues() {
282         MemoryHandles.insertCoordinates(intHandleIndexed, 1, 0L, 0L);
283     }
284 
285     @Test
286     public void testPermuteCoordinates() throws Throwable {
287         ValueLayout layout = MemoryLayouts.JAVA_INT;
288         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
289         VarHandle intHandle_swap = MemoryHandles.permuteCoordinates(intHandleIndexed,
290                 List.of(long.class, MemorySegment.class), 1, 0);
291         intHandle_swap.set(0L, segment, 1);
292         int oldValue = (int)intHandle_swap.getAndAdd(0L, segment, 42);
293         assertEquals(oldValue, 1);
294         int value = (int)intHandle_swap.get(0L, segment);
295         assertEquals(value, 43);
296         boolean swapped = (boolean)intHandle_swap.compareAndSet(0L, segment, 43, 12);
297         assertTrue(swapped);
298         oldValue = (int)intHandle_swap.compareAndExchange(0L, segment, 12, 42);
299         assertEquals(oldValue, 12);
300         value = (int)intHandle_swap.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(0L, segment);
301         assertEquals(value, 42);
302     }
303 
304     @Test(expectedExceptions = IllegalArgumentException.class)
305     public void testBadPermuteCoordinatesTooManyCoordinates() {
306         MemoryHandles.permuteCoordinates(intHandle, List.of(int.class, int.class), new int[2]);
307     }
308 
309     @Test(expectedExceptions = IllegalArgumentException.class)
310     public void testBadPermuteCoordinatesTooFewCoordinates() {
311         MemoryHandles.permuteCoordinates(intHandle, List.of());
312     }
313 
314     @Test(expectedExceptions = IllegalArgumentException.class)
315     public void testBadPermuteCoordinatesIndexTooBig() {
316         MemoryHandles.permuteCoordinates(intHandle, List.of(int.class, int.class), 3);
317     }
318 
319     @Test(expectedExceptions = IllegalArgumentException.class)
320     public void testBadPermuteCoordinatesIndexTooSmall() {
321         MemoryHandles.permuteCoordinates(intHandle, List.of(int.class, int.class), -1);
322     }
323 
324     @Test
325     public void testCollectCoordinates() throws Throwable {
326         ValueLayout layout = MemoryLayouts.JAVA_INT;
327         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
328         VarHandle intHandle_sum = MemoryHandles.collectCoordinates(intHandleIndexed, 1, SUM_OFFSETS);
329         intHandle_sum.set(segment, -2L, 2L, 1);
330         int oldValue = (int)intHandle_sum.getAndAdd(segment, -2L, 2L, 42);
331         assertEquals(oldValue, 1);
332         int value = (int)intHandle_sum.get(segment, -2L, 2L);
333         assertEquals(value, 43);
334         boolean swapped = (boolean)intHandle_sum.compareAndSet(segment, -2L, 2L, 43, 12);
335         assertTrue(swapped);
336         oldValue = (int)intHandle_sum.compareAndExchange(segment, -2L, 2L, 12, 42);
337         assertEquals(oldValue, 12);
338         value = (int)intHandle_sum.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment, -2L, 2L);
339         assertEquals(value, 42);
340     }
341 
342     @Test(expectedExceptions = IllegalArgumentException.class)
343     public void testBadCollectCoordinatesNegativePos() {
344         MemoryHandles.collectCoordinates(intHandle, -1, SUM_OFFSETS);
345     }
346 

349         MemoryHandles.collectCoordinates(intHandle, 1, SUM_OFFSETS);
350     }
351 
352     @Test(expectedExceptions = IllegalArgumentException.class)
353     public void testBadCollectCoordinatesWrongFilterType() {
354         MemoryHandles.collectCoordinates(intHandle, 0, SUM_OFFSETS);
355     }
356 
357     @Test(expectedExceptions = IllegalArgumentException.class)
358     public void testBadCollectCoordinatesWrongVoidFilterType() {
359         MemoryHandles.collectCoordinates(intHandle, 0, VOID_FILTER);
360     }
361 
362     @Test(expectedExceptions = IllegalArgumentException.class)
363     public void testBadCollectCoordinatesWrongFilterException() {
364         MemoryHandles.collectCoordinates(intHandle, 0, S2L_EX);
365     }
366 
367     @Test
368     public void testDropCoordinates() throws Throwable {
369         ValueLayout layout = MemoryLayouts.JAVA_INT;
370         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
371         VarHandle intHandle_dummy = MemoryHandles.dropCoordinates(intHandleIndexed, 1, float.class, String.class);
372         intHandle_dummy.set(segment, 1f, "hello", 0L, 1);
373         int oldValue = (int)intHandle_dummy.getAndAdd(segment, 1f, "hello", 0L, 42);
374         assertEquals(oldValue, 1);
375         int value = (int)intHandle_dummy.get(segment, 1f, "hello", 0L);
376         assertEquals(value, 43);
377         boolean swapped = (boolean)intHandle_dummy.compareAndSet(segment, 1f, "hello", 0L, 43, 12);
378         assertTrue(swapped);
379         oldValue = (int)intHandle_dummy.compareAndExchange(segment, 1f, "hello", 0L, 12, 42);
380         assertEquals(oldValue, 12);
381         value = (int)intHandle_dummy.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment, 1f, "hello", 0L);
382         assertEquals(value, 42);
383     }
384 
385     @Test(expectedExceptions = IllegalArgumentException.class)
386     public void testBadDropCoordinatesNegativePos() {
387         MemoryHandles.dropCoordinates(intHandle, -1);
388     }
389 

 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 /*
 26  * @test
 27  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false -Xverify:all TestAdaptVarHandles
 28  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true -Xverify:all TestAdaptVarHandles
 29  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false -Xverify:all TestAdaptVarHandles
 30  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true -Xverify:all TestAdaptVarHandles
 31  */
 32 
 33 import jdk.incubator.foreign.MemoryHandles;
 34 import jdk.incubator.foreign.MemoryLayout;

 35 import jdk.incubator.foreign.MemorySegment;
 36 import jdk.incubator.foreign.ResourceScope;
 37 import jdk.incubator.foreign.ValueLayout;
 38 import org.testng.annotations.*;
 39 import static org.testng.Assert.*;
 40 
 41 import java.lang.invoke.MethodHandle;
 42 import java.lang.invoke.MethodHandles;
 43 import java.lang.invoke.MethodType;
 44 import java.lang.invoke.VarHandle;
 45 import java.util.List;
 46 
 47 public class TestAdaptVarHandles {
 48 
 49     static MethodHandle S2I;
 50     static MethodHandle I2S;
 51     static MethodHandle CTX_I2S;
 52     static MethodHandle O2I;
 53     static MethodHandle I2O;
 54     static MethodHandle S2L;

 68             O2I = MethodHandles.explicitCastArguments(S2I, MethodType.methodType(int.class, Object.class));
 69             I2O = MethodHandles.explicitCastArguments(I2S, MethodType.methodType(Object.class, int.class));
 70             S2L = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "stringToLong", MethodType.methodType(long.class, String.class));
 71             S2L_EX = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "stringToLongException", MethodType.methodType(long.class, String.class));
 72             BASE_ADDR = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "baseAddress", MethodType.methodType(MemorySegment.class, MemorySegment.class));
 73             SUM_OFFSETS = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "sumOffsets", MethodType.methodType(long.class, long.class, long.class));
 74             VOID_FILTER = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "void_filter", MethodType.methodType(void.class, String.class));
 75 
 76             MethodHandle s2i_ex = MethodHandles.throwException(int.class, Throwable.class);
 77             s2i_ex = MethodHandles.insertArguments(s2i_ex, 0, new Throwable());
 78             S2I_EX = MethodHandles.dropArguments(s2i_ex, 0, String.class);
 79 
 80             MethodHandle i2s_ex = MethodHandles.throwException(String.class, Throwable.class);
 81             i2s_ex = MethodHandles.insertArguments(i2s_ex, 0, new Throwable());
 82             I2S_EX = MethodHandles.dropArguments(i2s_ex, 0, int.class);
 83         } catch (Throwable ex) {
 84             throw new ExceptionInInitializerError();
 85         }
 86     }
 87 
 88     static final VarHandle intHandleIndexed = MemoryLayout.sequenceLayout(ValueLayout.JAVA_INT)
 89             .varHandle(MemoryLayout.PathElement.sequenceElement());
 90 
 91     static final VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
 92 
 93     static final VarHandle floatHandle = ValueLayout.JAVA_FLOAT.varHandle();
 94 
 95     @Test
 96     public void testFilterValue() throws Throwable {
 97         ValueLayout layout = ValueLayout.JAVA_INT;
 98         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
 99         VarHandle intHandle = layout.varHandle();
100         VarHandle i2SHandle = MemoryHandles.filterValue(intHandle, S2I, I2S);
101         i2SHandle.set(segment, "1");
102         String oldValue = (String)i2SHandle.getAndAdd(segment, "42");
103         assertEquals(oldValue, "1");
104         String value = (String)i2SHandle.get(segment);
105         assertEquals(value, "43");
106         boolean swapped = (boolean)i2SHandle.compareAndSet(segment, "43", "12");
107         assertTrue(swapped);
108         oldValue = (String)i2SHandle.compareAndExchange(segment, "12", "42");
109         assertEquals(oldValue, "12");
110         value = (String)i2SHandle.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment);
111         assertEquals(value, "42");
112     }
113 
114     @Test
115     public void testFilterValueComposite() throws Throwable {
116         ValueLayout layout = ValueLayout.JAVA_INT;
117         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
118         VarHandle intHandle = layout.varHandle();
119         MethodHandle CTX_S2I = MethodHandles.dropArguments(S2I, 0, String.class, String.class);
120         VarHandle i2SHandle = MemoryHandles.filterValue(intHandle, CTX_S2I, CTX_I2S);
121         i2SHandle = MemoryHandles.insertCoordinates(i2SHandle, 1, "a", "b");
122         i2SHandle.set(segment, "1");
123         String oldValue = (String)i2SHandle.getAndAdd(segment, "42");
124         assertEquals(oldValue, "ab1");
125         String value = (String)i2SHandle.get(segment);
126         assertEquals(value, "ab43");
127         boolean swapped = (boolean)i2SHandle.compareAndSet(segment, "43", "12");
128         assertTrue(swapped);
129         oldValue = (String)i2SHandle.compareAndExchange(segment, "12", "42");
130         assertEquals(oldValue, "ab12");
131         value = (String)i2SHandle.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment);
132         assertEquals(value, "ab42");
133     }
134 
135     @Test
136     public void testFilterValueLoose() throws Throwable {
137         ValueLayout layout = ValueLayout.JAVA_INT;
138         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
139         VarHandle intHandle = layout.varHandle();
140         VarHandle i2SHandle = MemoryHandles.filterValue(intHandle, O2I, I2O);
141         i2SHandle.set(segment, "1");
142         String oldValue = (String)i2SHandle.getAndAdd(segment, "42");
143         assertEquals(oldValue, "1");
144         String value = (String)i2SHandle.get(segment);
145         assertEquals(value, "43");
146         boolean swapped = (boolean)i2SHandle.compareAndSet(segment, "43", "12");
147         assertTrue(swapped);
148         oldValue = (String)i2SHandle.compareAndExchange(segment, "12", "42");
149         assertEquals(oldValue, "12");
150         value = (String)(Object)i2SHandle.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment);
151         assertEquals(value, "42");
152     }
153 
154     @Test(expectedExceptions = IllegalArgumentException.class)
155     public void testBadFilterCarrier() {
156         MemoryHandles.filterValue(floatHandle, S2I, I2S);
157     }
158 
159     @Test(expectedExceptions = IllegalArgumentException.class)
160     public void testBadFilterUnboxArity() {
161         VarHandle floatHandle = ValueLayout.JAVA_INT.varHandle();
162         MemoryHandles.filterValue(floatHandle, S2I.bindTo(""), I2S);
163     }
164 
165     @Test(expectedExceptions = IllegalArgumentException.class)
166     public void testBadFilterBoxArity() {
167         VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
168         MemoryHandles.filterValue(intHandle, S2I, I2S.bindTo(42));
169     }
170 
171     @Test(expectedExceptions = IllegalArgumentException.class)
172     public void testBadFilterBoxPrefixCoordinates() {
173         VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
174         MemoryHandles.filterValue(intHandle,
175                 MethodHandles.dropArguments(S2I, 1, int.class),
176                 MethodHandles.dropArguments(I2S, 1, long.class));
177     }
178 
179     @Test(expectedExceptions = IllegalArgumentException.class)
180     public void testBadFilterBoxException() {
181         VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
182         MemoryHandles.filterValue(intHandle, I2S, S2L_EX);
183     }
184 
185     @Test(expectedExceptions = IllegalArgumentException.class)
186     public void testBadFilterUnboxException() {
187         VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
188         MemoryHandles.filterValue(intHandle, S2L_EX, I2S);
189     }
190 
191     @Test(expectedExceptions = IllegalStateException.class)
192     public void testBadFilterBoxHandleException() {
193         VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
194         VarHandle vh = MemoryHandles.filterValue(intHandle, S2I, I2S_EX);
195         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
196             MemorySegment seg = MemorySegment.allocateNative(ValueLayout.JAVA_INT, scope);
197             vh.set(seg, "42");
198             String x = (String) vh.get(seg); // should throw
199         }
200     }
201 
202     @Test(expectedExceptions = IllegalStateException.class)
203     public void testBadFilterUnboxHandleException() {
204         VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
205         VarHandle vh = MemoryHandles.filterValue(intHandle, S2I_EX, I2S);
206         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
207             MemorySegment seg = MemorySegment.allocateNative(ValueLayout.JAVA_INT, scope);
208             vh.set(seg, "42"); // should throw
209         }
210     }
211 
212     @Test
213     public void testFilterCoordinates() throws Throwable {
214         ValueLayout layout = ValueLayout.JAVA_INT;
215         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
216         VarHandle intHandle_longIndex = MemoryHandles.filterCoordinates(intHandleIndexed, 0, BASE_ADDR, S2L);
217         intHandle_longIndex.set(segment, "0", 1);
218         int oldValue = (int)intHandle_longIndex.getAndAdd(segment, "0", 42);
219         assertEquals(oldValue, 1);
220         int value = (int)intHandle_longIndex.get(segment, "0");
221         assertEquals(value, 43);
222         boolean swapped = (boolean)intHandle_longIndex.compareAndSet(segment, "0", 43, 12);
223         assertTrue(swapped);
224         oldValue = (int)intHandle_longIndex.compareAndExchange(segment, "0", 12, 42);
225         assertEquals(oldValue, 12);
226         value = (int)intHandle_longIndex.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment, "0");
227         assertEquals(value, 42);
228     }
229 
230     @Test(expectedExceptions = IllegalArgumentException.class)
231     public void testBadFilterCoordinatesNegativePos() {
232         MemoryHandles.filterCoordinates(intHandle, -1, SUM_OFFSETS);
233     }
234 

237         MemoryHandles.filterCoordinates(intHandle, 1, SUM_OFFSETS);
238     }
239 
240     @Test(expectedExceptions = IllegalArgumentException.class)
241     public void testBadFilterCoordinatesWrongFilterType() {
242         MemoryHandles.filterCoordinates(intHandleIndexed, 1, S2I);
243     }
244 
245     @Test(expectedExceptions = IllegalArgumentException.class)
246     public void testBadFilterCoordinatesWrongFilterException() {
247         MemoryHandles.filterCoordinates(intHandleIndexed, 1, S2L_EX);
248     }
249 
250     @Test(expectedExceptions = IllegalArgumentException.class)
251     public void testBadFilterCoordinatesTooManyFilters() {
252         MemoryHandles.filterCoordinates(intHandleIndexed, 1, S2L, S2L);
253     }
254 
255     @Test
256     public void testInsertCoordinates() throws Throwable {
257         ValueLayout layout = ValueLayout.JAVA_INT;
258         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
259         VarHandle intHandle_longIndex = MemoryHandles.insertCoordinates(intHandleIndexed, 0, segment, 0L);
260         intHandle_longIndex.set(1);
261         int oldValue = (int)intHandle_longIndex.getAndAdd(42);
262         assertEquals(oldValue, 1);
263         int value = (int)intHandle_longIndex.get();
264         assertEquals(value, 43);
265         boolean swapped = (boolean)intHandle_longIndex.compareAndSet(43, 12);
266         assertTrue(swapped);
267         oldValue = (int)intHandle_longIndex.compareAndExchange(12, 42);
268         assertEquals(oldValue, 12);
269         value = (int)intHandle_longIndex.toMethodHandle(VarHandle.AccessMode.GET).invokeExact();
270         assertEquals(value, 42);
271     }
272 
273     @Test(expectedExceptions = IllegalArgumentException.class)
274     public void testBadInsertCoordinatesNegativePos() {
275         MemoryHandles.insertCoordinates(intHandle, -1, 42);
276     }
277 
278     @Test(expectedExceptions = IllegalArgumentException.class)
279     public void testBadInsertCoordinatesPosTooBig() {
280         MemoryHandles.insertCoordinates(intHandle, 1, 42);
281     }
282 
283     @Test(expectedExceptions = ClassCastException.class)
284     public void testBadInsertCoordinatesWrongCoordinateType() {
285         MemoryHandles.insertCoordinates(intHandleIndexed, 1, "Hello");
286     }
287 
288     @Test(expectedExceptions = IllegalArgumentException.class)
289     public void testBadInsertCoordinatesTooManyValues() {
290         MemoryHandles.insertCoordinates(intHandleIndexed, 1, 0L, 0L);
291     }
292 
293     @Test
294     public void testPermuteCoordinates() throws Throwable {
295         ValueLayout layout = ValueLayout.JAVA_INT;
296         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
297         VarHandle intHandle_swap = MemoryHandles.permuteCoordinates(intHandleIndexed,
298                 List.of(long.class, MemorySegment.class), 1, 0);
299         intHandle_swap.set(0L, segment, 1);
300         int oldValue = (int)intHandle_swap.getAndAdd(0L, segment, 42);
301         assertEquals(oldValue, 1);
302         int value = (int)intHandle_swap.get(0L, segment);
303         assertEquals(value, 43);
304         boolean swapped = (boolean)intHandle_swap.compareAndSet(0L, segment, 43, 12);
305         assertTrue(swapped);
306         oldValue = (int)intHandle_swap.compareAndExchange(0L, segment, 12, 42);
307         assertEquals(oldValue, 12);
308         value = (int)intHandle_swap.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(0L, segment);
309         assertEquals(value, 42);
310     }
311 
312     @Test(expectedExceptions = IllegalArgumentException.class)
313     public void testBadPermuteCoordinatesTooManyCoordinates() {
314         MemoryHandles.permuteCoordinates(intHandle, List.of(int.class, int.class), new int[2]);
315     }
316 
317     @Test(expectedExceptions = IllegalArgumentException.class)
318     public void testBadPermuteCoordinatesTooFewCoordinates() {
319         MemoryHandles.permuteCoordinates(intHandle, List.of());
320     }
321 
322     @Test(expectedExceptions = IllegalArgumentException.class)
323     public void testBadPermuteCoordinatesIndexTooBig() {
324         MemoryHandles.permuteCoordinates(intHandle, List.of(int.class, int.class), 3);
325     }
326 
327     @Test(expectedExceptions = IllegalArgumentException.class)
328     public void testBadPermuteCoordinatesIndexTooSmall() {
329         MemoryHandles.permuteCoordinates(intHandle, List.of(int.class, int.class), -1);
330     }
331 
332     @Test
333     public void testCollectCoordinates() throws Throwable {
334         ValueLayout layout = ValueLayout.JAVA_INT;
335         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
336         VarHandle intHandle_sum = MemoryHandles.collectCoordinates(intHandleIndexed, 1, SUM_OFFSETS);
337         intHandle_sum.set(segment, -2L, 2L, 1);
338         int oldValue = (int)intHandle_sum.getAndAdd(segment, -2L, 2L, 42);
339         assertEquals(oldValue, 1);
340         int value = (int)intHandle_sum.get(segment, -2L, 2L);
341         assertEquals(value, 43);
342         boolean swapped = (boolean)intHandle_sum.compareAndSet(segment, -2L, 2L, 43, 12);
343         assertTrue(swapped);
344         oldValue = (int)intHandle_sum.compareAndExchange(segment, -2L, 2L, 12, 42);
345         assertEquals(oldValue, 12);
346         value = (int)intHandle_sum.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment, -2L, 2L);
347         assertEquals(value, 42);
348     }
349 
350     @Test(expectedExceptions = IllegalArgumentException.class)
351     public void testBadCollectCoordinatesNegativePos() {
352         MemoryHandles.collectCoordinates(intHandle, -1, SUM_OFFSETS);
353     }
354 

357         MemoryHandles.collectCoordinates(intHandle, 1, SUM_OFFSETS);
358     }
359 
360     @Test(expectedExceptions = IllegalArgumentException.class)
361     public void testBadCollectCoordinatesWrongFilterType() {
362         MemoryHandles.collectCoordinates(intHandle, 0, SUM_OFFSETS);
363     }
364 
365     @Test(expectedExceptions = IllegalArgumentException.class)
366     public void testBadCollectCoordinatesWrongVoidFilterType() {
367         MemoryHandles.collectCoordinates(intHandle, 0, VOID_FILTER);
368     }
369 
370     @Test(expectedExceptions = IllegalArgumentException.class)
371     public void testBadCollectCoordinatesWrongFilterException() {
372         MemoryHandles.collectCoordinates(intHandle, 0, S2L_EX);
373     }
374 
375     @Test
376     public void testDropCoordinates() throws Throwable {
377         ValueLayout layout = ValueLayout.JAVA_INT;
378         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
379         VarHandle intHandle_dummy = MemoryHandles.dropCoordinates(intHandleIndexed, 1, float.class, String.class);
380         intHandle_dummy.set(segment, 1f, "hello", 0L, 1);
381         int oldValue = (int)intHandle_dummy.getAndAdd(segment, 1f, "hello", 0L, 42);
382         assertEquals(oldValue, 1);
383         int value = (int)intHandle_dummy.get(segment, 1f, "hello", 0L);
384         assertEquals(value, 43);
385         boolean swapped = (boolean)intHandle_dummy.compareAndSet(segment, 1f, "hello", 0L, 43, 12);
386         assertTrue(swapped);
387         oldValue = (int)intHandle_dummy.compareAndExchange(segment, 1f, "hello", 0L, 12, 42);
388         assertEquals(oldValue, 12);
389         value = (int)intHandle_dummy.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment, 1f, "hello", 0L);
390         assertEquals(value, 42);
391     }
392 
393     @Test(expectedExceptions = IllegalArgumentException.class)
394     public void testBadDropCoordinatesNegativePos() {
395         MemoryHandles.dropCoordinates(intHandle, -1);
396     }
397 
< prev index next >