KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > trove > TLinkedListTest


1 ///////////////////////////////////////////////////////////////////////////////
2
// Copyright (c) 2001, Eric D. Friedman All Rights Reserved.
3
//
4
// This library is free software; you can redistribute it and/or
5
// modify it under the terms of the GNU Lesser General Public
6
// License as published by the Free Software Foundation; either
7
// version 2.1 of the License, or (at your option) any later version.
8
//
9
// This library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU Lesser General Public
15
// License along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
///////////////////////////////////////////////////////////////////////////////
18

19 package gnu.trove;
20
21 import junit.framework.*;
22 import java.io.*;
23 import java.util.*;
24
25 /**
26  *
27  * Created: Sat Nov 10 15:57:07 2001
28  *
29  * @author Eric D. Friedman
30  * @version $Id: TLinkedListTest.java,v 1.2 2006/11/15 21:11:21 robeden Exp $
31  */

32
33 public class TLinkedListTest extends TestCase {
34     protected TLinkedList<Data> list;
35     
36     public TLinkedListTest(String JavaDoc name) {
37         super(name);
38     }
39
40     public void setUp() throws Exception JavaDoc {
41         list = new TLinkedList<Data>();
42     }
43
44     public void tearDown() throws Exception JavaDoc {
45         list = null;
46     }
47
48     public void testAdd() throws Exception JavaDoc {
49         Data[] data = { new Data(1), new Data(2), new Data(3) };
50         for (int i = 0; i < data.length; i++) {
51             list.add(data[i]);
52         }
53         assertEquals(3,list.size());
54     }
55
56     public void testInsert() throws Exception JavaDoc {
57         Data[] data = { new Data(2), new Data(4), new Data(6) };
58         for (int i = 0; i < data.length; i++) {
59             list.add(data[i]);
60         }
61
62         list.insert( 0, new Data( 1 ) );
63         list.insert( 2, new Data( 3 ) );
64         list.insert( 4, new Data( 5 ) );
65         list.insert( list.size(), new Data( 7 ) );
66
67         assertEquals( 7, list.size() );
68         for( int i = 0; i < list.size(); i++ ) {
69             assertEquals(i + 1, list.get( i )._val );
70         }
71     }
72
73     public void testNextIterator() throws Exception JavaDoc {
74         Data[] data = new Data[100];
75         for (int i = 0; i < data.length; i++) {
76             data[i] = new Data(i);
77             list.add(data[i]);
78         }
79
80         int count = 0;
81         for (Iterator i = list.iterator(); i.hasNext();) {
82             assertEquals(data[count++], i.next());
83         }
84         assertEquals(data.length,count);
85
86         count = 4;
87         for (Iterator i = list.listIterator(4); i.hasNext();) {
88             assertEquals(data[count++], i.next());
89         }
90         assertEquals(data.length,count);
91     }
92
93     public void testPreviousIterator() throws Exception JavaDoc {
94         Data[] data = new Data[100];
95         for (int i = 0; i < data.length; i++) {
96             data[i] = new Data(i);
97             list.add(data[i]);
98         }
99
100         int count = 100;
101         for (ListIterator i = list.listIterator(list.size());
102              i.hasPrevious();) {
103             assertEquals(data[--count], i.previous());
104         }
105         assertEquals(0,count);
106
107         count = 5;
108         for (ListIterator i = list.listIterator(count); i.hasPrevious();) {
109             assertEquals(data[--count], i.previous());
110         }
111         assertEquals(0,count);
112     }
113
114     public void testIteratorSet() throws Exception JavaDoc {
115         Data[] data = new Data[100];
116         for (int i = 0; i < data.length; i++) {
117             data[i] = new Data(i);
118             list.add(data[i]);
119         }
120
121         ListIterator i;
122
123         i = list.listIterator(5);
124         i.next();
125         Data d = new Data(999);
126         i.set(d);
127         assertEquals(d, list.get(5));
128     }
129
130     public void testRemoveOnlyElementInList() throws Exception JavaDoc {
131         Data d = new Data(0);
132         list.add(d);
133         ListIterator i = list.listIterator();
134         assertTrue(i.hasNext());
135         assertEquals(d, i.next());
136         i.remove();
137         assertTrue(! i.hasNext());
138         assertTrue(! i.hasPrevious());
139         assertEquals(0, list.size());
140     }
141
142     public void testRemovePrevious() throws Exception JavaDoc {
143         Data[] d = { new Data(0), new Data(1), new Data(2) };
144         list.addAll(Arrays.asList(d));
145
146         ListIterator i = list.listIterator(list.size());
147         i.previous();
148         i.previous();
149         i.remove();
150         assertEquals(2, list.size());
151         assertTrue(i.hasPrevious());
152         assertEquals(d[0], i.previous());
153         assertTrue(! i.hasPrevious());
154         assertTrue(i.hasNext());
155         assertEquals(d[0], i.next());
156         assertTrue(i.hasNext());
157         assertEquals(d[2], i.next());
158         assertTrue(! i.hasNext());
159         assertTrue(i.hasPrevious());
160         assertEquals(2, list.size());
161     }
162
163     public void testRemoveLast() throws Exception JavaDoc {
164         Data[] d = { new Data(0), new Data(1), new Data(2) };
165         list.addAll(Arrays.asList(d));
166
167         ListIterator i = list.listIterator(list.size());
168         i.previous();
169         i.remove();
170         assertEquals(2, list.size());
171         assertTrue(i.hasPrevious());
172         assertTrue(! i.hasNext());
173     }
174
175     public void testRemoveFirst() throws Exception JavaDoc {
176         Data[] d = { new Data(0), new Data(1), new Data(2) };
177         list.addAll(Arrays.asList(d));
178
179         ListIterator i = list.listIterator(0);
180         i.next();
181         i.remove();
182         assertEquals(2, list.size());
183         assertTrue(! i.hasPrevious());
184         assertTrue(i.hasNext());
185     }
186     
187     public void testRemoveNext() throws Exception JavaDoc {
188         Data[] d = { new Data(0), new Data(1), new Data(2) };
189         list.addAll(Arrays.asList(d));
190
191         ListIterator i = list.listIterator();
192         assertTrue(i.hasNext());
193         i.next();
194         assertTrue(i.hasNext());
195         assertTrue(i.hasPrevious());
196         i.remove();
197         assertEquals(2, list.size());
198         assertTrue(! i.hasPrevious());
199         assertTrue(i.hasNext());
200         assertEquals(d[1], i.next());
201         assertTrue(i.hasNext());
202         assertEquals(d[2], i.next());
203         assertTrue(i.hasPrevious());
204         assertTrue(! i.hasNext());
205     }
206
207     public void testRemoveThrowsAfterAdd() throws Exception JavaDoc {
208         Data d = new Data(0);
209         list.add(d);
210         ListIterator i = list.listIterator();
211         boolean didThrow = false;
212         
213         try {
214             i.remove();
215         } catch (IllegalStateException JavaDoc e) {
216             didThrow = true;
217         } // end of try-catch
218
assertTrue(didThrow);
219     }
220
221     public void testRemoveThrowsWithoutPrevious() throws Exception JavaDoc {
222         Data d = new Data(0);
223         list.add(d);
224         ListIterator i = list.listIterator(list.size());
225         boolean didThrow = false;
226
227         assertTrue(i.hasPrevious());
228         try {
229             i.remove();
230         } catch (IllegalStateException JavaDoc e) {
231             didThrow = true;
232         } // end of try-catch
233
assertTrue(didThrow);
234     }
235
236     public void testRemoveThrowsWithoutNext() throws Exception JavaDoc {
237         Data d = new Data(0);
238         list.add(d);
239         ListIterator i = list.listIterator();
240         boolean didThrow = false;
241
242         assertTrue(i.hasNext());
243         try {
244             i.remove();
245         } catch (IllegalStateException JavaDoc e) {
246             didThrow = true;
247         } // end of try-catch
248
assertTrue(didThrow);
249     }
250
251     public void testIteratorAddFront() throws Exception JavaDoc {
252         Data[] d = { new Data(0), new Data(1), new Data(2) };
253         list.addAll(Arrays.asList(d));
254
255         ListIterator i = list.listIterator();
256         Data d1 = new Data(5);
257         assertTrue(! i.hasPrevious());
258         i.add(d1);
259         assertTrue(i.hasPrevious());
260         assertEquals(d1, i.previous());
261         assertEquals(d1, i.next());
262         assertEquals(d[0], i.next());
263         assertEquals(d1, list.get(0));
264     }
265
266     public void testIteratorAddBack() throws Exception JavaDoc {
267         Data[] d = { new Data(0), new Data(1), new Data(2) };
268         list.addAll(Arrays.asList(d));
269
270         ListIterator i = list.listIterator(list.size());
271         Data d1 = new Data(5);
272         assertEquals(3, list.size());
273         assertTrue(i.hasPrevious());
274         assertTrue(! i.hasNext());
275         i.add(d1);
276         assertTrue(i.hasPrevious());
277         assertTrue(! i.hasNext());
278         assertEquals(4, list.size());
279         
280         assertEquals(d1, i.previous());
281         assertEquals(d1, i.next());
282         assertEquals(d1, list.get(3));
283     }
284
285     public void testIteratorAddMiddle() throws Exception JavaDoc {
286         Data[] d = { new Data(0), new Data(1), new Data(2) };
287         list.addAll(Arrays.asList(d));
288
289         ListIterator i = list.listIterator(1);
290         Data d1 = new Data(5);
291         assertEquals(3, list.size());
292         assertTrue(i.hasPrevious());
293         assertTrue(i.hasNext());
294         i.add(d1);
295         assertTrue(i.hasPrevious());
296         assertTrue(i.hasNext());
297         assertEquals(4, list.size());
298         
299         assertEquals(d1, i.previous());
300         assertEquals(d1, i.next());
301         assertEquals(d1, list.get(1));
302     }
303
304     public void testIteratorSetSingleElementList() throws Exception JavaDoc {
305         Data d1 = new Data(5);
306         Data d2 = new Data(4);
307         list.add(d1);
308
309         ListIterator i = list.listIterator(0);
310         i.next();
311         i.set(d2);
312         assertEquals(1, list.size());
313         assertTrue(! i.hasNext());
314         assertTrue(i.hasPrevious());
315         assertEquals(d2, i.previous());
316         assertTrue(i.hasNext());
317         assertTrue(! i.hasPrevious());
318         assertEquals(d2, i.next());
319     }
320
321     public void testIteratorAddEmptyList() throws Exception JavaDoc {
322         ListIterator i = list.listIterator();
323         Data d1 = new Data(5);
324         assertTrue(! i.hasPrevious());
325         assertTrue(! i.hasNext());
326         i.add(d1);
327         assertTrue(i.hasPrevious());
328         assertTrue(! i.hasNext());
329         assertEquals(d1, i.previous());
330         assertEquals(d1, i.next());
331         assertEquals(d1, list.get(0));
332     }
333
334     public void testIteratorRemoveOnNext() throws Exception JavaDoc {
335         Data[] data = new Data[100];
336         for (int i = 0; i < data.length; i++) {
337             data[i] = new Data(i);
338             list.add(data[i]);
339         }
340
341         ListIterator i;
342
343         i = list.listIterator(5);
344         i.next();
345         i.remove();
346         Data d = new Data(6);
347         assertEquals(d, list.get(5));
348     }
349
350     public void testSerialize() throws Exception JavaDoc {
351         TLinkedList list1 = new TLinkedList();
352         Data[] data = new Data[100];
353         for (int i = 0; i < data.length; i++) {
354             data[i] = new Data(i);
355             list1.add(data[i]);
356         }
357         
358         ByteArrayOutputStream baos = new ByteArrayOutputStream();
359         ObjectOutputStream oos = new ObjectOutputStream(baos);
360         oos.writeObject(list1);
361
362         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
363         ObjectInputStream ois = new ObjectInputStream(bais);
364
365         TLinkedList list2 = (TLinkedList)ois.readObject();
366         assertEquals(list1, list2);
367     }
368
369     static class Data implements TLinkable {
370         protected int _val;
371         
372         public Data(int val) {
373             _val = val;
374         }
375         
376         protected TLinkable _next;
377         
378         /**
379          * Get the value of next.
380          * @return value of next.
381          */

382         public TLinkable getNext() {
383             return _next;
384         }
385         
386         /**
387          * Set the value of next.
388          * @param next value to assign to next.
389          */

390         public void setNext(TLinkable next) {
391             this._next = next;
392         }
393         protected TLinkable _previous;
394         
395         /**
396          * Get the value of previous.
397          * @return value of previous.
398          */

399         public TLinkable getPrevious() {
400             return _previous;
401         }
402         
403         /**
404          * Set the value of previous.
405          * @param previous value to assign to previous.
406          */

407         public void setPrevious(TLinkable previous) {
408             this._previous = previous;
409         }
410
411         public String JavaDoc toString() {
412             return "" + _val;
413         }
414
415         public boolean equals(Object JavaDoc o) {
416             Data that = (Data)o;
417             return this._val == that._val;
418         }
419     }
420     
421 } // TLinkedListTests
422
Popular Tags