use of java.util.ListIterator in project j2objc by google.
the class AbstractListTest method test_listIterator.
/**
* @tests java.util.AbstractList#listIterator()
*/
public void test_listIterator() {
Integer tempValue;
List list = new ArrayList();
list.add(new Integer(3));
list.add(new Integer(15));
list.add(new Integer(5));
list.add(new Integer(1));
list.add(new Integer(7));
ListIterator lit = list.listIterator();
assertTrue("Should not have previous", !lit.hasPrevious());
assertTrue("Should have next", lit.hasNext());
tempValue = (Integer) lit.next();
assertTrue("next returned wrong value. Wanted 3, got: " + tempValue, tempValue.intValue() == 3);
tempValue = (Integer) lit.previous();
SimpleList list2 = new SimpleList();
list2.add(new Object());
ListIterator lit2 = list2.listIterator();
lit2.add(new Object());
lit2.next();
//Regression test for Harmony-5808
list = new MockArrayList();
ListIterator it = list.listIterator();
it.add("one");
it.add("two");
assertEquals(2, list.size());
}
use of java.util.ListIterator in project mapdb by jankotek.
the class CopyOnWriteArrayListTest method testListIterator1.
// /**
// * lastIndexOf returns the index from the given starting point
// */
// public void testLastIndexOf2() {
// List full = populatedArray(3);
// full.add(one);
// full.add(three);
// assertEquals(3, full.lastIndexOf(one, 4));
// assertEquals(-1, full.lastIndexOf(three, 3));
// }
/**
* listIterator traverses all elements
*/
public void testListIterator1() {
List full = populatedArray(SIZE);
ListIterator i = full.listIterator();
int j;
for (j = 0; i.hasNext(); j++) assertEquals(j, i.next());
assertEquals(SIZE, j);
}
use of java.util.ListIterator in project j2objc by google.
the class CollectionsTest method test_unmodifiableListLjava_util_List.
/**
* @tests java.util.Collections#unmodifiableList(java.util.List)
*/
public void test_unmodifiableListLjava_util_List() {
// Test for method java.util.List
// java.util.Collections.unmodifiableList(java.util.List)
// test with a Sequential Access List
boolean exception = false;
List c = Collections.unmodifiableList(ll);
// Ensure a NPE is thrown if the list is NULL
try {
Collections.unmodifiableList(null);
fail("Expected NullPointerException for null list parameter");
} catch (NullPointerException e) {
}
assertTrue("Returned list is of incorrect size", c.size() == ll.size());
assertTrue("Returned List should not implement Random Access interface", !(c instanceof RandomAccess));
Iterator i = ll.iterator();
while (i.hasNext()) assertTrue("Returned list missing elements", c.contains(i.next()));
try {
c.add(new Object());
} catch (UnsupportedOperationException e) {
exception = true;
// Correct
}
if (!exception) {
fail("Allowed modification of list");
}
try {
c.remove(new Object());
fail("Allowed modification of list");
} catch (UnsupportedOperationException e) {
// Correct
}
// test with a Random Access List
List smallList = new ArrayList();
smallList.add(null);
smallList.add("yoink");
c = Collections.unmodifiableList(smallList);
assertNull("First element should be null", c.get(0));
assertTrue("List should contain null", c.contains(null));
assertTrue("T1. Returned List should implement Random Access interface", c instanceof RandomAccess);
smallList = new ArrayList();
for (int counter = 0; counter < 100; counter++) {
smallList.add(objArray[counter]);
}
List myList = Collections.unmodifiableList(smallList);
assertTrue("List should not contain null", !myList.contains(null));
assertTrue("T2. Returned List should implement Random Access interface", myList instanceof RandomAccess);
assertTrue("get failed on unmodifiable list", myList.get(50).equals(new Integer(50)));
ListIterator listIterator = myList.listIterator();
for (int counter = 0; listIterator.hasNext(); counter++) {
assertTrue("List has wrong elements", ((Integer) listIterator.next()).intValue() == counter);
}
new Support_UnmodifiableCollectionTest("", smallList).runTest();
}
use of java.util.ListIterator in project undertow by undertow-io.
the class HeaderValues method iterator.
private ListIterator<String> iterator(final int start, final boolean forwards) {
return new ListIterator<String>() {
int idx = start;
int returned = -1;
public boolean hasNext() {
return idx < size;
}
public boolean hasPrevious() {
return idx > 0;
}
public String next() {
try {
final String next;
if (forwards) {
int idx = this.idx;
next = get(idx);
returned = idx;
this.idx = idx + 1;
return next;
} else {
int idx = this.idx - 1;
next = get(idx);
this.idx = returned = idx;
}
return next;
} catch (IndexOutOfBoundsException e) {
throw new NoSuchElementException();
}
}
public int nextIndex() {
return idx;
}
public String previous() {
try {
final String prev;
if (forwards) {
int idx = this.idx - 1;
prev = get(idx);
this.idx = returned = idx;
} else {
int idx = this.idx;
prev = get(idx);
returned = idx;
this.idx = idx + 1;
return prev;
}
return prev;
} catch (IndexOutOfBoundsException e) {
throw new NoSuchElementException();
}
}
public int previousIndex() {
return idx - 1;
}
public void remove() {
if (returned == -1) {
throw new IllegalStateException();
}
HeaderValues.this.remove(returned);
returned = -1;
}
public void set(final String headerValue) {
if (returned == -1) {
throw new IllegalStateException();
}
HeaderValues.this.set(returned, headerValue);
}
public void add(final String headerValue) {
if (returned == -1) {
throw new IllegalStateException();
}
final int idx = this.idx;
HeaderValues.this.add(idx, headerValue);
this.idx = idx + 1;
returned = -1;
}
};
}
use of java.util.ListIterator in project robovm by robovm.
the class AbstractSequentialListTest method test_setILjava_lang_Object.
public void test_setILjava_lang_Object() {
AbstractSequentialList asl = new AbstractSequentialList() {
String[] buff = { "0", "1", "2", "3", "4", "5" };
final String illegalStr = "Illegal element";
int currPos = 0;
@Override
public int size() {
return buff.length;
}
@Override
public ListIterator listIterator(int index) {
currPos = index;
return new ListIterator() {
public void add(Object o) {
}
public boolean hasNext() {
return true;
}
public boolean hasPrevious() {
return false;
}
public Object next() {
return buff[currPos];
}
public int nextIndex() {
return 0;
}
public Object previous() {
return null;
}
public int previousIndex() {
return 0;
}
public void remove() {
buff[currPos] = "removed element";
}
public void set(Object o) {
if (o == null)
throw new NullPointerException();
if (o.equals(illegalStr))
throw new IllegalArgumentException();
buff[currPos] = (String) o;
}
};
}
};
try {
asl.set(asl.size() + 1, "new element");
fail("IndexOutOfBoundsException expected");
} catch (IndexOutOfBoundsException e) {
//expected
}
try {
asl.set(-1, "new element");
fail("IndexOutOfBoundsException expected");
} catch (IndexOutOfBoundsException e) {
//expected
}
for (int i = 0; i < asl.size(); i++) {
assertFalse(asl.get(i).toString().contains("new element"));
asl.set(i, "new element");
assertTrue(asl.get(i).toString().contains("new element"));
}
try {
asl.set(1, new Double(1));
fail("ClassCastException expected");
} catch (ClassCastException e) {
//
}
try {
asl.set(1, "Illegal element");
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException ee) {
//expected
}
try {
asl.set(1, null);
fail("NullPointerException expected");
} catch (NullPointerException ee) {
//expected
}
asl = new AbstractSequentialList() {
@Override
public int size() {
return 0;
}
@Override
public ListIterator listIterator(int index) {
return new Mock_unsupportedListIterator();
}
};
try {
asl.set(0, "New element");
fail("UnsupportedOperationException expected");
} catch (UnsupportedOperationException e) {
//expected
}
}
Aggregations