|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 | +import org.junit.jupiter.api.Test; |
| 25 | +import static org.junit.jupiter.api.Assertions.*; |
| 26 | + |
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | + |
| 29 | +/* |
| 30 | + * @test |
| 31 | + * @bug 8077559 8321180 |
| 32 | + * @summary Tests Compact String for maximum size strings |
| 33 | + * @requires os.maxMemory >= 7g & vm.bits == 64 |
| 34 | + * @run junit/othervm -XX:+CompactStrings -Xmx7g MaxSizeUTF16String |
| 35 | + * @run junit/othervm -XX:-CompactStrings -Xmx7g MaxSizeUTF16String |
| 36 | + */ |
| 37 | + |
| 38 | +public class MaxSizeUTF16String { |
| 39 | + |
| 40 | + private final static int MAX_UTF16_STRING_LENGTH = Integer.MAX_VALUE / 2; |
| 41 | + |
| 42 | + private final static String EXPECTED_OOME_MESSAGE = "UTF16 String size is"; |
| 43 | + |
| 44 | + // Create a large UTF-8 byte array with a single non-latin1 character |
| 45 | + private static byte[] generateUTF8Data(int byteSize) { |
| 46 | + byte[] nonAscii = "\u0100".getBytes(StandardCharsets.UTF_8); |
| 47 | + byte[] arr = new byte[byteSize]; |
| 48 | + System.arraycopy(nonAscii, 0, arr, 0, nonAscii.length); // non-latin1 at start |
| 49 | + return arr; |
| 50 | + } |
| 51 | + |
| 52 | + // Create a large char array with a single non-latin1 character |
| 53 | + private static char[] generateCharData(int size) { |
| 54 | + char[] nonAscii = "\u0100".toCharArray(); |
| 55 | + char[] arr = new char[size]; |
| 56 | + System.arraycopy(nonAscii, 0, arr, 0, nonAscii.length); // non-latin1 at start |
| 57 | + return arr; |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testMaxUTF8() { |
| 62 | + // Overly large UTF-8 data with 1 non-latin1 char |
| 63 | + final byte[] large_utf8_bytes = generateUTF8Data(MAX_UTF16_STRING_LENGTH + 1); |
| 64 | + int[] sizes = new int[] { |
| 65 | + MAX_UTF16_STRING_LENGTH + 1, |
| 66 | + MAX_UTF16_STRING_LENGTH, |
| 67 | + MAX_UTF16_STRING_LENGTH - 1}; |
| 68 | + for (int size : sizes) { |
| 69 | + System.err.println("Checking max UTF16 string len: " + size); |
| 70 | + try { |
| 71 | + // Use only part of the UTF-8 byte array |
| 72 | + new String(large_utf8_bytes, 0, size, StandardCharsets.UTF_8); |
| 73 | + if (size >= MAX_UTF16_STRING_LENGTH) { |
| 74 | + fail("Expected OutOfMemoryError with message prefix: " + EXPECTED_OOME_MESSAGE); |
| 75 | + } |
| 76 | + } catch (OutOfMemoryError ex) { |
| 77 | + if (!ex.getMessage().startsWith(EXPECTED_OOME_MESSAGE)) { |
| 78 | + fail("Failed: Not the OutOfMemoryError expected", ex); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testMaxCharArray() { |
| 86 | + // Overly large UTF-8 data with 1 non-latin1 char |
| 87 | + final char[] large_char_array = generateCharData(MAX_UTF16_STRING_LENGTH + 1); |
| 88 | + int[] sizes = new int[]{ |
| 89 | + MAX_UTF16_STRING_LENGTH + 1, |
| 90 | + MAX_UTF16_STRING_LENGTH, |
| 91 | + MAX_UTF16_STRING_LENGTH - 1}; |
| 92 | + for (int size : sizes) { |
| 93 | + System.err.println("Checking max UTF16 string len: " + size); |
| 94 | + try { |
| 95 | + // Large char array with 1 non-latin1 char |
| 96 | + new String(large_char_array, 0, size); |
| 97 | + if (size >= MAX_UTF16_STRING_LENGTH) { |
| 98 | + fail("Expected OutOfMemoryError with message prefix: " + EXPECTED_OOME_MESSAGE); |
| 99 | + } |
| 100 | + } catch (OutOfMemoryError ex) { |
| 101 | + if (!ex.getMessage().startsWith(EXPECTED_OOME_MESSAGE)) { |
| 102 | + throw new RuntimeException("Wrong exception message: " + ex.getMessage(), ex); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments