add different versions of IntList.insert and LongList.insert
The new methods are more flexible, so that you can insert parts of an array.
This commit is contained in:
@@ -132,24 +132,91 @@ public class IntListTest {
|
||||
|
||||
list.insert(0);
|
||||
Assertions.assertArrayEquals(new int[] {}, list.toArray());
|
||||
Assertions.assertTrue(list.isSorted());
|
||||
|
||||
list.insert(0, 1);
|
||||
Assertions.assertArrayEquals(new int[] { 1 }, list.toArray());
|
||||
Assertions.assertTrue(list.isSorted());
|
||||
|
||||
list.insert(1, 2, 2, 2);
|
||||
Assertions.assertArrayEquals(new int[] { 1, 2, 2, 2 }, list.toArray());
|
||||
Assertions.assertTrue(list.isSorted());
|
||||
|
||||
list.insert(1, 3);
|
||||
Assertions.assertArrayEquals(new int[] { 1, 3, 2, 2, 2 }, list.toArray());
|
||||
Assertions.assertFalse(list.isSorted());
|
||||
|
||||
list.insert(2, 4, 4, 4);
|
||||
Assertions.assertArrayEquals(new int[] { 1, 3, 4, 4, 4, 2, 2, 2 }, list.toArray());
|
||||
Assertions.assertFalse(list.isSorted());
|
||||
|
||||
list.insert(2);
|
||||
Assertions.assertArrayEquals(new int[] { 1, 3, 4, 4, 4, 2, 2, 2 }, list.toArray());
|
||||
Assertions.assertFalse(list.isSorted());
|
||||
|
||||
list.insert(8, 5, 5);
|
||||
Assertions.assertArrayEquals(new int[] { 1, 3, 4, 4, 4, 2, 2, 2, 5, 5 }, list.toArray());
|
||||
Assertions.assertFalse(list.isSorted());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertWithFlexibleApi() {
|
||||
final IntList list = new IntList();
|
||||
|
||||
list.insert(0, new int[] {}, 0, 0);
|
||||
Assertions.assertArrayEquals(new int[] {}, list.toArray());
|
||||
|
||||
list.insert(0, new int[] { 1, 3 }, 0, 2);
|
||||
Assertions.assertArrayEquals(new int[] { 1, 3 }, list.toArray());
|
||||
Assertions.assertTrue(list.isSorted());
|
||||
|
||||
list.insert(1, new int[] { -1, 2, -1 }, 1, 1); // only the 2 will be inserted
|
||||
Assertions.assertArrayEquals(new int[] { 1, 2, 3 }, list.toArray());
|
||||
Assertions.assertTrue(list.isSorted());
|
||||
|
||||
list.insert(2, new int[] { 7, 8 }, 0, 2);
|
||||
Assertions.assertArrayEquals(new int[] { 1, 2, 7, 8, 3 }, list.toArray());
|
||||
Assertions.assertFalse(list.isSorted());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertIntListWithFlexibleApi() {
|
||||
final IntList list = new IntList();
|
||||
|
||||
list.insert(0, IntList.of(), 0, 0);
|
||||
Assertions.assertArrayEquals(new int[] {}, list.toArray());
|
||||
|
||||
list.insert(0, IntList.of(1, 3), 0, 2);
|
||||
Assertions.assertArrayEquals(new int[] { 1, 3 }, list.toArray());
|
||||
Assertions.assertTrue(list.isSorted());
|
||||
|
||||
list.insert(1, IntList.of(-1, 2, -1), 1, 1); // only the 2 will be inserted
|
||||
Assertions.assertArrayEquals(new int[] { 1, 2, 3 }, list.toArray());
|
||||
Assertions.assertTrue(list.isSorted());
|
||||
|
||||
list.insert(2, IntList.of(7, 8), 0, 2);
|
||||
Assertions.assertArrayEquals(new int[] { 1, 2, 7, 8, 3 }, list.toArray());
|
||||
Assertions.assertFalse(list.isSorted());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertIntListWithSimpleApi() {
|
||||
final IntList list = new IntList();
|
||||
|
||||
list.insert(0, IntList.of());
|
||||
Assertions.assertArrayEquals(new int[] {}, list.toArray());
|
||||
|
||||
list.insert(0, IntList.of(1, 3));
|
||||
Assertions.assertArrayEquals(new int[] { 1, 3 }, list.toArray());
|
||||
Assertions.assertTrue(list.isSorted());
|
||||
|
||||
list.insert(1, IntList.of(2));
|
||||
Assertions.assertArrayEquals(new int[] { 1, 2, 3 }, list.toArray());
|
||||
Assertions.assertTrue(list.isSorted());
|
||||
|
||||
list.insert(3, IntList.of(6, 5));
|
||||
Assertions.assertArrayEquals(new int[] { 1, 2, 3, 6, 5 }, list.toArray());
|
||||
Assertions.assertFalse(list.isSorted());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -176,13 +243,45 @@ public class IntListTest {
|
||||
} catch (final IndexOutOfBoundsException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
// sourcePos is out of range
|
||||
list.insert(1, IntList.of(1), 1, 1);
|
||||
Assertions.fail();
|
||||
} catch (final IndexOutOfBoundsException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
// length is out of range
|
||||
list.insert(1, IntList.of(1), 0, 2);
|
||||
Assertions.fail();
|
||||
} catch (final IndexOutOfBoundsException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
// sourcePod+length is out of range
|
||||
list.insert(1, IntList.of(1, 2, 3), 2, 2);
|
||||
Assertions.fail();
|
||||
} catch (final IndexOutOfBoundsException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertNull() {
|
||||
public void testInsertNullArray() {
|
||||
final IntList list = new IntList();
|
||||
try {
|
||||
list.insert(0, null);
|
||||
list.insert(0, (int[]) null);
|
||||
Assertions.fail();
|
||||
} catch (final NullPointerException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertNullList() {
|
||||
final IntList list = new IntList();
|
||||
try {
|
||||
list.insert(0, (IntList) null);
|
||||
Assertions.fail();
|
||||
} catch (final NullPointerException e) {
|
||||
// expected
|
||||
|
||||
@@ -79,8 +79,8 @@ public class LongListTest {
|
||||
|
||||
@Test
|
||||
public void testRangeClosed() {
|
||||
final IntList expected = IntList.of(1, 2, 3, 4, 5);
|
||||
final IntList list = IntList.rangeClosed(1, 5);
|
||||
final LongList expected = LongList.of(1, 2, 3, 4, 5);
|
||||
final LongList list = LongList.rangeClosed(1, 5);
|
||||
Assertions.assertArrayEquals(expected.toArray(), list.toArray());
|
||||
Assertions.assertEquals(5, list.getCapacity());
|
||||
}
|
||||
@@ -160,6 +160,66 @@ public class LongListTest {
|
||||
Assertions.assertArrayEquals(new long[] { 1, 3, 4, 4, 4, 2, 2, 2, 5, 5 }, list.toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertWithFlexibleApi() {
|
||||
final LongList list = new LongList();
|
||||
|
||||
list.insert(0, new long[] {}, 0, 0);
|
||||
Assertions.assertArrayEquals(new long[] {}, list.toArray());
|
||||
|
||||
list.insert(0, new long[] { 1, 3 }, 0, 2);
|
||||
Assertions.assertArrayEquals(new long[] { 1, 3 }, list.toArray());
|
||||
Assertions.assertTrue(list.isSorted());
|
||||
|
||||
list.insert(1, new long[] { -1, 2, -1 }, 1, 1); // only the 2 will be inserted
|
||||
Assertions.assertArrayEquals(new long[] { 1, 2, 3 }, list.toArray());
|
||||
Assertions.assertTrue(list.isSorted());
|
||||
|
||||
list.insert(2, new long[] { 7, 8 }, 0, 2);
|
||||
Assertions.assertArrayEquals(new long[] { 1, 2, 7, 8, 3 }, list.toArray());
|
||||
Assertions.assertFalse(list.isSorted());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertLongListWithFlexibleApi() {
|
||||
final LongList list = new LongList();
|
||||
|
||||
list.insert(0, LongList.of(), 0, 0);
|
||||
Assertions.assertArrayEquals(new long[] {}, list.toArray());
|
||||
|
||||
list.insert(0, LongList.of(1, 3), 0, 2);
|
||||
Assertions.assertArrayEquals(new long[] { 1, 3 }, list.toArray());
|
||||
Assertions.assertTrue(list.isSorted());
|
||||
|
||||
list.insert(1, LongList.of(-1, 2, -1), 1, 1); // only the 2 will be inserted
|
||||
Assertions.assertArrayEquals(new long[] { 1, 2, 3 }, list.toArray());
|
||||
Assertions.assertTrue(list.isSorted());
|
||||
|
||||
list.insert(2, LongList.of(7, 8), 0, 2);
|
||||
Assertions.assertArrayEquals(new long[] { 1, 2, 7, 8, 3 }, list.toArray());
|
||||
Assertions.assertFalse(list.isSorted());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertLongListWithSimpleApi() {
|
||||
final LongList list = new LongList();
|
||||
|
||||
list.insert(0, LongList.of());
|
||||
Assertions.assertArrayEquals(new long[] {}, list.toArray());
|
||||
|
||||
list.insert(0, LongList.of(1, 3));
|
||||
Assertions.assertArrayEquals(new long[] { 1, 3 }, list.toArray());
|
||||
Assertions.assertTrue(list.isSorted());
|
||||
|
||||
list.insert(1, LongList.of(2));
|
||||
Assertions.assertArrayEquals(new long[] { 1, 2, 3 }, list.toArray());
|
||||
Assertions.assertTrue(list.isSorted());
|
||||
|
||||
list.insert(3, LongList.of(6, 5));
|
||||
Assertions.assertArrayEquals(new long[] { 1, 2, 3, 6, 5 }, list.toArray());
|
||||
Assertions.assertFalse(list.isSorted());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertOutOfBounds() {
|
||||
final LongList list = new LongList();
|
||||
@@ -184,13 +244,34 @@ public class LongListTest {
|
||||
} catch (final IndexOutOfBoundsException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
// sourcePos is out of range
|
||||
list.insert(1, LongList.of(1), 1, 1);
|
||||
Assertions.fail();
|
||||
} catch (final IndexOutOfBoundsException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
// length is out of range
|
||||
list.insert(1, LongList.of(1), 0, 2);
|
||||
Assertions.fail();
|
||||
} catch (final IndexOutOfBoundsException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
// sourcePod+length is out of range
|
||||
list.insert(1, LongList.of(1, 2, 3), 2, 2);
|
||||
Assertions.fail();
|
||||
} catch (final IndexOutOfBoundsException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertNull() {
|
||||
final LongList list = new LongList();
|
||||
try {
|
||||
list.insert(0, null);
|
||||
list.insert(0, (long[]) null);
|
||||
Assertions.fail();
|
||||
} catch (final NullPointerException e) {
|
||||
// expected
|
||||
|
||||
Reference in New Issue
Block a user