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