jdk/test/java/lang/String/ContentEquals.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2000-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 /*
       
    25  * @test
       
    26  * @bug 4242309 4982981
       
    27  * @summary Test equals and contentEquals in String
       
    28  */
       
    29 import java.util.Random;
       
    30 import java.nio.CharBuffer;
       
    31 
       
    32 // Yes, I used cut and paste for this test. Yes more code
       
    33 // sharing could have been accomplished.
       
    34 public class ContentEquals {
       
    35 
       
    36     private static Random rnd = new Random();
       
    37     private static final int ITERATIONS = 1000;
       
    38     private static final int STR_LEN = 20;
       
    39 
       
    40     public static void main(String[] args) throws Exception {
       
    41         testStringBuffer();
       
    42         testStringBuilder();
       
    43         testString();
       
    44         testCharSequence();
       
    45     }
       
    46 
       
    47     // Test a StringBuffer, which uses the old method from 1.4
       
    48     public static void testStringBuffer() throws Exception {
       
    49         for (int i=0; i<ITERATIONS; i++) {
       
    50             int length = rnd.nextInt(STR_LEN) + 1;
       
    51             StringBuffer testStringBuffer = new StringBuffer();
       
    52             for(int x=0; x<length; x++) {
       
    53                 char aChar = (char)rnd.nextInt();
       
    54                 testStringBuffer.append(aChar);
       
    55             }
       
    56             String testString = testStringBuffer.toString();
       
    57             char c = testStringBuffer.charAt(0);
       
    58             testStringBuffer.setCharAt(0, 'c');
       
    59             testStringBuffer.setCharAt(0, c);
       
    60             if (!testString.contentEquals(testStringBuffer))
       
    61                 throw new RuntimeException("ContentsEqual failure");
       
    62         }
       
    63     }
       
    64 
       
    65     // Test StringBuilder as a CharSequence using new method in 1.5
       
    66     public static void testStringBuilder() throws Exception {
       
    67         for (int i=0; i<ITERATIONS; i++) {
       
    68             int length = rnd.nextInt(STR_LEN) + 1;
       
    69             StringBuilder testStringBuilder = new StringBuilder();
       
    70             for(int x=0; x<length; x++) {
       
    71                 char aChar = (char)rnd.nextInt();
       
    72                 testStringBuilder.append(aChar);
       
    73             }
       
    74             String testString = testStringBuilder.toString();
       
    75             char c = testStringBuilder.charAt(0);
       
    76             testStringBuilder.setCharAt(0, 'c');
       
    77             testStringBuilder.setCharAt(0, c);
       
    78             if (!testString.contentEquals(testStringBuilder))
       
    79                 throw new RuntimeException("ContentsEqual failure");
       
    80         }
       
    81     }
       
    82 
       
    83     // Test a String as a CharSequence. This takes a different codepath in
       
    84     // the new method in 1.5
       
    85     public static void testString() throws Exception {
       
    86         for (int i=0; i<ITERATIONS; i++) {
       
    87             int length = rnd.nextInt(STR_LEN) + 1;
       
    88             StringBuilder testStringBuilder = new StringBuilder();
       
    89             for(int x=0; x<length; x++) {
       
    90                 char aChar = (char)rnd.nextInt();
       
    91                 testStringBuilder.append(aChar);
       
    92             }
       
    93             String testString = testStringBuilder.toString();
       
    94             char c = testStringBuilder.charAt(0);
       
    95             testStringBuilder.setCharAt(0, 'c');
       
    96             testStringBuilder.setCharAt(0, c);
       
    97             if (!testString.contentEquals(testStringBuilder.toString()))
       
    98                 throw new RuntimeException("ContentsEqual failure");
       
    99         }
       
   100     }
       
   101 
       
   102     // Test a CharSequence that is not an AbstractStringBuilder,
       
   103     // this takes a different codepath in the new method in 1.5
       
   104     public static void testCharSequence() throws Exception {
       
   105         for (int i=0; i<ITERATIONS; i++) {
       
   106             int length = rnd.nextInt(STR_LEN) + 1;
       
   107             StringBuilder testStringBuilder = new StringBuilder();
       
   108             for(int x=0; x<length; x++) {
       
   109                 char aChar = (char)rnd.nextInt();
       
   110                 testStringBuilder.append(aChar);
       
   111             }
       
   112             String testString = testStringBuilder.toString();
       
   113             char c = testStringBuilder.charAt(0);
       
   114             testStringBuilder.setCharAt(0, 'c');
       
   115             testStringBuilder.setCharAt(0, c);
       
   116             CharBuffer buf = CharBuffer.wrap(testStringBuilder.toString());
       
   117             if (!testString.contentEquals(buf))
       
   118                 throw new RuntimeException("ContentsEqual failure");
       
   119         }
       
   120     }
       
   121 }