jdk/test/java/lang/StringBuffer/IndexOf.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 30046 cf2c86e1819e
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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, 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
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4162796 4162796
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test indexOf and lastIndexOf
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
public class IndexOf {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    static Random generator = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private static boolean failure = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        simpleTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        compareIndexOfLastIndexOf();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        compareStringStringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        if (failure)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
           throw new RuntimeException("One or more BitSet failures.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static void report(String testName, int failCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        System.err.println(testName+": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                         (failCount==0 ? "Passed":"Failed("+failCount+")"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        if (failCount > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            failure = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static String generateTestString(int min, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        StringBuffer aNewString = new StringBuffer(120);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        int aNewLength = getRandomIndex(min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        for(int y=0; y<aNewLength; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            int achar = generator.nextInt(30)+30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            char test = (char)(achar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            aNewString.append(test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        return aNewString.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static int getRandomIndex(int constraint1, int constraint2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        int range = constraint2 - constraint1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        int x = generator.nextInt(range);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return constraint1 + x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static void simpleTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        int failCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        String sourceString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        StringBuffer sourceBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        String targetString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        for (int i=0; i<10000; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                sourceString = generateTestString(99, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                sourceBuffer = new StringBuffer(sourceString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                targetString = generateTestString(10, 11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            } while (sourceString.indexOf(targetString) != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            int index1 = generator.nextInt(90) + 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            sourceBuffer = sourceBuffer.replace(index1, index1, targetString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (sourceBuffer.indexOf(targetString) != index1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                failCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (sourceBuffer.indexOf(targetString, 5) != index1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                failCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (sourceBuffer.indexOf(targetString, 99) == index1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                failCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        report("Basic Test                   ", failCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // Note: it is possible although highly improbable that failCount will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // be > 0 even if everthing is working ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static void compareIndexOfLastIndexOf() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        int failCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        String sourceString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        StringBuffer sourceBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        String targetString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        for (int i=0; i<10000; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                sourceString = generateTestString(99, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                sourceBuffer = new StringBuffer(sourceString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                targetString = generateTestString(10, 11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            } while (sourceString.indexOf(targetString) != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            int index1 = generator.nextInt(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            sourceBuffer = sourceBuffer.replace(index1, index1, targetString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            // extremely remote possibility of > 1 match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            int matches = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            int index2 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            while((index2 = sourceBuffer.indexOf(targetString,index2+1)) != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                matches++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (matches > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (sourceBuffer.indexOf(targetString) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                sourceBuffer.lastIndexOf(targetString))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                failCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            sourceString = sourceBuffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (sourceString.indexOf(targetString) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                sourceString.lastIndexOf(targetString))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                failCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        report("IndexOf vs LastIndexOf       ", failCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static void compareStringStringBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        int failCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        for (int x=0; x<10000; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            String testString = generateTestString(1, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            int len = testString.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            StringBuffer testBuffer = new StringBuffer(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            testBuffer.append(testString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (!testString.equals(testBuffer.toString()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                throw new RuntimeException("Initial equality failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            int x1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            int x2 = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            while(x2 > testString.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                x1 = generator.nextInt(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                x2 = generator.nextInt(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                x2 = x1 + x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            String fragment = testString.substring(x1,x2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            int sAnswer = testString.indexOf(fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            int sbAnswer = testBuffer.indexOf(fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (sAnswer != sbAnswer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                failCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            int testIndex = getRandomIndex(-100, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            sAnswer = testString.indexOf(fragment, testIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            sbAnswer = testBuffer.indexOf(fragment, testIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (sAnswer != sbAnswer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                failCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            sAnswer = testString.lastIndexOf(fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            sbAnswer = testBuffer.lastIndexOf(fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (sAnswer != sbAnswer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                failCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            testIndex = getRandomIndex(-100, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            sAnswer = testString.lastIndexOf(fragment, testIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            sbAnswer = testBuffer.lastIndexOf(fragment, testIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (sAnswer != sbAnswer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                failCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        report("String vs StringBuffer       ", failCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}