Add IntList a list implementation for primitive integers.

This commit is contained in:
2017-02-03 20:31:58 +01:00
parent d3315a80d5
commit 145c2152d0
12 changed files with 1079 additions and 0 deletions

View File

@@ -0,0 +1,403 @@
package org.lucares.collections;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import org.junit.Assert;
import org.junit.Test;
public class IntListTest {
@Test
public void testEmptyList() {
final IntList list = new IntList();
Assert.assertEquals(true, list.isEmpty());
}
@Test
public void testCopyConstructor() {
final IntList list = new IntList();
list.addAll(1, 2, 3, 4, 5);
final IntList copy = new IntList(list);
Assert.assertArrayEquals(copy.toArray(), list.toArray());
}
@Test
public void testInvalidInitialCapacity() {
try {
new IntList(-1);
Assert.fail();
} catch (final IllegalArgumentException e) {
// expected
}
}
@Test
public void testAdd() {
final IntList list = new IntList();
list.addAll();
Assert.assertTrue(list.isEmpty());
Assert.assertEquals(0, list.size());
list.add(1);
Assert.assertFalse(list.isEmpty());
Assert.assertEquals(1, list.size());
list.add(2);
Assert.assertEquals(2, list.size());
Assert.assertEquals(1, list.get(0));
Assert.assertEquals(2, list.get(1));
}
@Test
public void testInsert() {
final IntList list = new IntList();
list.insert(0, 1);
Assert.assertArrayEquals(new int[] { 1 }, list.toArray());
list.insert(1, 2);
Assert.assertArrayEquals(new int[] { 1, 2 }, list.toArray());
list.insert(1, 3);
Assert.assertArrayEquals(new int[] { 1, 3, 2 }, list.toArray());
list.insert(2, 4, 4, 4);
Assert.assertArrayEquals(new int[] { 1, 3, 4, 4, 4, 2 }, list.toArray());
list.insert(6, 5, 5);
Assert.assertArrayEquals(new int[] { 1, 3, 4, 4, 4, 2, 5, 5 }, list.toArray());
}
@Test
public void testInsertOutOfBounds() {
final IntList list = new IntList();
try {
list.insert(-1, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.insert(1, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.add(1);
list.insert(2, 2);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
}
@Test
public void testSet() {
final IntList list = new IntList();
list.addAll(0, 1, 2, 3, 4, 5);
list.set(0, 10);
Assert.assertArrayEquals(new int[] { 10, 1, 2, 3, 4, 5 }, list.toArray());
list.set(1, 11);
Assert.assertArrayEquals(new int[] { 10, 11, 2, 3, 4, 5 }, list.toArray());
list.set(5, 55);
Assert.assertArrayEquals(new int[] { 10, 11, 2, 3, 4, 55 }, list.toArray());
}
@Test
public void testSetOutOfBounds() {
final IntList list = new IntList();
try {
list.set(-1, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.set(1, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
list.addAll(0, 1, 2, 3, 4, 5);
try {
list.set(6, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
}
@Test
public void testGrow() {
final IntList list = new IntList(1);
final int size = 100;
for (int i = 0; i < size; i++) {
list.add(i);
}
Assert.assertEquals(size, list.size());
for (int i = 0; i < size; i++) {
Assert.assertEquals(i, list.get(i));
}
}
@Test
public void testGrowIntListOfCapacityNull() {
final IntList list = new IntList(0);
final int size = 100;
for (int i = 0; i < size; i++) {
list.add(i);
}
Assert.assertEquals(size, list.size());
for (int i = 0; i < size; i++) {
Assert.assertEquals(i, list.get(i));
}
}
@Test
public void testAddArray() {
final IntList list = new IntList();
final int size = 100;
final int[] ints = ThreadLocalRandom.current().ints(size).toArray();
list.addAll(ints);
Assert.assertEquals(size, list.size());
for (int i = 0; i < size; i++) {
Assert.assertEquals(ints[i], list.get(i));
}
final int[] anotherInts = ThreadLocalRandom.current().ints(size).toArray();
list.addAll(anotherInts);
Assert.assertEquals(size * 2, list.size());
for (int i = 0; i < size; i++) {
Assert.assertEquals(anotherInts[i], list.get(size + i));
}
}
@Test
public void testGetArray() {
final IntList list = new IntList();
final int size = 100;
final int[] ints = ThreadLocalRandom.current().ints(size).toArray();
list.addAll(ints);
final int length = 20;
final int from = 10;
final int[] actualInts = list.get(from, length);
for (int i = 0; i < length; i++) {
Assert.assertEquals(actualInts[i], list.get(from + i));
Assert.assertEquals(actualInts[i], ints[from + i]);
}
}
@Test
public void testGetOutOfBounds() {
final IntList list = new IntList();
list.addAll(0, 1, 2, 3);
try {
list.get(-1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.get(4);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
}
@Test
public void testGetArrayOutOfBounds() {
final IntList list = new IntList();
list.addAll(0, 1, 2, 3);
try {
list.get(-1, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.get(1, -1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.get(3, 2);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.get(4, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
}
@Test
public void testToArrayEmptyLIst() {
final IntList list = new IntList();
final int[] actual = list.toArray();
Assert.assertArrayEquals(actual, new int[0]);
}
@Test
public void testRemove() {
final IntList list = new IntList();
final int size = 10;
final int[] ints = new int[size];
for (int i = 0; i < size; i++) {
ints[i] = i;
}
list.addAll(ints);
list.remove(2, 0);
Assert.assertArrayEquals(ints, list.toArray());
list.remove(9, 1);
final int[] expectedA = removeElements(ints, 9);
Assert.assertArrayEquals(expectedA, list.toArray());
list.remove(0, 1);
final int[] expectedB = removeElements(expectedA, 0);
Assert.assertArrayEquals(expectedB, list.toArray());
list.remove(7, 1);
final int[] expectedC = removeElements(expectedB, 7);
Assert.assertArrayEquals(expectedC, list.toArray());
list.remove(3, 3);
final int[] expectedD = removeElements(expectedC, 3, 4, 5);
Assert.assertArrayEquals(expectedD, list.toArray());
list.remove(0, 4);
final int[] expectedE = removeElements(expectedD, 0, 1, 2, 3);
Assert.assertArrayEquals(expectedE, list.toArray());
}
@Test
public void testRemoveOutOfRange() {
final IntList list = new IntList();
list.addAll(0, 1, 2, 3);
try {
list.remove(-1, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.remove(1, -1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.remove(3, 2);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.remove(4, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
}
@Test
public void testTrim() {
final IntList list = new IntList();
list.addAll(0, 1, 2, 3);
list.trim();
Assert.assertEquals(4, list.getCapacity());
}
@Test
public void testTrimOnEmptyList() {
final IntList list = new IntList();
list.trim();
Assert.assertEquals(0, list.getCapacity());
}
@Test
public void testHashCodeEquals() {
final IntList a = new IntList();
final IntList b = new IntList();
a.addAll(1, 2, 3, 4);
b.addAll(1, 2, 3, 4);
Assert.assertTrue(a.equals(b));
Assert.assertEquals(a.hashCode(), b.hashCode());
// trim only one of them
// we want to compare the actual content
a.trim();
Assert.assertTrue(a.equals(b));
Assert.assertEquals(a.hashCode(), b.hashCode());
// change one value
a.remove(2, 1);
a.insert(2, 99);
Assert.assertFalse(a.equals(b));
Assert.assertNotEquals(a.hashCode(), b.hashCode());
}
private int[] removeElements(final int[] data, final int... removedElements) {
final int[] result = new int[data.length - removedElements.length];
final List<Integer> blacklist = Arrays.stream(removedElements).boxed().collect(Collectors.toList());
int j = 0;
for (int i = 0; i < data.length; i++) {
if (!blacklist.contains(i)) {
result[j] = data[i];
j++;
}
}
return result;
}
}