Sort members.

This commit is contained in:
Gary Gregory 2018-11-23 00:24:01 -07:00
parent 2ec7843b94
commit dc828f8b16
1 changed files with 304 additions and 304 deletions

View File

@ -32,27 +32,27 @@ import java.util.Set;
*/ */
public class SetUniqueListTest<E> extends AbstractListTest<E> { public class SetUniqueListTest<E> extends AbstractListTest<E> {
class SetUniqueList307 extends SetUniqueList<E> {
/**
* Generated serial version ID.
*/
private static final long serialVersionUID = 1415013031022962158L;
public SetUniqueList307(final List<E> list, final Set<E> set) {
super(list, set);
}
}
boolean extraVerify = true;
public SetUniqueListTest(final String testName) { public SetUniqueListTest(final String testName) {
super(testName); super(testName);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@Override @Override
public List<E> makeObject() { public String getCompatibilityVersion() {
return new SetUniqueList<>(new ArrayList<E>(), new HashSet<E>()); return "4";
}
//-----------------------------------------------------------------------
@Override
public void testListIteratorSet() {
// override to block
resetFull();
final ListIterator<E> it = getCollection().listIterator();
it.next();
try {
it.set(null);
fail();
} catch (final UnsupportedOperationException ex) {}
} }
@Override @Override
@ -80,6 +80,199 @@ public class SetUniqueListTest<E> extends AbstractListTest<E> {
}; };
} }
//-----------------------------------------------------------------------
@Override
public List<E> makeObject() {
return new SetUniqueList<>(new ArrayList<E>(), new HashSet<E>());
}
@SuppressWarnings("unchecked")
public void testAdd() {
final SetUniqueList<E> lset = new SetUniqueList<>(new ArrayList<E>(), new HashSet<E>());
// Duplicate element
final E obj = (E) Integer.valueOf(1);
lset.add(obj);
lset.add(obj);
assertEquals("Duplicate element was added.", 1, lset.size());
// Unique element
lset.add((E) Integer.valueOf(2));
assertEquals("Unique element was not added.", 2, lset.size());
}
@SuppressWarnings("unchecked")
public void testAddAll() {
final SetUniqueList<E> lset = new SetUniqueList<>(new ArrayList<E>(), new HashSet<E>());
lset.addAll(
Arrays.asList((E[]) new Integer[] { Integer.valueOf(1), Integer.valueOf(1)}));
assertEquals("Duplicate element was added.", 1, lset.size());
}
@Override
public void testCollectionAddAll() {
// override for set behaviour
resetEmpty();
E[] elements = getFullElements();
boolean r = getCollection().addAll(Arrays.asList(elements));
getConfirmed().addAll(Arrays.asList(elements));
verify();
assertTrue("Empty collection should change after addAll", r);
for (final E element : elements) {
assertTrue("Collection should contain added element",
getCollection().contains(element));
}
resetFull();
final int size = getCollection().size();
elements = getOtherElements();
r = getCollection().addAll(Arrays.asList(elements));
getConfirmed().addAll(Arrays.asList(elements));
verify();
assertTrue("Full collection should change after addAll", r);
for (int i = 0; i < elements.length; i++) {
assertTrue("Full collection should contain added element " + i,
getCollection().contains(elements[i]));
}
assertEquals("Size should increase after addAll",
size + elements.length, getCollection().size());
}
@Override
public void testCollectionIteratorRemove() {
try {
extraVerify = false;
super.testCollectionIteratorRemove();
} finally {
extraVerify = true;
}
}
public void testCollections304() {
final List<String> list = new LinkedList<>();
final SetUniqueList<String> decoratedList = SetUniqueList.setUniqueList(list);
final String s1 = "Apple";
final String s2 = "Lemon";
final String s3 = "Orange";
final String s4 = "Strawberry";
decoratedList.add(s1);
decoratedList.add(s2);
decoratedList.add(s3);
assertEquals(3, decoratedList.size());
decoratedList.set(1, s4);
assertEquals(3, decoratedList.size());
decoratedList.add(1, s4);
assertEquals(3, decoratedList.size());
decoratedList.add(1, s2);
assertEquals(4, decoratedList.size());
}
@SuppressWarnings("unchecked")
public void testCollections307() {
List<E> list = new ArrayList<>();
List<E> uniqueList = SetUniqueList.setUniqueList(list);
final String hello = "Hello";
final String world = "World";
uniqueList.add((E) hello);
uniqueList.add((E) world);
List<E> subList = list.subList(0, 0);
List<E> subUniqueList = uniqueList.subList(0, 0);
assertFalse(subList.contains(world)); // passes
assertFalse(subUniqueList.contains(world)); // fails
List<E> worldList = new ArrayList<>();
worldList.add((E) world);
assertFalse(subList.contains("World")); // passes
assertFalse(subUniqueList.contains("World")); // fails
// repeat the test with a different class than HashSet;
// which means subclassing SetUniqueList below
list = new ArrayList<>();
uniqueList = new SetUniqueList307(list, new java.util.TreeSet<E>());
uniqueList.add((E) hello);
uniqueList.add((E) world);
subList = list.subList(0, 0);
subUniqueList = uniqueList.subList(0, 0);
assertFalse(subList.contains(world)); // passes
assertFalse(subUniqueList.contains(world)); // fails
worldList = new ArrayList<>();
worldList.add((E) world);
assertFalse(subList.contains("World")); // passes
assertFalse(subUniqueList.contains("World")); // fails
}
//-----------------------------------------------------------------------
public void testFactory() {
final Integer[] array = new Integer[] { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(1) };
final ArrayList<Integer> list = new ArrayList<>(Arrays.asList(array));
final SetUniqueList<Integer> lset = SetUniqueList.setUniqueList(list);
assertEquals("Duplicate element was added.", 2, lset.size());
assertEquals(Integer.valueOf(1), lset.get(0));
assertEquals(Integer.valueOf(2), lset.get(1));
assertEquals(Integer.valueOf(1), list.get(0));
assertEquals(Integer.valueOf(2), list.get(1));
}
public void testIntCollectionAddAll() {
// make a SetUniqueList with one element
final List<Integer> list = new SetUniqueList<>(new ArrayList<Integer>(), new HashSet<Integer>());
final Integer existingElement = Integer.valueOf(1);
list.add(existingElement);
// add two new unique elements at index 0
final Integer firstNewElement = Integer.valueOf(2);
final Integer secondNewElement = Integer.valueOf(3);
Collection<Integer> collection = Arrays.asList(firstNewElement, secondNewElement);
list.addAll(0, collection);
assertEquals("Unique elements should be added.", 3, list.size());
assertEquals("First new element should be at index 0", firstNewElement, list.get(0));
assertEquals("Second new element should be at index 1", secondNewElement, list.get(1));
assertEquals("Existing element should shift to index 2", existingElement, list.get(2));
// add a duplicate element and a unique element at index 0
final Integer thirdNewElement = Integer.valueOf(4);
collection = Arrays.asList(existingElement, thirdNewElement);
list.addAll(0, collection);
assertEquals("Duplicate element should not be added, unique element should be added.",
4, list.size());
assertEquals("Third new element should be at index 0", thirdNewElement, list.get(0));
}
@SuppressWarnings("unchecked")
public void testListIterator() {
final SetUniqueList<E> lset = new SetUniqueList<>(new ArrayList<E>(), new HashSet<E>());
final E obj1 = (E) Integer.valueOf(1);
final E obj2 = (E) Integer.valueOf(2);
lset.add(obj1);
lset.add(obj2);
// Attempts to add a duplicate object
for (final ListIterator<E> it = lset.listIterator(); it.hasNext();) {
it.next();
if (!it.hasNext()) {
it.add(obj1);
break;
}
}
assertEquals("Duplicate element was added", 2, lset.size());
}
@Override @Override
public void testListIteratorAdd() { public void testListIteratorAdd() {
// override to cope with Set behaviour // override to cope with Set behaviour
@ -109,58 +302,17 @@ public class SetUniqueListTest<E> extends AbstractListTest<E> {
} }
} }
//-----------------------------------------------------------------------
@Override @Override
public void testCollectionAddAll() { public void testListIteratorSet() {
// override for set behaviour // override to block
resetEmpty();
E[] elements = getFullElements();
boolean r = getCollection().addAll(Arrays.asList(elements));
getConfirmed().addAll(Arrays.asList(elements));
verify();
assertTrue("Empty collection should change after addAll", r);
for (final E element : elements) {
assertTrue("Collection should contain added element",
getCollection().contains(element));
}
resetFull(); resetFull();
final int size = getCollection().size(); final ListIterator<E> it = getCollection().listIterator();
elements = getOtherElements(); it.next();
r = getCollection().addAll(Arrays.asList(elements)); try {
getConfirmed().addAll(Arrays.asList(elements)); it.set(null);
verify(); fail();
assertTrue("Full collection should change after addAll", r); } catch (final UnsupportedOperationException ex) {}
for (int i = 0; i < elements.length; i++) {
assertTrue("Full collection should contain added element " + i,
getCollection().contains(elements[i]));
}
assertEquals("Size should increase after addAll",
size + elements.length, getCollection().size());
}
public void testIntCollectionAddAll() {
// make a SetUniqueList with one element
final List<Integer> list = new SetUniqueList<>(new ArrayList<Integer>(), new HashSet<Integer>());
final Integer existingElement = Integer.valueOf(1);
list.add(existingElement);
// add two new unique elements at index 0
final Integer firstNewElement = Integer.valueOf(2);
final Integer secondNewElement = Integer.valueOf(3);
Collection<Integer> collection = Arrays.asList(firstNewElement, secondNewElement);
list.addAll(0, collection);
assertEquals("Unique elements should be added.", 3, list.size());
assertEquals("First new element should be at index 0", firstNewElement, list.get(0));
assertEquals("Second new element should be at index 1", secondNewElement, list.get(1));
assertEquals("Existing element should shift to index 2", existingElement, list.get(2));
// add a duplicate element and a unique element at index 0
final Integer thirdNewElement = Integer.valueOf(4);
collection = Arrays.asList(existingElement, thirdNewElement);
list.addAll(0, collection);
assertEquals("Duplicate element should not be added, unique element should be added.",
4, list.size());
assertEquals("Third new element should be at index 0", thirdNewElement, list.get(0));
} }
@Override @Override
@ -177,71 +329,52 @@ public class SetUniqueListTest<E> extends AbstractListTest<E> {
assertEquals(new Long(1000), getCollection().get(1)); // set into 2, but shifted down to 1 assertEquals(new Long(1000), getCollection().get(1)); // set into 2, but shifted down to 1
} }
boolean extraVerify = true;
@Override
public void testCollectionIteratorRemove() {
try {
extraVerify = false;
super.testCollectionIteratorRemove();
} finally {
extraVerify = true;
}
}
@Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void verify() { public void testRetainAll() {
super.verify(); final List<E> list = new ArrayList<>(10);
final SetUniqueList<E> uniqueList = SetUniqueList.setUniqueList(list);
if (extraVerify) { for (int i = 0; i < 10; ++i) {
final int size = getCollection().size(); uniqueList.add((E)Integer.valueOf(i));
getCollection().add((E) new Long(1000));
assertEquals(size + 1, getCollection().size());
getCollection().add((E) new Long(1000));
assertEquals(size + 1, getCollection().size());
assertEquals(new Long(1000), getCollection().get(size));
getCollection().remove(size);
}
} }
//----------------------------------------------------------------------- final Collection<E> retained = new ArrayList<>(5);
public void testFactory() { for (int i = 0; i < 5; ++i) {
final Integer[] array = new Integer[] { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(1) }; retained.add((E)Integer.valueOf(i * 2));
final ArrayList<Integer> list = new ArrayList<>(Arrays.asList(array)); }
final SetUniqueList<Integer> lset = SetUniqueList.setUniqueList(list);
assertEquals("Duplicate element was added.", 2, lset.size()); assertTrue(uniqueList.retainAll(retained));
assertEquals(Integer.valueOf(1), lset.get(0)); assertEquals(5, uniqueList.size());
assertEquals(Integer.valueOf(2), lset.get(1)); assertTrue(uniqueList.contains(Integer.valueOf(0)));
assertEquals(Integer.valueOf(1), list.get(0)); assertTrue(uniqueList.contains(Integer.valueOf(2)));
assertEquals(Integer.valueOf(2), list.get(1)); assertTrue(uniqueList.contains(Integer.valueOf(4)));
assertTrue(uniqueList.contains(Integer.valueOf(6)));
assertTrue(uniqueList.contains(Integer.valueOf(8)));
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testAdd() { public void testRetainAllWithInitialList() {
final SetUniqueList<E> lset = new SetUniqueList<>(new ArrayList<E>(), new HashSet<E>()); // initialized with empty list
final List<E> list = new ArrayList<>(10);
// Duplicate element for (int i = 0; i < 5; ++i) {
final E obj = (E) Integer.valueOf(1); list.add((E)Integer.valueOf(i));
lset.add(obj); }
lset.add(obj); final SetUniqueList<E> uniqueList = SetUniqueList.setUniqueList(list);
assertEquals("Duplicate element was added.", 1, lset.size()); for (int i = 5; i < 10; ++i) {
uniqueList.add((E)Integer.valueOf(i));
// Unique element
lset.add((E) Integer.valueOf(2));
assertEquals("Unique element was not added.", 2, lset.size());
} }
@SuppressWarnings("unchecked") final Collection<E> retained = new ArrayList<>(5);
public void testAddAll() { for (int i = 0; i < 5; ++i) {
final SetUniqueList<E> lset = new SetUniqueList<>(new ArrayList<E>(), new HashSet<E>()); retained.add((E)Integer.valueOf(i * 2));
}
lset.addAll( assertTrue(uniqueList.retainAll(retained));
Arrays.asList((E[]) new Integer[] { Integer.valueOf(1), Integer.valueOf(1)})); assertEquals(5, uniqueList.size());
assertTrue(uniqueList.contains(Integer.valueOf(0)));
assertEquals("Duplicate element was added.", 1, lset.size()); assertTrue(uniqueList.contains(Integer.valueOf(2)));
assertTrue(uniqueList.contains(Integer.valueOf(4)));
assertTrue(uniqueList.contains(Integer.valueOf(6)));
assertTrue(uniqueList.contains(Integer.valueOf(8)));
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -283,58 +416,22 @@ public class SetUniqueListTest<E> extends AbstractListTest<E> {
assertSame(obj1, lset.get(0)); assertSame(obj1, lset.get(0));
} }
@SuppressWarnings("unchecked") public void testSetCollections444() {
public void testListIterator() { final SetUniqueList<Integer> lset = new SetUniqueList<>(new ArrayList<Integer>(), new HashSet<Integer>());
final SetUniqueList<E> lset = new SetUniqueList<>(new ArrayList<E>(), new HashSet<E>());
// Duplicate element
final Integer obj1 = Integer.valueOf(1);
final Integer obj2 = Integer.valueOf(2);
final E obj1 = (E) Integer.valueOf(1);
final E obj2 = (E) Integer.valueOf(2);
lset.add(obj1); lset.add(obj1);
lset.add(obj2); lset.add(obj2);
lset.set(0, obj1);
assertEquals(2, lset.size());
assertSame(obj1, lset.get(0));
assertSame(obj2, lset.get(1));
// Attempts to add a duplicate object assertTrue(lset.contains(obj1));
for (final ListIterator<E> it = lset.listIterator(); it.hasNext();) { assertTrue(lset.contains(obj2));
it.next();
if (!it.hasNext()) {
it.add(obj1);
break;
}
}
assertEquals("Duplicate element was added", 2, lset.size());
}
@SuppressWarnings("unchecked")
public void testUniqueListReInsert() {
final List<E> l = SetUniqueList.setUniqueList(new LinkedList<E>());
l.add((E) new Object());
l.add((E) new Object());
final E a = l.get(0);
// duplicate is removed
l.set(0, l.get(1));
assertEquals(1, l.size());
// old object is added back in
l.add(1, a);
assertEquals(2, l.size());
}
@SuppressWarnings("unchecked")
public void testUniqueListDoubleInsert() {
final List<E> l = SetUniqueList.setUniqueList(new LinkedList<E>());
l.add((E) new Object());
l.add((E) new Object());
// duplicate is removed
l.set(0, l.get(1));
assertEquals(1, l.size());
// duplicate should be removed again
l.add(1, l.get(0));
assertEquals(1, l.size());
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -365,7 +462,6 @@ public class SetUniqueListTest<E> extends AbstractListTest<E> {
assertTrue(s.contains(b)); assertTrue(s.contains(b));
assertFalse(s.contains(a)); assertFalse(s.contains(a));
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testSetInBiggerList() { public void testSetInBiggerList() {
/* /*
@ -438,29 +534,6 @@ public class SetUniqueListTest<E> extends AbstractListTest<E> {
assertTrue(s.contains(c)); assertTrue(s.contains(c));
} }
public void testCollections304() {
final List<String> list = new LinkedList<>();
final SetUniqueList<String> decoratedList = SetUniqueList.setUniqueList(list);
final String s1 = "Apple";
final String s2 = "Lemon";
final String s3 = "Orange";
final String s4 = "Strawberry";
decoratedList.add(s1);
decoratedList.add(s2);
decoratedList.add(s3);
assertEquals(3, decoratedList.size());
decoratedList.set(1, s4);
assertEquals(3, decoratedList.size());
decoratedList.add(1, s4);
assertEquals(3, decoratedList.size());
decoratedList.add(1, s2);
assertEquals(4, decoratedList.size());
}
public void testSubListIsUnmodifiable() { public void testSubListIsUnmodifiable() {
resetFull(); resetFull();
final List<E> subList = getCollection().subList(1, 3); final List<E> subList = getCollection().subList(1, 3);
@ -471,128 +544,55 @@ public class SetUniqueListTest<E> extends AbstractListTest<E> {
// expected // expected
} }
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testCollections307() { public void testUniqueListDoubleInsert() {
List<E> list = new ArrayList<>(); final List<E> l = SetUniqueList.setUniqueList(new LinkedList<E>());
List<E> uniqueList = SetUniqueList.setUniqueList(list); l.add((E) new Object());
l.add((E) new Object());
final String hello = "Hello"; // duplicate is removed
final String world = "World"; l.set(0, l.get(1));
uniqueList.add((E) hello); assertEquals(1, l.size());
uniqueList.add((E) world);
List<E> subList = list.subList(0, 0); // duplicate should be removed again
List<E> subUniqueList = uniqueList.subList(0, 0); l.add(1, l.get(0));
assertEquals(1, l.size());
assertFalse(subList.contains(world)); // passes
assertFalse(subUniqueList.contains(world)); // fails
List<E> worldList = new ArrayList<>();
worldList.add((E) world);
assertFalse(subList.contains("World")); // passes
assertFalse(subUniqueList.contains("World")); // fails
// repeat the test with a different class than HashSet;
// which means subclassing SetUniqueList below
list = new ArrayList<>();
uniqueList = new SetUniqueList307(list, new java.util.TreeSet<E>());
uniqueList.add((E) hello);
uniqueList.add((E) world);
subList = list.subList(0, 0);
subUniqueList = uniqueList.subList(0, 0);
assertFalse(subList.contains(world)); // passes
assertFalse(subUniqueList.contains(world)); // fails
worldList = new ArrayList<>();
worldList.add((E) world);
assertFalse(subList.contains("World")); // passes
assertFalse(subUniqueList.contains("World")); // fails
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testRetainAll() { public void testUniqueListReInsert() {
final List<E> list = new ArrayList<>(10); final List<E> l = SetUniqueList.setUniqueList(new LinkedList<E>());
final SetUniqueList<E> uniqueList = SetUniqueList.setUniqueList(list); l.add((E) new Object());
for (int i = 0; i < 10; ++i) { l.add((E) new Object());
uniqueList.add((E)Integer.valueOf(i));
final E a = l.get(0);
// duplicate is removed
l.set(0, l.get(1));
assertEquals(1, l.size());
// old object is added back in
l.add(1, a);
assertEquals(2, l.size());
} }
final Collection<E> retained = new ArrayList<>(5);
for (int i = 0; i < 5; ++i) {
retained.add((E)Integer.valueOf(i * 2));
}
assertTrue(uniqueList.retainAll(retained));
assertEquals(5, uniqueList.size());
assertTrue(uniqueList.contains(Integer.valueOf(0)));
assertTrue(uniqueList.contains(Integer.valueOf(2)));
assertTrue(uniqueList.contains(Integer.valueOf(4)));
assertTrue(uniqueList.contains(Integer.valueOf(6)));
assertTrue(uniqueList.contains(Integer.valueOf(8)));
}
@SuppressWarnings("unchecked")
public void testRetainAllWithInitialList() {
// initialized with empty list
final List<E> list = new ArrayList<>(10);
for (int i = 0; i < 5; ++i) {
list.add((E)Integer.valueOf(i));
}
final SetUniqueList<E> uniqueList = SetUniqueList.setUniqueList(list);
for (int i = 5; i < 10; ++i) {
uniqueList.add((E)Integer.valueOf(i));
}
final Collection<E> retained = new ArrayList<>(5);
for (int i = 0; i < 5; ++i) {
retained.add((E)Integer.valueOf(i * 2));
}
assertTrue(uniqueList.retainAll(retained));
assertEquals(5, uniqueList.size());
assertTrue(uniqueList.contains(Integer.valueOf(0)));
assertTrue(uniqueList.contains(Integer.valueOf(2)));
assertTrue(uniqueList.contains(Integer.valueOf(4)));
assertTrue(uniqueList.contains(Integer.valueOf(6)));
assertTrue(uniqueList.contains(Integer.valueOf(8)));
}
public void testSetCollections444() {
final SetUniqueList<Integer> lset = new SetUniqueList<>(new ArrayList<Integer>(), new HashSet<Integer>());
// Duplicate element
final Integer obj1 = Integer.valueOf(1);
final Integer obj2 = Integer.valueOf(2);
lset.add(obj1);
lset.add(obj2);
lset.set(0, obj1);
assertEquals(2, lset.size());
assertSame(obj1, lset.get(0));
assertSame(obj2, lset.get(1));
assertTrue(lset.contains(obj1));
assertTrue(lset.contains(obj2));
}
class SetUniqueList307 extends SetUniqueList<E> {
/**
* Generated serial version ID.
*/
private static final long serialVersionUID = 1415013031022962158L;
public SetUniqueList307(final List<E> list, final Set<E> set) {
super(list, set);
}
}
//-----------------------------------------------------------------------
@Override @Override
public String getCompatibilityVersion() { @SuppressWarnings("unchecked")
return "4"; public void verify() {
super.verify();
if (extraVerify) {
final int size = getCollection().size();
getCollection().add((E) new Long(1000));
assertEquals(size + 1, getCollection().size());
getCollection().add((E) new Long(1000));
assertEquals(size + 1, getCollection().size());
assertEquals(new Long(1000), getCollection().get(size));
getCollection().remove(size);
}
} }
// public void testCreate() throws Exception { // public void testCreate() throws Exception {