View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections.primitives;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.ConcurrentModificationException;
22  import java.util.List;
23  
24  import org.apache.commons.collections.primitives.adapters.BaseTestList;
25  import org.apache.commons.collections.primitives.adapters.DoubleListList;
26  import org.apache.commons.collections.primitives.adapters.ListDoubleList;
27  
28  /**
29   * @version $Revision: 480451 $ $Date: 2006-11-29 02:45:08 -0500 (Wed, 29 Nov 2006) $
30   * @author Rodney Waldhoff
31   */
32  public abstract class TestDoubleList extends BaseTestList {
33  
34      // conventional
35      // ------------------------------------------------------------------------
36  
37      public TestDoubleList(String testName) {
38          super(testName);
39      }
40  
41      // collections testing framework
42      // ------------------------------------------------------------------------
43  
44      // collections testing framework: double list
45      // ------------------------------------------------------------------------
46  
47      protected abstract DoubleList makeEmptyDoubleList();
48  
49      protected DoubleList makeFullDoubleList() {
50          DoubleList list = makeEmptyDoubleList();
51          double[] values = getFullDoubles();
52          for(int i=0;i<values.length;i++) {
53              list.add(values[i]);
54          }
55          return list;
56      }
57  
58      protected double[] getFullDoubles() {
59          double[] result = new double[19];
60          for(int i = 0; i < result.length; i++) {
61              result[i] = (double)(i);
62          }
63          return result;
64      }
65  
66      protected double[] getOtherDoubles() {
67          double[] result = new double[16];
68          for(int i = 0; i < result.length; i++) {
69              result[i] = (double)(i + 43);
70          }
71          return result;
72      }
73      
74      // collections testing framework: inherited
75      // ------------------------------------------------------------------------
76  
77      public List makeEmptyList() {
78          return new DoubleListList(makeEmptyDoubleList());
79      }
80          
81      public Object[] getFullElements() {
82          return wrapArray(getFullDoubles());
83      }
84  
85      public Object[] getOtherElements() {
86          return wrapArray(getOtherDoubles());
87      }
88  
89      // private utils
90      // ------------------------------------------------------------------------
91  
92      private Double[] wrapArray(double[] primitives) {
93          Double[] result = new Double[primitives.length];
94          for(int i=0;i<result.length;i++) {
95              result[i] = new Double(primitives[i]);            
96          }
97          return result;
98      }
99  
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 }