You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/06/02 16:54:39 UTC

svn commit: r781048 - in /incubator/pivot/trunk/core: src/pivot/collections/ src/pivot/collections/adapter/ src/pivot/collections/concurrent/ test/pivot/collections/test/

Author: gbrown
Date: Tue Jun  2 14:54:38 2009
New Revision: 781048

URL: http://svn.apache.org/viewvc?rev=781048&view=rev
Log:
Add more collection unit tests; fix identified issues in collection classes.

Added:
    incubator/pivot/trunk/core/test/pivot/collections/test/QueueTest.java
    incubator/pivot/trunk/core/test/pivot/collections/test/StackTest.java
    incubator/pivot/trunk/core/test/pivot/collections/test/SynchronizedQueueTest.java
    incubator/pivot/trunk/core/test/pivot/collections/test/SynchronizedStackTest.java
Modified:
    incubator/pivot/trunk/core/src/pivot/collections/ArrayList.java
    incubator/pivot/trunk/core/src/pivot/collections/ArrayQueue.java
    incubator/pivot/trunk/core/src/pivot/collections/ArrayStack.java
    incubator/pivot/trunk/core/src/pivot/collections/LinkedList.java
    incubator/pivot/trunk/core/src/pivot/collections/LinkedQueue.java
    incubator/pivot/trunk/core/src/pivot/collections/adapter/ListAdapter.java
    incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedStack.java
    incubator/pivot/trunk/core/test/pivot/collections/test/LinkedListTest.java

Modified: incubator/pivot/trunk/core/src/pivot/collections/ArrayList.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/ArrayList.java?rev=781048&r1=781047&r2=781048&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/ArrayList.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/ArrayList.java Tue Jun  2 14:54:38 2009
@@ -405,7 +405,7 @@
         sb.append(getClass().getName());
         sb.append(" [");
 
-        for (int i = 0; i < items.length; i++) {
+        for (int i = 0; i < length; i++) {
             if (i > 0) {
                 sb.append(", ");
             }

Modified: incubator/pivot/trunk/core/src/pivot/collections/ArrayQueue.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/ArrayQueue.java?rev=781048&r1=781047&r2=781048&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/ArrayQueue.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/ArrayQueue.java Tue Jun  2 14:54:38 2009
@@ -39,8 +39,17 @@
         super(comparator);
     }
 
+    public ArrayQueue(int capacity) {
+        super(capacity);
+    }
+
     public void enqueue(T item) {
-        add(item);
+        if (getComparator() == null) {
+            insert(item, 0);
+        } else {
+            add(item);
+        }
+
         queueListeners.itemEnqueued(this, item);
     }
 

Modified: incubator/pivot/trunk/core/src/pivot/collections/ArrayStack.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/ArrayStack.java?rev=781048&r1=781047&r2=781048&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/ArrayStack.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/ArrayStack.java Tue Jun  2 14:54:38 2009
@@ -39,6 +39,10 @@
         super(comparator);
     }
 
+    public ArrayStack(int capacity) {
+        super(capacity);
+    }
+
     public void push(T item) {
         add(item);
         stackListeners.itemPushed(this, item);

Modified: incubator/pivot/trunk/core/src/pivot/collections/LinkedList.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/LinkedList.java?rev=781048&r1=781047&r2=781048&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/LinkedList.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/LinkedList.java Tue Jun  2 14:54:38 2009
@@ -259,7 +259,11 @@
                 end = end.next;
             }
 
-            end = end.previous;
+            if (end == null) {
+                end = last;
+            } else {
+                end = end.previous;
+            }
 
             // Decouple the nodes from the list
             if (start.previous != null) {
@@ -449,6 +453,7 @@
             }
 
             sb.append(item);
+            i++;
         }
 
         sb.append("]");

Modified: incubator/pivot/trunk/core/src/pivot/collections/LinkedQueue.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/LinkedQueue.java?rev=781048&r1=781047&r2=781048&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/LinkedQueue.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/LinkedQueue.java Tue Jun  2 14:54:38 2009
@@ -38,7 +38,12 @@
     }
 
     public void enqueue(T item) {
-        add(item);
+        if (getComparator() == null) {
+            insert(item, 0);
+        } else {
+            add(item);
+        }
+
         queueListeners.itemEnqueued(this, item);
     }
 

Modified: incubator/pivot/trunk/core/src/pivot/collections/adapter/ListAdapter.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/adapter/ListAdapter.java?rev=781048&r1=781047&r2=781048&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/adapter/ListAdapter.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/adapter/ListAdapter.java Tue Jun  2 14:54:38 2009
@@ -74,7 +74,6 @@
         }
 
         list.add(index, item);
-
         listListeners.itemInserted(this, index);
     }
 

Modified: incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedStack.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedStack.java?rev=781048&r1=781047&r2=781048&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedStack.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/concurrent/SynchronizedStack.java Tue Jun  2 14:54:38 2009
@@ -59,6 +59,8 @@
     public synchronized void push(T item) {
         ((Stack<T>)collection).push(item);
         stackListeners.itemPushed(this, item);
+
+        notify();
     }
 
     public synchronized T pop() {

Modified: incubator/pivot/trunk/core/test/pivot/collections/test/LinkedListTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/pivot/collections/test/LinkedListTest.java?rev=781048&r1=781047&r2=781048&view=diff
==============================================================================
--- incubator/pivot/trunk/core/test/pivot/collections/test/LinkedListTest.java (original)
+++ incubator/pivot/trunk/core/test/pivot/collections/test/LinkedListTest.java Tue Jun  2 14:54:38 2009
@@ -20,6 +20,8 @@
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import java.util.Comparator;
+
 import org.junit.Test;
 
 import pivot.collections.LinkedList;
@@ -27,7 +29,6 @@
 
 public class LinkedListTest {
     @Test
-    @SuppressWarnings("unchecked")
     public void basicTest() {
         LinkedList<String> list = new LinkedList<String>();
         list.insert("B", 0);
@@ -58,4 +59,41 @@
         list.insert("G", 0);
         assertEquals(list, new LinkedList<String>("G", "B", "F", "D"));
     }
+
+    @Test
+    public void sortTest1() {
+        LinkedList<String> linkedList = new LinkedList<String>();
+        linkedList.setComparator(new Comparator<String>() {
+            public int compare(String s1, String s2) {
+                return s1.toLowerCase().compareTo(s2.toLowerCase());
+            }
+        });
+
+        linkedList.add("N");
+        linkedList.add("P");
+        linkedList.add("d");
+        linkedList.add("A");
+        linkedList.add("z");
+
+        assertEquals(linkedList, new LinkedList<String>("A", "d", "N", "P", "z"));
+    }
+
+    @Test
+    public void sortTest2() {
+        LinkedList<String> linkedList = new LinkedList<String>();
+
+        linkedList.add("N");
+        linkedList.add("P");
+        linkedList.add("d");
+        linkedList.add("A");
+        linkedList.add("z");
+
+        linkedList.setComparator(new Comparator<String>() {
+            public int compare(String s1, String s2) {
+                return s1.toLowerCase().compareTo(s2.toLowerCase());
+            }
+        });
+
+        assertEquals(linkedList, new LinkedList<String>("A", "d", "N", "P", "z"));
+    }
 }

Added: incubator/pivot/trunk/core/test/pivot/collections/test/QueueTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/pivot/collections/test/QueueTest.java?rev=781048&view=auto
==============================================================================
--- incubator/pivot/trunk/core/test/pivot/collections/test/QueueTest.java (added)
+++ incubator/pivot/trunk/core/test/pivot/collections/test/QueueTest.java Tue Jun  2 14:54:38 2009
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package pivot.collections.test;
+
+import static org.junit.Assert.assertTrue;
+
+import java.util.Comparator;
+
+import org.junit.Test;
+
+import pivot.collections.ArrayQueue;
+import pivot.collections.LinkedQueue;
+import pivot.collections.Queue;
+
+public class QueueTest {
+    @Test
+    public void queueTest() {
+        testQueue(new ArrayQueue<String>(5));
+        testQueue(new LinkedQueue<String>());
+        testSortedQueue(new ArrayQueue<String>(5));
+        testSortedQueue(new LinkedQueue<String>());
+    }
+
+    private static void testQueue(Queue<String> queue) {
+        int i = 0;
+        while (i < 5) {
+            char c = 'A';
+            c += i;
+            queue.enqueue(Character.toString(c));
+            i++;
+        }
+
+        i = 0;
+        while (!queue.isEmpty()) {
+            String s = queue.dequeue();
+            char c = s.charAt(0);
+            c -= i;
+            assertTrue(c == 'A');
+            i++;
+        }
+    }
+
+    private static void testSortedQueue(Queue<String> queue) {
+        queue.setComparator(new Comparator<String>() {
+            public int compare(String s1, String s2) {
+                return s1.compareTo(s2);
+            }
+        });
+
+        int i = 0;
+        while (i < 5) {
+            char c = 'A';
+            c += i;
+            queue.enqueue(Character.toString(c));
+            i++;
+        }
+
+        i = 4;
+        while (!queue.isEmpty()) {
+            String s = queue.dequeue();
+            char c = s.charAt(0);
+            c -= i;
+            assertTrue(c == 'A');
+            i--;
+        }
+    }
+}

Added: incubator/pivot/trunk/core/test/pivot/collections/test/StackTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/pivot/collections/test/StackTest.java?rev=781048&view=auto
==============================================================================
--- incubator/pivot/trunk/core/test/pivot/collections/test/StackTest.java (added)
+++ incubator/pivot/trunk/core/test/pivot/collections/test/StackTest.java Tue Jun  2 14:54:38 2009
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package pivot.collections.test;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import pivot.collections.ArrayStack;
+import pivot.collections.LinkedStack;
+import pivot.collections.Stack;
+
+public class StackTest {
+    @Test
+    public void stackTest() {
+        testStack(new ArrayStack<String>(5));
+        testStack(new LinkedStack<String>());
+    }
+
+    private static void testStack(Stack<String> stack) {
+        int i = 0;
+        while (i < 5) {
+            char c = 'A';
+            c += i;
+            stack.push(Character.toString(c));
+            i++;
+        }
+
+        i = 4;
+        while (!stack.isEmpty()) {
+            String s = stack.pop();
+            char c = s.charAt(0);
+            c -= i;
+            assertTrue(c == 'A');
+            i--;
+        }
+    }
+}

Added: incubator/pivot/trunk/core/test/pivot/collections/test/SynchronizedQueueTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/pivot/collections/test/SynchronizedQueueTest.java?rev=781048&view=auto
==============================================================================
--- incubator/pivot/trunk/core/test/pivot/collections/test/SynchronizedQueueTest.java (added)
+++ incubator/pivot/trunk/core/test/pivot/collections/test/SynchronizedQueueTest.java Tue Jun  2 14:54:38 2009
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package pivot.collections.test;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import pivot.collections.ArrayQueue;
+import pivot.collections.LinkedQueue;
+import pivot.collections.Queue;
+import pivot.collections.concurrent.SynchronizedQueue;
+import pivot.util.concurrent.Task;
+import pivot.util.concurrent.TaskListener;
+
+public class SynchronizedQueueTest {
+    @Test
+    public void synchronizedQueueTest() {
+        testSynchronizedQueue(new ArrayQueue<String>());
+        testSynchronizedQueue(new LinkedQueue<String>());
+    }
+
+    private static void testSynchronizedQueue(Queue<String> queue) {
+        final SynchronizedQueue<String> synchronizedQueue =
+            new SynchronizedQueue<String>(queue);
+
+        Task<Void> testTask = new Task<Void>() {
+            public Void execute() {
+                int i = 0;
+                while (i < 5) {
+                    synchronizedQueue.dequeue();
+                    i++;
+                }
+
+                return null;
+            }
+        };
+
+        testTask.execute(new TaskListener<Void>() {
+            public synchronized void taskExecuted(Task<Void> task) {
+                notify();
+            }
+
+            public synchronized void executeFailed(Task<Void> task) {
+                notify();
+            }
+        });
+
+        int i = 0;
+        while (i < 5) {
+            char c = 'A';
+            c += i;
+            synchronizedQueue.enqueue(Character.toString(c));
+
+            try {
+                Thread.sleep(500);
+            } catch(InterruptedException exception) {
+                throw new RuntimeException(exception);
+            }
+
+            i++;
+        }
+
+        synchronized(testTask) {
+            if (testTask.isPending()) {
+                try {
+                    testTask.wait(10000);
+                } catch(InterruptedException exception) {
+                    throw new RuntimeException(exception);
+                }
+            }
+        }
+
+        assertTrue(testTask.getFault() == null);
+    }
+}

Added: incubator/pivot/trunk/core/test/pivot/collections/test/SynchronizedStackTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/pivot/collections/test/SynchronizedStackTest.java?rev=781048&view=auto
==============================================================================
--- incubator/pivot/trunk/core/test/pivot/collections/test/SynchronizedStackTest.java (added)
+++ incubator/pivot/trunk/core/test/pivot/collections/test/SynchronizedStackTest.java Tue Jun  2 14:54:38 2009
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package pivot.collections.test;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import pivot.collections.ArrayStack;
+import pivot.collections.LinkedStack;
+import pivot.collections.Stack;
+import pivot.collections.concurrent.SynchronizedStack;
+import pivot.util.concurrent.Task;
+import pivot.util.concurrent.TaskListener;
+
+public class SynchronizedStackTest {
+    @Test
+    public void synchronizedStackTest() {
+        testSynchronizedStack(new ArrayStack<String>());
+        testSynchronizedStack(new LinkedStack<String>());
+    }
+
+    private static void testSynchronizedStack(Stack<String> stack) {
+        final SynchronizedStack<String> synchronizedStack =
+            new SynchronizedStack<String>(stack);
+
+        Task<Void> testTask = new Task<Void>() {
+            public Void execute() {
+                int i = 0;
+                while (i < 5) {
+                    synchronizedStack.pop();
+                    i++;
+                }
+
+                return null;
+            }
+        };
+
+        testTask.execute(new TaskListener<Void>() {
+            public synchronized void taskExecuted(Task<Void> task) {
+                notify();
+            }
+
+            public synchronized void executeFailed(Task<Void> task) {
+                notify();
+            }
+        });
+
+        int i = 0;
+        while (i < 5) {
+            char c = 'A';
+            c += i;
+            synchronizedStack.push(Character.toString(c));
+
+            try {
+                Thread.sleep(500);
+            } catch(InterruptedException exception) {
+                throw new RuntimeException(exception);
+            }
+
+            i++;
+        }
+
+        synchronized(testTask) {
+            if (testTask.isPending()) {
+                try {
+                    testTask.wait(10000);
+                } catch(InterruptedException exception) {
+                    throw new RuntimeException(exception);
+                }
+            }
+        }
+
+        assertTrue(testTask.getFault() == null);
+    }
+}