001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections.primitives;
018
019import java.io.Serializable;
020import java.util.ArrayList;
021import java.util.ConcurrentModificationException;
022import java.util.List;
023
024import org.apache.commons.collections.primitives.adapters.BaseTestList;
025import org.apache.commons.collections.primitives.adapters.IntListList;
026import org.apache.commons.collections.primitives.adapters.ListIntList;
027
028/**
029 * @version $Revision: 480451 $ $Date: 2006-11-29 02:45:08 -0500 (Wed, 29 Nov 2006) $
030 * @author Rodney Waldhoff
031 */
032public abstract class TestIntList extends BaseTestList {
033
034    // conventional
035    // ------------------------------------------------------------------------
036
037    public TestIntList(String testName) {
038        super(testName);
039    }
040
041    // collections testing framework
042    // ------------------------------------------------------------------------
043
044    // collections testing framework: int list
045    // ------------------------------------------------------------------------
046
047    protected abstract IntList makeEmptyIntList();
048
049    protected IntList makeFullIntList() {
050        IntList list = makeEmptyIntList();
051        int[] values = getFullIntegers();
052        for(int i=0;i<values.length;i++) {
053            list.add(values[i]);
054        }
055        return list;
056    }
057
058    protected int[] getFullIntegers() {
059        int[] result = new int[19];
060        for (int i = 0; i < result.length; i++) {
061            result[i] = i + 19;
062        }
063        return result;
064    }
065
066    protected int[] getOtherIntegers() {
067        int[] result = new int[16];
068        for (int i = 0; i < result.length; i++) {
069            result[i] = i + 43;
070        }
071        return result;
072    }
073    
074    // collections testing framework: inherited
075    // ------------------------------------------------------------------------
076
077    public List makeEmptyList() {
078        return new IntListList(makeEmptyIntList());
079    }
080        
081    public Object[] getFullElements() {
082        return wrapArray(getFullIntegers());
083    }
084
085    public Object[] getOtherElements() {
086        return wrapArray(getOtherIntegers());
087    }
088
089    // private utils
090    // ------------------------------------------------------------------------
091
092    private Integer[] wrapArray(int[] primitives) {
093        Integer[] result = new Integer[primitives.length];
094        for(int i=0;i<result.length;i++) {
095            result[i] = new Integer(primitives[i]);            
096        }
097        return result;
098    }
099
100    // tests
101    // ------------------------------------------------------------------------
102
103    public void testExceptionOnConcurrentModification() {
104        IntList list = makeFullIntList();
105        IntIterator iter = list.iterator();
106        iter.next();
107        list.add((int)3);
108        try {
109            iter.next();
110            fail("Expected ConcurrentModificationException");
111        } catch(ConcurrentModificationException e) {
112            // expected
113        }
114    }
115    
116    public void testAddAllIntListAtIndex() {
117        IntList source = makeFullIntList();
118        IntList dest = makeFullIntList();
119        dest.addAll(1,source);
120        
121        IntIterator iter = dest.iterator();
122        assertTrue(iter.hasNext());
123        assertEquals(source.get(0),iter.next());
124        for(int i=0;i<source.size();i++) {
125            assertTrue(iter.hasNext());
126            assertEquals(source.get(i),iter.next());
127        }
128        for(int i=1;i<source.size();i++) {
129            assertTrue(iter.hasNext());
130            assertEquals(source.get(i),iter.next());
131        }
132        assertFalse(iter.hasNext());
133    }
134
135    public void testToJustBigEnoughIntArray() {
136        IntList list = makeFullIntList();
137        int[] dest = new int[list.size()];
138        assertSame(dest,list.toArray(dest));
139        int i=0;
140        for(IntIterator iter = list.iterator(); iter.hasNext();i++) {
141            assertEquals(iter.next(),dest[i]);
142        }
143    }
144    
145    public void testToLargerThanNeededIntArray() {
146        IntList list = makeFullIntList();
147        int[] dest = new int[list.size()*2];
148        for(int i=0;i<dest.length;i++) {
149            dest[i] = Integer.MAX_VALUE;
150        }       
151        assertSame(dest,list.toArray(dest));
152        int i=0;
153        for(IntIterator iter = list.iterator(); iter.hasNext();i++) {
154            assertEquals(iter.next(),dest[i]);
155        }
156        for(;i<dest.length;i++) {
157            assertEquals(Integer.MAX_VALUE,dest[i]);
158        }
159    }
160    
161    public void testToSmallerThanNeededIntArray() {
162        IntList list = makeFullIntList();
163        int[] dest = new int[list.size()/2];
164        int[] dest2 = list.toArray(dest);
165        assertTrue(dest != dest2);
166        int i=0;
167        for(IntIterator iter = list.iterator(); iter.hasNext();i++) {
168            assertEquals(iter.next(),dest2[i]);
169        }
170    }
171    
172    public void testHashCodeSpecification() {
173        IntList list = makeFullIntList();
174        int hash = 1;
175        for(IntIterator iter = list.iterator(); iter.hasNext(); ) {
176            hash = 31*hash + iter.next();
177        }
178        assertEquals(hash,list.hashCode());
179    }
180
181    public void testEqualsWithTwoIntLists() {
182        IntList one = makeEmptyIntList();
183        assertEquals("Equals is reflexive on empty list",one,one);
184        IntList two = makeEmptyIntList();
185        assertEquals("Empty lists are equal",one,two);
186        assertEquals("Equals is symmetric on empty lists",two,one);
187        
188        one.add(1);
189        assertEquals("Equals is reflexive on non empty list",one,one);
190        assertTrue(!one.equals(two));
191        assertTrue(!two.equals(one));
192
193        two.add(1);
194        assertEquals("Non empty lists are equal",one,two);
195        assertEquals("Equals is symmetric on non empty list",one,two);
196        
197        one.add(1); one.add(2); one.add(3); one.add(5); one.add(8);
198        assertEquals("Equals is reflexive on larger non empty list",one,one);
199        assertTrue(!one.equals(two));
200        assertTrue(!two.equals(one));
201        
202        two.add(1); two.add(2); two.add(3); two.add(5); two.add(8);
203        assertEquals("Larger non empty lists are equal",one,two);
204        assertEquals("Equals is symmetric on larger non empty list",two,one);
205
206        one.add(9);
207        two.add(10);
208        assertTrue(!one.equals(two));
209        assertTrue(!two.equals(one));
210
211    }
212
213    public void testIntSubListEquals() {
214        IntList one = makeEmptyIntList();
215        assertEquals(one,one.subList(0,0));
216        assertEquals(one.subList(0,0),one);
217        
218        one.add(1);
219        assertEquals(one,one.subList(0,1));
220        assertEquals(one.subList(0,1),one);
221
222        one.add(1); one.add(2); one.add(3); one.add(5); one.add(8);
223        assertEquals(one.subList(0,4),one.subList(0,4));
224        assertEquals(one.subList(3,5),one.subList(3,5));
225    }
226    
227    public void testEqualsWithIntListAndList() {
228        IntList ilist = makeEmptyIntList();
229        List list = new ArrayList();
230        
231        assertTrue("Unwrapped, empty List should not be equal to empty IntList.",!ilist.equals(list));
232        assertTrue("Unwrapped, empty IntList should not be equal to empty List.",!list.equals(ilist));
233        
234        assertEquals(new ListIntList(list),ilist);
235        assertEquals(ilist,new ListIntList(list));
236        assertEquals(new IntListList(ilist),list);
237        assertEquals(list,new IntListList(ilist));
238        
239        ilist.add(1);
240        list.add(new Integer(1));
241
242        assertTrue("Unwrapped, non-empty List is not equal to non-empty IntList.",!ilist.equals(list));
243        assertTrue("Unwrapped, non-empty IntList is not equal to non-empty List.",!list.equals(ilist));
244        
245        assertEquals(new ListIntList(list),ilist);
246        assertEquals(ilist,new ListIntList(list));
247        assertEquals(new IntListList(ilist),list);
248        assertEquals(list,new IntListList(ilist));
249                
250        ilist.add(1); ilist.add(2); ilist.add(3); ilist.add(5); ilist.add(8);
251        list.add(new Integer(1)); list.add(new Integer(2)); list.add(new Integer(3)); list.add(new Integer(5)); list.add(new Integer(8));
252
253        assertTrue("Unwrapped, non-empty List is not equal to non-empty IntList.",!ilist.equals(list));
254        assertTrue("Unwrapped, non-empty IntList is not equal to non-empty List.",!list.equals(ilist));
255        
256        assertEquals(new ListIntList(list),ilist);
257        assertEquals(ilist,new ListIntList(list));
258        assertEquals(new IntListList(ilist),list);
259        assertEquals(list,new IntListList(ilist));
260        
261    }
262
263    public void testClearAndSize() {
264        IntList list = makeEmptyIntList();
265        assertEquals(0, list.size());
266        for(int i = 0; i < 100; i++) {
267            list.add(i);
268        }
269        assertEquals(100, list.size());
270        list.clear();
271        assertEquals(0, list.size());
272    }
273
274    public void testRemoveViaSubList() {
275        IntList list = makeEmptyIntList();
276        for(int i = 0; i < 100; i++) {
277            list.add(i);
278        }
279        IntList sub = list.subList(25,75);
280        assertEquals(50,sub.size());
281        for(int i = 0; i < 50; i++) {
282            assertEquals(100-i,list.size());
283            assertEquals(50-i,sub.size());
284            assertEquals(25+i,sub.removeElementAt(0));
285            assertEquals(50-i-1,sub.size());
286            assertEquals(100-i-1,list.size());
287        }
288        assertEquals(0,sub.size());
289        assertEquals(50,list.size());        
290    }
291    
292    public void testAddGet() {
293        IntList list = makeEmptyIntList();
294        for (int i = 0; i < 1000; i++) {
295            list.add(i);
296        }
297        for (int i = 0; i < 1000; i++) {
298            assertEquals(i, list.get(i));
299        }
300    }
301
302    public void testAddAndShift() {
303        IntList list = makeEmptyIntList();
304        list.add(0, 1);
305        assertEquals("Should have one entry", 1, list.size());
306        list.add(3);
307        list.add(4);
308        list.add(1, 2);
309        for(int i = 0; i < 4; i++) {
310            assertEquals("Should get entry back", i + 1, list.get(i));
311        }
312        list.add(0, 0);
313        for (int i = 0; i < 5; i++) {
314            assertEquals("Should get entry back", i, list.get(i));
315        }
316    }
317
318    public void testIsSerializable() throws Exception {
319        IntList list = makeFullIntList();
320        assertTrue(list instanceof Serializable);
321        byte[] ser = writeExternalFormToBytes((Serializable)list);
322        IntList deser = (IntList)(readExternalFormFromBytes(ser));
323        assertEquals(list,deser);
324        assertEquals(deser,list);
325    }
326
327    public void testIntListSerializeDeserializeThenCompare() throws Exception {
328        IntList list = makeFullIntList();
329        if(list instanceof Serializable) {
330            byte[] ser = writeExternalFormToBytes((Serializable)list);
331            IntList deser = (IntList)(readExternalFormFromBytes(ser));
332            assertEquals("obj != deserialize(serialize(obj))",list,deser);
333        }
334    }
335
336    public void testSubListsAreNotSerializable() throws Exception {
337        IntList list = makeFullIntList().subList(2,3);
338        assertTrue( ! (list instanceof Serializable) );
339    }
340
341    public void testSubListOutOfBounds() throws Exception {
342        try {
343            makeEmptyIntList().subList(2,3);
344            fail("Expected IndexOutOfBoundsException");
345        } catch(IndexOutOfBoundsException e) {
346            // expected
347        }
348
349        try {
350            makeFullIntList().subList(-1,3);
351            fail("Expected IndexOutOfBoundsException");
352        } catch(IndexOutOfBoundsException e) {
353            // expected
354        }
355
356
357        try {
358            makeFullIntList().subList(5,2);
359            fail("Expected IllegalArgumentException");
360        } catch(IllegalArgumentException e) {
361            // expected
362        }
363
364        try {
365            makeFullIntList().subList(2,makeFullIntList().size()+2);
366            fail("Expected IndexOutOfBoundsException");
367        } catch(IndexOutOfBoundsException e) {
368            // expected
369        }
370    }
371
372    public void testListIteratorOutOfBounds() throws Exception {
373        try {
374            makeEmptyIntList().listIterator(2);
375            fail("Expected IndexOutOfBoundsException");
376        } catch(IndexOutOfBoundsException e) {
377            // expected
378        }
379
380        try {
381            makeFullIntList().listIterator(-1);
382            fail("Expected IndexOutOfBoundsException");
383        } catch(IndexOutOfBoundsException e) {
384            // expected
385        }
386
387        try {
388            makeFullIntList().listIterator(makeFullIntList().size()+2);
389            fail("Expected IndexOutOfBoundsException");
390        } catch(IndexOutOfBoundsException e) {
391            // expected
392        }
393    }
394
395    public void testListIteratorSetWithoutNext() throws Exception {
396        IntListIterator iter = makeFullIntList().listIterator();
397        try {
398            iter.set(3);
399            fail("Expected IllegalStateException");
400        } catch(IllegalStateException e) {
401            // expected
402        }
403    }
404
405    public void testListIteratorSetAfterRemove() throws Exception {
406        IntListIterator iter = makeFullIntList().listIterator();
407        iter.next();
408        iter.remove();
409        try {            
410            iter.set(3);
411            fail("Expected IllegalStateException");
412        } catch(IllegalStateException e) {
413            // expected
414        }
415    }
416
417}