1 /*
  2  * Copyright (c) 2025, 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 /*
 26  * @test id=serial
 27  * @summary Ensures that value arrays can get arraycopied properly with Serial.
 28  *          This test will likely crash if that is not the case.
 29  * @bug 8370479
 30  * @enablePreview
 31  * @requires vm.flagless
 32  * @requires vm.gc.Serial
 33  * @library /test/lib /
 34  * @modules java.base/jdk.internal.value
 35             java.management
 36  * @build jdk.test.whitebox.WhiteBox
 37  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 38  * @run junit/othervm/timeout=480 -Xint -XX:+UseSerialGC -XX:+UseCompressedOops -Xlog:gc*=info
 39                                   -XX:+UseCompressedClassPointers
 40                                   -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 41                                   runtime.valhalla.inlinetypes.FlatArrayCopyingTest
 42  */
 43 
 44 /*
 45  * @test id=parallel
 46  * @summary Ensures that value arrays can get arraycopied properly with Parallel.
 47  *          This test will likely crash if that is not the case.
 48  * @bug 8370479
 49  * @enablePreview
 50  * @requires vm.flagless
 51  * @requires vm.gc.Parallel
 52  * @library /test/lib /
 53  * @modules java.base/jdk.internal.value
 54             java.management
 55  * @build jdk.test.whitebox.WhiteBox
 56  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 57  * @run junit/othervm/timeout=480 -Xint -XX:+UseParallelGC -XX:+UseCompressedOops -Xlog:gc*=info
 58                                   -XX:ParallelGCThreads=1 -XX:-UseDynamicNumberOfGCThreads
 59                                   -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 60                                   runtime.valhalla.inlinetypes.FlatArrayCopyingTest
 61  */
 62 
 63 /*
 64  * @test id=g1
 65  * @summary Ensures that value arrays can get arraycopied properly with G1.
 66  *          This test will likely crash if that is not the case.
 67  * @bug 8370479
 68  * @enablePreview
 69  * @requires vm.flagless
 70  * @requires vm.gc.G1
 71  * @library /test/lib /
 72  * @modules java.base/jdk.internal.value
 73             java.management
 74  * @build jdk.test.whitebox.WhiteBox
 75  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 76  * @run junit/othervm/timeout=480 -XX:+UnlockDiagnosticVMOptions
 77                                   -Xint -XX:+UseG1GC -XX:+UseCompressedOops -Xlog:gc*=info
 78                                   -XX:ParallelGCThreads=1 -XX:ConcGCThreads=1 -XX:-UseDynamicNumberOfGCThreads
 79                                   -XX:-G1UseConcRefinement -XX:+UseCompressedClassPointers
 80                                   -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 81                                   runtime.valhalla.inlinetypes.FlatArrayCopyingTest
 82  */
 83 
 84 /*
 85  * @test id=shenandoah
 86  * @summary Ensures that value arrays can get arraycopied properly with Shenandoah.
 87  *          This test will likely crash if that is not the case.
 88  * @bug 8370479
 89  * @enablePreview
 90  * @requires vm.flagless
 91  * @requires vm.gc.Shenandoah
 92  * @library /test/lib /
 93  * @modules java.base/jdk.internal.value
 94             java.management
 95  * @build jdk.test.whitebox.WhiteBox
 96  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 97  * @run junit/othervm/timeout=480 -XX:+UnlockDiagnosticVMOptions
 98                                   -Xint -XX:+UseShenandoahGC -XX:+UseCompressedOops -Xlog:gc*=info
 99                                   -XX:ParallelGCThreads=1 -XX:ConcGCThreads=1 -XX:-UseDynamicNumberOfGCThreads
100                                   -XX:+UseCompressedClassPointers
101                                   -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
102                                   runtime.valhalla.inlinetypes.FlatArrayCopyingTest
103  */
104 
105 package runtime.valhalla.inlinetypes;
106 
107 import java.util.Arrays;
108 import jdk.internal.value.ValueClass;
109 import org.junit.jupiter.api.Assertions;
110 import org.junit.jupiter.api.Test;
111 
112 import jdk.test.whitebox.WhiteBox;
113 
114 import static org.junit.jupiter.api.Assertions.*;
115 
116 public final class FlatArrayCopyingTest {
117     private static final WhiteBox WB = WhiteBox.getWhiteBox();
118 
119     public static value record Element(Identity underlying) {}
120     public static class Identity {}
121 
122     @Test
123     public void testCopyingOften() {
124         Object[] array = ValueClass.newNullableAtomicArray(Element.class, 16);
125         for (int i = 0; i < 1_000_000; i++) {
126             if (i == array.length) {
127                 array = Arrays.copyOf(array, array.length << 1);
128             }
129             // Do a very "random" full GC.
130             if (i == 5123123) {
131                 WB.fullGC();
132             }
133             array[i] = new Element(new Identity());
134         }
135         // Smallest power of 2 that fits 1 million elements.
136         assertEquals(1_048_576, array.length);
137     }
138 
139 }