for (int iterations=0; iterations<100; iterations++) {
String testString = basher.generateTestString(); boolean result = basher.Test1(testString); if (result == false) thrownew RuntimeException("Substring or replace failure.");
}
for (int iterations=0; iterations<100; iterations++) {
String testString = basher.generateTestString(); boolean result = basher.Test2(testString); if (result == false) thrownew RuntimeException("Insert or delete failure.");
}
for (int iterations=0; iterations<100; iterations++) {
String testString = basher.generateTestString(); boolean result = basher.Test3(testString); if (result == false) thrownew RuntimeException("Insert, delete or replace failure.");
}
}
privateint getRandomIndex(int constraint1, int constraint2) { int range = constraint2 - constraint1; int x = generator.nextInt(); return constraint1 + Math.abs(x % range);
}
static Random generator = new Random();
private String generateTestString() {
StringBuffer aNewString = new StringBuffer(120); int aNewLength = getRandomIndex(1,100); for(int y=0; y<aNewLength; y++) { int achar = generator.nextInt(); char test = (char)(achar);
aNewString.append(test);
} return aNewString.toString();
}
/** *firsttest,getsubstringofarandomstring *andreplacesamespotwithsamesubstring *thencheckforequalitywithoriginal *thistestsbothsubstringsandthereplacemethod
*/ privateboolean Test1(String before) {
StringBuffer bashed = new StringBuffer(before);
String slice; for (int i=0; i<100; i++) { int startIndex = getRandomIndex(0, before.length()); int endIndex = getRandomIndex(startIndex, before.length()); if (endIndex < bashed.length()) {
slice = bashed.substring(startIndex, endIndex);
} else {
slice = bashed.substring(startIndex);
}
bashed.replace(startIndex, endIndex, slice);
}
String after = bashed.toString(); if (!before.equals(after)) returnfalse; else returntrue;
}
/** *secondtest,deleterandomsectionofstring *theninsertsamesectionbackagain *thencheckforequalitywithoriginal *thistestsbothsubstrings,bothdeletesandinsertmethod
*/ privateboolean Test2(String before) {
StringBuffer bashed = new StringBuffer(before);
String slice; for (int i=0; i<100; i++) { int startIndex = getRandomIndex(0, before.length()); int endIndex = getRandomIndex(startIndex, before.length()); if (endIndex < bashed.length())
slice = bashed.substring(startIndex, endIndex); else
slice = bashed.substring(startIndex); if (slice.length() == 1)
bashed.deleteCharAt(startIndex); else
bashed.delete(startIndex, endIndex);
bashed.insert(startIndex, slice.toCharArray(), 0, slice.length());
}
String after = bashed.toString(); if (!before.equals(after)) returnfalse; else returntrue;
}
/** *Thirdtest,clonestringandmakesurethatthe *replaceoperationisequivalenttoadeletefollowed *byaninsertwiththeequivalentarguments *thistestsreplace,deleteandinsert
*/ privateboolean Test3(String before) {
StringBuffer bashed1 = new StringBuffer(before);
StringBuffer bashed2 = new StringBuffer(before); int startIndex = getRandomIndex(0, bashed1.length()); int endIndex = getRandomIndex(startIndex, bashed2.length());
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.