KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > util > misc > CircularListTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.management.util.misc;
24
25
26 import com.sun.appserv.management.util.misc.CircularList;
27
28
29 public class CircularListTest extends junit.framework.TestCase
30 {
31         public
32     CircularListTest()
33     {
34     }
35
36         public void
37     testCreate()
38     {
39         new CircularList<Object JavaDoc>( Object JavaDoc.class, 10 );
40     }
41     
42
43         public void
44     testCreateIllegal()
45     {
46         try
47         {
48             new CircularList<Object JavaDoc>( Object JavaDoc.class, 0 );
49             assert( false );
50         }
51         catch( IllegalArgumentException JavaDoc e )
52         {
53         }
54     }
55     
56         public void
57     testSizeAndCapacity()
58     {
59         final CircularList<Object JavaDoc> list = new CircularList<Object JavaDoc>( Object JavaDoc.class, 10 );
60         
61         assertEquals( 0, list.size() );
62         assertEquals( 10, list.capacity() );
63     }
64     
65         public void
66     testClear()
67     {
68         final CircularList<Object JavaDoc> list = new CircularList<Object JavaDoc>( Object JavaDoc.class, 10 );
69         
70         assertEquals( 0, list.size() );
71         
72         list.add( "hello" );
73         assertEquals( 1, list.size() );
74         list.clear();
75         assertEquals( 0, list.size() );
76         
77         assertEquals( 10, list.capacity() );
78     }
79         public void
80     testAddUntilFull()
81     {
82         final int capacity = 10;
83         final CircularList<Integer JavaDoc> list = new CircularList<Integer JavaDoc>( Integer JavaDoc.class, capacity );
84         assertEquals( capacity, list.capacity() );
85         
86         for( int i = 0; i < capacity; ++i )
87         {
88             list.add( new Integer JavaDoc( i ) );
89             assert( list.size() == i + 1 );
90             assert( ((Integer JavaDoc)list.get( i )).intValue() == i );
91         }
92         assert( list.capacity() == capacity );
93     }
94     
95         public void
96     testAddPastFull()
97     {
98         final int capacity = 10;
99         final int count = capacity * 10 + 1;
100         final CircularList<Integer JavaDoc> list = new CircularList<Integer JavaDoc>( Integer JavaDoc.class, capacity );
101
102         for( int i = 0; i < count; ++i )
103         {
104             list.add( new Integer JavaDoc( i ) );
105             
106             Integer JavaDoc value = (Integer JavaDoc)list.get( list.size() - 1 );
107             assertEquals( i, value.intValue() );
108             
109             if ( i >= capacity )
110             {
111                 value = (Integer JavaDoc)list.get( 0 );
112                 assertEquals( 1 + (i - capacity), value.intValue() );
113             }
114         }
115         assert( list.capacity() == capacity );
116     }
117
118         public void
119     testRemoveFirstLast()
120     {
121         final int capacity = 3;
122         final CircularList<String JavaDoc> list = new CircularList<String JavaDoc>( String JavaDoc.class, capacity );
123         
124         list.add( "hello" );
125         list.add( "xxx" );
126         list.add( "there" );
127         
128         assertEquals( "hello", list.removeFirst() );
129         assertEquals( "there", list.removeLast() );
130         assertEquals( 1, list.size() );
131     }
132     
133
134         public void
135     testRemoveFirst()
136     {
137         final int capacity = 100;
138         final CircularList<Integer JavaDoc> list = new CircularList<Integer JavaDoc>( Integer JavaDoc.class, capacity );
139
140         for( int i = 0; i < capacity; ++i )
141         {
142             list.add( new Integer JavaDoc( i ) );
143         }
144         
145         for( int i = 0; i < capacity; ++i )
146         {
147             final Integer JavaDoc value = (Integer JavaDoc)list.removeFirst();
148             assertEquals( i, value.intValue() );
149         }
150     }
151     
152         public void
153     testSet()
154     {
155         final int capacity = 100;
156         final CircularList<Integer JavaDoc> list = new CircularList<Integer JavaDoc>( Integer JavaDoc.class, capacity );
157
158         for( int i = 0; i < capacity; ++i )
159         {
160             list.add( new Integer JavaDoc( i ) );
161         }
162         
163         for( int i = 0; i < capacity; ++i )
164         {
165             final Integer JavaDoc value = (Integer JavaDoc)list.get( i );
166             list.set( i, value );
167             assertEquals( value, list.get( i ) );
168         }
169     }
170     
171         public void
172     testEquals()
173     {
174         final int capacity = 2;
175         final CircularList<String JavaDoc> list1 = new CircularList<String JavaDoc>( String JavaDoc.class, capacity );
176         final CircularList<String JavaDoc> list2 = new CircularList<String JavaDoc>( String JavaDoc.class, capacity );
177         assert( list1.equals( list2 ) );
178         
179         list1.add( "hello" );
180         list1.add( "there" );
181         list2.add( "hello" );
182         list2.add( "there" );
183         
184         assert( list1.equals( list2 ) );
185     }
186 }
187
188
189
190
191
192
193
Popular Tags