unsorted lists can become sorted after removal
The old code only set the list to sorted when the lists size was <= 1 after the removal.
This commit is contained in:
@@ -334,7 +334,12 @@ public final class IntList implements Serializable, Cloneable {
|
|||||||
|
|
||||||
size = size - (toIndex - fromIndex);
|
size = size - (toIndex - fromIndex);
|
||||||
|
|
||||||
sorted = size() <= 1 ? true : sorted; // lists of size 1 or smaller are always sorted
|
if (!sorted) {
|
||||||
|
sorted = true;
|
||||||
|
for (int i = 1; i < size && sorted; i++) {
|
||||||
|
sorted = data[i - 1] <= data[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -384,7 +389,12 @@ public final class IntList implements Serializable, Cloneable {
|
|||||||
}
|
}
|
||||||
size = insertPosition;
|
size = insertPosition;
|
||||||
|
|
||||||
sorted = size() <= 1 ? true : sorted; // lists of size 1 or smaller are always sorted
|
if (!sorted) {
|
||||||
|
sorted = true;
|
||||||
|
for (int i = 1; i < size && sorted; i++) {
|
||||||
|
sorted = data[i - 1] <= data[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1223,6 +1223,30 @@ public class IntListTest {
|
|||||||
Assert.assertTrue("unsorted list with no elements becomes sorted", list.isSorted());
|
Assert.assertTrue("unsorted list with no elements becomes sorted", list.isSorted());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSortedFlagRemove_unsortedBecomesSorted_emptyList() {
|
||||||
|
|
||||||
|
final IntList list = IntList.of(4, 3, 2, 1);
|
||||||
|
list.remove(0, 4); // removes all
|
||||||
|
Assert.assertTrue("empty list is sorted", list.isSorted());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSortedFlagRemove_unsortedBecomesSorted_oneElement() {
|
||||||
|
|
||||||
|
final IntList list = IntList.of(4, 3, 2, 1);
|
||||||
|
list.remove(1, 4); // removes 3,2,1
|
||||||
|
Assert.assertTrue("list with one element is", list.isSorted());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSortedFlagRemove_unsortedBecomesSorted() {
|
||||||
|
|
||||||
|
final IntList list = IntList.of(1, 2, 777, 4, 5);
|
||||||
|
list.remove(2, 3); // removes 777
|
||||||
|
Assert.assertTrue("list is sorted after remove", list.isSorted());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSortedFlagRemoveAll() {
|
public void testSortedFlagRemoveAll() {
|
||||||
|
|
||||||
@@ -1253,6 +1277,31 @@ public class IntListTest {
|
|||||||
Assert.assertTrue("unsorted list with no elements becomes sorted", list.isSorted());
|
Assert.assertTrue("unsorted list with no elements becomes sorted", list.isSorted());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSortedFlagRemoveIf_unsortedBecomesSorted_emptyAfterRemove() {
|
||||||
|
|
||||||
|
final IntList list = IntList.of(1, 3, 2);
|
||||||
|
list.removeIf((value, index) -> true); // makes the list sorted
|
||||||
|
Assert.assertTrue("list is empty", list.isEmpty());
|
||||||
|
Assert.assertTrue("empty list is sorted", list.isSorted());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSortedFlagRemoveIf_unsortedBecomesSorted_oneElementAfterRemove() {
|
||||||
|
|
||||||
|
final IntList list = IntList.of(1, 3, 2);
|
||||||
|
list.removeIf((value, index) -> value > 1); // makes the list sorted
|
||||||
|
Assert.assertTrue("list with one element is sorted", list.isSorted());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSortedFlagRemoveIf_unsortedBecomesSorted() {
|
||||||
|
|
||||||
|
final IntList list = IntList.of(1, 2, 3, 777, 4, 5);
|
||||||
|
list.removeIf((value, index) -> value == 777); // makes the list sorted
|
||||||
|
Assert.assertTrue("unsorted list becomes sorted", list.isSorted());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSortedFlagReplace() {
|
public void testSortedFlagReplace() {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user