1 | /* |
2 | * @(#) $Id: QueueTest.java 161424 2005-04-15 09:13:10Z 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 junit.framework.Assert; |
22 | import junit.framework.TestCase; |
23 | |
24 | /** |
25 | * Tests {@link Queue} |
26 | * |
27 | * @author The Apache Directory Project (dev@directory.apache.org) |
28 | * @author Trustin Lee (trustin@apache.org) |
29 | * @version $Rev: 161424 $, $Date: 2005-04-15 18:13:10 +0900 $ |
30 | */ |
31 | public class QueueTest extends TestCase |
32 | { |
33 | private int pushCount; |
34 | private int popCount; |
35 | |
36 | public void setUp() |
37 | { |
38 | pushCount = 0; |
39 | popCount = 0; |
40 | } |
41 | |
42 | public void testRotation() |
43 | { |
44 | Queue q = new Queue(); // DEFAULT_CAPACITY = 4 |
45 | testRotation0( q ); |
46 | } |
47 | |
48 | public void testExpandingRotation() |
49 | { |
50 | Queue q = new Queue(); // DEFAULT_CAPACITY = 4 |
51 | for( int i = 0; i < 10; i ++ ) |
52 | { |
53 | testRotation0( q ); |
54 | |
55 | // make expansion happen |
56 | int oldCapacity = q.capacity(); |
57 | for( int j = q.capacity(); j >= 0; j-- ) |
58 | { |
59 | q.push( new Integer( ++pushCount ) ); |
60 | } |
61 | |
62 | Assert.assertTrue( q.capacity() > oldCapacity ); |
63 | testRotation0( q ); |
64 | } |
65 | } |
66 | |
67 | private void testRotation0( Queue q ) |
68 | { |
69 | for( int i = 0; i < q.capacity() * 7 / 4; i ++ ) |
70 | { |
71 | q.push( new Integer( ++pushCount ) ); |
72 | Assert.assertEquals( ++popCount, ( ( Integer ) q.pop() ).intValue() ); |
73 | } |
74 | } |
75 | |
76 | public static void main( String[] args ) |
77 | { |
78 | junit.textui.TestRunner.run( QueueTest.class ); |
79 | } |
80 | } |