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