From 59f6f71a8911b948d10dddbf1c5f0b962e938b6f Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Wed, 14 Sep 2011 15:16:44 +0400 Subject: [PATCH] cleanup and test --- .../util/src/com/intellij/util/SmartList.java | 17 ++-- .../com/intellij/util/SmartListTest.java | 80 +++++++++++++++++-- 2 files changed, 81 insertions(+), 16 deletions(-) diff --git a/platform/util/src/com/intellij/util/SmartList.java b/platform/util/src/com/intellij/util/SmartList.java index 578a1e23ce04..dade91b8c0a2 100644 --- a/platform/util/src/com/intellij/util/SmartList.java +++ b/platform/util/src/com/intellij/util/SmartList.java @@ -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 extends AbstractList { add(elem); } - public SmartList(Collection c) { + public SmartList(@NotNull Collection c) { addAll(c); } @@ -65,7 +66,9 @@ public class SmartList extends AbstractList { 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 extends AbstractList { oldValue = (E)array[index]; array[index] = element; } + modCount++; return oldValue; } @@ -166,14 +170,9 @@ public class SmartList extends AbstractList { } } - public boolean isEmpty() { - return mySize == 0; - } - - public void sort(Comparator comparator) { + public void sort(@NotNull Comparator comparator) { if (mySize >= 2) { - E[] array = (E[])myElem; - Arrays.sort(array, 0, mySize, comparator); + Arrays.sort((E[])myElem, 0, mySize, comparator); } } diff --git a/platform/util/testSrc/com/intellij/util/SmartListTest.java b/platform/util/testSrc/com/intellij/util/SmartListTest.java index 0ab6edf5dd6f..035e040c5810 100644 --- a/platform/util/testSrc/com/intellij/util/SmartListTest.java +++ b/platform/util/testSrc/com/intellij/util/SmartListTest.java @@ -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 l = new SmartList(); - assertEquals(0, l.size()); + assertEquals(0, new SmartList().size()); } public void testOneElement() { @@ -56,15 +58,79 @@ public class SmartListTest extends TestCase { } public void testFourElement() { - List l = new SmartList(); - l.add(new Integer(1)); - l.add(new Integer(2)); - l.add(new Integer(3)); - l.add(new Integer(4)); + SmartList l = new SmartList(); + 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 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); } }