test/jdk/java/lang/StringBuffer/Capacity.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:
5603
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
     1
/*
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
     2
 * Copyright 2010 Google Inc.  All Rights Reserved.
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
     4
 *
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
     8
 *
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    13
 * accompanied this code).
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    14
 *
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    18
 *
6374
e214162c907e 6982137: Rebranding pass 2 - missed copyright changes
ohair
parents: 5603
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e214162c907e 6982137: Rebranding pass 2 - missed copyright changes
ohair
parents: 5603
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e214162c907e 6982137: Rebranding pass 2 - missed copyright changes
ohair
parents: 5603
diff changeset
    21
 * questions.
5603
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    22
 */
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    23
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    24
/*
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    25
 * @test
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    26
 * @bug 6952330
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    27
 * @summary Test StringBuffer/StringBuilder capacity handling.
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 6374
diff changeset
    28
 * @key randomness
5603
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    29
 */
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    30
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    31
import java.util.Random;
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    32
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    33
public class Capacity {
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    34
    void test(String[] args) throws Throwable {
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    35
        Random rnd = new Random();
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    36
        int[] sizes = { 0, 1, rnd.nextInt(10), rnd.nextInt(1000) };
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    37
        for (int size : sizes) {
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    38
            equal(16, new StringBuffer().capacity());
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    39
            equal(16, new StringBuilder().capacity());
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    40
            StringBuffer buff = new StringBuffer(size);
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    41
            StringBuilder bild = new StringBuilder(size);
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    42
            equal(size, buff.capacity());
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    43
            equal(size, bild.capacity());
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    44
            buff.ensureCapacity(size);
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    45
            bild.ensureCapacity(size);
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    46
            equal(size, buff.capacity());
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    47
            equal(size, bild.capacity());
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    48
            buff.ensureCapacity(size+1);
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    49
            bild.ensureCapacity(size+1);
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    50
            equal(size*2+2, buff.capacity());
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    51
            equal(size*2+2, bild.capacity());
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    52
            size = buff.capacity();
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    53
            buff.ensureCapacity(size*2+1);
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    54
            bild.ensureCapacity(size*2+1);
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    55
            equal(size*2+2, buff.capacity());
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    56
            equal(size*2+2, bild.capacity());
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    57
            size = buff.capacity();
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    58
            int newSize = size * 2 + 3;
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    59
            buff.ensureCapacity(newSize);
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    60
            bild.ensureCapacity(newSize);
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    61
            equal(newSize, buff.capacity());
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    62
            equal(newSize, bild.capacity());
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    63
            buff.ensureCapacity(0);
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    64
            bild.ensureCapacity(0);
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    65
            equal(newSize, buff.capacity());
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    66
            equal(newSize, bild.capacity());
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    67
        }
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    68
    }
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    69
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    70
    //--------------------- Infrastructure ---------------------------
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    71
    volatile int passed = 0, failed = 0;
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    72
    void pass() {passed++;}
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    73
    void fail() {failed++; Thread.dumpStack();}
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    74
    void fail(String msg) {System.err.println(msg); fail();}
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    75
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    76
    void check(boolean cond) {if (cond) pass(); else fail();}
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    77
    void equal(Object x, Object y) {
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    78
        if (x == null ? y == null : x.equals(y)) pass();
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    79
        else fail(x + " not equal to " + y);}
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    80
    public static void main(String[] args) throws Throwable {
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    81
        new Capacity().instanceMain(args);}
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    82
    public void instanceMain(String[] args) throws Throwable {
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    83
        try {test(args);} catch (Throwable t) {unexpected(t);}
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    84
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    85
        if (failed > 0) throw new AssertionError("Some tests failed");}
682e3deac7ce 6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff changeset
    86
}