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