cleanup and test

This commit is contained in:
Alexey Kudravtsev
2011-09-15 13:10:18 +04:00
parent 5faa21d5ca
commit 59f6f71a89
2 changed files with 81 additions and 16 deletions
@@ -16,6 +16,7 @@
package com.intellij.util;
import com.intellij.util.containers.EmptyIterator;
import org.jetbrains.annotations.NotNull;
import java.util.*;
@@ -31,7 +32,7 @@ public class SmartList<E> extends AbstractList<E> {
add(elem);
}
public SmartList(Collection<? extends E> c) {
public SmartList(@NotNull Collection<? extends E> c) {
addAll(c);
}
@@ -65,7 +66,9 @@ public class SmartList<E> extends AbstractList<E> {
if (newCapacity < minCapacity) {
newCapacity = minCapacity;
}
myElem = array = Arrays.copyOf(array, newCapacity);
Object[] oldArray = array;
myElem = array = new Object[newCapacity];
System.arraycopy(oldArray, 0, array, 0, oldCapacity);
}
array[mySize] = e;
}
@@ -99,6 +102,7 @@ public class SmartList<E> extends AbstractList<E> {
oldValue = (E)array[index];
array[index] = element;
}
modCount++;
return oldValue;
}
@@ -166,14 +170,9 @@ public class SmartList<E> extends AbstractList<E> {
}
}
public boolean isEmpty() {
return mySize == 0;
}
public void sort(Comparator<? super E> comparator) {
public void sort(@NotNull Comparator<? super E> comparator) {
if (mySize >= 2) {
E[] array = (E[])myElem;
Arrays.sort(array, 0, mySize, comparator);
Arrays.sort((E[])myElem, 0, mySize, comparator);
}
}
@@ -15,8 +15,11 @@
*/
package com.intellij.util;
import com.intellij.util.containers.EmptyIterator;
import junit.framework.TestCase;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
/**
@@ -24,8 +27,7 @@ import java.util.List;
*/
public class SmartListTest extends TestCase {
public void testEmpty() {
List<Integer> l = new SmartList<Integer>();
assertEquals(0, l.size());
assertEquals(0, new SmartList<Integer>().size());
}
public void testOneElement() {
@@ -56,15 +58,79 @@ public class SmartListTest extends TestCase {
}
public void testFourElement() {
List<Integer> l = new SmartList<Integer>();
l.add(new Integer(1));
l.add(new Integer(2));
l.add(new Integer(3));
l.add(new Integer(4));
SmartList<Integer> l = new SmartList<Integer>();
int modCount = 0;
assertEquals(modCount, l.getModificationCount());
l.add(new Integer(1)); assertEquals(++modCount, l.getModificationCount());
l.add(new Integer(2)); assertEquals(++modCount, l.getModificationCount());
l.add(new Integer(3)); assertEquals(++modCount, l.getModificationCount());
l.add(new Integer(4)); assertEquals(++modCount, l.getModificationCount());
assertEquals(4, l.size());
assertEquals(1, l.get(0).intValue());
assertEquals(2, l.get(1).intValue());
assertEquals(3, l.get(2).intValue());
assertEquals(4, l.get(3).intValue());
assertEquals(modCount, l.getModificationCount());
l.remove(2);
assertEquals(3, l.size());
assertEquals(++modCount, l.getModificationCount());
assertEquals("[1, 2, 4]", l.toString());
l.set(2, 3);
assertEquals(3, l.size());
assertEquals(++modCount, l.getModificationCount());
assertEquals("[1, 2, 3]", l.toString());
l.clear();
assertEquals(0, l.size());
assertEquals(++modCount, l.getModificationCount());
assertEquals("[]", l.toString());
boolean thrown = false;
try {
l.set(1, 3);
}
catch (IndexOutOfBoundsException e) {
thrown = true;
}
assertTrue("IndexOutOfBoundsException must be thrown", thrown);
l.clear();
assertEquals(0, l.size());
assertEquals(++modCount, l.getModificationCount());
assertEquals("[]", l.toString());
Iterator<Integer> iterator = l.iterator();
assertSame(EmptyIterator.getInstance(), iterator);
assertFalse(iterator.hasNext());
l.add(-2);
iterator = l.iterator();
assertNotSame(EmptyIterator.getInstance(), iterator);
assertTrue(iterator.hasNext());
assertEquals(-2, iterator.next().intValue());
assertFalse(iterator.hasNext());
thrown = false;
try {
l.get(1);
}
catch (IndexOutOfBoundsException e) {
thrown = true;
}
assertTrue("IndexOutOfBoundsException must be thrown", thrown);
l.addAll(l);
assertEquals(2, l.size());
assertEquals("[-2, -2]", l.toString());
thrown = false;
try {
l.addAll(l);
}
catch (ConcurrentModificationException e) {
thrown = true;
}
assertTrue("ConcurrentModificationException must be thrown", thrown);
}
}