use sorted flag in hashcode/equals

This commit is contained in:
2017-12-08 18:35:49 +01:00
parent 29d4e49298
commit b56f1e6688
2 changed files with 33 additions and 1 deletions

View File

@@ -645,10 +645,14 @@ public final class IntList implements Serializable, Cloneable {
@Override
public int hashCode() {
if (size == 0) {
return 0;
}
/*
* only consider values in the range of 0 to size
*/
int result = 1;
for (int i = 0; i < size; i++) {
result = 31 * result + data[i];
@@ -672,7 +676,13 @@ public final class IntList implements Serializable, Cloneable {
if (size != other.size) {
return false;
}
if (sorted != other.sorted) {
return false;
}
/*
* only consider values in the range of 0 to size
*/
for (int i = 0; i < size; i++) {
if (data[i] != other.data[i]) {
return false;

View File

@@ -587,7 +587,29 @@ public class IntListTest {
Assert.assertNotEquals(list, clone);
}
// TODO test clone of empty list
@Test
public void testCloneEmptyList() {
final IntList list = new IntList();
final IntList clone = list.clone();
Assert.assertEquals(list, clone);
}
@Test
public void testClonePreservesSortedFlagOnUnsortedList() {
final IntList list = IntList.of(3, 2, 1);
final IntList clone = list.clone();
Assert.assertEquals(list.isSorted(), clone.isSorted());
}
@Test
public void testClonePreservesSortedFlagOnSortedList() {
final IntList list = IntList.of(1, 2, 3);
final IntList clone = list.clone();
Assert.assertEquals(list.isSorted(), clone.isSorted());
}
@Test
public void testToString() {