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:
2017-12-20 18:59:24 +01:00
parent c73d43c214
commit ec32e3f566
2 changed files with 61 additions and 2 deletions

View File

@@ -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];
}
}
} }
/** /**

View File

@@ -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() {