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