1 /* 2 * Copyright (c) 2003, 2004, 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 /* @test 25 * @bug 4812591 4705328 5019111 26 * @summary Test append and insert methods with CharSequence params 27 * @key randomness 28 */ 29 30 package test.java.lang.StringBuffer; 31 32 import java.util.Random; 33 import org.testng.annotations.Test; 34 35 public class AppendCharSequence { 36 private static Random generator = new Random(); 37 38 // Android-changed: Add @Test annotation and remove empty arguments. 39 // public static void main(String[] args) throws Exception { 40 @Test main()41 public static void main() throws Exception { 42 bash(); 43 checkNulls(); 44 checkOffsets(); 45 checkConstructor(); 46 } 47 48 // Sanity test of contents bash()49 private static void bash() throws Exception { 50 for (int i=0; i<1000; i++) { 51 StringBuffer sb1 = generateTestBuffer(0, 100); 52 StringBuffer sb2 = generateTestBuffer(0, 100); 53 StringBuffer sb3 = generateTestBuffer(0, 100); 54 StringBuffer sb4 = generateTestBuffer(0, 100); 55 StringBuffer sb5 = new StringBuffer(); 56 57 String s1 = sb1.toString(); 58 String s2 = sb2.toString(); 59 String s3 = sb3.toString(); 60 String s4 = sb4.toString(); 61 String s5 = null; 62 63 // append(CharSequence cs) 64 sb5.append((CharSequence)sb1); 65 s5 = sb1.toString(); 66 67 if (!sb5.toString().equals(s5)) 68 throw new RuntimeException("StringBuffer.append failure 1"); 69 70 // append (CharSequence cs, int start, int end) 71 int index = generator.nextInt(100); 72 int len = generator.nextInt(100); 73 while (index > sb2.length() - len) { 74 index = generator.nextInt(100); 75 len = generator.nextInt(100); 76 } 77 sb5.append((CharSequence)sb2, index, index + len); 78 s5 = s5 + sb2.toString().substring(index, index + len); 79 80 if (!sb5.toString().equals(s5)) 81 throw new RuntimeException("StringBuffer.append failure 2"); 82 83 // insert(int dstOffset, CharSequence cs) 84 index = generator.nextInt(100); 85 while (index > s5.length()) { 86 index = generator.nextInt(100); 87 } 88 sb5.insert(index, (CharSequence)sb3); 89 s5 = new StringBuffer(s5).insert(index, sb3).toString(); 90 91 if (!sb5.toString().equals(s5)) 92 throw new RuntimeException("StringBuffer.insert failure 1"); 93 94 // insert(int dstOffset, CharSequence s, int start, int end) 95 int index1 = generator.nextInt(100); 96 while (index1 > s5.length()) { 97 index1 = generator.nextInt(100); 98 } 99 int index2 = generator.nextInt(100); 100 len = generator.nextInt(100); 101 while (index2 > sb4.length() - len) { 102 index2 = generator.nextInt(100); 103 len = generator.nextInt(100); 104 } 105 sb5.insert(index1, (CharSequence)sb4, index2, index2 + len); 106 s5 = new StringBuffer(s5).insert(index1, s4.toCharArray(), 107 index2, len).toString(); 108 109 if (!sb5.toString().equals(s5)) 110 throw new RuntimeException("StringBuffer.insert failure 2"); 111 } 112 } 113 getRandomIndex(int constraint1, int constraint2)114 private static int getRandomIndex(int constraint1, int constraint2) { 115 int range = constraint2 - constraint1; 116 int x = generator.nextInt(range); 117 return constraint1 + x; 118 } 119 generateTestBuffer(int min, int max)120 private static StringBuffer generateTestBuffer(int min, int max) { 121 StringBuffer aNewStringBuffer = new StringBuffer(); 122 int aNewLength = getRandomIndex(min, max); 123 for(int y=0; y<aNewLength; y++) { 124 int achar = generator.nextInt(30)+30; 125 char test = (char)(achar); 126 aNewStringBuffer.append(test); 127 } 128 return aNewStringBuffer; 129 } 130 131 // Check handling of null as "null" checkNulls()132 private static void checkNulls() throws Exception { 133 StringBuffer sb1 = new StringBuffer(); 134 CharSequence cs = null; 135 sb1.append("test"); 136 sb1.append(cs); 137 if (!sb1.toString().equals("testnull")) 138 throw new RuntimeException("StringBuffer.append failure 3"); 139 140 sb1 = new StringBuffer(); 141 sb1.append("test", 0, 2); 142 sb1.append(cs, 0, 2); 143 if (!sb1.toString().equals("tenu")) 144 throw new RuntimeException("StringBuffer.append failure 4"); 145 146 sb1 = new StringBuffer("test"); 147 sb1.insert(2, cs); 148 if (!sb1.toString().equals("tenullst")) 149 throw new RuntimeException("StringBuffer.insert failure 3"); 150 151 sb1 = new StringBuffer("test"); 152 sb1.insert(2, cs, 0, 2); 153 if (!sb1.toString().equals("tenust")) 154 throw new RuntimeException("StringBuffer.insert failure 4"); 155 } 156 157 // Test the bounds checking checkOffsets()158 private static void checkOffsets() throws Exception { 159 160 // append (CharSeqeunce cs, int start, int end) 161 for (int i=0; i<100; i++) { 162 StringBuffer sb = generateTestBuffer(0, 80); 163 CharSequence cs = (CharSequence)generateTestBuffer(0, 80); 164 int index = 0; 165 int len = 0; 166 while (index <= cs.length() - len) { 167 index = generator.nextInt(100) - 50; 168 len = generator.nextInt(100) - 50; 169 if (index < 0) 170 break; 171 if (len < 0) 172 break; 173 } 174 try { 175 sb.append(cs, index, index + len); 176 throw new RuntimeException("Append bounds checking failure"); 177 } catch (IndexOutOfBoundsException e) { 178 // Correct result 179 } 180 } 181 182 // insert(int dstOffset, CharSequence cs) 183 for (int i=0; i<100; i++) { 184 StringBuffer sb = new StringBuffer("test1"); 185 CharSequence cs = (CharSequence)new StringBuffer("test2"); 186 int index = 0; 187 while (index <= sb.length()) { 188 index = generator.nextInt(100) - 50; 189 if (index < 0) 190 break; 191 } 192 try { 193 sb.insert(index, cs); 194 throw new RuntimeException("Insert bounds checking failure"); 195 } catch (IndexOutOfBoundsException e) { 196 // Correct result 197 } 198 } 199 200 // insert(int dstOffset, CharSequence s, int start, int end) 201 for (int i=0; i<100; i++) { 202 StringBuffer sb = new StringBuffer("test1"); 203 CharSequence cs = (CharSequence)new StringBuffer("test2"); 204 int index1 = 0; 205 while (index1 <= sb.length()) { 206 index1 = generator.nextInt(100) - 50; 207 if (index1 < 0) 208 break; 209 } 210 int index2 = 0; 211 int len = 0; 212 while (index2 < sb.length() - len) { 213 index2 = generator.nextInt(100) - 50; 214 len = generator.nextInt(100) - 50; 215 if (index2 < 0) 216 break; 217 if (len < 0) 218 break; 219 } 220 try { 221 sb.insert(index1, cs, index2, index2 + len); 222 throw new RuntimeException("Insert bounds checking failure"); 223 } catch (IndexOutOfBoundsException e) { 224 // Correct result 225 } 226 } 227 } 228 229 // Test the CharSequence constructor checkConstructor()230 private static void checkConstructor() throws Exception { 231 for (int i=0; i<100; i++) { 232 StringBuffer sb = generateTestBuffer(0, 100); 233 CharSequence cs = (CharSequence)sb; 234 StringBuffer sb2 = new StringBuffer(cs); 235 if (!sb.toString().equals(sb2.toString())) { 236 throw new RuntimeException("CharSequence constructor failure"); 237 } 238 } 239 } 240 241 } 242