test/jdk/java/lang/String/ContentEquals.java
author herrick
Mon, 14 Oct 2019 14:36:45 -0400
branchJDK-8200758-branch
changeset 58584 910b14f4fe3a
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232042: [macos] Installation fails if application name contains spaces Submitted-by: almatvee Reviewed-by: aherrick, asemenyuk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4242309 4982981
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test equals and contentEquals in String
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 5506
diff changeset
    28
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
// Yes, I used cut and paste for this test. Yes more code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
// sharing could have been accomplished.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class ContentEquals {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private static Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static final int ITERATIONS = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static final int STR_LEN = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        testStringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        testStringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        testString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        testCharSequence();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // Test a StringBuffer, which uses the old method from 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static void testStringBuffer() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        for (int i=0; i<ITERATIONS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            int length = rnd.nextInt(STR_LEN) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            StringBuffer testStringBuffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            for(int x=0; x<length; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                char aChar = (char)rnd.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                testStringBuffer.append(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            String testString = testStringBuffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            char c = testStringBuffer.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            testStringBuffer.setCharAt(0, 'c');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            testStringBuffer.setCharAt(0, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            if (!testString.contentEquals(testStringBuffer))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                throw new RuntimeException("ContentsEqual failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // Test StringBuilder as a CharSequence using new method in 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static void testStringBuilder() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        for (int i=0; i<ITERATIONS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            int length = rnd.nextInt(STR_LEN) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            StringBuilder testStringBuilder = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            for(int x=0; x<length; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                char aChar = (char)rnd.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                testStringBuilder.append(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            String testString = testStringBuilder.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            char c = testStringBuilder.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            testStringBuilder.setCharAt(0, 'c');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            testStringBuilder.setCharAt(0, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (!testString.contentEquals(testStringBuilder))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                throw new RuntimeException("ContentsEqual failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // Test a String as a CharSequence. This takes a different codepath in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // the new method in 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static void testString() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        for (int i=0; i<ITERATIONS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            int length = rnd.nextInt(STR_LEN) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            StringBuilder testStringBuilder = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            for(int x=0; x<length; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                char aChar = (char)rnd.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                testStringBuilder.append(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            String testString = testStringBuilder.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            char c = testStringBuilder.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            testStringBuilder.setCharAt(0, 'c');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            testStringBuilder.setCharAt(0, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (!testString.contentEquals(testStringBuilder.toString()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                throw new RuntimeException("ContentsEqual failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // Test a CharSequence that is not an AbstractStringBuilder,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // this takes a different codepath in the new method in 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public static void testCharSequence() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        for (int i=0; i<ITERATIONS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            int length = rnd.nextInt(STR_LEN) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            StringBuilder testStringBuilder = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            for(int x=0; x<length; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                char aChar = (char)rnd.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                testStringBuilder.append(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            String testString = testStringBuilder.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            char c = testStringBuilder.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            testStringBuilder.setCharAt(0, 'c');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            testStringBuilder.setCharAt(0, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            CharBuffer buf = CharBuffer.wrap(testStringBuilder.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (!testString.contentEquals(buf))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                throw new RuntimeException("ContentsEqual failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}