add union(IntList,IntList) and addAll(IntList)

IntPredicate gets the current value and the index.
This was handy while removing duplicate values.
This commit is contained in:
2017-12-12 18:45:11 +01:00
parent 3dd1955749
commit 76e5dc403c
3 changed files with 296 additions and 29 deletions

View File

@@ -224,6 +224,64 @@ public class IntListTest {
}
}
@Test
public void testAddList() {
final IntList list = new IntList();
// adding empty list with no capacity
list.addAll(IntList.of());
Assert.assertEquals(new IntList(), list);
Assert.assertTrue(list.isSorted());
// adding empty list with capacity 2
list.addAll(new IntList(2));
Assert.assertEquals(new IntList(), list);
Assert.assertTrue(list.isSorted());
// adding sorted list to an empty list
list.addAll(IntList.of(1, 2, 3));
Assert.assertEquals(IntList.of(1, 2, 3), list);
Assert.assertTrue(list.isSorted());
// add empty list to a sorted list
list.addAll(IntList.of());
Assert.assertEquals(IntList.of(1, 2, 3), list);
Assert.assertTrue(list.isSorted());
// adding sorted list to a sorted list so that the list stays sorted
list.addAll(IntList.of(3, 4, 5));
Assert.assertEquals(IntList.of(1, 2, 3, 3, 4, 5), list);
Assert.assertTrue(list.isSorted());
// adding sorted list to a sorted list, but the new list is not sorted
list.clear();
list.addAll(IntList.of(1, 2, 3));
list.addAll(IntList.of(0));
Assert.assertEquals(IntList.of(1, 2, 3, 0), list);
Assert.assertFalse(list.isSorted());
// adding unsorted list to a sorted list
list.clear();
list.addAll(IntList.of(1, 2, 3));
list.addAll(IntList.of(6, 5, 4));
Assert.assertEquals(IntList.of(1, 2, 3, 6, 5, 4), list);
Assert.assertFalse(list.isSorted());
// adding unsorted list to an empty list
list.clear();
list.addAll(IntList.of(3, 2, 1));
Assert.assertEquals(IntList.of(3, 2, 1), list);
Assert.assertFalse(list.isSorted());
// adding sorted list to an unsorted list
list.clear();
list.addAll(IntList.of(3, 2, 1));
list.addAll(IntList.of(1, 2, 3));
Assert.assertEquals(IntList.of(3, 2, 1, 1, 2, 3), list);
Assert.assertFalse(list.isSorted());
}
@Test
public void testGetArray() {
final IntList list = new IntList();
@@ -806,7 +864,7 @@ public class IntListTest {
public void testRemoveIf() {
final IntList list = IntList.of(1, 2, 3, 4, 5, 6);
list.removeIf(i -> i % 2 == 0);
list.removeIf((value, index) -> value % 2 == 0);
Assert.assertArrayEquals(new int[] { 1, 3, 5 }, list.toArray());
}
@@ -814,7 +872,7 @@ public class IntListTest {
public void testRemoveIfNegationOfPredicate() {
final IntList list = IntList.of(1, 2, 3, 4, 5, 6);
final IntPredicate predicate = i -> i % 2 == 0;
final IntPredicate predicate = (value, index) -> value % 2 == 0;
list.removeIf(predicate.negate());
Assert.assertArrayEquals(new int[] { 2, 4, 6 }, list.toArray());
}
@@ -823,8 +881,8 @@ public class IntListTest {
public void testRemoveIfWithAndCombinedPredicates() {
final IntList list = IntList.of(1, 2, 3, 4, 5, 6);
final IntPredicate predicateA = i -> i % 2 == 0;
final IntPredicate predicateB = i -> i == 3 || i == 4;
final IntPredicate predicateA = (value, index) -> value % 2 == 0;
final IntPredicate predicateB = (value, index) -> value == 3 || value == 4;
list.removeIf(predicateA.and(predicateB));
Assert.assertArrayEquals(new int[] { 1, 2, 3, 5, 6 }, list.toArray());
}
@@ -833,8 +891,8 @@ public class IntListTest {
public void testRemoveIfWithOrCombinedPredicates() {
final IntList list = IntList.of(1, 2, 3, 4, 5, 6);
final IntPredicate predicateA = i -> i % 2 == 0;
final IntPredicate predicateB = i -> i == 3 || i == 4;
final IntPredicate predicateA = (value, index) -> value % 2 == 0;
final IntPredicate predicateB = (value, index) -> value == 3 || value == 4;
list.removeIf(predicateA.or(predicateB));
Assert.assertArrayEquals(new int[] { 1, 5 }, list.toArray());
}
@@ -843,7 +901,7 @@ public class IntListTest {
public void testRemoveIfOnEmptyList() {
final IntList list = IntList.of();
list.removeIf(i -> false);
list.removeIf((value, index) -> false);
Assert.assertArrayEquals(new int[] {}, list.toArray());
}
@@ -1067,14 +1125,14 @@ public class IntListTest {
public void testSortedFlagRemoveIf() {
final IntList list = IntList.of(4, 3, 2, 1);
list.removeIf(v -> v >= 3); // removes 3 and 4
list.removeIf((value, index) -> value >= 3); // removes 3 and 4
Assert.assertFalse("unsorted list with two elements is not sorted", list.isSorted());
list.removeIf(v -> v >= 2); // removes 2
list.removeIf((value, index) -> value >= 2); // removes 2
Assert.assertTrue("unsorted list with one element becomes sorted", list.isSorted());
list.add(-1); // make list unsorted again
list.removeIf(v -> true); // remove both elements
list.removeIf((value, index) -> true); // remove both elements
Assert.assertTrue("unsorted list with no elements becomes sorted", list.isSorted());
}
@@ -1153,4 +1211,73 @@ public class IntListTest {
Assert.assertEquals(IntList.of(4), actual);
}
}
@Test
public void testUnionSortedLists_emptyLists() {
final IntList a = IntList.of();
final IntList b = IntList.of();
Assert.assertEquals(IntList.of(), IntList.union(a, b));
Assert.assertEquals(IntList.union(a, b), IntList.union(b, a));
}
@Test
public void testUnionSortedLists_uniqueValues() {
final IntList a = IntList.of(0, 1, 3, 4);
final IntList b = IntList.of(2, 4, 5);
final IntList actual = IntList.union(a, b);
Assert.assertEquals(IntList.of(0, 1, 2, 3, 4, 5), actual);
Assert.assertEquals(IntList.union(a, b), IntList.union(b, a));
}
@Test
public void testUnionSortedLists_duplicateValues_inMiddleOfListA() {
final IntList a = IntList.of(1, 2, 2, 3);
final IntList b = IntList.of(1, 3);
final IntList actual = IntList.union(a, b);
Assert.assertEquals(IntList.of(1, 2, 3), actual);
Assert.assertEquals(IntList.union(a, b), IntList.union(b, a));
}
@Test
public void testUnionSortedLists_duplicateValues_inMiddleOfBothLists() {
final IntList a = IntList.of(1, 2, 2, 3);
final IntList b = IntList.of(1, 2, 2, 4);
final IntList actual = IntList.union(a, b);
Assert.assertEquals(IntList.of(1, 2, 3, 4), actual);
Assert.assertEquals(IntList.union(a, b), IntList.union(b, a));
}
@Test
public void testUnionSortedLists_duplicateValues_atEndOfListA_whenHighestValueInBIsSmaller() {
final IntList a = IntList.of();
final IntList b = IntList.of(2, 2);
final IntList actual = IntList.union(a, b);
Assert.assertEquals(IntList.of(2), actual);
Assert.assertEquals(IntList.union(a, b), IntList.union(b, a));
}
@Test
public void testUnionUnsortedLists() {
final IntList a = IntList.of(1, 0, 3, 4);
final IntList b = IntList.of(2, 5, 4);
final IntList actual = IntList.union(a, b);
Assert.assertEquals(IntList.of(0, 1, 2, 3, 4, 5), actual);
Assert.assertEquals(IntList.union(a, b), IntList.union(b, a));
}
@Test
public void testUnionUnsortedLists_oneListIsSorted() {
final IntList a = IntList.of(1, 2, 3);
final IntList b = IntList.of(2, 5, 4);
final IntList actual = IntList.union(a, b);
Assert.assertEquals(IntList.of(1, 2, 3, 4, 5), actual);
Assert.assertEquals(IntList.union(a, b), IntList.union(b, a));
}
}