1 /*
   2  * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4812591 4705328 5019111
  26  * @summary Test append and insert methods with CharSequence params
  27  * @key randomness
  28  */
  29 
  30 import java.util.Random;
  31 
  32 public class AppendCharSequence {
  33     private static Random generator = new Random();
  34 
  35     public static void main(String[] args) throws Exception {
  36         bash();
  37         checkNulls();
  38         checkOffsets();
  39         checkConstructor();
  40     }
  41 
  42     // Sanity test of contents
  43     private static void bash() throws Exception {
  44         for (int i=0; i<1000; i++) {
  45             StringBuffer sb1 = generateTestBuffer(0, 100);
  46             StringBuffer sb2 = generateTestBuffer(0, 100);
  47             StringBuffer sb3 = generateTestBuffer(0, 100);
  48             StringBuffer sb4 = generateTestBuffer(0, 100);
  49             StringBuffer sb5 = new StringBuffer();
  50 
  51             String s1 = sb1.toString();
  52             String s2 = sb2.toString();
  53             String s3 = sb3.toString();
  54             String s4 = sb4.toString();
  55             String s5 = null;
  56 
  57             // append(CharSequence cs)
  58             sb5.append((CharSequence)sb1);
  59             s5 = sb1.toString();
  60 
  61             if (!sb5.toString().equals(s5))
  62                 throw new RuntimeException("StringBuffer.append failure 1");
  63 
  64             // append (CharSequence cs, int start, int end)
  65             int index = generator.nextInt(100);
  66             int len = generator.nextInt(100);
  67             while (index > sb2.length() - len) {
  68                 index = generator.nextInt(100);
  69                 len = generator.nextInt(100);
  70             }
  71             sb5.append((CharSequence)sb2, index, index + len);
  72             s5 = s5 + sb2.toString().substring(index, index + len);
  73 
  74             if (!sb5.toString().equals(s5))
  75                 throw new RuntimeException("StringBuffer.append failure 2");
  76 
  77             // insert(int dstOffset, CharSequence cs)
  78             index = generator.nextInt(100);
  79             while (index > s5.length()) {
  80                 index = generator.nextInt(100);
  81             }
  82             sb5.insert(index, (CharSequence)sb3);
  83             s5 = new StringBuffer(s5).insert(index, sb3).toString();
  84 
  85             if (!sb5.toString().equals(s5))
  86                 throw new RuntimeException("StringBuffer.insert failure 1");
  87 
  88             // insert(int dstOffset, CharSequence s, int start, int end)
  89             int index1 = generator.nextInt(100);
  90             while (index1 > s5.length()) {
  91                 index1 = generator.nextInt(100);
  92             }
  93             int index2 = generator.nextInt(100);
  94             len = generator.nextInt(100);
  95             while (index2 > sb4.length() - len) {
  96                 index2 = generator.nextInt(100);
  97                 len = generator.nextInt(100);
  98             }
  99             sb5.insert(index1, (CharSequence)sb4, index2, index2 + len);
 100             s5 = new StringBuffer(s5).insert(index1, s4.toCharArray(),
 101                                              index2, len).toString();
 102 
 103             if (!sb5.toString().equals(s5))
 104                 throw new RuntimeException("StringBuffer.insert failure 2");
 105         }
 106     }
 107 
 108     private static int getRandomIndex(int constraint1, int constraint2) {
 109         int range = constraint2 - constraint1;
 110         int x = generator.nextInt(range);
 111         return constraint1 + x;
 112     }
 113 
 114     private static StringBuffer generateTestBuffer(int min, int max) {
 115         StringBuffer aNewStringBuffer = new StringBuffer();
 116         int aNewLength = getRandomIndex(min, max);
 117         for(int y=0; y<aNewLength; y++) {
 118             int achar = generator.nextInt(30)+30;
 119             char test = (char)(achar);
 120             aNewStringBuffer.append(test);
 121         }
 122         return aNewStringBuffer;
 123     }
 124 
 125     // Check handling of null as "null"
 126     private static void checkNulls() throws Exception {
 127         StringBuffer sb1 = new StringBuffer();
 128         CharSequence cs = null;
 129         sb1.append("test");
 130         sb1.append(cs);
 131         if (!sb1.toString().equals("testnull"))
 132             throw new RuntimeException("StringBuffer.append failure 3");
 133 
 134         sb1 = new StringBuffer();
 135         sb1.append("test", 0, 2);
 136         sb1.append(cs, 0, 2);
 137         if (!sb1.toString().equals("tenu"))
 138             throw new RuntimeException("StringBuffer.append failure 4");
 139 
 140         sb1 = new StringBuffer("test");
 141         sb1.insert(2, cs);
 142         if (!sb1.toString().equals("tenullst"))
 143             throw new RuntimeException("StringBuffer.insert failure 3");
 144 
 145         sb1 = new StringBuffer("test");
 146         sb1.insert(2, cs, 0, 2);
 147         if (!sb1.toString().equals("tenust"))
 148             throw new RuntimeException("StringBuffer.insert failure 4");
 149     }
 150 
 151     // Test the bounds checking
 152     private static void checkOffsets() throws Exception {
 153 
 154         // append (CharSeqeunce cs, int start, int end)
 155         for (int i=0; i<100; i++) {
 156             StringBuffer sb = generateTestBuffer(0, 80);
 157             CharSequence cs = (CharSequence)generateTestBuffer(0, 80);
 158             int index = 0;
 159             int len = 0;
 160             while (index <= cs.length() - len) {
 161                 index = generator.nextInt(100) - 50;
 162                 len = generator.nextInt(100) - 50;
 163                 if (index < 0)
 164                     break;
 165                 if (len < 0)
 166                     break;
 167             }
 168             try {
 169                 sb.append(cs, index, index + len);
 170                 throw new RuntimeException("Append bounds checking failure");
 171             } catch (IndexOutOfBoundsException e) {
 172                 // Correct result
 173             }
 174         }
 175 
 176         // insert(int dstOffset, CharSequence cs)
 177         for (int i=0; i<100; i++) {
 178             StringBuffer sb = new StringBuffer("test1");
 179             CharSequence cs = (CharSequence)new StringBuffer("test2");
 180             int index = 0;
 181             while (index <= sb.length()) {
 182                 index = generator.nextInt(100) - 50;
 183                 if (index < 0)
 184                     break;
 185             }
 186             try {
 187                 sb.insert(index, cs);
 188                 throw new RuntimeException("Insert bounds checking failure");
 189             } catch (IndexOutOfBoundsException e) {
 190                 // Correct result
 191             }
 192         }
 193 
 194         // insert(int dstOffset, CharSequence s, int start, int end)
 195         for (int i=0; i<100; i++) {
 196             StringBuffer sb = new StringBuffer("test1");
 197             CharSequence cs = (CharSequence)new StringBuffer("test2");
 198             int index1 = 0;
 199             while (index1 <= sb.length()) {
 200                 index1 = generator.nextInt(100) - 50;
 201                 if (index1 < 0)
 202                     break;
 203             }
 204             int index2 = 0;
 205             int len = 0;
 206             while (index2 < sb.length() - len) {
 207                 index2 = generator.nextInt(100) - 50;
 208                 len = generator.nextInt(100) - 50;
 209                 if (index2 < 0)
 210                     break;
 211                 if (len < 0)
 212                     break;
 213             }
 214             try {
 215                 sb.insert(index1, cs, index2, index2 + len);
 216                 throw new RuntimeException("Insert bounds checking failure");
 217             } catch (IndexOutOfBoundsException e) {
 218                 // Correct result
 219             }
 220         }
 221     }
 222 
 223     // Test the CharSequence constructor
 224     private static void checkConstructor() throws Exception {
 225         for (int i=0; i<100; i++) {
 226             StringBuffer sb = generateTestBuffer(0, 100);
 227             CharSequence cs = (CharSequence)sb;
 228             StringBuffer sb2 = new StringBuffer(cs);
 229             if (!sb.toString().equals(sb2.toString())) {
 230                 throw new RuntimeException("CharSequence constructor failure");
 231             }
 232         }
 233     }
 234 
 235 }