package com.carrotsearch.hppc;
import static com.carrotsearch.hppc.TestUtils.*;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.*;
import org.junit.rules.MethodRule;
import com.carrotsearch.hppc.cursors.*;
import com.carrotsearch.hppc.predicates.BytePredicate;
import com.carrotsearch.hppc.procedures.*;
/**
* Unit tests for {@link ByteArrayList}.
*/
public class ByteArrayListTest
{
/**
* Per-test fresh initialized instance.
*/
public ByteArrayList list;
/* replaceIf:applied. */ byte /* end */ key1 = 1;
/* replaceIf:applied. */ byte /* end */ key2 = 2;
/* replaceIf:applied. */ byte /* end */ key3 = 3;
/**
* Require assertions for all tests.
*/
@Rule
public MethodRule requireAssertions = new RequireAssertionsRule();
/* */
@Before
public void initialize()
{
list = new ByteArrayList();
}
@After
public void checkTrailingSpaceUninitialized()
{
if (list != null)
for (int i = list.elementsCount; i < list.buffer.length; i++)
assertTrue(((byte) 0) == list.buffer[i]);
}
/* */
@Test
public void testInitiallyEmpty()
{
assertEquals(0, list.size());
}
/* */
@Test
public void testAdd()
{
list.add(
(byte) 1,
(byte) 2);
assertListEquals(list.toArray(), 1, 2);
}
/* */
@Test
public void testAddTwoArgs()
{
list.add(
(byte) 1,
(byte) 2);
list.add(
(byte) 3,
(byte) 4);
assertListEquals(list.toArray(), 1, 2, 3, 4);
}
/* */
@Test
public void testAddArray()
{
list.add(newArray(list.buffer, 0, 1, 2, 3), 1, 2);
assertListEquals(list.toArray(), 1, 2);
}
/* */
@Test
public void testAddVarArg()
{
list.add(newArray(list.buffer, 0, 1, 2, 3));
list.add(
(byte) 4,
(byte) 5,
(byte) 6,
(byte) 7);
assertListEquals(list.toArray(), 0, 1, 2, 3, 4, 5, 6, 7);
}
/* */
@Test
public void testAddAll()
{
ByteArrayList list2 = new ByteArrayList();
list2.add(newArray(list2.buffer, 0, 1, 2));
list.addAll(list2);
list.addAll(list2);
assertListEquals(list.toArray(), 0, 1, 2, 0, 1, 2);
}
/* removeIf:applied. */
/* */
@Test
public void testInsert()
{
list.insert(0, (byte) 1);
list.insert(0, (byte) 2);
list.insert(2, (byte) 3);
list.insert(1, (byte) 4);
assertListEquals(list.toArray(), 2, 4, 1, 3);
}
/* */
@Test
public void testSet()
{
list.add(newArray(list.buffer, 0, 1, 2));
assertEquals2(0, list.set(0, (byte) 3));
assertEquals2(1, list.set(1, (byte) 4));
assertEquals2(2, list.set(2, (byte) 5));
assertListEquals(list.toArray(), 3, 4, 5);
}
/* */
@Test
public void testRemove()
{
list.add(newArray(list.buffer, 0, 1, 2, 3, 4));
list.remove(0);
list.remove(2);
list.remove(1);
assertListEquals(list.toArray(), 1, 4);
}
/* */
@Test
public void testRemoveRange()
{
list.add(newArray(list.buffer, 0, 1, 2, 3, 4));
list.removeRange(0, 2);
assertListEquals(list.toArray(), 2, 3, 4);
list.removeRange(2, 3);
assertListEquals(list.toArray(), 2, 3);
list.removeRange(1, 1);
assertListEquals(list.toArray(), 2, 3);
list.removeRange(0, 1);
assertListEquals(list.toArray(), 3);
}
/* */
@Test
public void testRemoveFirstLast()
{
list.add(newArray(list.buffer, 0, 1, 2, 1, 0));
assertEquals(-1, list.removeFirstOccurrence( (byte) 5));
assertEquals(-1, list.removeLastOccurrence( (byte) 5));
assertListEquals(list.toArray(), 0, 1, 2, 1, 0);
assertEquals(1, list.removeFirstOccurrence( (byte) 1));
assertListEquals(list.toArray(), 0, 2, 1, 0);
assertEquals(3, list.removeLastOccurrence( (byte) 0));
assertListEquals(list.toArray(), 0, 2, 1);
assertEquals(0, list.removeLastOccurrence( (byte) 0));
assertListEquals(list.toArray(), 2, 1);
assertEquals(-1, list.removeLastOccurrence( (byte) 0));
/* removeIf:applied. */
}
/* */
@Test
public void testRemoveAll()
{
list.add(newArray(list.buffer, 0, 1, 0, 1, 0));
assertEquals(0, list.removeAllOccurrences( (byte) 2));
assertEquals(3, list.removeAllOccurrences( (byte) 0));
assertListEquals(list.toArray(), 1, 1);
assertEquals(2, list.removeAllOccurrences( (byte) 1));
assertTrue(list.isEmpty());
/* removeIf:applied. */
}
/* */
@Test
public void testRemoveAllFromLookupContainer()
{
list.add(newArray(list.buffer, 0, 1, 2, 1, 0));
ByteOpenHashSet list2 = new ByteOpenHashSet();
list2.add(newArray(list2.keys, 0, 2));
assertEquals(3, list.removeAll(list2));
assertEquals(0, list.removeAll(list2));
assertListEquals(list.toArray(), 1, 1);
}
/* */
@Test
public void testRemoveAllWithPredicate()
{
list.add(newArray(list.buffer, 0, key1, key2, key1, 4));
assertEquals(3, list.removeAll(new BytePredicate()
{
public boolean apply(/* replaceIf:applied. */ byte /* end */ v)
{
return v == key1 || v == key2;
};
}));
assertListEquals(list.toArray(), 0, 4);
}
/* */
@Test
public void testRetainAllWithPredicate()
{
list.add(newArray(list.buffer, 0, key1, key2, key1, 0));
assertEquals(2, list.retainAll(new BytePredicate()
{
public boolean apply(/* replaceIf:applied. */ byte /* end */ v)
{
return v == key1 || v == key2;
};
}));
assertListEquals(list.toArray(), 1, 2, 1);
}
/* */
@Test
public void testRemoveAllWithPredicateInterrupted()
{
list.add(newArray(list.buffer, 0, key1, key2, key1, 4));
final RuntimeException t = new RuntimeException();
try
{
assertEquals(3, list.removeAll(new BytePredicate()
{
public boolean apply(/* replaceIf:applied. */ byte /* end */ v)
{
if (v == key2) throw t;
return v == key1;
};
}));
fail();
}
catch (RuntimeException e)
{
// Make sure it's really our exception...
if (e != t) throw e;
}
// And check if the list is in consistent state.
assertListEquals(list.toArray(), 0, key2, key1, 4);
assertEquals(4, list.size());
}
/* */
@Test
public void testIndexOf()
{
list.add(newArray(list.buffer, 0, 1, 2, 1, 0));
/* removeIf:applied. */
/* removeIf:applied. */
assertEquals(0, list.indexOf( (byte) 0));
assertEquals(-1, list.indexOf( (byte) 3));
assertEquals(2, list.indexOf( (byte) 2));
}
/* */
@Test
public void testLastIndexOf()
{
list.add(newArray(list.buffer, 0, 1, 2, 1, 0));
/* removeIf:applied. */
/* removeIf:applied. */
assertEquals2(4, list.lastIndexOf( (byte) 0));
assertEquals2(-1, list.lastIndexOf( (byte) 3));
assertEquals2(2, list.lastIndexOf( (byte) 2));
}
/* */
@Test
public void testEnsureCapacity()
{
list.ensureCapacity(100);
assertTrue(list.buffer.length >= 100);
list.ensureCapacity(1000);
list.ensureCapacity(1000);
assertTrue(list.buffer.length >= 1000);
}
/* Test resizing and cleaning of buffer content. */
@Test
public void testResize()
{
list.ensureCapacity(20);
Arrays.fill(list.buffer, (byte) 1);
list.resize(10);
assertEquals(10, list.size());
for (int i = 0; i < list.size(); i++)
assertEquals2(((byte) 0), list.get(i));
Arrays.fill(list.buffer, ((byte) 0));
for (int i = 5; i < list.size(); i++)
list.set(i, (byte) 1);
list.resize(5);
assertEquals(5, list.size());
for (int i = list.size(); i < list.buffer.length; i++)
assertEquals2(((byte) 0), list.buffer[i]);
}
/* */
@Test
public void testTrimToSize()
{
list.add(newArray(list.buffer, 1, 2));
list.trimToSize();
assertEquals(2, list.buffer.length);
}
/* */
@Test
public void testRelease()
{
list.add(newArray(list.buffer, 1, 2));
list.release();
assertEquals(0, list.size());
list.add(newArray(list.buffer, 1, 2));
assertEquals(2, list.size());
}
/* */
@Test
public void testGrowth()
{
final int maxGrowth = 10;
final int count = 500;
list = new ByteArrayList(0, new BoundedProportionalArraySizingStrategy(
5, maxGrowth, 2));
for (int i = 0; i < count; i++)
list.add( (byte) i);
assertEquals(count, list.size());
for (int i = 0; i < count; i++)
assertEquals2( (byte) i, list.get(i));
assertTrue("Buffer size: 510 <= " + list.buffer.length,
list.buffer.length <= count + maxGrowth);
}
/* */
@Test
public void testIterable()
{
list.add(newArray(list.buffer, 0, 1, 2, 3));
int count = 0;
for (ByteCursor cursor : list)
{
count++;
assertEquals2(list.get(cursor.index), cursor.value);
assertEquals2(list.buffer[cursor.index], cursor.value);
}
assertEquals(count, list.size());
count = 0;
list.resize(0);
for (@SuppressWarnings("unused") ByteCursor cursor : list)
{
count++;
}
assertEquals(0, count);
}
/* */
@Test
public void testIterator()
{
list.add(newArray(list.buffer, 0, 1, 2, 3));
Iterator<ByteCursor> iterator = list.iterator();
int count = 0;
while (iterator.hasNext())
{
iterator.hasNext();
iterator.hasNext();
iterator.hasNext();
iterator.next();
count++;
}
assertEquals(count, list.size());
list.resize(0);
assertFalse(list.iterator().hasNext());
}
/* */
@Test
/* removeIf:applied. */
public void testForEachWithProcedure()
{
list.add(newArray(list.buffer, 1, 2, 3));
final AtomicInteger holder = new AtomicInteger();
((ByteArrayList) list).forEach(new ByteProcedure() {
int index = 0;
public void apply(byte v)
{
assertEquals2(v, list.get(index++));
holder.set(index);
}
});
assertEquals(holder.get(), list.size());
}
/* */
@Test
/* removeIf:applied. */
public void testForEachReturnValueFromAnonymousClass()
{
list.add(newArray(list.buffer, 1, 2, 3));
int result = ((ByteArrayList) list).forEach(new ByteProcedure() {
int index = 0;
public void apply(byte v)
{
assertEquals2(v, list.get(index++));
}
}).index;
assertEquals(result, list.size());
}
/* */
@Test
public void testClear()
{
list.add(newArray(list.buffer, 1, 2, 3));
list.clear();
/* removeIf:applied. */
// We don't care about the initialization for primitive types.
// Clean anyway for @After assertions.
Arrays.fill(list.buffer, ((byte) 0));
}
/* */
@Test
public void testFrom()
{
ByteArrayList variable = ByteArrayList.from(
(byte) 1,
(byte) 2,
(byte) 3);
assertEquals(3, variable.size());
assertListEquals(variable.toArray(), 1, 2, 3);
}
/* */
@Test
public void testHashCodeEquals()
{
ByteArrayList l0 = ByteArrayList.from();
assertEquals(1, l0.hashCode());
assertEquals(l0, ByteArrayList.from());
ByteArrayList l1 = ByteArrayList.from(
(byte) 1,
(byte) 2,
(byte) 3);
ByteArrayList l2 = ByteArrayList.from(
(byte) 1,
(byte) 2,
(byte) 3);
assertEquals(l1.hashCode(), l2.hashCode());
assertEquals(l1, l2);
}
/* */
@Test
public void testHashCodeEqualsWithOtherContainer()
{
ByteStack l1 = ByteStack.from(
(byte) 1,
(byte) 2,
(byte) 3);
ByteArrayList l2 = ByteArrayList.from(
(byte) 1,
(byte) 2,
(byte) 3);
assertEquals(l1.hashCode(), l2.hashCode());
assertEquals(l1, l2);
}
/* removeIf:applied. */
}