1 /* 2 * Copyright (c) 2015, 2019, 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 java.util.Arrays; 25 26 import org.testng.annotations.Test; 27 28 import static org.testng.Assert.assertEquals; 29 import static org.testng.Assert.assertTrue; 30 31 /* 32 * @test 33 * @bug 8077559 8221430 34 * @summary Tests Compact String. This test is testing StringBuffer 35 * behavior related to Compact String. 36 * @run testng/othervm -XX:+CompactStrings CompactStringBuffer 37 * @run testng/othervm -XX:-CompactStrings CompactStringBuffer 38 */ 39 40 public class CompactStringBuffer { 41 42 /* 43 * Tests for "A" 44 */ 45 @Test 46 public void testCompactStringBufferForLatinA() { 47 final String ORIGIN = "A"; 48 /* 49 * Because right now ASCII is the default encoding parameter for source 50 * code in JDK build environment, so we escape them. same as below. 51 */ 52 check(new StringBuffer(ORIGIN).append(new char[] { '\uFF21' }), 53 "A\uFF21"); 423 check(new StringBuffer().append(ascii).insert(3, "AB").toString(), 424 "abcABdefgh"); 425 check(new StringBuffer().append(ascii).insert(3, "\u4e01\u4e02").toString(), 426 "abc\u4e01\u4e02defgh"); 427 428 check(new StringBuffer().append(asciiMixed).insert(0, 'A').toString(), 429 "Aabc\u4e00\u4e01\u4e02fgh"); 430 check(new StringBuffer().append(asciiMixed).insert(3, "A").toString(), 431 "abcA\u4e00\u4e01\u4e02fgh"); 432 433 check(new StringBuffer().append(ascii).insert(3, 1234567).toString(), 434 "abc1234567defgh"); 435 check(new StringBuffer().append(bmp).insert(3, 1234567).toString(), 436 "\u4e00\u4e01\u4e021234567\u4e03\u4e04\u4e05\u4e06\u4e07\u4e08"); 437 438 //////////////////////////////////////////////////////////////////// 439 check(new StringBuffer().append(ascii).append(1.23456).toString(), 440 "abcdefgh1.23456"); 441 check(new StringBuffer().append(bmp).append(1.23456).toString(), 442 "\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07\u4e081.23456"); 443 444 //////////////////////////////////////////////////////////////////// 445 check(new StringBuffer((CharSequence)new StringBuffer(ascii)).toString(), 446 ascii); 447 check(new StringBuffer((CharSequence)new StringBuffer(asciiMixed)).toString(), 448 asciiMixed); 449 } 450 451 private void checkGetChars(StringBuffer sb, int srcBegin, int srcEnd, 452 char expected[]) { 453 char[] dst = new char[srcEnd - srcBegin]; 454 sb.getChars(srcBegin, srcEnd, dst, 0); 455 assertTrue(Arrays.equals(dst, expected)); 456 } 457 458 private void checkSetCharAt(StringBuffer sb, int index, char ch, 459 String expected) { 460 sb.setCharAt(index, ch); 461 check(sb, expected); 462 } 463 464 private void checkSetLength(StringBuffer sb, int newLength, String expected) { 465 sb.setLength(newLength); 466 check(sb, expected); 467 } 468 | 1 /* 2 * Copyright (c) 2015, 2018, 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 java.util.Arrays; 25 26 import org.testng.annotations.Test; 27 28 import static org.testng.Assert.assertEquals; 29 import static org.testng.Assert.assertTrue; 30 31 /* 32 * @test 33 * @bug 8077559 34 * @summary Tests Compact String. This test is testing StringBuffer 35 * behavior related to Compact String. 36 * @run testng/othervm -XX:+CompactStrings CompactStringBuffer 37 * @run testng/othervm -XX:-CompactStrings CompactStringBuffer 38 */ 39 40 public class CompactStringBuffer { 41 42 /* 43 * Tests for "A" 44 */ 45 @Test 46 public void testCompactStringBufferForLatinA() { 47 final String ORIGIN = "A"; 48 /* 49 * Because right now ASCII is the default encoding parameter for source 50 * code in JDK build environment, so we escape them. same as below. 51 */ 52 check(new StringBuffer(ORIGIN).append(new char[] { '\uFF21' }), 53 "A\uFF21"); 423 check(new StringBuffer().append(ascii).insert(3, "AB").toString(), 424 "abcABdefgh"); 425 check(new StringBuffer().append(ascii).insert(3, "\u4e01\u4e02").toString(), 426 "abc\u4e01\u4e02defgh"); 427 428 check(new StringBuffer().append(asciiMixed).insert(0, 'A').toString(), 429 "Aabc\u4e00\u4e01\u4e02fgh"); 430 check(new StringBuffer().append(asciiMixed).insert(3, "A").toString(), 431 "abcA\u4e00\u4e01\u4e02fgh"); 432 433 check(new StringBuffer().append(ascii).insert(3, 1234567).toString(), 434 "abc1234567defgh"); 435 check(new StringBuffer().append(bmp).insert(3, 1234567).toString(), 436 "\u4e00\u4e01\u4e021234567\u4e03\u4e04\u4e05\u4e06\u4e07\u4e08"); 437 438 //////////////////////////////////////////////////////////////////// 439 check(new StringBuffer().append(ascii).append(1.23456).toString(), 440 "abcdefgh1.23456"); 441 check(new StringBuffer().append(bmp).append(1.23456).toString(), 442 "\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07\u4e081.23456"); 443 } 444 445 private void checkGetChars(StringBuffer sb, int srcBegin, int srcEnd, 446 char expected[]) { 447 char[] dst = new char[srcEnd - srcBegin]; 448 sb.getChars(srcBegin, srcEnd, dst, 0); 449 assertTrue(Arrays.equals(dst, expected)); 450 } 451 452 private void checkSetCharAt(StringBuffer sb, int index, char ch, 453 String expected) { 454 sb.setCharAt(index, ch); 455 check(sb, expected); 456 } 457 458 private void checkSetLength(StringBuffer sb, int newLength, String expected) { 459 sb.setLength(newLength); 460 check(sb, expected); 461 } 462 |