1 | /* |
2 | * @(#) $Id: QueueTest.java 332218 2005-11-10 03:52:42Z trustin $ |
3 | * |
4 | * Copyright 2004 The Apache Software Foundation |
5 | * |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * |
18 | */ |
19 | package org.apache.mina.util; |
20 | |
21 | import java.util.Iterator; |
22 | |
23 | import junit.framework.Assert; |
24 | import junit.framework.TestCase; |
25 | |
26 | /** |
27 | * Tests {@link Queue} |
28 | * |
29 | * @author The Apache Directory Project (dev@directory.apache.org) |
30 | * @version $Rev: 332218 $, $Date: 2005-11-10 12:52:42 +0900 $ |
31 | */ |
32 | public class QueueTest extends TestCase |
33 | { |
34 | private int pushCount; |
35 | private int popCount; |
36 | |
37 | public void setUp() |
38 | { |
39 | pushCount = 0; |
40 | popCount = 0; |
41 | } |
42 | |
43 | public void testRotation() |
44 | { |
45 | Queue q = new Queue(); // DEFAULT_CAPACITY = 4 |
46 | testRotation0( q ); |
47 | } |
48 | |
49 | public void testExpandingRotation() |
50 | { |
51 | Queue q = new Queue(); // DEFAULT_CAPACITY = 4 |
52 | for( int i = 0; i < 10; i ++ ) |
53 | { |
54 | testRotation0( q ); |
55 | |
56 | // make expansion happen |
57 | int oldCapacity = q.capacity(); |
58 | for( int j = q.capacity(); j >= 0; j-- ) |
59 | { |
60 | q.push( new Integer( ++pushCount ) ); |
61 | } |
62 | |
63 | Assert.assertTrue( q.capacity() > oldCapacity ); |
64 | testRotation0( q ); |
65 | } |
66 | } |
67 | |
68 | private void testRotation0( Queue q ) |
69 | { |
70 | for( int i = 0; i < q.capacity() * 7 / 4; i ++ ) |
71 | { |
72 | q.push( new Integer( ++pushCount ) ); |
73 | Assert.assertEquals( ++popCount, ( ( Integer ) q.pop() ).intValue() ); |
74 | } |
75 | } |
76 | |
77 | public void testRandomAddOnQueue() |
78 | { |
79 | Queue q = new Queue(); |
80 | // Create a queue with 5 elements and capacity 8; |
81 | for( int i = 0; i < 5; i ++ ) |
82 | { |
83 | q.push( new Integer( i ) ); |
84 | } |
85 | |
86 | q.add( 0, new Integer( 100 ) ); |
87 | q.add( 3, new Integer( 200 ) ); |
88 | q.add( 7, new Integer( 300 ) ); |
89 | |
90 | Iterator i = q.iterator(); |
91 | Assert.assertEquals( 8, q.size() ); |
92 | Assert.assertEquals( new Integer( 100 ), i.next() ); |
93 | Assert.assertEquals( new Integer( 0 ), i.next() ); |
94 | Assert.assertEquals( new Integer( 1 ), i.next() ); |
95 | Assert.assertEquals( new Integer( 200 ), i.next() ); |
96 | Assert.assertEquals( new Integer( 2 ), i.next() ); |
97 | Assert.assertEquals( new Integer( 3 ), i.next() ); |
98 | Assert.assertEquals( new Integer( 4 ), i.next() ); |
99 | Assert.assertEquals( new Integer( 300 ), i.next() ); |
100 | |
101 | try |
102 | { |
103 | i.next(); |
104 | Assert.fail(); |
105 | } |
106 | catch( Exception e ) |
107 | { |
108 | // OK |
109 | } |
110 | } |
111 | |
112 | public void testRandomAddOnRotatedQueue() |
113 | { |
114 | Queue q = getRotatedQueue(); |
115 | |
116 | q.add( 0, new Integer( 100 ) ); // addFirst |
117 | q.add( 2, new Integer( 200 ) ); |
118 | q.add( 4, new Integer( 300 ) ); |
119 | q.add( 10, new Integer( 400 ) ); |
120 | q.add( 12, new Integer( 500 ) ); // addLast |
121 | |
122 | Iterator i = q.iterator(); |
123 | Assert.assertEquals( 13, q.size() ); |
124 | Assert.assertEquals( new Integer( 100 ), i.next() ); |
125 | Assert.assertEquals( new Integer( 0 ), i.next() ); |
126 | Assert.assertEquals( new Integer( 200 ), i.next() ); |
127 | Assert.assertEquals( new Integer( 1 ), i.next() ); |
128 | Assert.assertEquals( new Integer( 300 ), i.next() ); |
129 | Assert.assertEquals( new Integer( 2 ), i.next() ); |
130 | Assert.assertEquals( new Integer( 3 ), i.next() ); |
131 | Assert.assertEquals( new Integer( 4 ), i.next() ); |
132 | Assert.assertEquals( new Integer( 5 ), i.next() ); |
133 | Assert.assertEquals( new Integer( 6 ), i.next() ); |
134 | Assert.assertEquals( new Integer( 400 ), i.next() ); |
135 | Assert.assertEquals( new Integer( 7 ), i.next() ); |
136 | Assert.assertEquals( new Integer( 500 ), i.next() ); |
137 | |
138 | try |
139 | { |
140 | i.next(); |
141 | Assert.fail(); |
142 | } |
143 | catch( Exception e ) |
144 | { |
145 | // OK |
146 | } |
147 | } |
148 | |
149 | public void testRandomRemoveOnQueue() |
150 | { |
151 | Queue q = new Queue(); |
152 | |
153 | // Create a queue with 5 elements and capacity 8; |
154 | for( int i = 0; i < 5; i ++ ) |
155 | { |
156 | q.push( new Integer( i ) ); |
157 | } |
158 | |
159 | q.remove( 0 ); |
160 | q.remove( 2 ); |
161 | q.remove( 2 ); |
162 | |
163 | Iterator i = q.iterator(); |
164 | Assert.assertEquals( 2, q.size() ); |
165 | Assert.assertEquals( new Integer( 1 ), i.next() ); |
166 | Assert.assertEquals( new Integer( 2 ), i.next() ); |
167 | |
168 | try |
169 | { |
170 | i.next(); |
171 | Assert.fail(); |
172 | } |
173 | catch( Exception e ) |
174 | { |
175 | // OK |
176 | } |
177 | } |
178 | |
179 | public void testRandomRemoveOnRotatedQueue() |
180 | { |
181 | Queue q = getRotatedQueue(); |
182 | |
183 | q.remove( 0 ); // removeFirst |
184 | q.remove( 2 ); // removeLast in the first half |
185 | q.remove( 2 ); // removeFirst in the first half |
186 | q.remove( 4 ); // removeLast |
187 | |
188 | Iterator i = q.iterator(); |
189 | Assert.assertEquals( 4, q.size() ); |
190 | Assert.assertEquals( new Integer( 1 ), i.next() ); |
191 | Assert.assertEquals( new Integer( 2 ), i.next() ); |
192 | Assert.assertEquals( new Integer( 5 ), i.next() ); |
193 | Assert.assertEquals( new Integer( 6 ), i.next() ); |
194 | |
195 | try |
196 | { |
197 | i.next(); |
198 | Assert.fail(); |
199 | } |
200 | catch( Exception e ) |
201 | { |
202 | // OK |
203 | } |
204 | } |
205 | |
206 | private Queue getRotatedQueue() |
207 | { |
208 | Queue q = new Queue(); |
209 | |
210 | // Ensure capacity: 16 |
211 | for( int i = 0; i < 16; i ++ ) |
212 | { |
213 | q.push( new Object() ); |
214 | } |
215 | q.clear(); |
216 | |
217 | // Rotate it |
218 | for( int i = 0; i < 12; i ++ ) |
219 | { |
220 | q.push( new Object() ); |
221 | q.pop(); |
222 | } |
223 | |
224 | // Now push items |
225 | for( int i = 0; i < 8; i ++ ) |
226 | { |
227 | q.push( new Integer( i ) ); |
228 | } |
229 | |
230 | return q; |
231 | } |
232 | |
233 | public static void main( String[] args ) |
234 | { |
235 | junit.textui.TestRunner.run( QueueTest.class ); |
236 | } |
237 | } |