Path: blob/master/test/jdk/java/lang/String/ContentEquals.java
41149 views
/*1* Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4242309 498298126* @summary Test equals and contentEquals in String27* @key randomness28*/29import java.util.Random;30import java.nio.CharBuffer;3132// Yes, I used cut and paste for this test. Yes more code33// sharing could have been accomplished.34public class ContentEquals {3536private static Random rnd = new Random();37private static final int ITERATIONS = 1000;38private static final int STR_LEN = 20;3940public static void main(String[] args) throws Exception {41testStringBuffer();42testStringBuilder();43testString();44testCharSequence();45}4647// Test a StringBuffer, which uses the old method from 1.448public static void testStringBuffer() throws Exception {49for (int i=0; i<ITERATIONS; i++) {50int length = rnd.nextInt(STR_LEN) + 1;51StringBuffer testStringBuffer = new StringBuffer();52for(int x=0; x<length; x++) {53char aChar = (char)rnd.nextInt();54testStringBuffer.append(aChar);55}56String testString = testStringBuffer.toString();57char c = testStringBuffer.charAt(0);58testStringBuffer.setCharAt(0, 'c');59testStringBuffer.setCharAt(0, c);60if (!testString.contentEquals(testStringBuffer))61throw new RuntimeException("ContentsEqual failure");62}63}6465// Test StringBuilder as a CharSequence using new method in 1.566public static void testStringBuilder() throws Exception {67for (int i=0; i<ITERATIONS; i++) {68int length = rnd.nextInt(STR_LEN) + 1;69StringBuilder testStringBuilder = new StringBuilder();70for(int x=0; x<length; x++) {71char aChar = (char)rnd.nextInt();72testStringBuilder.append(aChar);73}74String testString = testStringBuilder.toString();75char c = testStringBuilder.charAt(0);76testStringBuilder.setCharAt(0, 'c');77testStringBuilder.setCharAt(0, c);78if (!testString.contentEquals(testStringBuilder))79throw new RuntimeException("ContentsEqual failure");80}81}8283// Test a String as a CharSequence. This takes a different codepath in84// the new method in 1.585public static void testString() throws Exception {86for (int i=0; i<ITERATIONS; i++) {87int length = rnd.nextInt(STR_LEN) + 1;88StringBuilder testStringBuilder = new StringBuilder();89for(int x=0; x<length; x++) {90char aChar = (char)rnd.nextInt();91testStringBuilder.append(aChar);92}93String testString = testStringBuilder.toString();94char c = testStringBuilder.charAt(0);95testStringBuilder.setCharAt(0, 'c');96testStringBuilder.setCharAt(0, c);97if (!testString.contentEquals(testStringBuilder.toString()))98throw new RuntimeException("ContentsEqual failure");99}100}101102// Test a CharSequence that is not an AbstractStringBuilder,103// this takes a different codepath in the new method in 1.5104public static void testCharSequence() throws Exception {105for (int i=0; i<ITERATIONS; i++) {106int length = rnd.nextInt(STR_LEN) + 1;107StringBuilder testStringBuilder = new StringBuilder();108for(int x=0; x<length; x++) {109char aChar = (char)rnd.nextInt();110testStringBuilder.append(aChar);111}112String testString = testStringBuilder.toString();113char c = testStringBuilder.charAt(0);114testStringBuilder.setCharAt(0, 'c');115testStringBuilder.setCharAt(0, c);116CharBuffer buf = CharBuffer.wrap(testStringBuilder.toString());117if (!testString.contentEquals(buf))118throw new RuntimeException("ContentsEqual failure");119}120}121}122123124