KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > primitives > TestLongList


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 JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.ConcurrentModificationException JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.apache.commons.collections.primitives.adapters.BaseTestList;
25 import org.apache.commons.collections.primitives.adapters.ListLongList;
26 import org.apache.commons.collections.primitives.adapters.LongListList;
27
28 /**
29  * @version $Revision: 480451 $ $Date: 2006-11-28 23:45:08 -0800 (Tue, 28 Nov 2006) $
30  * @author Rodney Waldhoff
31  */

32 public abstract class TestLongList extends BaseTestList {
33
34     // conventional
35
// ------------------------------------------------------------------------
36

37     public TestLongList(String JavaDoc testName) {
38         super(testName);
39     }
40
41     // collections testing framework
42
// ------------------------------------------------------------------------
43

44     // collections testing framework: long list
45
// ------------------------------------------------------------------------
46

47     protected abstract LongList makeEmptyLongList();
48
49     protected LongList makeFullLongList() {
50         LongList list = makeEmptyLongList();
51         long[] values = getFullLongs();
52         for(int i=0;i<values.length;i++) {
53             list.add(values[i]);
54         }
55         return list;
56     }
57
58     protected long[] getFullLongs() {
59         long[] result = new long[19];
60         for(int i = 0; i < result.length; i++) {
61             result[i] = (long)i + ((long)Integer.MAX_VALUE - (long)10);
62         }
63         return result;
64     }
65
66     protected long[] getOtherLongs() {
67         long[] result = new long[16];
68         for (int i = 0; i < result.length; i++) {
69             result[i] = (long)i + (long)43;
70         }
71         return result;
72     }
73     
74     // collections testing framework: inherited
75
// ------------------------------------------------------------------------
76

77     public List JavaDoc makeEmptyList() {
78         return new LongListList(makeEmptyLongList());
79     }
80         
81     public Object JavaDoc[] getFullElements() {
82         return wrapArray(getFullLongs());
83     }
84
85     public Object JavaDoc[] getOtherElements() {
86         return wrapArray(getOtherLongs());
87     }
88
89     // private utils
90
// ------------------------------------------------------------------------
91

92     private Long JavaDoc[] wrapArray(long[] primitives) {
93         Long JavaDoc[] result = new Long JavaDoc[primitives.length];
94         for(int i=0;i<result.length;i++) {
95             result[i] = new Long JavaDoc(primitives[i]);
96         }
97         return result;
98     }
99
100     // tests
101
// ------------------------------------------------------------------------
102

103     public void testExceptionOnConcurrentModification() {
104         LongList list = makeFullLongList();
105         LongIterator iter = list.iterator();
106         iter.next();
107         list.add((long)3);
108         try {
109             iter.next();
110             fail("Expected ConcurrentModificationException");
111         } catch(ConcurrentModificationException JavaDoc e) {
112             // expected
113
}
114     }
115     
116     public void testAddAllLongListAtIndex() {
117         LongList source = makeFullLongList();
118         LongList dest = makeFullLongList();
119         dest.addAll(1,source);
120         
121         LongIterator 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 testToJustBigEnoughLongArray() {
136         LongList list = makeFullLongList();
137         long[] dest = new long[list.size()];
138         assertSame(dest,list.toArray(dest));
139         int i=0;
140         for(LongIterator iter = list.iterator(); iter.hasNext();i++) {
141             assertEquals(iter.next(),dest[i]);
142         }
143     }
144     
145     public void testToLargerThanNeededLongArray() {
146         LongList list = makeFullLongList();
147         long[] dest = new long[list.size()*2];
148         for(int i=0;i<dest.length;i++) {
149             dest[i] = Long.MAX_VALUE;
150         }
151         assertSame(dest,list.toArray(dest));
152         int i=0;
153         for(LongIterator iter = list.iterator(); iter.hasNext();i++) {
154             assertEquals(iter.next(),dest[i]);
155         }
156         for(;i<dest.length;i++) {
157             assertEquals(Long.MAX_VALUE,dest[i]);
158         }
159     }
160     
161     public void testToSmallerThanNeededLongArray() {
162         LongList list = makeFullLongList();
163         long[] dest = new long[list.size()/2];
164         long[] dest2 = list.toArray(dest);
165         assertTrue(dest != dest2);
166         int i=0;
167         for(LongIterator iter = list.iterator(); iter.hasNext();i++) {
168             assertEquals(iter.next(),dest2[i]);
169         }
170     }
171     
172     public void testHashCodeSpecification() {
173         LongList list = makeFullLongList();
174         int hash = 1;
175         for(LongIterator iter = list.iterator(); iter.hasNext(); ) {
176             long val = iter.next();
177             hash = 31*hash + ((int)(val ^ (val >>> 32)));
178         }
179         assertEquals(hash,list.hashCode());
180     }
181
182     public void testEqualsWithTwoLongLists() {
183         LongList one = makeEmptyLongList();
184         assertEquals("Equals is reflexive on empty list",one,one);
185         LongList two = makeEmptyLongList();
186         assertEquals("Empty lists are equal",one,two);
187         assertEquals("Equals is symmetric on empty lists",two,one);
188         
189         one.add((long)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((long)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((long)1); one.add((long)2); one.add((long)3); one.add((long)5); one.add((long)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((long)1); two.add((long)2); two.add((long)3); two.add((long)5); two.add((long)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((long)9);
208         two.add((long)10);
209         assertTrue(!one.equals(two));
210         assertTrue(!two.equals(one));
211
212     }
213
214     public void testLongSubListEquals() {
215         LongList one = makeEmptyLongList();
216         assertEquals(one,one.subList(0,0));
217         assertEquals(one.subList(0,0),one);
218         
219         one.add((long)1);
220         assertEquals(one,one.subList(0,1));
221         assertEquals(one.subList(0,1),one);
222
223         one.add((long)1); one.add((long)2); one.add((long)3); one.add((long)5); one.add((long)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 testEqualsWithLongListAndList() {
229         LongList ilist = makeEmptyLongList();
230         List JavaDoc list = new ArrayList JavaDoc();
231         
232         assertTrue("Unwrapped, empty List should not be equal to empty LongList.",!ilist.equals(list));
233         assertTrue("Unwrapped, empty LongList should not be equal to empty List.",!list.equals(ilist));
234         
235         assertEquals(new ListLongList(list),ilist);
236         assertEquals(ilist,new ListLongList(list));
237         assertEquals(new LongListList(ilist),list);
238         assertEquals(list,new LongListList(ilist));
239         
240         ilist.add((long)1);
241         list.add(new Long JavaDoc(1));
242
243         assertTrue("Unwrapped, non-empty List is not equal to non-empty LongList.",!ilist.equals(list));
244         assertTrue("Unwrapped, non-empty LongList is not equal to non-empty List.",!list.equals(ilist));
245         
246         assertEquals(new ListLongList(list),ilist);
247         assertEquals(ilist,new ListLongList(list));
248         assertEquals(new LongListList(ilist),list);
249         assertEquals(list,new LongListList(ilist));
250                 
251         ilist.add(1); ilist.add(2); ilist.add(3); ilist.add(5); ilist.add(8);
252         list.add(new Long JavaDoc(1)); list.add(new Long JavaDoc(2)); list.add(new Long JavaDoc(3)); list.add(new Long JavaDoc(5)); list.add(new Long JavaDoc(8));
253
254         assertTrue("Unwrapped, non-empty List is not equal to non-empty LongList.",!ilist.equals(list));
255         assertTrue("Unwrapped, non-empty LongList is not equal to non-empty List.",!list.equals(ilist));
256         
257         assertEquals(new ListLongList(list),ilist);
258         assertEquals(ilist,new ListLongList(list));
259         assertEquals(new LongListList(ilist),list);
260         assertEquals(list,new LongListList(ilist));
261         
262     }
263
264     public void testClearAndSize() {
265         LongList list = makeEmptyLongList();
266         assertEquals(0, list.size());
267         for(int i = 0; i < 100; i++) {
268             list.add((long)i);
269         }
270         assertEquals(100, list.size());
271         list.clear();
272         assertEquals(0, list.size());
273     }
274
275     public void testRemoveViaSubList() {
276         LongList list = makeEmptyLongList();
277         for(int i = 0; i < 100; i++) {
278             list.add((long)i);
279         }
280         LongList 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(25+i,sub.removeElementAt(0));
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         LongList list = makeEmptyLongList();
295         for (int i = 0; i < 1000; i++) {
296             list.add((long)i);
297         }
298         for (int i = 0; i < 1000; i++) {
299             assertEquals((long)i, list.get(i));
300         }
301     }
302
303     public void testAddAndShift() {
304         LongList list = makeEmptyLongList();
305         list.add(0, (long)1);
306         assertEquals("Should have one entry", 1, list.size());
307         list.add((long)3);
308         list.add((long)4);
309         list.add(1, (long)2);
310         for(int i = 0; i < 4; i++) {
311             assertEquals("Should get entry back", (long)(i + 1), list.get(i));
312         }
313         list.add(0, (long)0);
314         for (int i = 0; i < 5; i++) {
315             assertEquals("Should get entry back", (long)i, list.get(i));
316         }
317     }
318
319     public void testIsSerializable() throws Exception JavaDoc {
320         LongList list = makeFullLongList();
321         assertTrue(list instanceof Serializable JavaDoc);
322         byte[] ser = writeExternalFormToBytes((Serializable JavaDoc)list);
323         LongList deser = (LongList)(readExternalFormFromBytes(ser));
324         assertEquals(list,deser);
325         assertEquals(deser,list);
326     }
327
328     public void testLongListSerializeDeserializeThenCompare() throws Exception JavaDoc {
329         LongList list = makeFullLongList();
330         if(list instanceof Serializable JavaDoc) {
331             byte[] ser = writeExternalFormToBytes((Serializable JavaDoc)list);
332             LongList deser = (LongList)(readExternalFormFromBytes(ser));
333             assertEquals("obj != deserialize(serialize(obj))",list,deser);
334         }
335     }
336
337     public void testSubListsAreNotSerializable() throws Exception JavaDoc {
338         LongList list = makeFullLongList().subList(2,3);
339         assertTrue( ! (list instanceof Serializable JavaDoc) );
340     }
341
342     public void testSubListOutOfBounds() throws Exception JavaDoc {
343         try {
344             makeEmptyLongList().subList(2,3);
345             fail("Expected IndexOutOfBoundsException");
346         } catch(IndexOutOfBoundsException JavaDoc e) {
347             // expected
348
}
349
350         try {
351             makeFullLongList().subList(-1,3);
352             fail("Expected IndexOutOfBoundsException");
353         } catch(IndexOutOfBoundsException JavaDoc e) {
354             // expected
355
}
356
357
358         try {
359             makeFullLongList().subList(5,2);
360             fail("Expected IllegalArgumentException");
361         } catch(IllegalArgumentException JavaDoc e) {
362             // expected
363
}
364
365         try {
366             makeFullLongList().subList(2,makeFullLongList().size()+2);
367             fail("Expected IndexOutOfBoundsException");
368         } catch(IndexOutOfBoundsException JavaDoc e) {
369             // expected
370
}
371     }
372
373     public void testListIteratorOutOfBounds() throws Exception JavaDoc {
374         try {
375             makeEmptyLongList().listIterator(2);
376             fail("Expected IndexOutOfBoundsException");
377         } catch(IndexOutOfBoundsException JavaDoc e) {
378             // expected
379
}
380
381         try {
382             makeFullLongList().listIterator(-1);
383             fail("Expected IndexOutOfBoundsException");
384         } catch(IndexOutOfBoundsException JavaDoc e) {
385             // expected
386
}
387
388         try {
389             makeFullLongList().listIterator(makeFullLongList().size()+2);
390             fail("Expected IndexOutOfBoundsException");
391         } catch(IndexOutOfBoundsException JavaDoc e) {
392             // expected
393
}
394     }
395
396     public void testListIteratorSetWithoutNext() throws Exception JavaDoc {
397         LongListIterator iter = makeFullLongList().listIterator();
398         try {
399             iter.set(3);
400             fail("Expected IllegalStateException");
401         } catch(IllegalStateException JavaDoc e) {
402             // expected
403
}
404     }
405
406     public void testListIteratorSetAfterRemove() throws Exception JavaDoc {
407         LongListIterator iter = makeFullLongList().listIterator();
408         iter.next();
409         iter.remove();
410         try {
411             iter.set(3);
412             fail("Expected IllegalStateException");
413         } catch(IllegalStateException JavaDoc e) {
414             // expected
415
}
416     }
417
418 }
419
Popular Tags