|
| 1 | +/* |
| 2 | + * Copyright (c) 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 | + * @test |
| 26 | + * @summary Test MemorySegment::fill |
| 27 | + * @run junit TestFill |
| 28 | + */ |
| 29 | + |
| 30 | +import org.junit.jupiter.params.ParameterizedTest; |
| 31 | +import org.junit.jupiter.params.provider.Arguments; |
| 32 | +import org.junit.jupiter.params.provider.MethodSource; |
| 33 | + |
| 34 | +import java.lang.foreign.Arena; |
| 35 | +import java.lang.foreign.ValueLayout; |
| 36 | +import java.util.Arrays; |
| 37 | +import java.util.concurrent.CompletableFuture; |
| 38 | +import java.util.concurrent.atomic.AtomicReference; |
| 39 | +import java.util.stream.IntStream; |
| 40 | +import java.util.stream.Stream; |
| 41 | + |
| 42 | +import static org.junit.jupiter.api.Assertions.*; |
| 43 | + |
| 44 | +final class TestFill { |
| 45 | + |
| 46 | + // Make sure negative values are treated as expected |
| 47 | + private static final byte VALUE = -71; |
| 48 | + |
| 49 | + @ParameterizedTest |
| 50 | + @MethodSource("sizes") |
| 51 | + void testFill(int len) { |
| 52 | + int offset = 16; |
| 53 | + int expandedLen = offset + MAX_SIZE + offset; |
| 54 | + |
| 55 | + // Make sure fill only affects the intended region XXXXXX |
| 56 | + // |
| 57 | + // ................XXXXXX................ |
| 58 | + // | offset | len | offset | |
| 59 | + |
| 60 | + try (var arena = Arena.ofConfined()) { |
| 61 | + var segment = arena.allocate(expandedLen); |
| 62 | + var slice = segment.asSlice(offset, len); |
| 63 | + slice.fill(VALUE); |
| 64 | + |
| 65 | + var expected = new byte[expandedLen]; |
| 66 | + Arrays.fill(expected, offset, offset + len, VALUE); |
| 67 | + |
| 68 | + // This checks the actual fill region as well as potential under and overflows |
| 69 | + assertArrayEquals(expected, segment.toArray(ValueLayout.JAVA_BYTE)); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @ParameterizedTest |
| 74 | + @MethodSource("values") |
| 75 | + void testValues(int value) { |
| 76 | + int size = 0b1111; |
| 77 | + try (var arena = Arena.ofConfined()) { |
| 78 | + var segment = arena.allocate(size); |
| 79 | + segment.fill((byte) value); |
| 80 | + assertTrue(segment.elements(ValueLayout.JAVA_BYTE) |
| 81 | + .map(s -> s.get(ValueLayout.JAVA_BYTE, 0)) |
| 82 | + .allMatch(v -> v == value), "Failed to fill with value " + value); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @ParameterizedTest |
| 87 | + @MethodSource("sizes") |
| 88 | + void testReadOnly(int len) { |
| 89 | + try (var arena = Arena.ofConfined()) { |
| 90 | + var segment = arena.allocate(10).asReadOnly(); |
| 91 | + assertThrows(IllegalArgumentException.class, () -> segment.fill(VALUE)); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @ParameterizedTest |
| 96 | + @MethodSource("sizes") |
| 97 | + void testConfinement(int len) { |
| 98 | + try (var arena = Arena.ofConfined()) { |
| 99 | + var segment = arena.allocate(10); |
| 100 | + AtomicReference<RuntimeException> ex = new AtomicReference<>(); |
| 101 | + CompletableFuture<Void> future = CompletableFuture.runAsync(() -> { |
| 102 | + try { |
| 103 | + segment.fill(VALUE); |
| 104 | + } catch (RuntimeException e) { |
| 105 | + ex.set(e); |
| 106 | + } |
| 107 | + }); |
| 108 | + future.join(); |
| 109 | + assertInstanceOf(WrongThreadException.class, ex.get()); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @ParameterizedTest |
| 114 | + @MethodSource("sizes") |
| 115 | + void testScope(int len) { |
| 116 | + var arena = Arena.ofConfined(); |
| 117 | + var segment = arena.allocate(len); |
| 118 | + arena.close(); |
| 119 | + assertThrows(IllegalStateException.class, () -> segment.fill(VALUE)); |
| 120 | + } |
| 121 | + |
| 122 | + private static final int MAX_SIZE = 1 << 10; |
| 123 | + |
| 124 | + private static Stream<Arguments> sizes() { |
| 125 | + return IntStream.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 23, 32, 63, 128, 256, 511, MAX_SIZE) |
| 126 | + .boxed() |
| 127 | + .map(Arguments::of); |
| 128 | + } |
| 129 | + |
| 130 | + private static Stream<Arguments> values() { |
| 131 | + return IntStream.rangeClosed(Byte.MIN_VALUE, Byte.MAX_VALUE) |
| 132 | + .boxed() |
| 133 | + .map(Arguments::of); |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments