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