@Test(groups = "fast") public void testResize() throws Exception { Assert.assertEquals(arraySize4.size(), 0); Assert.assertEquals(arraySize4.capacity(), 4); arraySize4.append(0L); arraySize4.append(1L); arraySize4.append(2L); arraySize4.append(4L); arraySize4.append(8L); Assert.assertEquals(arraySize4.size(), 5); Assert.assertEquals(arraySize4.capacity(), 12); Assert.assertEquals(arraySize4.get(0).longValue(), 0); Assert.assertEquals(arraySize4.get(1).longValue(), 1); Assert.assertEquals(arraySize4.get(2).longValue(), 2); Assert.assertEquals(arraySize4.get(3).longValue(), 4); Assert.assertEquals(arraySize4.get(4).longValue(), 8); }
@Override public synchronized Long remove(int i) throws ArrayIndexOutOfBoundsException { if (isEmpty(i)) { return null; } else { Long previousValue = data[i]; data[i] = EMPTY; size--; return convertValue(previousValue); } }
@Override public synchronized int append(Long value) { int myWritePosition; if (nextWritePosition >= data.length) { int sizeIncrease = (int) (data.length * MIN_GROWTH_FACTOR); sizeIncrease = Math.max(sizeIncrease, MIN_GROWTH_AMOUNT); internalResize(data.length + sizeIncrease); } // find an empty slot if the current one isn't (may have been set by set(int i, Long value) while (!isEmpty(nextWritePosition)) { nextWritePosition++; if (nextWritePosition >= data.length - 1) { // resize in advance for next write resize(data.length + MIN_GROWTH_AMOUNT); } } checkState(isEmpty(nextWritePosition) && nextWritePosition < data.length); myWritePosition = nextWritePosition; // prepare for next write nextWritePosition++; size++; data[myWritePosition] = value; return myWritePosition; }
@Test(groups = "fast") public void testSanity() throws Exception { array.append(1L); Assert.assertEquals(array.size(), 7); Assert.assertEquals(array.get(0).longValue(), 1L); array.append(2L); Assert.assertEquals(array.size(), 8); Assert.assertEquals(array.get(5).longValue(), 6L); Assert.assertEquals(array.get(7).longValue(), 2L); }
@Override public synchronized Long get(int i) throws IndexOutOfBoundsException { if (i >= size()) { throw new ArrayIndexOutOfBoundsException(); } // return null if the slot is empty return data[i] >= 0 ? data[i] : null; }
@Test(groups = "fast", expectedExceptions = IllegalStateException.class) public void testIteratorRemoveNoNext() throws Exception { Iterator<Long> iter = array.iterator(); iter.remove(); }
@Override public int resize(int sizeHint) { return internalResize(sizeHint); }
@Test(groups = "fast") public void testSanity() throws Exception { array.append(1L); Assert.assertEquals(array.size(), 7); Assert.assertEquals(array.get(0).longValue(), 1L); array.append(2L); Assert.assertEquals(array.size(), 8); Assert.assertEquals(array.get(5).longValue(), 6L); Assert.assertEquals(array.get(7).longValue(), 2L); }
@Test(groups = "fast", expectedExceptions = IllegalStateException.class) public void testIteratorRepeatedRemove() throws Exception { Iterator<Long> iter = array.iterator(); iter.next(); iter.remove(); Assert.assertEquals(iter.next().longValue(), 2L); iter.remove(); iter.remove(); }
@Test(groups = "fast") public void testSetNoSizeGrowth() throws Exception { arraySize16.append(1L); arraySize16.append(1L); arraySize16.append(1L); arraySize16.append(1L); arraySize16.set(3, 10L); Assert.assertEquals(arraySize16.size(), 4); Assert.assertEquals(arraySize16.get(3).longValue(), 10L); }
@Test(groups = "fast") public void testResize() throws Exception { Assert.assertEquals(arraySize4.size(), 0); Assert.assertEquals(arraySize4.capacity(), 4); arraySize4.append(0L); arraySize4.append(1L); arraySize4.append(2L); arraySize4.append(4L); arraySize4.append(8L); Assert.assertEquals(arraySize4.size(), 5); Assert.assertEquals(arraySize4.capacity(), 12); Assert.assertEquals(arraySize4.get(0).longValue(), 0); Assert.assertEquals(arraySize4.get(1).longValue(), 1); Assert.assertEquals(arraySize4.get(2).longValue(), 2); Assert.assertEquals(arraySize4.get(3).longValue(), 4); Assert.assertEquals(arraySize4.get(4).longValue(), 8); }