test/jdk/java/lang/StringBuffer/SBBasher.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) 1998, 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 4120694
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test new methods in StringBuffer
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class SBBasher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        SBBasher basher = new SBBasher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        for (int iterations=0; iterations<100; iterations++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
            String testString = basher.generateTestString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            boolean result = basher.Test1(testString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            if (result == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                throw new RuntimeException("Substring or replace failure.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        for (int iterations=0; iterations<100; iterations++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            String testString = basher.generateTestString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            boolean result = basher.Test2(testString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            if (result == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                throw new RuntimeException("Insert or delete failure.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        for (int iterations=0; iterations<100; iterations++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            String testString = basher.generateTestString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            boolean result = basher.Test3(testString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            if (result == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
              throw new RuntimeException("Insert, delete or replace failure.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private int getRandomIndex(int constraint1, int constraint2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        int range = constraint2 - constraint1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        int x = generator.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return constraint1 + Math.abs(x % range);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static Random generator = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private String generateTestString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        StringBuffer aNewString = new StringBuffer(120);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        int aNewLength = getRandomIndex(1,100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        for(int y=0; y<aNewLength; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            int achar = generator.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            char test = (char)(achar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            aNewString.append(test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return aNewString.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * first test, get substring of a random string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * and replace same spot with same substring
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * then check for equality with original
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * this tests both substrings and the replace method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private boolean Test1(String before) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        StringBuffer bashed = new StringBuffer(before);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        String slice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        for (int i=0; i<100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            int startIndex = getRandomIndex(0, before.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            int endIndex = getRandomIndex(startIndex, before.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (endIndex < bashed.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                slice = bashed.substring(startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                slice = bashed.substring(startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            bashed.replace(startIndex, endIndex, slice);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        String after = bashed.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (!before.equals(after))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * second test, delete random section of string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * then insert same section back again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * then check for equality with original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * this tests both substrings, both deletes and insert method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private boolean Test2(String before) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        StringBuffer bashed = new StringBuffer(before);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        String slice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        for (int i=0; i<100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            int startIndex = getRandomIndex(0, before.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            int endIndex = getRandomIndex(startIndex, before.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (endIndex < bashed.length())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                slice = bashed.substring(startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                slice = bashed.substring(startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (slice.length() == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                bashed.deleteCharAt(startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                bashed.delete(startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            bashed.insert(startIndex, slice.toCharArray(), 0, slice.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        String after = bashed.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (!before.equals(after))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Third test, clone string and make sure that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * replace operation is equivalent to a delete followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * by an insert with the equivalent arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * this tests replace, delete and insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private boolean Test3(String before) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        StringBuffer bashed1 = new StringBuffer(before);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        StringBuffer bashed2 = new StringBuffer(before);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int startIndex = getRandomIndex(0, bashed1.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        int endIndex = getRandomIndex(startIndex, bashed2.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        String insertString = generateTestString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        bashed1.delete(startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        bashed1.insert(startIndex, insertString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        bashed2.replace(startIndex, endIndex, insertString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        String result1 = bashed1.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        String result2 = bashed2.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (!result1.equals(result2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}