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.DoubleListList;
026import org.apache.commons.collections.primitives.adapters.ListDoubleList;
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 TestDoubleList extends BaseTestList {
033
034    // conventional
035    // ------------------------------------------------------------------------
036
037    public TestDoubleList(String testName) {
038        super(testName);
039    }
040
041    // collections testing framework
042    // ------------------------------------------------------------------------
043
044    // collections testing framework: double list
045    // ------------------------------------------------------------------------
046
047    protected abstract DoubleList makeEmptyDoubleList();
048
049    protected DoubleList makeFullDoubleList() {
050        DoubleList list = makeEmptyDoubleList();
051        double[] values = getFullDoubles();
052        for(int i=0;i<values.length;i++) {
053            list.add(values[i]);
054        }
055        return list;
056    }
057
058    protected double[] getFullDoubles() {
059        double[] result = new double[19];
060        for(int i = 0; i < result.length; i++) {
061            result[i] = (double)(i);
062        }
063        return result;
064    }
065
066    protected double[] getOtherDoubles() {
067        double[] result = new double[16];
068        for(int i = 0; i < result.length; i++) {
069            result[i] = (double)(i + 43);
070        }
071        return result;
072    }
073    
074    // collections testing framework: inherited
075    // ------------------------------------------------------------------------
076
077    public List makeEmptyList() {
078        return new DoubleListList(makeEmptyDoubleList());
079    }
080        
081    public Object[] getFullElements() {
082        return wrapArray(getFullDoubles());
083    }
084
085    public Object[] getOtherElements() {
086        return wrapArray(getOtherDoubles());
087    }
088
089    // private utils
090    // ------------------------------------------------------------------------
091
092    private Double[] wrapArray(double[] primitives) {
093        Double[] result = new Double[primitives.length];
094        for(int i=0;i<result.length;i++) {
095            result[i] = new Double(primitives[i]);            
096        }
097        return result;
098    }
099
100    // tests
101    // ------------------------------------------------------------------------
102
103    public void testExceptionOnConcurrentModification() {
104        DoubleList list = makeFullDoubleList();
105        DoubleIterator iter = list.iterator();
106        iter.next();
107        list.add((double)3);
108        try {
109            iter.next();
110            fail("Expected ConcurrentModificationException");
111        } catch(ConcurrentModificationException e) {
112            // expected
113        }
114    }
115    
116    public void testAddAllDoubleListAtIndex() {
117        DoubleList source = makeFullDoubleList();
118        DoubleList dest = makeFullDoubleList();
119        dest.addAll(1,source);
120        
121        DoubleIterator iter = dest.iterator();
122        assertTrue(iter.hasNext());
123        assertEquals(source.get(0),iter.next(),0d);
124        for(int i=0;i<source.size();i++) {
125            assertTrue(iter.hasNext());
126            assertEquals(source.get(i),iter.next(),0d);
127        }
128        for(int i=1;i<source.size();i++) {
129            assertTrue(iter.hasNext());
130            assertEquals(source.get(i),iter.next(),0d);
131        }
132        assertFalse(iter.hasNext());
133    }
134
135    public void testToJustBigEnoughDoubleArray() {
136        DoubleList list = makeFullDoubleList();
137        double[] dest = new double[list.size()];
138        assertSame(dest,list.toArray(dest));
139        int i=0;
140        for(DoubleIterator iter = list.iterator(); iter.hasNext();i++) {
141            assertEquals(iter.next(),dest[i], 0f);
142        }
143    }
144    
145    public void testToLargerThanNeededDoubleArray() {
146        DoubleList list = makeFullDoubleList();
147        double[] dest = new double[list.size()*2];
148        for(int i=0;i<dest.length;i++) {
149            dest[i] = Double.MAX_VALUE;
150        }       
151        assertSame(dest,list.toArray(dest));
152        int i=0;
153        for(DoubleIterator iter = list.iterator(); iter.hasNext();i++) {
154            assertEquals(iter.next(),dest[i], 0f);
155        }
156        for(;i<dest.length;i++) {
157            assertEquals(Double.MAX_VALUE,dest[i], 0f);
158        }
159    }
160    
161    public void testToSmallerThanNeededDoubleArray() {
162        DoubleList list = makeFullDoubleList();
163        double[] dest = new double[list.size()/2];
164        double[] dest2 = list.toArray(dest);
165        assertTrue(dest != dest2);
166        int i=0;
167        for(DoubleIterator iter = list.iterator(); iter.hasNext();i++) {
168            assertEquals(iter.next(),dest2[i], 0f);
169        }
170    }
171    
172    public void testHashCodeSpecification() {
173        DoubleList list = makeFullDoubleList();
174                int hash = 1;
175                for(DoubleIterator iter = list.iterator(); iter.hasNext(); ) {
176            long bits = Double.doubleToLongBits(iter.next());
177            hash = 31*hash + ((int)(bits ^ (bits >>> 32)));
178                }
179        assertEquals(hash,list.hashCode());
180    }
181
182    public void testEqualsWithTwoDoubleLists() {
183        DoubleList one = makeEmptyDoubleList();
184        assertEquals("Equals is reflexive on empty list",one,one);
185        DoubleList two = makeEmptyDoubleList();
186        assertEquals("Empty lists are equal",one,two);
187        assertEquals("Equals is symmetric on empty lists",two,one);
188        
189        one.add((double)1);
190        assertEquals("Equals is reflexive on non empty list",one,one);
191        assertTrue(!one.equals(two));
192        assertTrue(!two.equals(one));
193
194        two.add((double)1);
195        assertEquals("Non empty lists are equal",one,two);
196        assertEquals("Equals is symmetric on non empty list",one,two);
197        
198        one.add((double)1); one.add((double)2); one.add((double)3); one.add((double)5); one.add((double)8);
199        assertEquals("Equals is reflexive on larger non empty list",one,one);
200        assertTrue(!one.equals(two));
201        assertTrue(!two.equals(one));
202        
203        two.add((double)1); two.add((double)2); two.add((double)3); two.add((double)5); two.add((double)8);
204        assertEquals("Larger non empty lists are equal",one,two);
205        assertEquals("Equals is symmetric on larger non empty list",two,one);
206
207        one.add((double)9);
208        two.add((double)10);
209        assertTrue(!one.equals(two));
210        assertTrue(!two.equals(one));
211
212    }
213
214    public void testDoubleSubListEquals() {
215        DoubleList one = makeEmptyDoubleList();
216        assertEquals(one,one.subList(0,0));
217        assertEquals(one.subList(0,0),one);
218        
219        one.add((double)1);
220        assertEquals(one,one.subList(0,1));
221        assertEquals(one.subList(0,1),one);
222
223        one.add((double)1); one.add((double)2); one.add((double)3); one.add((double)5); one.add((double)8);
224        assertEquals(one.subList(0,4),one.subList(0,4));
225        assertEquals(one.subList(3,5),one.subList(3,5));
226    }
227    
228    public void testEqualsWithDoubleListAndList() {
229        DoubleList ilist = makeEmptyDoubleList();
230        List list = new ArrayList();
231        
232        assertTrue("Unwrapped, empty List should not be equal to empty DoubleList.",!ilist.equals(list));
233        assertTrue("Unwrapped, empty DoubleList should not be equal to empty List.",!list.equals(ilist));
234        
235        assertEquals(new ListDoubleList(list),ilist);
236        assertEquals(ilist,new ListDoubleList(list));
237        assertEquals(new DoubleListList(ilist),list);
238        assertEquals(list,new DoubleListList(ilist));
239        
240        ilist.add((double)1);
241        list.add(new Double((double)1));
242
243        assertTrue("Unwrapped, non-empty List is not equal to non-empty DoubleList.",!ilist.equals(list));
244        assertTrue("Unwrapped, non-empty DoubleList is not equal to non-empty List.",!list.equals(ilist));
245        
246        assertEquals(new ListDoubleList(list),ilist);
247        assertEquals(ilist,new ListDoubleList(list));
248        assertEquals(new DoubleListList(ilist),list);
249        assertEquals(list,new DoubleListList(ilist));
250                
251        ilist.add((double)1); ilist.add((double)2); ilist.add((double)3); ilist.add((double)5); ilist.add((double)8);
252        list.add(new Double((double)1)); list.add(new Double((double)2)); list.add(new Double((double)3)); list.add(new Double((double)5)); list.add(new Double((double)8));
253
254        assertTrue("Unwrapped, non-empty List is not equal to non-empty DoubleList.",!ilist.equals(list));
255        assertTrue("Unwrapped, non-empty DoubleList is not equal to non-empty List.",!list.equals(ilist));
256        
257        assertEquals(new ListDoubleList(list),ilist);
258        assertEquals(ilist,new ListDoubleList(list));
259        assertEquals(new DoubleListList(ilist),list);
260        assertEquals(list,new DoubleListList(ilist));
261        
262    }
263
264    public void testClearAndSize() {
265        DoubleList list = makeEmptyDoubleList();
266        assertEquals(0, list.size());
267        for(int i = 0; i < 100; i++) {
268            list.add((double)i);
269        }
270        assertEquals(100, list.size());
271        list.clear();
272        assertEquals(0, list.size());
273    }
274
275    public void testRemoveViaSubList() {
276        DoubleList list = makeEmptyDoubleList();
277        for(int i = 0; i < 100; i++) {
278            list.add((double)i);
279        }
280        DoubleList sub = list.subList(25,75);
281        assertEquals(50,sub.size());
282        for(int i = 0; i < 50; i++) {
283            assertEquals(100-i,list.size());
284            assertEquals(50-i,sub.size());
285            assertEquals((double)(25+i),sub.removeElementAt(0), 0f);
286            assertEquals(50-i-1,sub.size());
287            assertEquals(100-i-1,list.size());
288        }
289        assertEquals(0,sub.size());
290        assertEquals(50,list.size());        
291    }
292    
293    public void testAddGet() {
294        DoubleList list = makeEmptyDoubleList();
295        for (int i = 0; i < 255; i++) {
296            list.add((double)i);
297        }
298        for (int i = 0; i < 255; i++) {
299            assertEquals((double)i, list.get(i), 0f);
300        }
301    }
302
303    public void testAddAndShift() {
304        DoubleList list = makeEmptyDoubleList();
305        list.add(0, (double)1);
306        assertEquals("Should have one entry", 1, list.size());
307        list.add((double)3);
308        list.add((double)4);
309        list.add(1, (double)2);
310        for(int i = 0; i < 4; i++) {
311            assertEquals("Should get entry back", (double)(i + 1), list.get(i), 0f);
312        }
313        list.add(0, (double)0);
314        for (int i = 0; i < 5; i++) {
315            assertEquals("Should get entry back", (double)i, list.get(i), 0f);
316        }
317    }
318
319    public void testIsSerializable() throws Exception {
320        DoubleList list = makeFullDoubleList();
321        assertTrue(list instanceof Serializable);
322        byte[] ser = writeExternalFormToBytes((Serializable)list);
323        DoubleList deser = (DoubleList)(readExternalFormFromBytes(ser));
324        assertEquals(list,deser);
325        assertEquals(deser,list);
326    }
327
328    public void testDoubleListSerializeDeserializeThenCompare() throws Exception {
329        DoubleList list = makeFullDoubleList();
330        if(list instanceof Serializable) {
331            byte[] ser = writeExternalFormToBytes((Serializable)list);
332            DoubleList deser = (DoubleList)(readExternalFormFromBytes(ser));
333            assertEquals("obj != deserialize(serialize(obj))",list,deser);
334        }
335    }
336
337    public void testSubListsAreNotSerializable() throws Exception {
338        DoubleList list = makeFullDoubleList().subList(2,3);
339        assertTrue( ! (list instanceof Serializable) );
340    }
341
342    public void testSubListOutOfBounds() throws Exception {
343        try {
344            makeEmptyDoubleList().subList(2,3);
345            fail("Expected IndexOutOfBoundsException");
346        } catch(IndexOutOfBoundsException e) {
347            // expected
348        }
349
350        try {
351            makeFullDoubleList().subList(-1,3);
352            fail("Expected IndexOutOfBoundsException");
353        } catch(IndexOutOfBoundsException e) {
354            // expected
355        }
356
357
358        try {
359            makeFullDoubleList().subList(5,2);
360            fail("Expected IllegalArgumentException");
361        } catch(IllegalArgumentException e) {
362            // expected
363        }
364
365        try {
366            makeFullDoubleList().subList(2,makeFullDoubleList().size()+2);
367            fail("Expected IndexOutOfBoundsException");
368        } catch(IndexOutOfBoundsException e) {
369            // expected
370        }
371    }
372
373    public void testListIteratorOutOfBounds() throws Exception {
374        try {
375            makeEmptyDoubleList().listIterator(2);
376            fail("Expected IndexOutOfBoundsException");
377        } catch(IndexOutOfBoundsException e) {
378            // expected
379        }
380
381        try {
382            makeFullDoubleList().listIterator(-1);
383            fail("Expected IndexOutOfBoundsException");
384        } catch(IndexOutOfBoundsException e) {
385            // expected
386        }
387
388        try {
389            makeFullDoubleList().listIterator(makeFullDoubleList().size()+2);
390            fail("Expected IndexOutOfBoundsException");
391        } catch(IndexOutOfBoundsException e) {
392            // expected
393        }
394    }
395
396    public void testListIteratorSetWithoutNext() throws Exception {
397        DoubleListIterator iter = makeFullDoubleList().listIterator();
398        try {
399            iter.set((double)3);
400            fail("Expected IllegalStateException");
401        } catch(IllegalStateException e) {
402            // expected
403        }
404    }
405
406    public void testListIteratorSetAfterRemove() throws Exception {
407        DoubleListIterator iter = makeFullDoubleList().listIterator();
408        iter.next();
409        iter.remove();
410        try {            
411            iter.set((double)3);
412            fail("Expected IllegalStateException");
413        } catch(IllegalStateException e) {
414            // expected
415        }
416    }
417
418}