~ubuntu-branches/ubuntu/utopic/jetty/utopic-proposed

« back to all changes in this revision

Viewing changes to modules/util5/src/test/java/org/mortbay/util/ArrayQueueTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2009-08-09 08:48:10 UTC
  • Revision ID: james.westby@ubuntu.com-20090809084810-k522b97ind2robyd
ImportĀ upstreamĀ versionĀ 6.1.19

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//========================================================================
 
2
//Copyright 2004-2008 Mort Bay Consulting Pty. Ltd.
 
3
//------------------------------------------------------------------------
 
4
//Licensed under the Apache License, Version 2.0 (the "License");
 
5
//you may not use this file except in compliance with the License.
 
6
//You may obtain a copy of the License at 
 
7
//http://www.apache.org/licenses/LICENSE-2.0
 
8
//Unless required by applicable law or agreed to in writing, software
 
9
//distributed under the License is distributed on an "AS IS" BASIS,
 
10
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
11
//See the License for the specific language governing permissions and
 
12
//limitations under the License.
 
13
//========================================================================
 
14
 
 
15
package org.mortbay.util;
 
16
 
 
17
import junit.framework.TestCase;
 
18
 
 
19
public class ArrayQueueTest extends TestCase
 
20
{
 
21
    
 
22
    public void testWrap() throws Exception
 
23
    {
 
24
        ArrayQueue<String> queue = new ArrayQueue<String>(3,3);
 
25
        
 
26
        assertEquals(0,queue.size());
 
27
 
 
28
        for (int i=0;i<10;i++)
 
29
        {
 
30
            queue.offer("one");
 
31
            assertEquals(1,queue.size());
 
32
 
 
33
            queue.offer("two");
 
34
            assertEquals(2,queue.size());
 
35
 
 
36
            queue.offer("three");
 
37
            assertEquals(3,queue.size());
 
38
 
 
39
            assertEquals("one",queue.get(0));
 
40
            assertEquals("two",queue.get(1));
 
41
            assertEquals("three",queue.get(2));
 
42
 
 
43
            assertEquals("[one, two, three]",queue.toString());
 
44
 
 
45
            assertEquals("two",queue.remove(1));
 
46
            assertEquals(2,queue.size());
 
47
            
 
48
            assertEquals("one",queue.remove());
 
49
            assertEquals(1,queue.size());
 
50
 
 
51
            assertEquals("three",queue.poll());
 
52
            assertEquals(0,queue.size());
 
53
            
 
54
            assertEquals(null,queue.poll());
 
55
 
 
56
            queue.offer("xxx");
 
57
            queue.offer("xxx");
 
58
            assertEquals(2,queue.size());
 
59
            assertEquals("xxx",queue.poll());
 
60
            assertEquals("xxx",queue.poll());
 
61
            assertEquals(0,queue.size());
 
62
 
 
63
        }
 
64
 
 
65
    }
 
66
 
 
67
    public void testRemove() throws Exception
 
68
    {
 
69
        ArrayQueue<String> queue = new ArrayQueue<String>(3,3);
 
70
       
 
71
        queue.add("0");
 
72
        queue.add("x");
 
73
        
 
74
        for (int i=1;i<100;i++)
 
75
        {
 
76
            queue.add(""+i);
 
77
            queue.add("x");
 
78
            queue.remove(queue.size()-3);
 
79
            queue.set(queue.size()-3,queue.get(queue.size()-3)+"!");
 
80
        }
 
81
        
 
82
        for (int i=0;i<99;i++)
 
83
            assertEquals(i+"!",queue.get(i));
 
84
    }
 
85
 
 
86
    public void testGrow() throws Exception
 
87
    {
 
88
        ArrayQueue<String> queue = new ArrayQueue<String>(3,5);
 
89
        assertEquals(3,queue.getCapacity());
 
90
 
 
91
        queue.add("0");
 
92
        queue.add("a");
 
93
        queue.add("b");
 
94
        assertEquals(3,queue.getCapacity());
 
95
        queue.add("c");
 
96
        assertEquals(8,queue.getCapacity());
 
97
        
 
98
        for (int i=0;i<4;i++)
 
99
            queue.add(""+('d'+i));
 
100
        assertEquals(8,queue.getCapacity());
 
101
        for (int i=0;i<4;i++)
 
102
            queue.poll();
 
103
        assertEquals(8,queue.getCapacity());
 
104
        for (int i=0;i<4;i++)
 
105
            queue.add(""+('d'+i));
 
106
        assertEquals(8,queue.getCapacity());
 
107
        for (int i=0;i<4;i++)
 
108
            queue.poll();
 
109
        assertEquals(8,queue.getCapacity());
 
110
        for (int i=0;i<4;i++)
 
111
            queue.add(""+('d'+i));
 
112
        assertEquals(8,queue.getCapacity());
 
113
 
 
114
        queue.add("z");
 
115
        assertEquals(13,queue.getCapacity());
 
116
        
 
117
        queue.clear();
 
118
        assertEquals(13,queue.getCapacity());
 
119
        for (int i=0;i<12;i++)
 
120
            queue.add(""+('a'+i));
 
121
        assertEquals(13,queue.getCapacity());
 
122
        queue.clear();
 
123
        assertEquals(13,queue.getCapacity());
 
124
        for (int i=0;i<12;i++)
 
125
            queue.add(""+('a'+i));
 
126
        assertEquals(13,queue.getCapacity());
 
127
        
 
128
            
 
129
    }
 
130
    
 
131
    public void testFullEmpty() throws Exception
 
132
    {
 
133
        ArrayQueue<String> queue = new ArrayQueue<String>(2);
 
134
        assertTrue(queue.offer("one"));
 
135
        assertTrue(queue.offer("two"));
 
136
        assertFalse(queue.offer("three"));
 
137
        
 
138
        try
 
139
        {
 
140
            queue.add("four");
 
141
            assertTrue(false);
 
142
        }
 
143
        catch(Exception e)
 
144
        {
 
145
            
 
146
        }
 
147
 
 
148
        assertEquals("one",queue.peek());
 
149
        assertEquals("one",queue.remove());
 
150
        assertEquals("two",queue.remove());
 
151
        try
 
152
        {
 
153
            assertEquals("three",queue.remove());
 
154
            assertTrue(false);
 
155
        }
 
156
        catch(Exception e)
 
157
        {
 
158
            
 
159
        }
 
160
 
 
161
        assertEquals(null,queue.poll());
 
162
    }
 
163
        
 
164
}