This commit is contained in:
Gary Gregory 2019-11-03 19:00:50 -05:00
commit c30586294f
5 changed files with 251 additions and 0 deletions

View File

@ -83,4 +83,64 @@ public class FixedSizeListTest<E> extends AbstractListTest<E> {
Assert.assertEquals("Modifying an the underlying list is allowed", Assert.assertEquals("Modifying an the underlying list is allowed",
sizeBefore + 1, fixedSizeList.size()); sizeBefore + 1, fixedSizeList.size());
} }
private FixedSizeList<String> initFixedSizeList() {
List<String> decoratedList = new ArrayList<>();
decoratedList.add("item 1");
decoratedList.add("item 2");
//
return FixedSizeList.fixedSizeList(decoratedList);
}
public void testAdd() {
FixedSizeList<String> fixedSizeList = initFixedSizeList();
try {
fixedSizeList.add(2,"New Value");
fail();
} catch (UnsupportedOperationException ex) {}
}
public void testAddAll() {
FixedSizeList<String> fixedSizeList = initFixedSizeList();
List<String> addList = new ArrayList<>();
addList.add("item 3");
addList.add("item 4");
try {
fixedSizeList.addAll(2, addList);
fail();
} catch (UnsupportedOperationException ex) {}
}
public void testRemove() {
FixedSizeList<String> fixedSizeList = initFixedSizeList();
try {
fixedSizeList.remove(1);
fail();
} catch (UnsupportedOperationException ex) {}
}
public void testSubList() {
FixedSizeList<String> fixedSizeList = initFixedSizeList();
List<String> subFixedSizeList = fixedSizeList.subList(1, 1);
Assert.assertNotNull(subFixedSizeList);
Assert.assertEquals(0, subFixedSizeList.size());
}
public void testIsFull() {
FixedSizeList<String> fixedSizeList = initFixedSizeList();
Assert.assertTrue(fixedSizeList.isFull());
}
public void testMaxSize() {
FixedSizeList<String> fixedSizeList = initFixedSizeList();
Assert.assertEquals(2, fixedSizeList.maxSize());
}
} }

View File

@ -98,6 +98,54 @@ public class LazyListTest extends AbstractObjectTest {
assertNotNull(fourthElement); assertNotNull(fourthElement);
} }
public void testGetWithNull() {
final List<Integer> hours = Arrays.asList(7, 5, 8, 2);
final Transformer<Integer, LocalDateTime> transformer = input -> LocalDateTime.now().withHour(hours.get(input));
List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), transformer);
LocalDateTime fourthElement = list.get(3);
assertFalse(list.isEmpty());
assertNotNull(fourthElement);
list.remove(3);
list.add(3,null);
fourthElement = list.get(3);
assertNotNull(fourthElement);
}
public void testSubListWitheFactory() {
final Factory<LocalDateTime> dateFactory = LocalDateTime::now;
List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), dateFactory);
LocalDateTime fourthElement = list.get(3);
assertFalse(list.isEmpty());
assertNotNull(fourthElement);
testSubList(list);
}
public void testSubListWithTransformer() {
final List<Integer> hours = Arrays.asList(7, 5, 8, 2);
final Transformer<Integer, LocalDateTime> transformer = input -> LocalDateTime.now().withHour(hours.get(input));
List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), transformer);
LocalDateTime fourthElement = list.get(3);
assertFalse(list.isEmpty());
assertNotNull(fourthElement);
testSubList(list);
}
private void testSubList(List<LocalDateTime> list) {
List<LocalDateTime> subList = list.subList(1, 3);
assertFalse(subList.isEmpty());
assertNotNull(subList);
assertEquals(2, subList.size());
subList = list.subList(0, 1);
assertFalse(subList.isEmpty());
assertEquals(1, subList.size());
subList = list.subList(1, 1);
assertTrue(subList.isEmpty());
subList = list.subList(0, list.size());
assertFalse(subList.isEmpty());
assertEquals(list.size(), subList.size());
}
} }

View File

@ -130,6 +130,28 @@ public class PredicatedListTest<E> extends AbstractListTest<E> {
list.contains("three")); list.contains("three"));
} }
public void testSubList() {
final List<E> list = makeTestList();
list.add((E) "zero");
//subList without any element of list
List<E> subList = list.subList(0, 0);
assertNotNull(subList);
assertEquals(0, subList.size());
//subList with one element oif list
subList = list.subList(0, 1);
assertEquals(1, subList.size());
final List<E> elements = new ArrayList<>();
elements.add((E) "one");
elements.add((E) "two");
elements.add((E) "three");
list.addAll(1, elements);
//subList with all elements of list
subList = list.subList(0, list.size());
assertEquals(list.size(), subList.size());
}
@Override @Override
public String getCompatibilityVersion() { public String getCompatibilityVersion() {
return "4"; return "4";

View File

@ -127,6 +127,30 @@ public class TransformedListTest<E> extends AbstractListTest<E> {
assertEquals(true, list.remove(Integer.valueOf((String) els[0]))); assertEquals(true, list.remove(Integer.valueOf((String) els[0])));
} }
public void testSubList() {
final List<E> list = makeObject();
List<E> subList = list.subList(0, 0);
assertNotNull(subList);
list.add((E) "zero");
//subList without any element of list
subList = list.subList(0, 0);
assertNotNull(subList);
assertEquals(0, subList.size());
//subList with one element oif list
subList = list.subList(0, 1);
assertEquals(1, subList.size());
final List<E> elements = new ArrayList<>();
elements.add((E) "one");
elements.add((E) "two");
elements.add((E) "three");
list.addAll(1, elements);
//subList with all elements of list
subList = list.subList(0, list.size());
assertEquals(list.size(), subList.size());
}
@Override @Override
public String getCompatibilityVersion() { public String getCompatibilityVersion() {
return "4"; return "4";

View File

@ -18,6 +18,10 @@ package org.apache.commons.collections4.multimap;
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
import java.util.Map;
import java.util.HashMap;
import java.util.Arrays;
import java.util.ArrayList;
import junit.framework.Test; import junit.framework.Test;
@ -145,6 +149,99 @@ public class ArrayListValuedHashMapTest<K, V> extends AbstractMultiValuedMapTest
assertNotSame(map1.hashCode(), map2.hashCode()); assertNotSame(map1.hashCode(), map2.hashCode());
} }
public void testArrayListValuedHashMap() {
ListValuedMap<K, V> listMap = null;
ListValuedMap<K, V> listMap1 = null;
Map<K, V> map = new HashMap<>();
Map<K, V> map1 = new HashMap<>();
map.put((K) "A", (V) "W");
map.put((K) "B", (V) "X");
map.put((K) "C", (V) "F");
listMap = new ArrayListValuedHashMap<>(map);
assertEquals(1, listMap.get((K) "A").size());
assertEquals(1, listMap.get((K) "B").size());
assertEquals(1, listMap.get((K) "C").size());
listMap1 = new ArrayListValuedHashMap<>(map1);
assertEquals("{}", listMap1.toString());
}
public void testTrimToSize(){
final ArrayListValuedHashMap<K, V> listMap = new ArrayListValuedHashMap<>(4);
assertEquals("{}", listMap.toString());
listMap.put((K) "A", (V) "W");
listMap.put((K) "A", (V) "X");
listMap.put((K) "B", (V) "F");
assertEquals(2, listMap.get((K) "A").size());
assertEquals(1, listMap.get((K) "B").size());
listMap.trimToSize();
assertEquals(2, listMap.get((K) "A").size());
assertEquals(1, listMap.get((K) "B").size());
}
public void testWrappedListAdd() {
final ListValuedMap<K, V> listMap = makeObject();
List<V> listA = listMap.get((K) "A");
listA.add(0, (V) "W");
listA.add(1, (V) "X");
listA.add(2, (V) "F");
assertEquals("{A=[W, X, F]}", listMap.toString());
listMap.get((K) "A").set(1, (V) "Q");
assertEquals("{A=[W, Q, F]}", listMap.toString());
}
public void testWrappedListAddAll() {
final ListValuedMap<K, V> listMap = makeObject();
List<V> listA = listMap.get((K) "A");
List<V> list = Arrays.asList((V) "W", (V) "X", (V) "F");
listA.addAll(0, list);
assertEquals("{A=[W, X, F]}", listMap.toString());
List<V> list1 = Arrays.asList((V) "Q", (V) "Q", (V) "L");
listA.addAll(3, list1);
assertEquals("{A=[W, X, F, Q, Q, L]}", listMap.toString());
assertEquals("W", listMap.get((K) "A").get(0));
assertEquals("X", listMap.get((K) "A").get(1));
assertEquals("F", listMap.get((K) "A").get(2));
assertEquals("Q", listMap.get((K) "A").get(3));
assertEquals("Q", listMap.get((K) "A").get(4));
assertEquals("L", listMap.get((K) "A").get(5));
assertEquals(0, listMap.get((K) "A").indexOf("W"));
assertEquals(2, listMap.get((K) "A").indexOf("F"));
assertEquals(-1, listMap.get((K) "A").indexOf("C"));
assertEquals(3, listMap.get((K) "A").indexOf("Q"));
assertEquals(4, listMap.get((K) "A").lastIndexOf("Q"));
assertEquals(-1, listMap.get((K) "A").lastIndexOf("A"));
List<V> list2 = new ArrayList<>();
listMap.get((K) "B").addAll(0, list2);
assertEquals("{A=[W, X, F, Q, Q, L]}", listMap.toString());
List<V> list3 = listMap.get((K) "A").subList(1, 4);
assertEquals(3, list3.size());
assertEquals("Q", list3.get(2));
}
public void testValuesListIteratorMethods(){
final ListValuedMap<K, V> listMap = makeObject();
List<V> listA = listMap.get((K) "A");
List<V> list = Arrays.asList((V) "W", (V) "X", (V) "F", (V) "Q", (V) "Q", (V)"F");
listA.addAll(0, list);
ListIterator<V> it = listMap.get((K) "A").listIterator(1);
assertTrue(it.hasNext());
assertEquals("X", it.next());
assertEquals("F", it.next());
assertTrue(it.hasPrevious());
assertEquals("F", it.previous());
assertEquals(2, it.nextIndex());
assertEquals(1, it.previousIndex());
it.set((V) "Z");
assertEquals("Z", it.next());
assertEquals("Q", it.next());
}
// public void testCreate() throws Exception { // public void testCreate() throws Exception {
// writeExternalFormToDisk((java.io.Serializable) makeObject(), // writeExternalFormToDisk((java.io.Serializable) makeObject(),
// "src/test/resources/data/test/ArrayListValuedHashMap.emptyCollection.version4.1.obj"); // "src/test/resources/data/test/ArrayListValuedHashMap.emptyCollection.version4.1.obj");