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