377 @Test(dataProvider="resizeOps")
378 public void testResizeRoundtripNative(Consumer<MemoryAddress> checker, Consumer<MemoryAddress> initializer, SequenceLayout seq) {
379 try (MemorySegment segment = MemorySegment.allocateNative(seq)) {
380 MemoryAddress first = segment.baseAddress();
381 initializer.accept(first);
382 MemoryAddress second = MemorySegment.ofByteBuffer(segment.asByteBuffer()).baseAddress();
383 checker.accept(second);
384 }
385 }
386
387 @Test(expectedExceptions = IllegalStateException.class)
388 public void testBufferOnClosedScope() {
389 MemorySegment leaked;
390 try (MemorySegment segment = MemorySegment.allocateNative(bytes)) {
391 leaked = segment;
392 }
393 leaked.asByteBuffer();
394 }
395
396 @Test(expectedExceptions = { UnsupportedOperationException.class,
397 OutOfMemoryError.class })
398 public void testTooBigForByteBuffer() {
399 if (System.getProperty("sun.arch.data.model").equals("32")) {
400 throw new SkipException("32-bit Unsafe does not support this allocation size");
401 }
402
403 MemorySegment.allocateNative((long) Integer.MAX_VALUE * 2).asByteBuffer();
404 }
405
406 @Test(dataProvider="resizeOps")
407 public void testCopyHeapToNative(Consumer<MemoryAddress> checker, Consumer<MemoryAddress> initializer, SequenceLayout seq) {
408 checkByteArrayAlignment(seq.elementLayout());
409 int bytes = (int)seq.byteSize();
410 try (MemorySegment nativeArray = MemorySegment.allocateNative(bytes);
411 MemorySegment heapArray = MemorySegment.ofArray(new byte[bytes])) {
412 initializer.accept(heapArray.baseAddress());
413 MemoryAddress.copy(heapArray.baseAddress(), nativeArray.baseAddress(), bytes);
414 checker.accept(nativeArray.baseAddress());
415 }
416 }
417
418 @Test(dataProvider="resizeOps")
419 public void testCopyNativeToHeap(Consumer<MemoryAddress> checker, Consumer<MemoryAddress> initializer, SequenceLayout seq) {
420 checkByteArrayAlignment(seq.elementLayout());
421 int bytes = (int)seq.byteSize();
422 try (MemorySegment nativeArray = MemorySegment.allocateNative(seq);
|
377 @Test(dataProvider="resizeOps")
378 public void testResizeRoundtripNative(Consumer<MemoryAddress> checker, Consumer<MemoryAddress> initializer, SequenceLayout seq) {
379 try (MemorySegment segment = MemorySegment.allocateNative(seq)) {
380 MemoryAddress first = segment.baseAddress();
381 initializer.accept(first);
382 MemoryAddress second = MemorySegment.ofByteBuffer(segment.asByteBuffer()).baseAddress();
383 checker.accept(second);
384 }
385 }
386
387 @Test(expectedExceptions = IllegalStateException.class)
388 public void testBufferOnClosedScope() {
389 MemorySegment leaked;
390 try (MemorySegment segment = MemorySegment.allocateNative(bytes)) {
391 leaked = segment;
392 }
393 leaked.asByteBuffer();
394 }
395
396 @Test(expectedExceptions = { UnsupportedOperationException.class,
397 IllegalArgumentException.class })
398 public void testTooBigForByteBuffer() {
399 MemorySegment.allocateNative((long) Integer.MAX_VALUE * 2).asByteBuffer();
400 }
401
402 @Test(dataProvider="resizeOps")
403 public void testCopyHeapToNative(Consumer<MemoryAddress> checker, Consumer<MemoryAddress> initializer, SequenceLayout seq) {
404 checkByteArrayAlignment(seq.elementLayout());
405 int bytes = (int)seq.byteSize();
406 try (MemorySegment nativeArray = MemorySegment.allocateNative(bytes);
407 MemorySegment heapArray = MemorySegment.ofArray(new byte[bytes])) {
408 initializer.accept(heapArray.baseAddress());
409 MemoryAddress.copy(heapArray.baseAddress(), nativeArray.baseAddress(), bytes);
410 checker.accept(nativeArray.baseAddress());
411 }
412 }
413
414 @Test(dataProvider="resizeOps")
415 public void testCopyNativeToHeap(Consumer<MemoryAddress> checker, Consumer<MemoryAddress> initializer, SequenceLayout seq) {
416 checkByteArrayAlignment(seq.elementLayout());
417 int bytes = (int)seq.byteSize();
418 try (MemorySegment nativeArray = MemorySegment.allocateNative(seq);
|