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> {
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) {
super(testName);
}
//-----------------------------------------------------------------------
@Override
public List<E> makeObject() {
return new SetUniqueList<>(new ArrayList<E>(), new HashSet<E>());
}
//-----------------------------------------------------------------------
@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) {}
public String getCompatibilityVersion() {
return "4";
}
@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
public void testListIteratorAdd() {
// override to cope with Set behaviour
@ -109,58 +302,17 @@ public class SetUniqueListTest<E> extends AbstractListTest<E> {
}
}
//-----------------------------------------------------------------------
@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));
}
public void testListIteratorSet() {
// override to block
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());
}
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));
final ListIterator<E> it = getCollection().listIterator();
it.next();
try {
it.set(null);
fail();
} catch (final UnsupportedOperationException ex) {}
}
@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
}
boolean extraVerify = true;
@Override
public void testCollectionIteratorRemove() {
try {
extraVerify = false;
super.testCollectionIteratorRemove();
} finally {
extraVerify = true;
@SuppressWarnings("unchecked")
public void testRetainAll() {
final List<E> list = new ArrayList<>(10);
final SetUniqueList<E> uniqueList = SetUniqueList.setUniqueList(list);
for (int i = 0; i < 10; ++i) {
uniqueList.add((E)Integer.valueOf(i));
}
}
@Override
@SuppressWarnings("unchecked")
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);
final Collection<E> retained = new ArrayList<>(5);
for (int i = 0; i < 5; ++i) {
retained.add((E)Integer.valueOf(i * 2));
}
}
//-----------------------------------------------------------------------
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));
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 testAdd() {
final SetUniqueList<E> lset = new SetUniqueList<>(new ArrayList<E>(), new HashSet<E>());
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));
}
// Duplicate element
final E obj = (E) Integer.valueOf(1);
lset.add(obj);
lset.add(obj);
assertEquals("Duplicate element was added.", 1, lset.size());
final Collection<E> retained = new ArrayList<>(5);
for (int i = 0; i < 5; ++i) {
retained.add((E)Integer.valueOf(i * 2));
}
// 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());
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")
@ -283,58 +416,22 @@ public class SetUniqueListTest<E> extends AbstractListTest<E> {
assertSame(obj1, lset.get(0));
}
@SuppressWarnings("unchecked")
public void testListIterator() {
final SetUniqueList<E> lset = new SetUniqueList<>(new ArrayList<E>(), new HashSet<E>());
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);
final E obj1 = (E) Integer.valueOf(1);
final E obj2 = (E) 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));
// 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());
}
@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());
assertTrue(lset.contains(obj1));
assertTrue(lset.contains(obj2));
}
@SuppressWarnings("unchecked")
@ -365,7 +462,6 @@ public class SetUniqueListTest<E> extends AbstractListTest<E> {
assertTrue(s.contains(b));
assertFalse(s.contains(a));
}
@SuppressWarnings("unchecked")
public void testSetInBiggerList() {
/*
@ -438,29 +534,6 @@ public class SetUniqueListTest<E> extends AbstractListTest<E> {
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() {
resetFull();
final List<E> subList = getCollection().subList(1, 3);
@ -471,128 +544,55 @@ public class SetUniqueListTest<E> extends AbstractListTest<E> {
// expected
}
}
@SuppressWarnings("unchecked")
public void testCollections307() {
List<E> list = new ArrayList<>();
List<E> uniqueList = SetUniqueList.setUniqueList(list);
public void testUniqueListDoubleInsert() {
final List<E> l = SetUniqueList.setUniqueList(new LinkedList<E>());
l.add((E) new Object());
l.add((E) new Object());
final String hello = "Hello";
final String world = "World";
uniqueList.add((E) hello);
uniqueList.add((E) world);
// duplicate is removed
l.set(0, l.get(1));
assertEquals(1, l.size());
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
// duplicate should be removed again
l.add(1, l.get(0));
assertEquals(1, l.size());
}
@SuppressWarnings("unchecked")
public void testRetainAll() {
final List<E> list = new ArrayList<>(10);
final SetUniqueList<E> uniqueList = SetUniqueList.setUniqueList(list);
for (int i = 0; i < 10; ++i) {
uniqueList.add((E)Integer.valueOf(i));
}
public void testUniqueListReInsert() {
final List<E> l = SetUniqueList.setUniqueList(new LinkedList<E>());
l.add((E) new Object());
l.add((E) new Object());
final Collection<E> retained = new ArrayList<>(5);
for (int i = 0; i < 5; ++i) {
retained.add((E)Integer.valueOf(i * 2));
}
final E a = l.get(0);
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)));
// 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 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
public String getCompatibilityVersion() {
return "4";
@SuppressWarnings("unchecked")
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 {