001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Iterator;
021:
022: import junit.framework.Test;
023:
024: import org.apache.commons.collections.collection.AbstractTestCollection;
025:
026: /**
027: * Test cases for UnboundedFifoBuffer.
028: *
029: * @version $Revision: 219316 $ $Date: 2005-07-16 12:17:02 +0100 (Sat, 16 Jul 2005) $
030: *
031: * @author Unknown
032: */
033: public class TestUnboundedFifoBuffer extends AbstractTestCollection {
034:
035: public TestUnboundedFifoBuffer(String n) {
036: super (n);
037: }
038:
039: public static Test suite() {
040: return BulkTest.makeSuite(TestUnboundedFifoBuffer.class);
041: }
042:
043: //-----------------------------------------------------------------------
044: /**
045: * Verifies that the ArrayList has the same elements in the same
046: * sequence as the UnboundedFifoBuffer.
047: */
048: public void verify() {
049: super .verify();
050: Iterator iterator1 = collection.iterator();
051: Iterator iterator2 = confirmed.iterator();
052: while (iterator2.hasNext()) {
053: assertTrue(iterator1.hasNext());
054: Object o1 = iterator1.next();
055: Object o2 = iterator2.next();
056: assertEquals(o1, o2);
057: }
058: }
059:
060: //-----------------------------------------------------------------------
061: /**
062: * Overridden because UnboundedFifoBuffer doesn't allow null elements.
063: * @return false
064: */
065: public boolean isNullSupported() {
066: return false;
067: }
068:
069: /**
070: * Overridden because UnboundedFifoBuffer isn't fail fast.
071: * @return false
072: */
073: public boolean isFailFastSupported() {
074: return false;
075: }
076:
077: //-----------------------------------------------------------------------
078: /**
079: * Returns an empty ArrayList.
080: *
081: * @return an empty ArrayList
082: */
083: public Collection makeConfirmedCollection() {
084: return new ArrayList();
085: }
086:
087: /**
088: * Returns a full ArrayList.
089: *
090: * @return a full ArrayList
091: */
092: public Collection makeConfirmedFullCollection() {
093: Collection c = makeConfirmedCollection();
094: c.addAll(java.util.Arrays.asList(getFullElements()));
095: return c;
096: }
097:
098: /**
099: * Returns an empty UnboundedFifoBuffer with a small capacity.
100: *
101: * @return an empty UnboundedFifoBuffer
102: */
103: public Collection makeCollection() {
104: return new UnboundedFifoBuffer(5);
105: }
106:
107: //-----------------------------------------------------------------------
108: /**
109: * Tests that UnboundedFifoBuffer removes elements in the right order.
110: */
111: public void testUnboundedFifoBufferRemove() {
112: resetFull();
113: int size = confirmed.size();
114: for (int i = 0; i < size; i++) {
115: Object o1 = ((UnboundedFifoBuffer) collection).remove();
116: Object o2 = ((ArrayList) confirmed).remove(0);
117: assertEquals("Removed objects should be equal", o1, o2);
118: verify();
119: }
120: }
121:
122: /**
123: * Tests that the constructor correctly throws an exception.
124: */
125: public void testConstructorException1() {
126: try {
127: new UnboundedFifoBuffer(0);
128: } catch (IllegalArgumentException ex) {
129: return;
130: }
131: fail();
132: }
133:
134: /**
135: * Tests that the constructor correctly throws an exception.
136: */
137: public void testConstructorException2() {
138: try {
139: new UnboundedFifoBuffer(-20);
140: } catch (IllegalArgumentException ex) {
141: return;
142: }
143: fail();
144: }
145:
146: //-----------------------------------------------------------------------
147: public void testInternalStateAdd() {
148: UnboundedFifoBuffer test = new UnboundedFifoBuffer(2);
149: assertEquals(3, test.m_buffer.length);
150: assertEquals(0, test.m_head);
151: assertEquals(0, test.m_tail);
152: test.add("A");
153: assertEquals(3, test.m_buffer.length);
154: assertEquals(0, test.m_head);
155: assertEquals(1, test.m_tail);
156: test.add("B");
157: assertEquals(3, test.m_buffer.length);
158: assertEquals(0, test.m_head);
159: assertEquals(2, test.m_tail);
160: test.add("C"); // forces m_buffer increase
161: assertEquals(5, test.m_buffer.length);
162: assertEquals(0, test.m_head);
163: assertEquals(3, test.m_tail);
164: test.add("D");
165: assertEquals(5, test.m_buffer.length);
166: assertEquals(0, test.m_head);
167: assertEquals(4, test.m_tail);
168: }
169:
170: public void testInternalStateAddWithWrap() {
171: UnboundedFifoBuffer test = new UnboundedFifoBuffer(3);
172: assertEquals(4, test.m_buffer.length);
173: assertEquals(0, test.m_head);
174: assertEquals(0, test.m_tail);
175: test.add("A");
176: assertEquals(4, test.m_buffer.length);
177: assertEquals(0, test.m_head);
178: assertEquals(1, test.m_tail);
179: test.add("B");
180: assertEquals(4, test.m_buffer.length);
181: assertEquals(0, test.m_head);
182: assertEquals(2, test.m_tail);
183: test.add("C");
184: assertEquals(4, test.m_buffer.length);
185: assertEquals(0, test.m_head);
186: assertEquals(3, test.m_tail);
187: test.remove("A");
188: assertEquals(4, test.m_buffer.length);
189: assertEquals(1, test.m_head);
190: assertEquals(3, test.m_tail);
191: test.remove("B");
192: assertEquals(4, test.m_buffer.length);
193: assertEquals(2, test.m_head);
194: assertEquals(3, test.m_tail);
195: test.add("D");
196: assertEquals(4, test.m_buffer.length);
197: assertEquals(2, test.m_head);
198: assertEquals(0, test.m_tail);
199: test.add("E");
200: assertEquals(4, test.m_buffer.length);
201: assertEquals(2, test.m_head);
202: assertEquals(1, test.m_tail);
203: }
204:
205: public void testInternalStateRemove1() {
206: UnboundedFifoBuffer test = new UnboundedFifoBuffer(4);
207: test.add("A");
208: test.add("B");
209: test.add("C");
210: assertEquals(5, test.m_buffer.length);
211: assertEquals(0, test.m_head);
212: assertEquals(3, test.m_tail);
213:
214: test.remove("A");
215: assertEquals(5, test.m_buffer.length);
216: assertEquals(1, test.m_head);
217: assertEquals(3, test.m_tail);
218:
219: test.add("D");
220: assertEquals(5, test.m_buffer.length);
221: assertEquals(1, test.m_head);
222: assertEquals(4, test.m_tail);
223: }
224:
225: public void testInternalStateRemove2() {
226: UnboundedFifoBuffer test = new UnboundedFifoBuffer(4);
227: test.add("A");
228: test.add("B");
229: test.add("C");
230: assertEquals(5, test.m_buffer.length);
231: assertEquals(0, test.m_head);
232: assertEquals(3, test.m_tail);
233:
234: test.remove("B");
235: assertEquals(5, test.m_buffer.length);
236: assertEquals(0, test.m_head);
237: assertEquals(2, test.m_tail);
238:
239: test.add("D");
240: assertEquals(5, test.m_buffer.length);
241: assertEquals(0, test.m_head);
242: assertEquals(3, test.m_tail);
243: }
244:
245: public void testInternalStateIteratorRemove1() {
246: UnboundedFifoBuffer test = new UnboundedFifoBuffer(4);
247: test.add("A");
248: test.add("B");
249: test.add("C");
250: assertEquals(5, test.m_buffer.length);
251: assertEquals(0, test.m_head);
252: assertEquals(3, test.m_tail);
253:
254: Iterator it = test.iterator();
255: it.next();
256: it.remove();
257: assertEquals(5, test.m_buffer.length);
258: assertEquals(1, test.m_head);
259: assertEquals(3, test.m_tail);
260:
261: test.add("D");
262: assertEquals(5, test.m_buffer.length);
263: assertEquals(1, test.m_head);
264: assertEquals(4, test.m_tail);
265: }
266:
267: public void testInternalStateIteratorRemove2() {
268: UnboundedFifoBuffer test = new UnboundedFifoBuffer(4);
269: test.add("A");
270: test.add("B");
271: test.add("C");
272:
273: Iterator it = test.iterator();
274: it.next();
275: it.next();
276: it.remove();
277: assertEquals(5, test.m_buffer.length);
278: assertEquals(0, test.m_head);
279: assertEquals(2, test.m_tail);
280:
281: test.add("D");
282: assertEquals(5, test.m_buffer.length);
283: assertEquals(0, test.m_head);
284: assertEquals(3, test.m_tail);
285: }
286:
287: public void testInternalStateIteratorRemoveWithTailAtEnd1() {
288: UnboundedFifoBuffer test = new UnboundedFifoBuffer(3);
289: test.add("A");
290: test.add("B");
291: test.add("C");
292: test.remove("A");
293: test.add("D");
294: assertEquals(4, test.m_buffer.length);
295: assertEquals(1, test.m_head);
296: assertEquals(0, test.m_tail);
297:
298: Iterator it = test.iterator();
299: assertEquals("B", it.next());
300: it.remove();
301: assertEquals(4, test.m_buffer.length);
302: assertEquals(2, test.m_head);
303: assertEquals(0, test.m_tail);
304: }
305:
306: public void testInternalStateIteratorRemoveWithTailAtEnd2() {
307: UnboundedFifoBuffer test = new UnboundedFifoBuffer(3);
308: test.add("A");
309: test.add("B");
310: test.add("C");
311: test.remove("A");
312: test.add("D");
313: assertEquals(4, test.m_buffer.length);
314: assertEquals(1, test.m_head);
315: assertEquals(0, test.m_tail);
316:
317: Iterator it = test.iterator();
318: assertEquals("B", it.next());
319: assertEquals("C", it.next());
320: it.remove();
321: assertEquals(4, test.m_buffer.length);
322: assertEquals(1, test.m_head);
323: assertEquals(3, test.m_tail);
324: }
325:
326: public void testInternalStateIteratorRemoveWithTailAtEnd3() {
327: UnboundedFifoBuffer test = new UnboundedFifoBuffer(3);
328: test.add("A");
329: test.add("B");
330: test.add("C");
331: test.remove("A");
332: test.add("D");
333: assertEquals(4, test.m_buffer.length);
334: assertEquals(1, test.m_head);
335: assertEquals(0, test.m_tail);
336:
337: Iterator it = test.iterator();
338: assertEquals("B", it.next());
339: assertEquals("C", it.next());
340: assertEquals("D", it.next());
341: it.remove();
342: assertEquals(4, test.m_buffer.length);
343: assertEquals(1, test.m_head);
344: assertEquals(3, test.m_tail);
345: }
346:
347: public void testInternalStateIteratorRemoveWithWrap1() {
348: UnboundedFifoBuffer test = new UnboundedFifoBuffer(3);
349: test.add("A");
350: test.add("B");
351: test.add("C");
352: test.remove("A");
353: test.remove("B");
354: test.add("D");
355: test.add("E");
356: assertEquals(4, test.m_buffer.length);
357: assertEquals(2, test.m_head);
358: assertEquals(1, test.m_tail);
359:
360: Iterator it = test.iterator();
361: assertEquals("C", it.next());
362: it.remove();
363: assertEquals(4, test.m_buffer.length);
364: assertEquals(3, test.m_head);
365: assertEquals(1, test.m_tail);
366: }
367:
368: public void testInternalStateIteratorRemoveWithWrap2() {
369: UnboundedFifoBuffer test = new UnboundedFifoBuffer(3);
370: test.add("A");
371: test.add("B");
372: test.add("C");
373: test.remove("A");
374: test.remove("B");
375: test.add("D");
376: test.add("E");
377: assertEquals(4, test.m_buffer.length);
378: assertEquals(2, test.m_head);
379: assertEquals(1, test.m_tail);
380:
381: Iterator it = test.iterator();
382: assertEquals("C", it.next());
383: assertEquals("D", it.next());
384: it.remove();
385: assertEquals(4, test.m_buffer.length);
386: assertEquals(2, test.m_head);
387: assertEquals(0, test.m_tail);
388: }
389:
390: public void testInternalStateIteratorRemoveWithWrap3() {
391: UnboundedFifoBuffer test = new UnboundedFifoBuffer(3);
392: test.add("A");
393: test.add("B");
394: test.add("C");
395: test.remove("A");
396: test.remove("B");
397: test.add("D");
398: test.add("E");
399: assertEquals(4, test.m_buffer.length);
400: assertEquals(2, test.m_head);
401: assertEquals(1, test.m_tail);
402:
403: Iterator it = test.iterator();
404: assertEquals("C", it.next());
405: assertEquals("D", it.next());
406: assertEquals("E", it.next());
407: it.remove();
408: assertEquals(4, test.m_buffer.length);
409: assertEquals(2, test.m_head);
410: assertEquals(0, test.m_tail);
411: }
412:
413: }
|