CircularFifoBuffer

HomePage | RecentChanges | Preferences

While trying out Tim O'Brien's suggestion (http://www.mail-archive.com/commons-user%40jakarta.apache.org/msg11963.html), I ran into a problem, which is illustrated by this simple test program:

Code

import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.buffer.CircularFifoBuffer;

public class TestBuffer {
   
    public static void main(String[] args) {
       
        Buffer buffer = new CircularFifoBuffer(5);
        add( buffer, "Page 1" ); 
        add( buffer, "Page 2" );
        add( buffer, "Page 3" );
        add( buffer, "Page 4" );
        add( buffer, "Page 1" );
        add( buffer, "Page 2" );
        
        /* 
         *  Next line will cause
         *  ArrayIndexOutOfBoundsException: -1 
         */
        add( buffer, "Page 1" );
    }
    
    public static void add(Buffer buffer, String page) {
        buffer.remove( page );
        buffer.add( page );
        System.out.println( buffer );
    }
}

Output

run:
     [java] [Page 1]
     [java] [Page 1, Page 2]
     [java] [Page 1, Page 2, Page 3]
     [java] [Page 1, Page 2, Page 3, Page 4]
     [java] [Page 2, Page 3, Page 4, Page 1]
     [java] [Page 3, Page 4, Page 1, Page 2]
     [java] java.lang.ArrayIndexOutOfBoundsException: -1
...
     [java] Caused by: java.lang.ArrayIndexOutOfBoundsException: -1
     [java]     at org.apache.commons.collections.buffer.BoundedFifoBuffer$1.remove(BoundedFifoBuffer.java:347)
     [java]     at java.util.AbstractCollection.remove(AbstractCollection.java:255)
     [java]     at TestBuffer.add(TestBuffer.java:22)
     [java]     at TestBuffer.main(TestBuffer.java:18)
...
     [java] --- Nested Exception ---
     [java] java.lang.ArrayIndexOutOfBoundsException: -1
     [java]     at org.apache.commons.collections.buffer.BoundedFifoBuffer$1.remove(BoundedFifoBuffer.java:347)
     [java]     at java.util.AbstractCollection.remove(AbstractCollection.java:255)
     [java]     at TestBuffer.add(TestBuffer.java:22)
     [java]     at TestBuffer.main(TestBuffer.java:18)
...

Nevermind... I made a test case and the problem seems to have already been fixed.

See: [Bug 33071]

Index: src/test/org/apache/commons/collections/buffer/TestCircularFifoBuffer.java
===================================================================
--- src/test/org/apache/commons/collections/buffer/TestCircularFifoBuffer.java(r
evision 218985)
+++ src/test/org/apache/commons/collections/buffer/TestCircularFifoBuffer.java(w
orking copy)
@@ -366,6 +366,29 @@
         fifo.remove("8");  // remove element at position two in array
         assertEquals("[4, 5, 6, 7]", fifo.toString());
     }
+
+    public void testRemoveError10() throws Exception {
+        // May be related to bug 33071
+        // See http://wiki.wendysmoak.com/cgi-bin/wiki.pl?CircularFifoBuffer
+        CircularFifoBuffer fifo = new CircularFifoBuffer(5);
+        fifo.remove("1");
+        fifo.add("1");
+        fifo.remove("2");
+        fifo.add("2");
+        fifo.remove("3");
+        fifo.add("3");
+        fifo.remove("4");
+        fifo.add("4");
+        fifo.remove("1");
+        fifo.add("1");
+        fifo.remove("2");
+        fifo.add("2");
+        fifo.remove("1");
+        fifo.add("1");
+
+        assertEquals("[3, 4, 2, 1]", fifo.toString());
+
+    }

     //-----------------------------------------------------------------------
     public void testRepeatedSerialization() throws Exception {

HomePage | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited July 13, 2005 10:19 pm by WendySmoak (diff)
Search: