Path: blob/master/test/jdk/java/lang/StringBuffer/SBBasher.java
41152 views
/*1* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 412069426* @summary Test new methods in StringBuffer27* @key randomness28*/2930import java.lang.*;31import java.util.*;3233public class SBBasher {34public static void main(String[] args) throws Exception {35SBBasher basher = new SBBasher();3637for (int iterations=0; iterations<100; iterations++) {38String testString = basher.generateTestString();39boolean result = basher.Test1(testString);40if (result == false)41throw new RuntimeException("Substring or replace failure.");42}4344for (int iterations=0; iterations<100; iterations++) {45String testString = basher.generateTestString();46boolean result = basher.Test2(testString);47if (result == false)48throw new RuntimeException("Insert or delete failure.");49}5051for (int iterations=0; iterations<100; iterations++) {52String testString = basher.generateTestString();53boolean result = basher.Test3(testString);54if (result == false)55throw new RuntimeException("Insert, delete or replace failure.");56}5758}5960private int getRandomIndex(int constraint1, int constraint2) {61int range = constraint2 - constraint1;62int x = generator.nextInt();63return constraint1 + Math.abs(x % range);64}6566static Random generator = new Random();6768private String generateTestString() {69StringBuffer aNewString = new StringBuffer(120);70int aNewLength = getRandomIndex(1,100);71for(int y=0; y<aNewLength; y++) {72int achar = generator.nextInt();73char test = (char)(achar);74aNewString.append(test);75}76return aNewString.toString();77}7879/**80* first test, get substring of a random string81* and replace same spot with same substring82* then check for equality with original83* this tests both substrings and the replace method84*/85private boolean Test1(String before) {86StringBuffer bashed = new StringBuffer(before);87String slice;88for (int i=0; i<100; i++) {89int startIndex = getRandomIndex(0, before.length());90int endIndex = getRandomIndex(startIndex, before.length());91if (endIndex < bashed.length()) {92slice = bashed.substring(startIndex, endIndex);93}94else {95slice = bashed.substring(startIndex);96}97bashed.replace(startIndex, endIndex, slice);98}99String after = bashed.toString();100if (!before.equals(after))101return false;102else103return true;104}105106/**107* second test, delete random section of string108* then insert same section back again109* then check for equality with original110* this tests both substrings, both deletes and insert method111*/112private boolean Test2(String before) {113StringBuffer bashed = new StringBuffer(before);114String slice;115for (int i=0; i<100; i++) {116int startIndex = getRandomIndex(0, before.length());117int endIndex = getRandomIndex(startIndex, before.length());118if (endIndex < bashed.length())119slice = bashed.substring(startIndex, endIndex);120else121slice = bashed.substring(startIndex);122if (slice.length() == 1)123bashed.deleteCharAt(startIndex);124else125bashed.delete(startIndex, endIndex);126bashed.insert(startIndex, slice.toCharArray(), 0, slice.length());127}128String after = bashed.toString();129if (!before.equals(after))130return false;131else132return true;133}134135/**136* Third test, clone string and make sure that the137* replace operation is equivalent to a delete followed138* by an insert with the equivalent arguments139* this tests replace, delete and insert140*/141private boolean Test3(String before) {142StringBuffer bashed1 = new StringBuffer(before);143StringBuffer bashed2 = new StringBuffer(before);144int startIndex = getRandomIndex(0, bashed1.length());145int endIndex = getRandomIndex(startIndex, bashed2.length());146147String insertString = generateTestString();148149bashed1.delete(startIndex, endIndex);150bashed1.insert(startIndex, insertString);151bashed2.replace(startIndex, endIndex, insertString);152153String result1 = bashed1.toString();154String result2 = bashed2.toString();155if (!result1.equals(result2))156return false;157else158return true;159}160161}162163164