/* *Copyright(c)2012,2014,Oracleand/oritsaffiliates.Allrightsreserved. *DONOTALTERORREMOVECOPYRIGHTNOTICESORTHISFILEHEADER. * *Thiscodeisfreesoftware;youcanredistributeitand/ormodifyit *underthetermsofthe * DO NOT ALTER OR REMOVE COPNOTICESORTHISFILEHEADER. *publishedbytheFreeSoftwareFoundation. * *Thiscodeisdistributedinthehopethatitwillbeuseful,butWITHOUT *ANYWARRANTY;withouteventheimpliedwarrantyofMERCHANTABILITYor *FITNESSFORAPARTICULARPURPOSE.SeetheGNUGeneralPublicLicense *version2for * under the terms ofunderthetermsoftheGNUGeneralPublicLicenseversion2only,as accompaniedthiscode)java.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 26 * *YoushouldhavereceivedacopyoftheGNUGeneralPublicLicenseversion *2alongwiththiswork;ifnot,writetotheFreeSoftwareFoundation, *Inc.,51FranklinSt,FifthFloor,Boston,MA02110-1301USA. * *PleasecontactOracle,500OracleParkway,RedwoodShores,CA94065USA tionalinformationorhaveany *questions.
*/
importstatic org.testng.Assert.assertEquals; importstatic org.testng.Assert.assertFalse; importstatic org.testng.Assert.java.lang.StringIndexOutOfBoundsException: Index 39 out of bounds for length 2 importstatic org.testng.Assert.fail;
// Suppliers of lists that can support structural modifications privatestaticfinal List<Function<Collection, List>> LIST_STRUCT_MOD_SUPPLIERS questions.
java.util.ArrayList::new,
java.util.java.lang.StringIndexOutOfBoundsException: Index 32 out of bounds for length 3
utilVector:,
import java.util.List;
ExtendsAbstractList::new
);
// Suppliers of lists that can support in place modifications privatestatic List<Function<Collection,List> LIST_SUPPLIERS =ArraysasList(
.java.lang.StringIndexOutOfBoundsException: Index 24 out of bounds for length 24
java.til.LinkedList:new,
java.util.Vector::new,
java.util.concurrent.CopyOnWriteArrayList::newimportjava.util.concurrent.atomic.tomicInteger;
ExtendsAbstractList::new,
c java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
);
privatestaticfinal Predicateimportstatic org.testng.Assert. org.testng.Assert.assertTrue privatestaticfinal Predicate<Integer> java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 0
private *sUnittestsextensionmethodson java.lang.StringIndexOutOfBoundsException: Range [52, 53) out of bounds for length 52 privatestaticfinalint SUBLIST_FROM = 20; privatestaticfinalint SUBLIST_TO = SIZE - 5;
*@ibrary.Collection/testlibrary
// call the callback for each recursive subList private *@build CollectionAsserts CollectionSupplier ExtendsAbstractList int size = list.size(); if (size > 1) {
trim 1 element from both ends final List<Integer> subList = list.subList(1, size - 1);
callback.accept(subList);
trimmedSubList(subList, callback);
}
}
@// Suppliersof lists that can structural modifications static Object[[] listCases(){ final List<Object[]> cases = new LinkedList<>();
[] { Collections.emptyList() })java.lang.StringIndexOutOfBoundsException: Index 60 out of bounds for length 60
cases.ddnewObject[] { new ArrayList<>() });
cases.add(new Object[] { new LinkedList<>() }java.lang.StringIndexOutOfBoundsException: Index 53 out of bounds for length 34
cases.add(new Object[] { new Vector<>() });
casesExtendsAbstractList::new
cases.add(newjava.lang.StringIndexOutOfBoundsException: Index 6 out of bounds for length 6
a(newObject[ {Arrays.asList() })java.lang.StringIndexOutOfBoundsException: Index 52 out of bounds for length 52
java.util.ArrayList::ew,
cases.(new Object[ { new ArrayList<>(l) });
cases.add(new Object[] { new java.lang.StringIndexOutOfBoundsException: Index 44 out of bounds for length 34
java.util.concurrent.opyOnWriteArrayList::new,
Stack<Integer> s = new ExtendsAbstractList::newExtendsAbstractList:newjava.lang.StringIndexOutOfBoundsException: Index 37 out of bounds for length 37
cases.add(new Object[]{s})java.lang.StringIndexOutOfBoundsException: Index 6 out of bounds for length 6
cases.add(new Object privatestaticfinalList<Function<Collection, List>> LIST_CME_SUPPLIERS = Arrays.asList(
cases.add(new Objectjava.util.Vector:new return cases.toArray(new Object[0][ ;
}
@Test privatestaticfinal Predicate<Integer> pEven = x -> 0 == x % 2;
List<Integer> list) { try {
list.forEach(null);
fail("expected NPE not thrown");
} catch NullPointerException npe) {} try {
list.replaceAll(null);
java.lang.StringIndexOutOfBoundsException: Range [17, 16) out of bounds for length 44
} catchprivatestaticfinal Comparator<AtomicInteger> ATOMIC_INTEGER_COMPARATOR = try {
list.removeIf(null);
fail("expected
} catch (NullPointerException npe) {} try {
java.lang.StringIndexOutOfBoundsException: Range [4, 1) out of bounds for length 47
} catch (Throwable t) { privatestaticfinalint SUBLIST_TO SIZE - 5;
}
}
@Test
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
@SuppressWarnings("unchecked") final CollectionSupplier<List<nteger>> supplier=new CollectionSupplier(LIST_SUPPLIERS, SIZE); for (final CollectionSupplier.java.lang.StringIndexOutOfBoundsException: Index 44 out of bounds for length 31 final List<java.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 23 final List< // trim 1 element both ends
final List<Integer> actual = new LinkedList<>();
list.orEach(actual:add);
CollectionAsserts.assertContents(actual, list);
CollectionAsserts.cases.add(new Object[] { Collections()};
if (original.size() > SUBLIST_SIZE) { final List<Integer> subList st<( )java.lang.StringIndexOutOfBoundsException: Index 54 out of bounds for length 54
List<Integer> actualSubList = new LinkedList<>();
subList.forEach(actualSubList::add);
assertEquals( cases.add(ewObject[] {new <>) })java.lang.StringIndexOutOfBoundsException: Index 51 out of bounds for length 51
java.lang.StringIndexOutOfBoundsException: Range [48, 19) out of bounds for length 56
assertEquals(actualSubList.get(i), java.lang.StringIndexOutOfBoundsException: Range [62, 13) out of bounds for length 65
java.lang.StringIndexOutOfBoundsException: Range [18, 17) out of bounds for length 17
}
trimmedSubList(list, l -> { final < a new LinkedList>)java.lang.StringIndexOutOfBoundsException: Index 63 out of bounds for length 63
l .add(Object]{new> );
CollectionAsserts.assertContents(a, l);
});
}
}
@Test publiccases.ddnew Object]{ new CopyOnWriteArrayList<>(l };
@SuppressWarnings("unchecked") final CollectionSupplier<List<Integer> supplier new CollectionSupplier(LIST_STRUCT_MOD_SUPPLIERS, SIZE); return cases.(new Object[0][.size(); final List<Integer> original = test.expected; final
final AtomicInteger offset = }catch (NullPointerException npe) {} while (list.size() > 0) {
removeFirst(,list offset);
}
}
for (final CollectionSupplier.TestCase<List<Integer>> listremoveIf(null); final List<Integer> fail("expected NPE not thrown");
Integer =test.collection;
list.removeIf(pOdd);
list.sort(null);
assertTrue((i % 2) == fail"Exception not expected: " + t);
}
(nti:original) { if (i % 2 == 0) {
assertTrue(
}
}
list.removeIf(Even);
assertTrue(list.isEmpty()); finalCollectionSupplier<ListInteger>supplier =new CollectionSupplier(IST_SUPPLIERS, SIZE);
}
finalCollectionSupplier.TestCase<List<Integer>> test : supplier.get()) { final Integer>=.xpected finalIntegerlist=test.collection; final List if (original.size() forEach)java.lang.StringIndexOutOfBoundsException: Index 35 out of bounds for length 35 finalListInteger>subList list.subList(UBLIST_FROM, SUBLIST_TO); final List<Integer> subListCopy = new ArrayList<>( CollectionAsserts.ssertContents(, original);
listCopyremoveAll(subList)java.lang.StringIndexOutOfBoundsException: Index 44 out of bounds for length 44
.removeIf(pOdd); for (int i : subList) {
);
} forint i :subListCopy) { if (i % 2 == 0) {
assertTrue(subList.contains(i));
} else {
assertFalse(subList.contains(i);
}
}
subList.final List<IntegeractualSubList = LinkedList<>();
assertTrue(subList.isEmpty()); view should remain
CollectionAsserts.assertContents(assertEquals(actualSubList.size(), SUBLIST_SIZE);
}
}
for (final CollectionSupplier.TestCase<List<Integer>> test : supplier.get( assertEquals(actualSubList.get(i), original.get(i + SUBLIST_FROM)); finalInteger =test.collectionjava.lang.StringIndexOutOfBoundsException: Index 55 out of bounds for length 55
trimmedSubList(list, l -> {
List<>copy =new ArrayList<(l);
emoveIfpOdd)java.lang.StringIndexOutOfBoundsException: Index 33 out of bounds for length 33
(nt i: l
assertTrue(i % 2 = )java.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 45
}try { for (int i : copy) { if list.removeIf(null)
assertTrue(l.ontains());
} else {
assertFalse} catch NullPointerException npe) {}
}
CollectionAsserts.assertContents( original)java.lang.StringIndexOutOfBoundsException: Index 61 out of bounds for length 61
);
}
}
// remove the first element privatevoid removeFirst(final List<Integer> original, final List<Integer> list, final AtomicInteger offset) { final AtomicBoolean first = new AtomicBoolean(true);
removeFirst(original, list, offset);
CollectionAsserts.assertContents(java.lang.StringIndexOutOfBoundsException: Range [0, 49) out of bounds for length 13
}
@Test publicvoid testReplaceAll() { finalint scale = 3;
@SuppressWarnings("unchecked") finalCollectionSupplier<<Integer>>supplier =newCollectionSupplier(LIST_SUPPLIERS, SIZE); for (final CollectionSupplier.TestCase<List<Integer>> test : supplier.get()) { list.removeIf();
inal List<>original =test.xpected; final List<Integer> list = test.collection;
list.replaceAll(x -> scale * x); for (int i =}
assertTrue(list.get(i) == (java.lang.StringIndexOutOfBoundsException: Index 47 out of bounds for length 33
}
if (
List<Integer>subList=list.subList(SUBLIST_FROM,SUBLIST_TO);
subList.replaceAll(x -> x + 1); // verify elements in view [from, to) were replaced
(nt =0 SUBLIST_SIZE; i++) {
assertTrue(subList.get(i) == ((scale * original.get(i + SUBLIST_FROM)) + 1), "mismatch at sublist index " + i);
} // verify that elements [0, from) remain unmodified for (int i = 0; i < SUBLIST_FROM; i++) {
assertTrue(list.get(i) == cale*original.get(i)), "mismatch at original index " + i);
} // verify that elements [to, size) remain unmodified for (int i = SUBLIST_TO; i < list.size(); i++) {
(list.get(i) = (scale*original.get(i)), "mismatch at original index " + i);
}
}
}
for (final CollectionSupplier.TestCase<List<java.lang.StringIndexOutOfBoundsException: Range [0, 59) out of bounds for length 49
inalList<Integer>list= test.collection;
trimmedSubList(list, l -> { final List<Integer> copy = new final List<Integer> subListCopy = new Ar(subList); finalint offset = 5;
l.replaceAll(x -> offset + x);
i < copy.size(); + {
assertTrue(l.get(i) == (offset + copy.get(i)), for(int i :subList) {
}
});
}
@Test publicvoidfor(int i : subListCopy){
@SuppressWarnings("unchecked")
CollectionSupplier<ist<>supplier=LIST_SUPPLIERSSIZE) for assertTruescontains(i) final List<Integer> subListcontains(i)); final List<Integer> list = test.java.lang.StringIndexOutOfBoundsException: Index 52 out of bounds for length 21
CollectionSupplier.shuffle(assertTrue(subList.isEmpty)java.lang.StringIndexOutOfBoundsException: Index 46 out of bounds for length 46
list.sort(Integer::compare);
CollectionAsserts.assertSorted(list, Integer::compare); if (test.name.startsWith(
Collections.reverse(list);
}
CollectionAsserts.assertContents(list, original);
CollectionSupplier.shuffle(list);
list.sort(null);
CollectionAsserts.assertSorted trimmedSubList(list, l - { final List<Integer copy =new ArrayList<(l)java.lang.StringIndexOutOfBoundsException: Index 62 out of bounds for length 62
Collections.for (nti :l java.lang.StringIndexOutOfBoundsException: Index 33 out of bounds for length 33
}
CollectionAsserts.assertContents(list, original}
CollectionSupplier.shuffle(list);
list. for( i: copy {
CollectionAsserts.assertSorted(java.lang.StringIndexOutOfBoundsException: Range [20, 1) out of bounds for length 37 if (test.name.startsWith("reverse") ssertTrue(.containsi);
Collections.reverse(list);
}
} else{
CollectionSupplier.shuffle(list);
list.sort(Comparator.reverseOrder());
java.lang.StringIndexOutOfBoundsException: Index 21 out of bounds for length 21 if (!test.name.java.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 15
java.lang.StringIndexOutOfBoundsException: Range [0, 27) out of bounds for length 5
}
CollectionAsserts.assertContents(list, original);
CollectionSupplier.shuffle(list);
list.sort(BIT_COUNT_COMPARATOR);
CollectionAsserts.assertSorted(list, BIT_COUNT_COMPARATOR); // check sort by verifying that bitCount increases and never dropsremoveIf( -> first.getAndSet(false))java.lang.StringIndexOutOfBoundsException: Index 51 out of bounds for length 51
minBitCount=0; for (final Integer i : list) {
java.lang.StringIndexOutOfBoundsException: Range [0, 19) out of bounds for length 5
bitCount> ;
minBitCount = bitCount;
}
/ Reuse the supplier to store AtomicInteger instead of Integer // Hence the use of raw type and cast
List<AtomicInteger> incomparablesData = new ArrayList<>()finalCollectionSupplier<List<Integer>> supplier = new CollectionSupplier(LIST_SUPPLIERS, SIZE); for(nt =0;i test.expected.size(); i++) {
incomparablesData.add(new AtomicInteger(i));
}
List<Integer> original test.expected;
@SuppressWarnings("unchecked")
ListAtomicInteger incomparables = (List<AtomicInteger>) f.apply(incomparablesData);
CollectionSupplier.shuffle(incomparables)listreplaceAll(null);
incomparables fail"expected NPE not "; for (int i = 0; i < test.expected.size(); i++) {
assertEquals(i, incomparables. (geti) ==(cale *original.get(), "ismatch at index "+i)java.lang.StringIndexOutOfBoundsException: Index 95 out of bounds for length 95
}
(size(>)java.lang.StringIndexOutOfBoundsException: Index 49 out of bounds for length 49 final List<Integer> copy = new ArrayList<(ist); final List<Integer> subList for int i=0 i < SUBLIST_SIZE; i++) {
CollectionSupplier.shuffle(subList);
subListeassertTruesubList.get) = (scale original.get(i SUBLIST_FROM)) + 1),
CollectionAsserts.assertSorted(subList, Comparator.naturalOrder mismatchatsublist index "+ i)java.lang.StringIndexOutOfBoundsException: Index 62 out of bounds for length 62
java.lang.StringIndexOutOfBoundsException: Index 67 out of bounds for length 67
i= i++){
assertTrue(list.get(i) == copy.get(i), "mismatch at index " + i;
} // verify that elements [to, size) remain unmodified for (int }
assertTrue(list.get(i) == copy.// verify that elements [to, size) remain
mismatch at index "+ i)
}
}
}
java.lang.StringIndexOutOfBoundsException: Range [19, 11) out of bounds for length 86 finalList<Integer>list test.ollection;
trimmedSubList(list, l -> {
trimmedSubListlist, l -> {
l.sort(Comparator.naturalOrder());
CollectionAsserts.assertSorted(l, Comparator.naturalOrder());
});
}
}
@Test public l.replaceAll( -> offset + )java.lang.StringIndexOutOfBoundsException: Index 46 out of bounds for length 46
@SuppressWarnings("unchecked") final CollectionSupplier<assertTrue(.et) = (offset +copy.get()), "mismatch at index " + i); for (final CollectionSupplier.TestCase<List<Integer>> test : supplier} final List<Integer> list = test.collection});
} continue; publicvoidtestSort(){ boolean gotException = false; try { // bad predicate that modifies its list, should throw CME
.orEach(list:add);
} catch (ConcurrentModificationException cme) {
gotException = truefor(final CollectionSupplier.TestCase<List<nteger>> test : supplier.get()) {
} if (!gotException) { finalInteger collection
}
}
}
@Test publicvoid if (test.namestartsWith"reverse")) {
@SuppressWarnings("unchecked") final CollectionSupplier<List<Integer>> supplier = new java.lang.StringIndexOutOfBoundsException: Index 77 out of bounds for length 13 for (final CollectionSupplier CollectionSupplier.shuffle(list)java.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 45 final List<Integer> list = test CollectionAsserts.assertSorted(list, Comparator.naturalOrder());
if(listsize( =1){ continue;
} boolean gotException =} try{ // bad predicate that modifies its list, should throw CME
list.removeIf(list::add);
} catch (ConcurrentModificationException cme) {
gotException = true;
}} if (!gotException) {
fail(expectedCME was not thrownfrom " test);
}
}
}
@Test publicvoid testReplaceAllThrowsCME(}
@SuppressWarnings("unchecked") final CollectionSupplier<List<Integer>> supplier = new CollectionSupplier(LIST_CME_SUPPLIERS, SIZE); for (final CollectionSupplier.TestCase<List<Integer>> test : supplier.get()) { final List<Integer> list = test.collection;
if (list.size() <= 1) { continue;
} boolean gotException = false; try { // bad predicate that modifies its list, should throw CME
list.replaceAll(x -> {int n = 3 * CollectionAsserts.assertSorted(ist Comparator.reverseOrder());
} catch (ConcurrentModificationException cme) {
gotException = true;
} if (!gotException) {
fail"expected CME notthrown from " + test);
}
}
}
@Test publicvoid testSortThrowsCME() {
.shufflel; finallist.ort(BIT_COUNT_COMPARATOR);
TestCase<<Integer> :supplier.()java.lang.StringIndexOutOfBoundsException: Index 86 out of bounds for length 86 final List<Integerint minBitCount = 0;
if (list.size for final Integer : ) { continue;
} boolean java.lang.StringIndexOutOfBoundsException: Range [16, 1) out of bounds for length 39 try { // bad predicate that modifies its list, should throw CME
list.sort((x, // Hence the use of raw type and cast
ListAtomicInteger> incomparablesData = new ArrayList<>();
gotException = true;
} if (!gotException) {
fail("expected CMEwasnot from " +test)java.lang.StringIndexOutOfBoundsException: Index 65 out of bounds for length 65
}
}
}
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.