test/jdk/java/lang/StringBuffer/AppendCharSequence.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 53977 723f665d0596
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
/*
53977
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2019, 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
53977
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
    25
 * @bug 4812591 4705328 5019111 8218228
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test append and insert methods with CharSequence params
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 AppendCharSequence {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    private static Random generator = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        bash();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        checkNulls();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        checkOffsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        checkConstructor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    // Sanity test of contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static void bash() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        for (int i=0; i<1000; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            StringBuffer sb1 = generateTestBuffer(0, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            StringBuffer sb2 = generateTestBuffer(0, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            StringBuffer sb3 = generateTestBuffer(0, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            StringBuffer sb4 = generateTestBuffer(0, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            StringBuffer sb5 = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            String s1 = sb1.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            String s2 = sb2.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            String s3 = sb3.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            String s4 = sb4.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            String s5 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            // append(CharSequence cs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            sb5.append((CharSequence)sb1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            s5 = sb1.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            if (!sb5.toString().equals(s5))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                throw new RuntimeException("StringBuffer.append failure 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            // append (CharSequence cs, int start, int end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            int index = generator.nextInt(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            int len = generator.nextInt(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            while (index > sb2.length() - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                index = generator.nextInt(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                len = generator.nextInt(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            sb5.append((CharSequence)sb2, index, index + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            s5 = s5 + sb2.toString().substring(index, index + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            if (!sb5.toString().equals(s5))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                throw new RuntimeException("StringBuffer.append failure 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            // insert(int dstOffset, CharSequence cs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            index = generator.nextInt(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            while (index > s5.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                index = generator.nextInt(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            sb5.insert(index, (CharSequence)sb3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            s5 = new StringBuffer(s5).insert(index, sb3).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (!sb5.toString().equals(s5))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                throw new RuntimeException("StringBuffer.insert failure 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            // insert(int dstOffset, CharSequence s, int start, int end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            int index1 = generator.nextInt(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            while (index1 > s5.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                index1 = generator.nextInt(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            int index2 = generator.nextInt(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            len = generator.nextInt(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            while (index2 > sb4.length() - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                index2 = generator.nextInt(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                len = generator.nextInt(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            sb5.insert(index1, (CharSequence)sb4, index2, index2 + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            s5 = new StringBuffer(s5).insert(index1, s4.toCharArray(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                             index2, len).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (!sb5.toString().equals(s5))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                throw new RuntimeException("StringBuffer.insert failure 2");
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
    private static int getRandomIndex(int constraint1, int constraint2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int range = constraint2 - constraint1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int x = generator.nextInt(range);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return constraint1 + x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static StringBuffer generateTestBuffer(int min, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        StringBuffer aNewStringBuffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        int aNewLength = getRandomIndex(min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        for(int y=0; y<aNewLength; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            int achar = generator.nextInt(30)+30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            char test = (char)(achar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            aNewStringBuffer.append(test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return aNewStringBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    // Check handling of null as "null"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private static void checkNulls() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        StringBuffer sb1 = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        CharSequence cs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        sb1.append("test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        sb1.append(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (!sb1.toString().equals("testnull"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new RuntimeException("StringBuffer.append failure 3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        sb1 = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        sb1.append("test", 0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        sb1.append(cs, 0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (!sb1.toString().equals("tenu"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new RuntimeException("StringBuffer.append failure 4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        sb1 = new StringBuffer("test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        sb1.insert(2, cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (!sb1.toString().equals("tenullst"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new RuntimeException("StringBuffer.insert failure 3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        sb1 = new StringBuffer("test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        sb1.insert(2, cs, 0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (!sb1.toString().equals("tenust"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new RuntimeException("StringBuffer.insert failure 4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    // Test the bounds checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private static void checkOffsets() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // append (CharSeqeunce cs, int start, int end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        for (int i=0; i<100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            StringBuffer sb = generateTestBuffer(0, 80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            CharSequence cs = (CharSequence)generateTestBuffer(0, 80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            while (index <= cs.length() - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                index = generator.nextInt(100) - 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                len = generator.nextInt(100) - 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                if (index < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                if (len < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                sb.append(cs, index, index + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                throw new RuntimeException("Append bounds checking failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            } catch (IndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                // Correct result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // insert(int dstOffset, CharSequence cs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        for (int i=0; i<100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            StringBuffer sb = new StringBuffer("test1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            CharSequence cs = (CharSequence)new StringBuffer("test2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            while (index <= sb.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                index = generator.nextInt(100) - 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                if (index < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                sb.insert(index, cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                throw new RuntimeException("Insert bounds checking failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            } catch (IndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                // Correct result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // insert(int dstOffset, CharSequence s, int start, int end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        for (int i=0; i<100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            StringBuffer sb = new StringBuffer("test1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            CharSequence cs = (CharSequence)new StringBuffer("test2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            int index1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            while (index1 <= sb.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                index1 = generator.nextInt(100) - 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                if (index1 < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            int index2 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            while (index2 < sb.length() - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                index2 = generator.nextInt(100) - 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                len = generator.nextInt(100) - 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                if (index2 < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                if (len < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                sb.insert(index1, cs, index2, index2 + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                throw new RuntimeException("Insert bounds checking failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            } catch (IndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                // Correct result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    // Test the CharSequence constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private static void checkConstructor() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        for (int i=0; i<100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            StringBuffer sb = generateTestBuffer(0, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            CharSequence cs = (CharSequence)sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            StringBuffer sb2 = new StringBuffer(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            if (!sb.toString().equals(sb2.toString())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                throw new RuntimeException("CharSequence constructor failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
53977
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   233
        checkNegativeLenCharSeq(-1);
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   234
        checkNegativeLenCharSeq(-16);
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   235
        checkNegativeLenCharSeq(-17);
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   236
        checkNegativeLenCharSeq(Integer.MIN_VALUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
53977
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   239
    // Test constructing from CharSequence of negative length
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   240
    private static void checkNegativeLenCharSeq(int len) {
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   241
        try {
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   242
            CharSequence seq = new MyNegativeLenCharSeq(len);
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   243
            StringBuffer sb = new StringBuffer(seq);
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   244
        } catch (NegativeArraySizeException expected) {
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   245
        } catch (Throwable exc) {
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   246
            throw new RuntimeException("Unexpected: " + exc, exc);
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   247
        }
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   248
    }
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   249
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   250
    private static class MyNegativeLenCharSeq implements CharSequence {
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   251
        int length;
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   252
        MyNegativeLenCharSeq(int length) {
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   253
            this.length = length;
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   254
        }
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   255
        public char charAt(int i) {
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   256
            throw new UnsupportedOperationException();
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   257
        }
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   258
        public int length() { return length; }
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   259
        public CharSequence subSequence(int st, int e) {
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   260
            throw new UnsupportedOperationException();
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   261
        }
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   262
        public String toString() { return ""; }
723f665d0596 8218228: The constructor StringBuffer(CharSequence) violates spec for negatively sized argument
igerasim
parents: 47216
diff changeset
   263
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
}