1 /*
2 * Copyright (c) 2026, 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 jdk.internal.value.ValueClass against preview-only things
27 * @modules java.base/jdk.internal.value
28 * @enablePreview
29 * @run junit ValueClassPreviewTest
30 */
31
32 import java.util.Optional;
33 import java.util.OptionalInt;
34
35 import jdk.internal.value.ValueClass;
36 import org.junit.jupiter.api.Test;
37
38 import static org.junit.jupiter.api.Assertions.*;
39
40 class ValueClassPreviewTest {
41 @Test
42 void testHasBinaryPayload() {
43 assertTrue(ValueClass.hasBinaryPayload(Integer.class));
44 assertTrue(ValueClass.hasBinaryPayload(OptionalInt.class));
45 assertFalse(ValueClass.hasBinaryPayload(Optional.class));
46
47 value record R1(int a, long b, float c) {}
48 assertTrue(ValueClass.hasBinaryPayload(R1.class));
49 value record R2(R1 a) {}
50 assertTrue(ValueClass.hasBinaryPayload(R2.class));
51 value record R3(String a) {}
52 assertFalse(ValueClass.hasBinaryPayload(R3.class));
53 value record R4(R3 a) {}
54 assertFalse(ValueClass.hasBinaryPayload(R4.class));
55 }
56
57 @Test
58 void testSpecialCopy() {
59 Object[] original = makeArray(4);
60 assertThrows(NegativeArraySizeException.class, () -> ValueClass.copyOfSpecialArray(original, -1));
61 assertArrayEquals(original, ValueClass.copyOfSpecialArray(original, 4));
62 Object[] padded = makeArray(5);
63 padded[4] = null;
64 assertArrayEquals(padded, ValueClass.copyOfSpecialArray(original, 5));
65 Object[] truncated = makeArray(3);
66 assertArrayEquals(truncated, ValueClass.copyOfSpecialArray(original, 3));
67 }
68
69 @Test
70 void testSpecialCopyOfRange() {
71 Object[] original = makeArray(4);
72 assertThrows(ArrayIndexOutOfBoundsException.class, () -> ValueClass.copyOfRangeSpecialArray(original, -1, 5));
73 assertThrows(ArrayIndexOutOfBoundsException.class, () -> ValueClass.copyOfRangeSpecialArray(original, 5, 5));
74 assertThrows(IllegalArgumentException.class, () -> ValueClass.copyOfRangeSpecialArray(original, 4, 2));
75 assertArrayEquals(original, ValueClass.copyOfRangeSpecialArray(original, 0, 4));
76 Object[] padded = makeArray(5);
77 padded[4] = null;
78 assertArrayEquals(padded, ValueClass.copyOfRangeSpecialArray(original, 0, 5));
79 Object[] truncated = makeArray(3);
80 assertArrayEquals(truncated, ValueClass.copyOfRangeSpecialArray(original, 0, 3));
81 }
82
83 private static Object[] makeArray(int l) {
84 Object[] arr = ValueClass.newNullableAtomicArray(Integer.class, l);
85 for (int i = 0; i < l; i++) {
86 arr[i] = Integer.valueOf(i);
87 }
88 return arr;
89 }
90 }