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
//========================================================================
15
package org.mortbay.util;
17
import junit.framework.TestCase;
19
public class ArrayQueueTest extends TestCase
22
public void testWrap() throws Exception
24
ArrayQueue<String> queue = new ArrayQueue<String>(3,3);
26
assertEquals(0,queue.size());
28
for (int i=0;i<10;i++)
31
assertEquals(1,queue.size());
34
assertEquals(2,queue.size());
37
assertEquals(3,queue.size());
39
assertEquals("one",queue.get(0));
40
assertEquals("two",queue.get(1));
41
assertEquals("three",queue.get(2));
43
assertEquals("[one, two, three]",queue.toString());
45
assertEquals("two",queue.remove(1));
46
assertEquals(2,queue.size());
48
assertEquals("one",queue.remove());
49
assertEquals(1,queue.size());
51
assertEquals("three",queue.poll());
52
assertEquals(0,queue.size());
54
assertEquals(null,queue.poll());
58
assertEquals(2,queue.size());
59
assertEquals("xxx",queue.poll());
60
assertEquals("xxx",queue.poll());
61
assertEquals(0,queue.size());
67
public void testRemove() throws Exception
69
ArrayQueue<String> queue = new ArrayQueue<String>(3,3);
74
for (int i=1;i<100;i++)
78
queue.remove(queue.size()-3);
79
queue.set(queue.size()-3,queue.get(queue.size()-3)+"!");
82
for (int i=0;i<99;i++)
83
assertEquals(i+"!",queue.get(i));
86
public void testGrow() throws Exception
88
ArrayQueue<String> queue = new ArrayQueue<String>(3,5);
89
assertEquals(3,queue.getCapacity());
94
assertEquals(3,queue.getCapacity());
96
assertEquals(8,queue.getCapacity());
99
queue.add(""+('d'+i));
100
assertEquals(8,queue.getCapacity());
101
for (int i=0;i<4;i++)
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++)
109
assertEquals(8,queue.getCapacity());
110
for (int i=0;i<4;i++)
111
queue.add(""+('d'+i));
112
assertEquals(8,queue.getCapacity());
115
assertEquals(13,queue.getCapacity());
118
assertEquals(13,queue.getCapacity());
119
for (int i=0;i<12;i++)
120
queue.add(""+('a'+i));
121
assertEquals(13,queue.getCapacity());
123
assertEquals(13,queue.getCapacity());
124
for (int i=0;i<12;i++)
125
queue.add(""+('a'+i));
126
assertEquals(13,queue.getCapacity());
131
public void testFullEmpty() throws Exception
133
ArrayQueue<String> queue = new ArrayQueue<String>(2);
134
assertTrue(queue.offer("one"));
135
assertTrue(queue.offer("two"));
136
assertFalse(queue.offer("three"));
148
assertEquals("one",queue.peek());
149
assertEquals("one",queue.remove());
150
assertEquals("two",queue.remove());
153
assertEquals("three",queue.remove());
161
assertEquals(null,queue.poll());