From: <cg...@us...> - 2009-01-11 03:21:30
|
Revision: 5915 http://jython.svn.sourceforge.net/jython/?rev=5915&view=rev Author: cgroves Date: 2009-01-11 03:21:25 +0000 (Sun, 11 Jan 2009) Log Message: ----------- Cleanup Modified Paths: -------------- trunk/jython/src/org/python/core/PyDictionary.java trunk/jython/src/org/python/core/PyObjectList.java trunk/jython/src/org/python/core/PySequenceList.java trunk/jython/tests/java/javatests/ListTest.java trunk/jython/tests/java/javatests/TestSupport.java Modified: trunk/jython/src/org/python/core/PyDictionary.java =================================================================== --- trunk/jython/src/org/python/core/PyDictionary.java 2009-01-11 01:12:16 UTC (rev 5914) +++ trunk/jython/src/org/python/core/PyDictionary.java 2009-01-11 03:21:25 UTC (rev 5915) @@ -12,7 +12,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; -import org.python.core.PyMapSet.PySetIter; import org.python.expose.ExposedClassMethod; import org.python.expose.ExposedMethod; import org.python.expose.ExposedNew; @@ -27,7 +26,7 @@ public class PyDictionary extends PyObject implements ConcurrentMap { public static final PyType TYPE = PyType.fromClass(PyDictionary.class); - + protected final ConcurrentMap<PyObject, PyObject> table; /** @@ -39,7 +38,6 @@ /** * For derived types - * @param subtype */ public PyDictionary(PyType subtype) { super(subtype); @@ -47,31 +45,27 @@ } /** - * Create an new dictionary which is based on the hashtable. - * @param t the hashtable used. The supplied hashtable is used as - * is and must only contain PyObject key:value pairs. + * Create a new dictionary which is based on given map. */ public PyDictionary(Map<PyObject, PyObject> t) { table = new ConcurrentHashMap<PyObject, PyObject>(t); } - /** - * Create an new dictionary which is based on the map and for derived types. - * @param subtype - * @param t the hashtable used. The supplied hashtable is used as - * is and must only contain PyObject key:value pairs. + /** + * Create a new derived dictionary which is based on the given map. */ public PyDictionary(PyType subtype, Map<PyObject, PyObject> t) { super(subtype); table = new ConcurrentHashMap<PyObject, PyObject>(t); } - + /** * Create a new dictionary with the element as content. - * @param elements The initial elements that is inserted in the - * dictionary. Even numbered elements are keys, - * odd numbered elements are values. + * + * @param elements + * The initial elements that is inserted in the dictionary. Even numbered elements + * are keys, odd numbered elements are values. */ public PyDictionary(PyObject elements[]) { this(); @@ -85,7 +79,7 @@ protected final void dict___init__(PyObject[] args, String[] keywords) { updateCommon(args, keywords, "dict"); } - + public static PyObject fromkeys(PyObject keys) { return fromkeys(keys, Py.None); } @@ -210,10 +204,10 @@ } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.dict___eq___doc) - final PyObject dict___eq__(PyObject ob_other) { + final PyObject dict___eq__(PyObject ob_other) { PyType thisType = getType(); PyType otherType = ob_other.getType(); - if (otherType != thisType && !thisType.isSubType(otherType) + if (otherType != thisType && !thisType.isSubType(otherType) && !otherType.isSubType(thisType)) { return null; } @@ -291,7 +285,7 @@ final int dict___cmp__(PyObject ob_other) { PyType thisType = getType(); PyType otherType = ob_other.getType(); - if (otherType != thisType && !thisType.isSubType(otherType) + if (otherType != thisType && !thisType.isSubType(otherType) && !otherType.isSubType(thisType)) { return -2; } @@ -435,7 +429,7 @@ } for (int i = 0; i < keywords.length; i++) { dict___setitem__(Py.newString(keywords[i]), args[nargs + i]); - } + } } /** @@ -539,7 +533,7 @@ } } - + /** * Return a value based on key * from the dictionary. @@ -578,18 +572,17 @@ @ExposedMethod(doc = BuiltinDocs.dict_popitem_doc) final PyObject dict_popitem() { - Iterator it = table.entrySet().iterator(); + Iterator<Entry<PyObject, PyObject>> it = table.entrySet().iterator(); if (!it.hasNext()) throw Py.KeyError("popitem(): dictionary is empty"); - Entry entry = (Entry)it.next(); - PyTuple tuple = new PyTuple( - (PyObject)entry.getKey(), (PyObject)entry.getValue()); + Entry<PyObject, PyObject> entry = it.next(); + PyTuple tuple = new PyTuple(entry.getKey(), entry.getValue()); it.remove(); return tuple; } /** - * Return a copy of the dictionarys list of (key, value) tuple + * Return a copy of the dictionary's list of (key, value) tuple * pairs. */ public PyList items() { @@ -606,7 +599,7 @@ } /** - * Return a copy of the dictionarys list of keys. + * Return a copy of the dictionary's list of keys. */ public PyList keys() { return dict_keys(); @@ -623,7 +616,7 @@ } /** - * Return an interator over (key, value) pairs. + * Returns an iterator over (key, value) pairs. */ public PyObject iteritems() { return dict_iteritems(); @@ -635,7 +628,7 @@ } /** - * Return an interator over (key, value) pairs. + * Returns an iterator over the dictionary's keys. */ public PyObject iterkeys() { return dict_iterkeys(); @@ -647,7 +640,7 @@ } /** - * Return an interator over (key, value) pairs. + * Returns an iterator over the dictionary's values. */ public PyObject itervalues() { return dict_itervalues(); @@ -703,7 +696,7 @@ iterator = items.iterator(); size = items.size(); } - + public PyObject __iternext__() { if (table.size() != size) { throw Py.RuntimeError("dictionary changed size during iteration"); @@ -716,13 +709,12 @@ } } - /* The following methods implement the java.util.Map interface - which allows PyDictionary to be passed to java methods that take - java.util.Map as a parameter. Basically, the Map methods are a - wrapper around the PyDictionary's Map container stored in member - variable 'table'. These methods simply convert java Object to - PyObjects on insertion, and PyObject to Objects on retrieval. */ - + /* + * The following methods implement the java.util.Map interface which allows PyDictionary to be + * passed to java methods that take java.util.Map as a parameter. Basically, the Map methods are + * a wrapper around the PyDictionary's Map container stored in member variable 'table'. These + * methods convert java Object to PyObjects on insertion, and PyObject to Objects on retrieval. + */ /** @see java.util.Map#entrySet() */ public Set entrySet() { return new PyMapEntrySet(table.entrySet()); @@ -733,17 +725,15 @@ return new PyMapKeyValSet(table.keySet()); } - /** Return a copy of the dictionarys list of values. */ + /** @see java.util.Map#values() */ public Collection values() { return new PyMapKeyValSet(table.values()); } - /** @see java.util.Map#putAll(Map map) */ public void putAll(Map map) { - Iterator i = map.entrySet().iterator(); - while (i.hasNext()) { - Entry entry = (Entry)i.next(); + for (Object o : map.entrySet()) { + Entry entry = (Entry)o; table.put(Py.java2py(entry.getKey()), Py.java2py(entry.getValue())); } } @@ -772,19 +762,19 @@ public boolean containsKey(Object key) { return table.containsKey(Py.java2py(key)); } - - /** @see java.util.Map#isEmpty(Object key) */ + + /** @see java.util.Map#isEmpty() */ public boolean isEmpty() { return table.isEmpty(); } - - /** @see java.util.Map#size(Object key) */ + + /** @see java.util.Map#size() */ public int size() { return table.size(); } /** Convert return values to java objects */ - static final Object tojava(Object val) { + final static Object tojava(Object val) { return val == null ? null : ((PyObject)val).__tojava__(Object.class); } @@ -805,19 +795,20 @@ } } + /** Basic implementation of Entry that just holds onto a key and value and returns them. */ -class SimpleEntry<K, V> implements Entry<K, V> { - - public SimpleEntry(K key, V value){ +class SimpleEntry implements Entry { + + public SimpleEntry(Object key, Object value){ this.key = key; this.value = value; } - - public K getKey() { + + public Object getKey() { return key; } - public V getValue() { + public Object getValue() { return value; } @@ -841,31 +832,29 @@ return key + "=" + value; } - public V setValue(V val) { + public Object setValue(Object val) { throw new UnsupportedOperationException("Not supported by this view"); } - protected K key; + protected Object key; - protected V value; + protected Object value; } /** - * Wrapper for a Entry object returned from the java.util.Set - * object which in turn is returned by the entrySet method of - * java.util.Map. This is needed to correctly convert from PyObjects - * to java Objects. Note that we take care in the equals and hashCode - * methods to make sure these methods are consistent with Entry - * objects that contain java Objects for a value so that on the java - * side they can be reliable compared. + * Wrapper for a Entry object returned from the java.util.Set object which in turn is returned by + * the entrySet method of java.util.Map. This is needed to correctly convert from PyObjects to java + * Objects. Note that we take care in the equals and hashCode methods to make sure these methods are + * consistent with Entry objects that contain java Objects for a value so that on the java side they + * can be reliable compared. */ class PyToJavaMapEntry extends SimpleEntry { - /** Create a copy of the Entry with Py.None coverted to null */ + /** Create a copy of the Entry with Py.None converted to null */ PyToJavaMapEntry(Entry entry) { super(entry.getKey(), entry.getValue()); } - + public boolean equals(Object o) { if (o == null || !(o instanceof Entry)) return false; Entry me = new JavaToPyMapEntry((Entry)o); @@ -877,7 +866,7 @@ public Object getKey() { return PyDictionary.tojava(key); } - + public Object getValue() { return PyDictionary.tojava(value); } @@ -892,24 +881,22 @@ } /** - * MapEntry Object for java MapEntry objects passed to the java.util.Set - * interface which is returned by the entrySet method of PyDictionary. - * Essentially like PyTojavaMapEntry, but going the other way converting java - * Objects to PyObjects. + * MapEntry Object for java MapEntry objects passed to the java.util.Set interface which is returned + * by the entrySet method of PyDictionary. Essentially like PyTojavaMapEntry, but going the other + * way converting java Objects to PyObjects. */ class JavaToPyMapEntry extends SimpleEntry { - + public JavaToPyMapEntry(Entry entry) { super(Py.java2py(entry.getKey()), Py.java2py(entry.getValue())); } } /** - * Wrapper collection class for the keySet and values methods of - * java.util.Map + * Wrapper collection class for the keySet and values methods of java.util.Map */ class PyMapKeyValSet extends PyMapSet { - + PyMapKeyValSet(Collection coll) { super(coll); } @@ -917,21 +904,18 @@ Object toJava(Object o) { return PyDictionary.tojava(o); } - + Object toPython(Object o) { return Py.java2py(o); } } /** - * Set wrapper for the java.util.EntrySet method. Entry - * objects are wrapped further in JavaToPyMapEntry and - * PyToJavaMapEntry. Note - The set interface is reliable for - * standard objects like strings and integers, but may be inconstant - * for other types of objects since the equals method may return false - * for Entry object that hold more elaborate PyObject types. - * However, We insure that this iterface works when the Entry - * object originates from a Set object retrieved from a PyDictionary. + * Set wrapper for the entrySet method. Entry objects are wrapped further in JavaToPyMapEntry and + * PyToJavaMapEntry. Note - The set interface is reliable for standard objects like strings and + * integers, but may be inconsistent for other types of objects since the equals method may return + * false for Entry object that hold more elaborate PyObject types. However, we insure that this + * interface works when the Entry object originates from a Set object retrieved from a PyDictionary. */ class PyMapEntrySet extends PyMapSet { @@ -939,13 +923,13 @@ super(coll); } - // We know that PyMapEntrySet will only contains Entrys, so - // if the object being passed in is null or not a Entry, then - // return null which will match nothing for remove and contains methods. + // We know that PyMapEntrySet will only contains entries, so if the object being passed in is + // null or not an Entry, then return null which will match nothing for remove and contains + // methods. Object toPython(Object o) { if(o == null || !(o instanceof Entry)) return null; - if(o instanceof PyToJavaMapEntry) { + if (o instanceof PyToJavaMapEntry) { // Use the original entry from PyDictionary return ((PyToJavaMapEntry)o).getEntry(); } else { @@ -959,15 +943,12 @@ } /** - * PyMapSet serves as a wrapper around Set Objects returned by the - * java.util.Map interface of PyDictionary. entrySet, values and - * keySet methods return this type for the java.util.Map - * implementation. This class is necessary as a wrapper to convert - * PyObjects to java Objects for methods that return values, and - * convert Objects to PyObjects for methods that take values. The - * translation is necessary to provide java access to jython - * dictionary objects. This wrapper also provides the expected backing - * functionality such that changes to the wrapper set or reflected in + * PyMapSet serves as a wrapper around Set Objects returned by the java.util.Map interface of + * PyDictionary. entrySet, values and keySet methods return this type for the java.util.Map + * implementation. This class is necessary as a wrapper to convert PyObjects to java Objects for + * methods that return values, and convert Objects to PyObjects for methods that take values. The + * translation is necessary to provide java access to jython dictionary objects. This wrapper also + * provides the expected backing functionality such that changes to the wrapper set or reflected in * PyDictionary. */ abstract class PyMapSet extends AbstractSet { @@ -983,15 +964,15 @@ public int size() { return coll.size(); } - + public boolean contains(Object o) { return coll.contains(toPython(o)); } - + public boolean remove(Object o) { return coll.remove(toPython(o)); } - + public void clear() { coll.clear(); } @@ -1013,15 +994,15 @@ public Object next() { return toJava(itr.next()); } - + public void remove() { itr.remove(); } } - + public Iterator iterator() { return new PySetIter(coll.iterator()); } - + private final Collection coll; } Modified: trunk/jython/src/org/python/core/PyObjectList.java =================================================================== --- trunk/jython/src/org/python/core/PyObjectList.java 2009-01-11 01:12:16 UTC (rev 5914) +++ trunk/jython/src/org/python/core/PyObjectList.java 2009-01-11 03:21:25 UTC (rev 5915) @@ -4,7 +4,6 @@ import java.io.Serializable; import java.util.AbstractList; import java.util.Collection; -import java.util.Iterator; import java.util.RandomAccess; /** @@ -20,17 +19,15 @@ * @author Clark Updike */ public class PyObjectList - extends AbstractList implements RandomAccess, Cloneable, Serializable { + extends AbstractList<Object> implements RandomAccess, Cloneable, Serializable { - /* Design note: - * This class let's PySequenceList implement java.util.List by delegating - * to an instance of this. The major distinction is that the backing array - * is PyObject[], not Object[] (as you'd get by delegating to ArrayList). - * There are 2 major benefits: 1) A lot of casting can be avoided - * internally (although use of PySequenceList descendants as java - * collections does involve some casting); 2) PySequenceList descendants - * can still do bulk array operations, allowing better performance and - * reuse of much of the pre-collections bulk operation implementation. + /* + * Design note: This class lets PySequenceList implement java.util.List by delegating to it. The + * major distinction is that the backing array is PyObject[], not Object[] (as you'd get by + * delegating to ArrayList). There are 2 major benefits: 1) A lot of casting can be avoided + * internally (although use of PySequenceList descendants as java collections does involve some + * casting); 2) PySequenceList descendants can still do bulk array operations, allowing better + * performance and reuse of much of the pre-collections bulk operation implementation. */ @@ -49,7 +46,7 @@ array.baseArray = pyObjArr; } - public PyObjectList(Collection c) { + public PyObjectList(Collection<PyObject> c) { array = new PyObjectArray(); array.appendArray(c.toArray()); } @@ -62,8 +59,7 @@ * For internal jython usage, use {@link #pyadd(int, PyObject)}. */ public void add(int index, Object element) { - array.add(index, Py.java2py(element)); - modCount += array.getModCountIncr(); + pyadd(index, Py.java2py(element)); } public void pyadd(int index, PyObject element) { @@ -75,8 +71,7 @@ * For internal jython usage, use {@link #pyadd(PyObject)}. */ public boolean add(Object o) { - array.add(Py.java2py(o)); - modCount += array.getModCountIncr(); + pyadd(Py.java2py(o)); return true; } @@ -90,7 +85,7 @@ try { PyObjectList tol = (PyObjectList) super.clone(); tol.array = (PyObjectArray) array.clone(); - modCount = 0; + tol.modCount = 0; return tol; } catch (CloneNotSupportedException eCNSE) { throw new InternalError("Unexpected CloneNotSupportedException.\n" @@ -113,8 +108,7 @@ * Use <code>pyget(int)</code> for internal jython usage. */ public Object get(int index) { - PyObject obj = array.get(index); - return obj.__tojava__(Object.class); + return array.get(index).__tojava__(Object.class); } PyObject pyget(int index) { @@ -137,7 +131,7 @@ * Use <code>pyset(int, PyObject)</code> for internal jython usage. */ public Object set(int index, Object element) { - return array.set(index, Py.java2py(element) ).__tojava__(Object.class); + return pyset(index, Py.java2py(element)).__tojava__(Object.class); } PyObject pyset(int index, PyObject element) { @@ -148,11 +142,11 @@ return array.getSize(); } - public boolean addAll(Collection c) { + public boolean addAll(Collection<? extends Object> c) { return addAll(size(), c); } - public boolean addAll(int index, Collection c) { + public boolean addAll(int index, Collection<? extends Object> c) { if (c instanceof PySequenceList) { PySequenceList cList = (PySequenceList)c; PyObject[] cArray = cList.getArray(); @@ -160,21 +154,20 @@ array.makeInsertSpace(index, cOrigSize); array.replaceSubArray(index, index + cOrigSize, cArray, 0, cOrigSize); } else { - // need to use add to convert anything pulled from a collection - // into a PyObject - for (Iterator i = c.iterator(); i.hasNext(); ) { - add(i.next()); + // need to use add to convert anything pulled from a collection into a PyObject + for (Object element : c) { + add(element); } } return c.size() > 0; } - /** - * Get the backing array. The array should generally not be modified. - * To get a copy of the array, see {@link #toArray()} which returns a copy. - * - * @return backing array object - */ + /** + * Get the backing array. The array should generally not be modified. To get a copy of the + * array, see {@link #toArray()} which returns a copy. + * + * @return backing array object + */ protected PyObject[] getArray() { return (PyObject[])array.getArray(); } Modified: trunk/jython/src/org/python/core/PySequenceList.java =================================================================== --- trunk/jython/src/org/python/core/PySequenceList.java 2009-01-11 01:12:16 UTC (rev 5914) +++ trunk/jython/src/org/python/core/PySequenceList.java 2009-01-11 03:21:25 UTC (rev 5915) @@ -30,7 +30,7 @@ list = new PyObjectList(elements); } - public PySequenceList(PyType type, Collection c) { + public PySequenceList(PyType type, Collection<PyObject> c) { super(type); list = new PyObjectList(c); } Modified: trunk/jython/tests/java/javatests/ListTest.java =================================================================== --- trunk/jython/tests/java/javatests/ListTest.java 2009-01-11 01:12:16 UTC (rev 5914) +++ trunk/jython/tests/java/javatests/ListTest.java 2009-01-11 03:21:25 UTC (rev 5915) @@ -1,151 +1,140 @@ -//Copyright (c) Corporation for National Research Initiatives +// Copyright (c) Corporation for National Research Initiatives package javatests; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.NoSuchElementException; +import org.python.util.Generic; + /** * @author updikca1 */ public abstract class ListTest { - + public static ListTest getArrayListTest(final boolean makeReadOnly) { return new ListTest() { - public List newInstance(Collection c) { - List l = null; - if(c == null) { - l = new ArrayList(); + + public List<Object> newInstance(Collection<Object> c) { + List<Object> l = null; + if (c == null) { + l = Generic.list(); } else { - l = new ArrayList(c); + l = new ArrayList<Object>(c); } - return (makeReadOnly) - ? Collections.unmodifiableList(l) - : l; + return (makeReadOnly) ? Collections.unmodifiableList(l) : l; } + public boolean isReadOnly() { return makeReadOnly; - } + } }; } - - public static void verifyImutability(List l) { - + + public static void verifyImutability(List<Object> l) { String message = "Expected UnsupportedOperationException."; - try { - l.add(0, new Integer(0)); - TestSupport.assertThat(false, message); + l.add(0, 0); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} - try { - l.add(new Integer(0)); - TestSupport.assertThat(false, message); + l.add(0); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} - try { - l.addAll(null); - TestSupport.assertThat(false, message); + l.addAll(null); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} - try { - l.addAll(0, null); - TestSupport.assertThat(false, message); + l.addAll(0, null); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} - try { - l.clear(); - TestSupport.assertThat(false, message); + l.clear(); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} - try { - l.remove(0); - TestSupport.assertThat(false, message); + l.remove(0); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} - try { l.remove(new Object()); - TestSupport.assertThat(false, message); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} - try { - l.removeAll(null); - TestSupport.assertThat(false, message); + l.removeAll(null); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} - try { l.retainAll(null); - TestSupport.assertThat(false, message); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} try { - l.set(0,new Integer(0)); - TestSupport.assertThat(false, message); - } catch (UnsupportedOperationException e) {} + l.set(0, 0); + TestSupport.fail(message); + } catch (UnsupportedOperationException e) {} } - - private final List nullList; - - protected List defaultList() { - List l = new ArrayList(); + + private final List<Object> nullList; + + protected List<Object> defaultList() { + List<Object> l = Generic.list(); for (int i = 0; i < 4; i++) { - l.add(new Integer(i)); + l.add(i); } return newInstance(l); } - + /** * Implementations must supply an empty list if the collection is null. - * @param c Initial collection or null for empty. + * + * @param c + * Initial collection or null for empty. * @return the List instance */ - public List newInstance(Collection c) { + public List<Object> newInstance(Collection<Object> c) { throw new UnsupportedOperationException("This method must be overridden"); } - + /** * @return true if the list is read-only (like PyTuple) */ public boolean isReadOnly() { throw new UnsupportedOperationException("This method must be overridden"); } - + { nullList = newInstance(Arrays.asList(new Object[] {null})); } - + public void testAll() { - test_get(); test_equals(); test_size(); test_contains(); test_containsAll(); - try { defaultList().hashCode(); test_hashCode(); } catch (Exception e) { // skip unhashable types } - test_subList(); test_lastIndexOf(); test_listIterator(); test_toArray(); test_toArray_typed(); - - if(!isReadOnly()) { + if (!isReadOnly()) { test_add(); test_add_index(); test_set(); test_clear(); test_addAll(); - test_addAll_index(); + test_addAll_index(); test_remove(); test_remove_index(); test_removeAll(); @@ -154,481 +143,404 @@ verifyImutability(newInstance(null)); } } - + /** Tests get(int index) */ public void test_get() { - TestSupport.assertThat(defaultList().get(0).equals(new Integer(0)), - "get() did not return expected value of Integer(0)"); + List<Object> l = defaultList(); + TestSupport.assertThat(l.get(0).equals(0), + "get() did not return expected value of Integer(0)"); try { - defaultList().get(-1); - TestSupport.assertThat(false, - "get(<negative index>) did not throw IndexOutOfBoundsException"); + l.get(-1); + TestSupport.fail("get(<negative index>) did not throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) {} - try { - defaultList().get(-1); - TestSupport.assertThat(false, - "get(<index too big>) did not throw IndexOutOfBoundsException"); + l.get(l.size()); + TestSupport.fail("get(<index too big>) did not throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) {} } - + /** Tests set(int index, Object element) */ public void test_set() { - try { newInstance(null).set(-1, "spam"); - TestSupport.assertThat(false, - "get(<negative index>) did not throw IndexOutOfBoundsException"); + TestSupport.fail("get(<negative index>) did not throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) {} - try { newInstance(null).set(0, "spam"); - TestSupport.assertThat(false, - "set(<index too big>) did not throw IndexOutOfBoundsException"); + TestSupport.fail("set(<index too big>) did not throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) {} - - List a = defaultList(); - a.set(a.size() - 1 , "spam"); - TestSupport.assertThat(a.get(a.size() - 1).equals("spam"), - "set() object was not retrieved via get()"); + List<Object> a = defaultList(); + a.set(a.size() - 1, "spam"); + TestSupport.assertThat(a.get(a.size() - 1).equals("spam"), + "set() object was not retrieved via get()"); } - - /** Tests add(Object o) */ - public void test_add() { - List a = newInstance(null); + + /** Tests add(Object o) */ + public void test_add() { + List<Object> a = newInstance(null); for (int i = 0; i < 4; i++) { - a.add(new Integer(i)); + a.add(i); } TestSupport.assertEquals(a, defaultList(), "add(Object o) failed"); } - + /** Tests isEmpty() */ public void test_isEmpty() { - List a = newInstance(null); - TestSupport.assertThat(a.isEmpty(), - "isEmpty() is false on an emtpy List"); + List<Object> a = newInstance(null); + TestSupport.assertThat(a.isEmpty(), "isEmpty() is false on an emtpy List"); a.addAll(defaultList()); - TestSupport.assertThat(!a.isEmpty(), - "isEmpty() is true on a non-empty List)" ); + TestSupport.assertThat(!a.isEmpty(), "isEmpty() is true on a non-empty List)"); a.clear(); - TestSupport.assertThat(a.isEmpty(), - "isEmpty() is false on an emtpy List"); + TestSupport.assertThat(a.isEmpty(), "isEmpty() is false on an emtpy List"); } - + /** Tests size() */ - public void test_size() { - List b = newInstance(null); + public void test_size() { + List<Object> b = newInstance(null); TestSupport.assertThat(b.size() == 0, "empty list size was not 0"); - TestSupport.assertThat(defaultList().size() == 4, - "default list did not have a size of 4"); + TestSupport.assertThat(defaultList().size() == 4, "default list did not have a size of 4"); } - - /** Tests add(int index, Object element) */ + + /** Tests add(int index, Object element) */ public void test_add_index() { - List a = newInstance(null); - List b = defaultList(); + List<Object> a = newInstance(null); + List<Object> b = defaultList(); for (int i = 0; i < b.size(); i++) { a.add(i, b.get(i)); } - try { - a.add(a.size() + 1, new Integer(a.size() + 1)); - TestSupport.assertThat(false, "expected IndexOutOfBoundsException"); + a.add(a.size() + 1, new Integer(a.size() + 1)); + TestSupport.fail("expected IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) {} - try { - a.add(-1, new Integer(-1)); - TestSupport.assertThat(false, "expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) {} + a.add(-1, new Integer(-1)); + TestSupport.fail("expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) {} } - - /** Tests equals(Object o)*/ + + /** Tests equals(Object o) */ public void test_equals() { - TestSupport.assertEquals(defaultList(), defaultList(), - "Identical lists weren't equal()"); - TestSupport.assertNotEquals(newInstance(null), defaultList(), - "Different lists were equal()"); - TestSupport.assertNotEquals(newInstance(null), new Object(), - "List was equal to a non-List type"); + TestSupport.assertEquals(defaultList(), defaultList(), "Identical lists weren't equal()"); + TestSupport.assertNotEquals(newInstance(null), + defaultList(), + "Different lists were equal()"); + TestSupport.assertNotEquals(newInstance(null), + new Object(), + "List was equal to a non-List type"); } - + /** Tests addAll(Collection c) */ public void test_addAll() { - List a = defaultList(); - List b = defaultList(); - - TestSupport.assertThat(a.addAll(b) == true, - "Mutating addAll(Collection) returned false"); - TestSupport.assertThat(a.addAll(newInstance(null)) == false, - "Idempotent addAll(Collection) returned true"); - TestSupport.assertThat(b.addAll(b) == true, - "Mutating addAll(Collection) returned false"); - TestSupport.assertEquals(a, b, - "Expected equal objects from addAll(collection)"); - TestSupport.assertThat(a.size() == 8, - "Expected List to have size 8 after addAll(Collection)"); + List<Object> a = defaultList(); + List<Object> b = defaultList(); + TestSupport.assertThat(a.addAll(b) == true, "Mutating addAll(Collection) returned false"); + TestSupport.assertThat(a.addAll(newInstance(null)) == false, + "Idempotent addAll(Collection) returned true"); + TestSupport.assertThat(b.addAll(b) == true, "Mutating addAll(Collection) returned false"); + TestSupport.assertEquals(a, b, "Expected equal objects from addAll(collection)"); + TestSupport.assertThat(a.size() == 8, + "Expected List to have size 8 after addAll(Collection)"); } - - /** Tests indexOf(Object o) */ + + /** Tests indexOf(Object o) */ public void indexOf() { - TestSupport.assertThat(defaultList().indexOf(new Integer(3)) == 3, - "indexOf(3) did not return 3"); - TestSupport.assertThat(defaultList().indexOf(new Integer(42)) == -1, - "indexOf() non-existing entry did not return -1"); - TestSupport.assertThat(defaultList().indexOf(null) == -1, - "indexOf() non-existing null did not return -1"); - + TestSupport.assertThat(defaultList().indexOf(3) == 3, "indexOf(3) did not return 3"); + TestSupport.assertThat(defaultList().indexOf(42) == -1, + "indexOf() non-existing entry did not return -1"); + TestSupport.assertThat(defaultList().indexOf(null) == -1, + "indexOf() non-existing null did not return -1"); } - - /** Tests contains(Object o) */ - public void test_contains() { - TestSupport.assertThat(defaultList().contains(new Integer(42)) == false, - "contains() returned true for non-existing entry"); - TestSupport.assertThat(defaultList().contains(new Integer(0)) == true, - "contains() returned false for existing entry"); - TestSupport.assertThat(nullList.contains(null) == true, - "contains() returned false for existing null entry"); - TestSupport.assertThat(defaultList().contains(null) == false, - "contains() returned true for non-existing null entry"); + + /** Tests contains(Object o) */ + public void test_contains() { + TestSupport.assertThat(defaultList().contains(42) == false, + "contains() returned true for non-existing entry"); + TestSupport.assertThat(defaultList().contains(0) == true, + "contains() returned false for existing entry"); + TestSupport.assertThat(nullList.contains(null) == true, + "contains() returned false for existing null entry"); + TestSupport.assertThat(defaultList().contains(null) == false, + "contains() returned true for non-existing null entry"); } - + /** Tests remove(Object o) */ public void test_remove() { - List a = defaultList(); + List<Object> a = defaultList(); a.add(null); - TestSupport.assertThat(a.remove(null) == true, - "remove() existing null entry returned false"); - TestSupport.assertThat(a.remove(null) == false, - "remove() non-existing null entry returned false"); + TestSupport.assertThat(a.remove(null) == true, + "remove() existing null entry returned false"); + TestSupport.assertThat(a.remove(null) == false, + "remove() non-existing null entry returned false"); a.add("spam"); - TestSupport.assertThat(a.remove("spam") == true, - "remove() existing entry returned false"); - TestSupport.assertThat(a.remove("spam") == false, - "remove() non-existing entry returned true"); + TestSupport.assertThat(a.remove("spam") == true, "remove() existing entry returned false"); + TestSupport.assertThat(a.remove("spam") == false, + "remove() non-existing entry returned true"); } - - + /** Tests remove(int index) */ - public void test_remove_index() { - - List a = defaultList(); + public void test_remove_index() { + List<Object> a = defaultList(); for (int i = 0, n = a.size(); i < n; i++) { a.remove(0); } - TestSupport.assertThat(a.size() == 0, - "remove()-d all entries but size() not 0"); - + TestSupport.assertThat(a.size() == 0, "remove()-d all entries but size() not 0"); try { a.remove(0); - TestSupport.assertThat(false, - "removing a non-existing index did not throw exception"); - } catch(IndexOutOfBoundsException e) {} + TestSupport.fail("removing a non-existing index did not throw exception"); + } catch (IndexOutOfBoundsException e) {} } - + /** Tests lastIndexOf(Object o) */ public void test_lastIndexOf() { - // avoid calling any mutable methods - List l = new ArrayList(defaultList()); - l.add(new Integer(0)); - + // avoid calling any mutable methods + List<Object> l = new ArrayList<Object>(defaultList()); + l.add(0); // now get the immutable version - List a = newInstance(l); - - TestSupport.assertThat(a.lastIndexOf(new Integer(0)) == 4, - "lastIndexOf() did not return 4"); - TestSupport.assertThat(a.lastIndexOf(new Integer(42)) == -1, - "lastIndexOf() non-existing value did not return -1"); + List<Object> a = newInstance(l); + TestSupport.assertThat(a.lastIndexOf(0) == 4, "lastIndexOf() did not return 4"); + TestSupport.assertThat(a.lastIndexOf(42) == -1, + "lastIndexOf() non-existing value did not return -1"); } - - /** Tests removeAll(Collection c) */ + + /** Tests removeAll(Collection c) */ public void test_removeAll() { - List a = defaultList(); - TestSupport.assertThat(a.removeAll(a) == true, - "mutating removeAll() did not return true"); - TestSupport.assertThat(a.removeAll(a) == false, - "idempotent removeAll did not return false"); - TestSupport.assertThat(a.removeAll(nullList) == false, - "idempotent removeAll did not return false"); - - List yanl = newInstance(null); + List<Object> a = defaultList(); + TestSupport.assertThat(a.removeAll(a) == true, "mutating removeAll() did not return true"); + TestSupport.assertThat(a.removeAll(a) == false, "idempotent removeAll did not return false"); + TestSupport.assertThat(a.removeAll(nullList) == false, + "idempotent removeAll did not return false"); + List<Object> yanl = newInstance(null); yanl.addAll(nullList); - TestSupport.assertThat(yanl.removeAll(nullList) == true, - "mutating removeAll() did not return true"); - TestSupport.assertThat(yanl.size() == 0, - "empty list had non-zero size"); + TestSupport.assertThat(yanl.removeAll(nullList) == true, + "mutating removeAll() did not return true"); + TestSupport.assertThat(yanl.size() == 0, "empty list had non-zero size"); TestSupport.assertThat(yanl.removeAll(newInstance(null)) == false, - "idempotent removeAll did not return false"); - + "idempotent removeAll did not return false"); } - - /** Tests addAll(int index, Collection c) */ + + /** Tests addAll(int index, Collection c) */ public void test_addAll_index() { - List a = defaultList(); - List b = newInstance(null); - TestSupport.assertThat(b.addAll(0,a) == true, - "mutating addAll(index, Collection) did not return true"); - TestSupport.assertEquals(a, b, - "addAll(index, Collection) instances failed equals test"); - TestSupport.assertThat(a.addAll(0, newInstance(null)) == false, - "idempotent addAll(index, Collection) did not return false"); - TestSupport.assertThat(b.addAll(0,b) == true, - "mutating addAll(index, Collection) did not return true"); - + List<Object> a = defaultList(); + List<Object> b = newInstance(null); + TestSupport.assertThat(b.addAll(0, a) == true, + "mutating addAll(index, Collection) did not return true"); + TestSupport.assertEquals(a, b, "addAll(index, Collection) instances failed equals test"); + TestSupport.assertThat(a.addAll(0, newInstance(null)) == false, + "idempotent addAll(index, Collection) did not return false"); + TestSupport.assertThat(b.addAll(0, b) == true, + "mutating addAll(index, Collection) did not return true"); // Since PyObjectList has some specific handling when it detects // addAll on a PySequenceList, make sure the general case works. b = newInstance(null); - b.addAll(new ArrayList(defaultList())); - TestSupport.assertEquals(defaultList(), b, - "addAll(index, <ArrayList>) failed equals test"); + b.addAll(new ArrayList<Object>(defaultList())); + TestSupport.assertEquals(defaultList(), b, "addAll(index, <ArrayList>) failed equals test"); } - - /** Tests hashCode() */ + /** Tests hashCode() */ public void test_hashCode() { - List a = defaultList(); - TestSupport.assertThat(a.hashCode() == defaultList().hashCode(), - "Instances with same internal state have different hashcode"); - TestSupport.assertThat(a.hashCode() != newInstance(null).hashCode(), - "Instances with different internal state have the same hashcode"); - + List<Object> a = defaultList(); + TestSupport.assertThat(a.hashCode() == defaultList().hashCode(), + "Instances with same internal state have different hashcode"); + TestSupport.assertThat(a.hashCode() != newInstance(null).hashCode(), + "Instances with different internal state have the same hashcode"); if (isReadOnly() == false) { - List b = newInstance(null); + List<Object> b = newInstance(null); b.addAll(a); b.remove(0); - TestSupport.assertThat(a.hashCode()!= b.hashCode(), - "Instances with different internal state have the same hashcode"); + TestSupport.assertThat(a.hashCode() != b.hashCode(), + "Instances with different internal state have the same hashcode"); } - } - - /** Tests clear() */ + + /** Tests clear() */ public void test_clear() { - List a = defaultList(); + List<Object> a = defaultList(); a.clear(); - TestSupport.assertThat(a.size() == 0, - "clear()-ed list did not have size of 0"); + TestSupport.assertThat(a.size() == 0, "clear()-ed list did not have size of 0"); } - - /** Tests subList(int fromIndex, int toIndex) */ + + /** Tests subList(int fromIndex, int toIndex) */ public void test_subList() { - List a = defaultList(); - TestSupport.assertThat((a.subList(0, a.size()) != a), - "subList() returned the same instance"); - TestSupport.assertEquals(a.subList(0, a.size()), a, - "Complete subList() did not equal original List"); - TestSupport.assertThat(a.subList(0,0).size() == 0, - "empty subList had non-zero size"); - + List<Object> a = defaultList(); + TestSupport.assertThat((a.subList(0, a.size()) != a), + "subList() returned the same instance"); + TestSupport.assertEquals(a.subList(0, a.size()), + a, + "Complete subList() did not equal original List"); + TestSupport.assertThat(a.subList(0, 0).size() == 0, "empty subList had non-zero size"); try { - a.subList(-1,1); - TestSupport.assertThat(false, "Expected IndexOutOfBoundsException"); + a.subList(-1, 1); + TestSupport.fail("Expected IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) {} - try { - a.subList(1,0); - TestSupport.assertThat(false, "Expected IllegalArgumentException"); + a.subList(1, 0); + TestSupport.fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) {} - try { - a.subList(0,a.size() + 1); - TestSupport.assertThat(false, "Expected IndexOutOfBoundsException"); + a.subList(0, a.size() + 1); + TestSupport.fail("Expected IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) {} - if (!isReadOnly()) { - a.subList(0, a.size()).clear(); - TestSupport.assertThat(a.size() == 0, - "clear()-ed sublist did not have zero size"); - List c = newInstance(null); + TestSupport.assertThat(a.size() == 0, "clear()-ed sublist did not have zero size"); + List<Object> c = newInstance(null); c.addAll(defaultList()); - List d = c.subList(1,3); - TestSupport.assertThat(d.size() == 2, - "Expected subList to have size of 2"); - TestSupport.assertThat(c.set(1,"canned").equals(new Integer(1)), - "subList.set() did not return Integer(1) from index 1" + - " of defaultList"); + List<Object> d = c.subList(1, 3); + TestSupport.assertThat(d.size() == 2, "Expected subList to have size of 2"); + TestSupport.assertThat(c.set(1, "canned").equals(1), + "subList.set() did not return Integer(1) from index 1" + + " of defaultList"); TestSupport.assertThat(d.get(0).equals("canned"), - "subList does not update with changes to parent"); - d.set(0,"spam"); - TestSupport.assertThat(c.get(1).equals("spam"), - "parent does not update with changes to subList child"); + "subList does not update with changes to parent"); + d.set(0, "spam"); + TestSupport.assertThat(c.get(1).equals("spam"), + "parent does not update with changes to subList child"); } else { - List b = a.subList(0, a.size()); + List<Object> b = a.subList(0, a.size()); verifyImutability(b); } - } - + /** Tests retainAll(Collection c) */ - public void test_retainAll() { - List a = defaultList(); + public void test_retainAll() { + List<Object> a = defaultList(); a.retainAll(defaultList()); - TestSupport.assertEquals(a, defaultList(), - "retainAll(<equal List>) does not equal original list"); + TestSupport.assertEquals(a, + defaultList(), + "retainAll(<equal List>) does not equal original list"); a = defaultList(); a.retainAll(newInstance(null)); - TestSupport.assertThat(a.size() == 0, - "retainAll(<empty List>))does not have size of zero"); - + TestSupport.assertThat(a.size() == 0, "retainAll(<empty List>))does not have size of zero"); a = defaultList(); a.remove(0); a.remove(0); - a.add(new Integer(4)); - a.add(new Integer(5)); - List b = newInstance(null); - b.add(new Integer(2)); - b.add(new Integer(3)); + a.add(4); + a.add(5); + List<Object> b = newInstance(null); + b.add(2); + b.add(3); a.retainAll(b); - TestSupport.assertEquals(a, b, - "retainAll() on overlap of indices [2,3] did not return that List"); + TestSupport.assertEquals(a, + b, + "retainAll() on overlap of indices [2,3] did not return that List"); } - + /** Tests containsAll(Collection c) */ public void test_containsAll() { TestSupport.assertThat(defaultList().containsAll(defaultList()), - "containsAll(<identical List> was false"); - TestSupport.assertThat(defaultList().containsAll(newInstance(null)), - "containsAll(<empty List>) was false"); + "containsAll(<identical List> was false"); + TestSupport.assertThat(defaultList().containsAll(newInstance(null)), + "containsAll(<empty List>) was false"); TestSupport.assertThat(newInstance(null).containsAll(defaultList()) == false, - "containsAll(<disjoint List>) returned true"); - TestSupport.assertThat(defaultList().containsAll(defaultList().subList(1,3)), - "containsAll(<subList>) was false"); + "containsAll(<disjoint List>) returned true"); + TestSupport.assertThat(defaultList().containsAll(defaultList().subList(1, 3)), + "containsAll(<subList>) was false"); } - + /** Tests iterator() */ public void test_iterator() { - - TestSupport.assertThat(newInstance(null).iterator().hasNext() == false, - "Iterator for empty list thinks it hasNext()"); + TestSupport.assertThat(newInstance(null).iterator().hasNext() == false, + "Iterator for empty list thinks it hasNext()"); try { newInstance(null).iterator().next(); - TestSupport.assertThat(false, "expected NoSuchElementException"); + TestSupport.fail("expected NoSuchElementException"); } catch (NoSuchElementException e) {} - - List a = defaultList(); + List<Object> a = defaultList(); int i = 0; - for (Iterator iter = a.iterator(); iter.hasNext(); ) { - TestSupport.assertThat(iter.next() == a.get(i++), - "Iterator next() failed identity test"); + for (Object element : a) { + TestSupport.assertThat(element == a.get(i++), "Iterator next() failed identity test"); } - TestSupport.assertThat(i == a.size(), - "Iterator did not iterator over entire list"); + TestSupport.assertThat(i == a.size(), "Iterator did not iterator over entire list"); } - + public void test_listIterator() { - - ListIterator li = newInstance(null).listIterator(); - TestSupport.assertThat(li.hasNext() == false, - "ListIterator.hasNext() is true for empty List"); - - TestSupport.assertThat(li.hasPrevious() == false, - "ListIterator.hasPrevious() is true for empty List"); - + ListIterator<Object> li = newInstance(null).listIterator(); + TestSupport.assertThat(!li.hasNext(), "ListIterator.hasNext() is true for empty List"); + TestSupport.assertThat(!li.hasPrevious(), + "ListIterator.hasPrevious() is true for empty List"); try { li.next(); - TestSupport.assertThat(false, "expected NoSuchElementException"); + TestSupport.fail("expected NoSuchElementException"); } catch (NoSuchElementException e) {} - try { li.previous(); - TestSupport.assertThat(false, "expected NoSuchElementException"); + TestSupport.fail("expected NoSuchElementException"); } catch (NoSuchElementException e) {} - int nextIndex = li.nextIndex(); - TestSupport.assertThat(nextIndex == 0, - "ListIterator.nextIndex() on empty List did not return 0"); - + TestSupport.assertThat(nextIndex == 0, + "ListIterator.nextIndex() on empty List did not return 0"); int prevIndex = li.previousIndex(); TestSupport.assertThat(prevIndex == -1, - "ListIterator.previousIndex() on empty List did not return -1"); - - List l = new ArrayList(); - l.add(new Integer(1)); + "ListIterator.previousIndex() on empty List did not return -1"); + List<Object> l = Generic.list(); + l.add(1); li = newInstance(l).listIterator(); - TestSupport.assertThat(li.hasPrevious() == false, - "ListIterator.hasPrevious() is true with nothing previous"); - - TestSupport.assertThat(li.hasNext() == true, - "ListIterator.hasNext() is false with next present"); - TestSupport.assertThat(li.next().equals(new Integer(1)), - "ListIterator.next() did not return expected Integer(1)"); - + TestSupport.assertThat(!li.hasPrevious(), + "ListIterator.hasPrevious() is true with nothing previous"); + TestSupport.assertThat(li.hasNext(), "ListIterator.hasNext() is false with next present"); + TestSupport.assertThat(li.next().equals(1), + "ListIterator.next() did not return expected Integer(1)"); if (!isReadOnly()) { - li.remove(); - TestSupport.assertThat(li.hasNext() == false, - "ListIterator.hasNext() is true for empty List"); - - TestSupport.assertThat(li.hasPrevious() == false, - "ListIterator.hasPrevious() is true for empty List"); - try { - li.set(new Integer(42)); - TestSupport.assertThat(false, "expected IllegalStateException"); - } catch (IllegalStateException e) {} - - try { - li.remove(); - TestSupport.assertThat(false, "expected IllegalStateException"); - } catch (IllegalStateException e) {} + li.remove(); + TestSupport.assertThat(!li.hasNext(), "ListIterator.hasNext() is true for empty List"); + TestSupport.assertThat(!li.hasPrevious(), + "ListIterator.hasPrevious() is true for empty List"); + try { + li.set(42); + TestSupport.fail("expected IllegalStateException"); + } catch (IllegalStateException e) {} + try { + li.remove(); + TestSupport.fail("expected IllegalStateException"); + } catch (IllegalStateException e) {} } - - l = new ArrayList(); - l.add(new Integer(0)); - l.add(new Integer(1)); - l.add(new Integer(2)); - + l = Generic.list(); + l.add(0); + l.add(1); + l.add(2); li = newInstance(l).listIterator(); - for (int i = 0, n = l.size(); i < n; i++) { - TestSupport.assertThat(li.next().equals(new Integer(i)), - "ListIterator.previous did not return expected value"); + TestSupport.assertThat(li.next().equals(i), + "ListIterator.previous did not return expected value"); } - while (!isReadOnly() && li.hasNext()) { li.next(); - li.set(new Integer(42)); - TestSupport.assertThat(li.previous().equals(new Integer(42)), - "ListIterator.previous() did not return the value that was set()"); + li.set(42); + TestSupport.assertThat(li.previous().equals(42), + "ListIterator.previous() did not return the value that was set()"); li.remove(); } - - if(isReadOnly()) { + if (isReadOnly()) { li = newInstance(null).listIterator(); } - li = defaultList().listIterator(2); - TestSupport.assertThat(li.next().equals(new Integer(2)), - "List.listIteraor(index) did not return expected value"); - TestSupport.assertThat(li.next().equals(new Integer(3)), - "List.listIteraor(index) did not return expected value"); - TestSupport.assertThat(li.hasNext() == false, - "listIterator.hasNext() at end of list returned true"); - + TestSupport.assertThat(li.next().equals(2), + "List.listIteraor(index) did not return expected value"); + ... [truncated message content] |