|
| 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.testng.annotations.Test; |
| 25 | + |
| 26 | +import static org.testng.Assert.assertEquals; |
| 27 | + |
| 28 | +import java.util.Arrays; |
| 29 | + |
| 30 | +/** |
| 31 | + * @test |
| 32 | + * @bug 8302323 |
| 33 | + * @summary Test StringBuilder.repeat sanity tests |
| 34 | + * @run testng/othervm -XX:-CompactStrings StringBuilderRepeat |
| 35 | + * @run testng/othervm -XX:+CompactStrings StringBuilderRepeat |
| 36 | + */ |
| 37 | +@Test |
| 38 | +public class StringBuilderRepeat { |
| 39 | + private static class MyChars implements CharSequence { |
| 40 | + private static final char[] DATA = new char[] { 'a', 'b', 'c' }; |
| 41 | + |
| 42 | + @Override |
| 43 | + public int length() { |
| 44 | + return DATA.length; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public char charAt(int index) { |
| 49 | + return DATA[index]; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public CharSequence subSequence(int start, int end) { |
| 54 | + return new String(Arrays.copyOfRange(DATA, start, end)); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private static final MyChars MYCHARS = new MyChars(); |
| 59 | + |
| 60 | + public void sanity() { |
| 61 | + StringBuilder sb = new StringBuilder(); |
| 62 | + // prime the StringBuilder |
| 63 | + sb.append("repeat"); |
| 64 | + |
| 65 | + // single character Latin1 |
| 66 | + sb.repeat('1', 0); |
| 67 | + sb.repeat('2', 1); |
| 68 | + sb.repeat('3', 5); |
| 69 | + |
| 70 | + // single string Latin1 (optimized) |
| 71 | + sb.repeat("1", 0); |
| 72 | + sb.repeat("2", 1); |
| 73 | + sb.repeat("3", 5); |
| 74 | + |
| 75 | + // multi string Latin1 |
| 76 | + sb.repeat("-1", 0); |
| 77 | + sb.repeat("-2", 1); |
| 78 | + sb.repeat("-3", 5); |
| 79 | + |
| 80 | + // single character UTF16 |
| 81 | + sb.repeat('\u2460', 0); |
| 82 | + sb.repeat('\u2461', 1); |
| 83 | + sb.repeat('\u2462', 5); |
| 84 | + |
| 85 | + // single string UTF16 (optimized) |
| 86 | + sb.repeat("\u2460", 0); |
| 87 | + sb.repeat("\u2461", 1); |
| 88 | + sb.repeat("\u2462", 5); |
| 89 | + |
| 90 | + // multi string UTF16 |
| 91 | + |
| 92 | + sb.repeat("-\u2460", 0); |
| 93 | + sb.repeat("-\u2461", 1); |
| 94 | + sb.repeat("-\u2462", 5); |
| 95 | + |
| 96 | + // CharSequence |
| 97 | + sb.repeat(MYCHARS, 3); |
| 98 | + |
| 99 | + // null |
| 100 | + sb.repeat((String)null, 0); |
| 101 | + sb.repeat((String)null, 1); |
| 102 | + sb.repeat((String)null, 5); |
| 103 | + sb.repeat((CharSequence)null, 0); |
| 104 | + sb.repeat((CharSequence)null, 1); |
| 105 | + sb.repeat((CharSequence)null, 5); |
| 106 | + |
| 107 | + |
| 108 | + String expected = "repeat233333233333-2-3-3-3-3-3\u2461\u2462\u2462\u2462\u2462\u2462\u2461\u2462\u2462\u2462\u2462\u2462-\u2461-\u2462-\u2462-\u2462-\u2462-\u2462abcabcabc" + |
| 109 | + "nullnullnullnullnullnullnullnullnullnullnullnull"; |
| 110 | + assertEquals(expected, sb.toString()); |
| 111 | + |
| 112 | + // Codepoints |
| 113 | + |
| 114 | + sb.setLength(0); |
| 115 | + |
| 116 | + sb.repeat(0, 0); |
| 117 | + sb.repeat(0, 1); |
| 118 | + sb.repeat(0, 5); |
| 119 | + sb.repeat((int)' ', 0); |
| 120 | + sb.repeat((int)' ', 1); |
| 121 | + sb.repeat((int)' ', 5); |
| 122 | + sb.repeat(0x2460, 0); |
| 123 | + sb.repeat(0x2461, 1); |
| 124 | + sb.repeat(0x2462, 5); |
| 125 | + sb.repeat(0x10FFFF, 0); |
| 126 | + sb.repeat(0x10FFFF, 1); |
| 127 | + sb.repeat(0x10FFFF, 5); |
| 128 | + |
| 129 | + expected = "\u0000\u0000\u0000\u0000\u0000\u0000\u0020\u0020\u0020\u0020\u0020\u0020\u2461\u2462\u2462\u2462\u2462\u2462\udbff\udfff\udbff\udfff\udbff\udfff\udbff\udfff\udbff\udfff\udbff\udfff"; |
| 130 | + assertEquals(expected, sb.toString()); |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + public void exceptions() { |
| 135 | + StringBuilder sb = new StringBuilder(); |
| 136 | + |
| 137 | + try { |
| 138 | + sb.repeat(' ', Integer.MAX_VALUE); |
| 139 | + throw new RuntimeException("No OutOfMemoryError thrown"); |
| 140 | + } catch (OutOfMemoryError | IndexOutOfBoundsException ex) { |
| 141 | + // Okay |
| 142 | + } |
| 143 | + |
| 144 | + try { |
| 145 | + sb.repeat(" ", Integer.MAX_VALUE); |
| 146 | + throw new RuntimeException("No OutOfMemoryError thrown"); |
| 147 | + } catch (OutOfMemoryError | IndexOutOfBoundsException ex) { |
| 148 | + // Okay |
| 149 | + } |
| 150 | + |
| 151 | + try { |
| 152 | + sb.repeat(MYCHARS, Integer.MAX_VALUE); |
| 153 | + throw new RuntimeException("No OutOfMemoryError thrown"); |
| 154 | + } catch (OutOfMemoryError | IndexOutOfBoundsException ex) { |
| 155 | + // Okay |
| 156 | + } |
| 157 | + |
| 158 | + try { |
| 159 | + sb.repeat(' ', -1); |
| 160 | + throw new RuntimeException("No IllegalArgumentException thrown"); |
| 161 | + } catch (IllegalArgumentException ex) { |
| 162 | + // Okay |
| 163 | + } |
| 164 | + |
| 165 | + try { |
| 166 | + sb.repeat("abc", -1); |
| 167 | + throw new RuntimeException("No IllegalArgumentException thrown"); |
| 168 | + } catch (IllegalArgumentException ex) { |
| 169 | + // Okay |
| 170 | + } |
| 171 | + |
| 172 | + try { |
| 173 | + sb.repeat(MYCHARS, -1); |
| 174 | + throw new RuntimeException("No IllegalArgumentException thrown"); |
| 175 | + } catch (IllegalArgumentException ex) { |
| 176 | + // Okay |
| 177 | + } |
| 178 | + |
| 179 | + try { |
| 180 | + sb.repeat(0x10FFFF + 1, -1); |
| 181 | + throw new RuntimeException("No IllegalArgumentException thrown"); |
| 182 | + } catch (IllegalArgumentException ex) { |
| 183 | + // Okay |
| 184 | + } |
| 185 | + |
| 186 | + try { |
| 187 | + sb.repeat(-1, -1); |
| 188 | + throw new RuntimeException("No IllegalArgumentException thrown"); |
| 189 | + } catch (IllegalArgumentException ex) { |
| 190 | + // Okay |
| 191 | + } |
| 192 | + |
| 193 | + } |
| 194 | +} |
0 commit comments