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 8013395
26 * @summary Test StringBuffer.toString caching
27 */
28
29 public class ToStringCache {
30
31 // we can't test that we actually use a cached value (the benchmarks
32 // verify that) but we have to test that the cache is cleared when
33 // expected
34
35 public static void main(String[] args) throws Exception {
36 String original = "The original String";
37
38 StringBuffer sb = new StringBuffer(original);
39
40 String a = sb.toString();
41 checkEqual(a, original);
42
43 String b = sb.toString();
44 checkEqual(a, b);
45
181 a = b;
182
183 sb.insert(0, 1L);
184 b = sb.toString();
185 checkUnequal(a, b);
186 a = b;
187
188 sb.insert(0, 1.0f);
189 b = sb.toString();
190 checkUnequal(a, b);
191 a = b;
192
193 sb.insert(0, 1.0d);
194 b = sb.toString();
195 checkUnequal(a, b);
196 a = b;
197
198 sb.reverse();
199 b = sb.toString();
200 checkUnequal(a, b);
201
202 // non-mutating methods
203
204 // Reset to known value
205 sb = new StringBuffer(original);
206 a = sb.toString();
207 b = sb.toString();
208 checkEqual(a, b);
209
210 int l = sb.length();
211 b = sb.toString();
212 checkEqual(a, b);
213
214 int cap = sb.capacity();
215 b = sb.toString();
216 checkEqual(a, b);
217
218 sb.ensureCapacity(100);
219 b = sb.toString();
220 checkEqual(a, b);
|
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 8013395 8014814
26 * @summary Test StringBuffer.toString caching
27 */
28
29 public class ToStringCache {
30
31 // we can't test that we actually use a cached value (the benchmarks
32 // verify that) but we have to test that the cache is cleared when
33 // expected
34
35 public static void main(String[] args) throws Exception {
36 String original = "The original String";
37
38 StringBuffer sb = new StringBuffer(original);
39
40 String a = sb.toString();
41 checkEqual(a, original);
42
43 String b = sb.toString();
44 checkEqual(a, b);
45
181 a = b;
182
183 sb.insert(0, 1L);
184 b = sb.toString();
185 checkUnequal(a, b);
186 a = b;
187
188 sb.insert(0, 1.0f);
189 b = sb.toString();
190 checkUnequal(a, b);
191 a = b;
192
193 sb.insert(0, 1.0d);
194 b = sb.toString();
195 checkUnequal(a, b);
196 a = b;
197
198 sb.reverse();
199 b = sb.toString();
200 checkUnequal(a, b);
201
202 // Extra checks that append(null) works correctly
203
204 sb.append((String)null);
205 b = sb.toString();
206 checkUnequal(a, b);
207 a = b;
208
209 sb.append((StringBuffer)null);
210 b = sb.toString();
211 checkUnequal(a, b);
212 a = b;
213
214 sb.append((StringBuilder)null);
215 b = sb.toString();
216 checkUnequal(a, b);
217 a = b;
218
219 sb.append((CharSequence)null);
220 b = sb.toString();
221 checkUnequal(a, b);
222 a = b;
223
224 // non-mutating methods
225
226 // Reset to known value
227 sb = new StringBuffer(original);
228 a = sb.toString();
229 b = sb.toString();
230 checkEqual(a, b);
231
232 int l = sb.length();
233 b = sb.toString();
234 checkEqual(a, b);
235
236 int cap = sb.capacity();
237 b = sb.toString();
238 checkEqual(a, b);
239
240 sb.ensureCapacity(100);
241 b = sb.toString();
242 checkEqual(a, b);
|