You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by nd...@apache.org on 2006/08/24 05:42:33 UTC

svn commit: r434296 [11/19] - in /incubator/harmony/enhanced/classlib/trunk: make/ modules/concurrent/ modules/concurrent/.settings/ modules/concurrent/META-INF/ modules/concurrent/make/ modules/concurrent/src/ modules/concurrent/src/main/ modules/conc...

Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AbstractQueuedSynchronizerTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AbstractQueuedSynchronizerTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AbstractQueuedSynchronizerTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AbstractQueuedSynchronizerTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,1300 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/licenses/publicdomain
+ * Other contributors include Andrew Wright, Jeffrey Hayes, 
+ * Pat Fisher, Mike Judd. 
+ */
+
+
+import junit.framework.*;
+import java.util.*;
+import java.util.concurrent.*;
+import java.util.concurrent.locks.*;
+import java.io.*;
+
+public class AbstractQueuedSynchronizerTest extends JSR166TestCase {
+    public static void main(String[] args) {
+        junit.textui.TestRunner.run (suite());
+    }
+    public static Test suite() {
+        return new TestSuite(AbstractQueuedSynchronizerTest.class);
+    }
+
+    /**
+     * A simple mutex class, adapted from the
+     * AbstractQueuedSynchronizer javadoc.  Exclusive acquire tests
+     * exercise this as a sample user extension.  Other
+     * methods/features of AbstractQueuedSynchronizerTest are tested
+     * via other test classes, including those for ReentrantLock,
+     * ReentrantReadWriteLock, and Semaphore
+     */
+    static class Mutex extends AbstractQueuedSynchronizer {
+        public boolean isHeldExclusively() { return getState() == 1; }
+        
+        public boolean tryAcquire(int acquires) {
+            assertTrue(acquires == 1); 
+            return compareAndSetState(0, 1);
+        }
+        
+        public boolean tryRelease(int releases) {
+            if (getState() == 0) throw new IllegalMonitorStateException();
+            setState(0);
+            return true;
+        }
+        
+        public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); }
+
+    }
+
+    
+    /**
+     * A simple latch class, to test shared mode.
+     */
+    static class BooleanLatch extends AbstractQueuedSynchronizer { 
+        public boolean isSignalled() { return getState() != 0; }
+
+        public int tryAcquireShared(int ignore) {
+            return isSignalled()? 1 : -1;
+        }
+        
+        public boolean tryReleaseShared(int ignore) {
+            setState(1);
+            return true;
+        }
+    }
+
+    /**
+     * A runnable calling acquireInterruptibly
+     */
+    class InterruptibleSyncRunnable implements Runnable {
+        final Mutex sync;
+        InterruptibleSyncRunnable(Mutex l) { sync = l; }
+        public void run() {
+            try {
+                sync.acquireInterruptibly(1);
+            } catch(InterruptedException success){}
+        }
+    }
+
+
+    /**
+     * A runnable calling acquireInterruptibly that expects to be
+     * interrupted
+     */
+    class InterruptedSyncRunnable implements Runnable {
+        final Mutex sync;
+        InterruptedSyncRunnable(Mutex l) { sync = l; }
+        public void run() {
+            try {
+                sync.acquireInterruptibly(1);
+                threadShouldThrow();
+            } catch(InterruptedException success){}
+        }
+    }
+
+    /**
+     * isHeldExclusively is false upon construction
+     */
+    public void testIsHeldExclusively() { 
+	Mutex rl = new Mutex();
+        assertFalse(rl.isHeldExclusively());
+    }
+    
+    /**
+     * acquiring released sync succeeds
+     */
+    public void testAcquire() { 
+	Mutex rl = new Mutex();
+        rl.acquire(1);
+        assertTrue(rl.isHeldExclusively());
+        rl.release(1);
+        assertFalse(rl.isHeldExclusively());
+    }
+
+    /**
+     * tryAcquire on an released sync succeeds
+     */
+    public void testTryAcquire() { 
+	Mutex rl = new Mutex();
+        assertTrue(rl.tryAcquire(1));
+        assertTrue(rl.isHeldExclusively());
+        rl.release(1);
+    }
+
+    /**
+     * hasQueuedThreads reports whether there are waiting threads
+     */
+    public void testhasQueuedThreads() { 
+	final Mutex sync = new Mutex();
+        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
+        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
+        try {
+            assertFalse(sync.hasQueuedThreads());
+            sync.acquire(1);
+            t1.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.hasQueuedThreads());
+            t2.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.hasQueuedThreads());
+            t1.interrupt();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.hasQueuedThreads());
+            sync.release(1);
+            Thread.sleep(SHORT_DELAY_MS);
+            assertFalse(sync.hasQueuedThreads());
+            t1.join();
+            t2.join();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+    /**
+     * isQueued(null) throws NPE
+     */
+    public void testIsQueuedNPE() { 
+	final Mutex sync = new Mutex();
+        try {
+            sync.isQueued(null);
+            shouldThrow();
+        } catch (NullPointerException success) {
+        }
+    }
+
+    /**
+     * isQueued reports whether a thread is queued.
+     */
+    public void testIsQueued() { 
+	final Mutex sync = new Mutex();
+        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
+        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
+        try {
+            assertFalse(sync.isQueued(t1));
+            assertFalse(sync.isQueued(t2));
+            sync.acquire(1);
+            t1.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.isQueued(t1));
+            t2.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.isQueued(t1));
+            assertTrue(sync.isQueued(t2));
+            t1.interrupt();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertFalse(sync.isQueued(t1));
+            assertTrue(sync.isQueued(t2));
+            sync.release(1);
+            Thread.sleep(SHORT_DELAY_MS);
+            assertFalse(sync.isQueued(t1));
+            Thread.sleep(SHORT_DELAY_MS);
+            assertFalse(sync.isQueued(t2));
+            t1.join();
+            t2.join();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+    /**
+     * getFirstQueuedThread returns first waiting thread or null if none
+     */
+    public void testGetFirstQueuedThread() { 
+	final Mutex sync = new Mutex();
+        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
+        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
+        try {
+            assertNull(sync.getFirstQueuedThread());
+            sync.acquire(1);
+            t1.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertEquals(t1, sync.getFirstQueuedThread());
+            t2.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertEquals(t1, sync.getFirstQueuedThread());
+            t1.interrupt();
+            Thread.sleep(SHORT_DELAY_MS);
+            Thread.sleep(SHORT_DELAY_MS);
+            assertEquals(t2, sync.getFirstQueuedThread());
+            sync.release(1);
+            Thread.sleep(SHORT_DELAY_MS);
+            assertNull(sync.getFirstQueuedThread());
+            t1.join();
+            t2.join();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+
+    /**
+     * hasContended reports false if no thread has ever blocked, else true
+     */
+    public void testHasContended() { 
+	final Mutex sync = new Mutex();
+        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
+        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
+        try {
+            assertFalse(sync.hasContended());
+            sync.acquire(1);
+            t1.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.hasContended());
+            t2.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.hasContended());
+            t1.interrupt();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.hasContended());
+            sync.release(1);
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.hasContended());
+            t1.join();
+            t2.join();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+    /**
+     * getQueuedThreads includes waiting threads
+     */
+    public void testGetQueuedThreads() { 
+	final Mutex sync = new Mutex();
+        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
+        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
+        try {
+            assertTrue(sync.getQueuedThreads().isEmpty());
+            sync.acquire(1);
+            assertTrue(sync.getQueuedThreads().isEmpty());
+            t1.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.getQueuedThreads().contains(t1));
+            t2.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.getQueuedThreads().contains(t1));
+            assertTrue(sync.getQueuedThreads().contains(t2));
+            t1.interrupt();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertFalse(sync.getQueuedThreads().contains(t1));
+            assertTrue(sync.getQueuedThreads().contains(t2));
+            sync.release(1);
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.getQueuedThreads().isEmpty());
+            t1.join();
+            t2.join();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+    /**
+     * getExclusiveQueuedThreads includes waiting threads
+     */
+    public void testGetExclusiveQueuedThreads() { 
+	final Mutex sync = new Mutex();
+        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
+        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
+        try {
+            assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
+            sync.acquire(1);
+            assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
+            t1.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
+            t2.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
+            assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
+            t1.interrupt();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertFalse(sync.getExclusiveQueuedThreads().contains(t1));
+            assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
+            sync.release(1);
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
+            t1.join();
+            t2.join();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+    /**
+     * getSharedQueuedThreads does not include exclusively waiting threads
+     */
+    public void testGetSharedQueuedThreads() { 
+	final Mutex sync = new Mutex();
+        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
+        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
+        try {
+            assertTrue(sync.getSharedQueuedThreads().isEmpty());
+            sync.acquire(1);
+            assertTrue(sync.getSharedQueuedThreads().isEmpty());
+            t1.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.getSharedQueuedThreads().isEmpty());
+            t2.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.getSharedQueuedThreads().isEmpty());
+            t1.interrupt();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.getSharedQueuedThreads().isEmpty());
+            sync.release(1);
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.getSharedQueuedThreads().isEmpty());
+            t1.join();
+            t2.join();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+    /**
+     * tryAcquireNanos is interruptible.
+     */
+    public void testInterruptedException2() { 
+	final Mutex sync = new Mutex();
+	sync.acquire(1);
+	Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+			sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000);
+			threadShouldThrow();
+		    } catch(InterruptedException success){}
+		}
+	    });
+        try {
+            t.start();
+            t.interrupt();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    }
+
+
+    /**
+     * TryAcquire on exclusively held sync fails
+     */
+    public void testTryAcquireWhenSynced() { 
+	final Mutex sync = new Mutex();
+	sync.acquire(1);
+	Thread t = new Thread(new Runnable() {
+                public void run() {
+                    threadAssertFalse(sync.tryAcquire(1));
+		}
+	    });
+        try {
+            t.start();
+            t.join();
+            sync.release(1);
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+    /**
+     * tryAcquireNanos on an exclusively held sync times out
+     */
+    public void testAcquireNanos_Timeout() { 
+	final Mutex sync = new Mutex();
+	sync.acquire(1);
+	Thread t = new Thread(new Runnable() {
+                public void run() {
+		    try {
+                        threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000));
+                    } catch (Exception ex) {
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+        try {
+            t.start();
+            t.join();
+            sync.release(1);
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+    
+   
+    /**
+     * getState is true when acquired and false when not
+     */
+    public void testGetState() {
+	final Mutex sync = new Mutex();
+	sync.acquire(1);
+	assertTrue(sync.isHeldExclusively());
+	sync.release(1);
+	assertFalse(sync.isHeldExclusively());
+	Thread t = new Thread(new Runnable() { 
+		public void run() {
+		    sync.acquire(1);
+		    try {
+			Thread.sleep(SMALL_DELAY_MS);
+		    }
+		    catch(Exception e) {
+                        threadUnexpectedException();
+                    }
+		    sync.release(1);
+		}
+	    });
+	try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(sync.isHeldExclusively());
+            t.join();
+            assertFalse(sync.isHeldExclusively());
+        } catch(Exception e){
+            unexpectedException();
+        }
+    }
+
+
+    /**
+     * acquireInterruptibly is interruptible.
+     */
+    public void testAcquireInterruptibly1() { 
+	final Mutex sync = new Mutex();
+	sync.acquire(1);
+	Thread t = new Thread(new InterruptedSyncRunnable(sync));
+        try {
+            t.start();
+            t.interrupt();
+            sync.release(1);
+            t.join();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+    /**
+     * acquireInterruptibly succeeds when released, else is interruptible
+     */
+    public void testAcquireInterruptibly2() {
+	final Mutex sync = new Mutex();	
+	try {
+            sync.acquireInterruptibly(1);
+        } catch(Exception e) {
+            unexpectedException();
+        }
+	Thread t = new Thread(new InterruptedSyncRunnable(sync));
+        try {
+            t.start();
+            t.interrupt();
+            assertTrue(sync.isHeldExclusively());
+            t.join();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * owns is true for a condition created by sync else false
+     */
+    public void testOwns() {
+	final Mutex sync = new Mutex();	
+        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
+        final Mutex sync2 = new Mutex();
+        assertTrue(sync.owns(c));
+        assertFalse(sync2.owns(c));
+    }
+
+    /**
+     * Calling await without holding sync throws IllegalMonitorStateException
+     */
+    public void testAwait_IllegalMonitor() {
+	final Mutex sync = new Mutex();	
+        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
+        try {
+            c.await();
+            shouldThrow();
+        }
+        catch (IllegalMonitorStateException success) {
+        }
+        catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * Calling signal without holding sync throws IllegalMonitorStateException
+     */
+    public void testSignal_IllegalMonitor() {
+	final Mutex sync = new Mutex();	
+        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
+        try {
+            c.signal();
+            shouldThrow();
+        }
+        catch (IllegalMonitorStateException success) {
+        }
+        catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * awaitNanos without a signal times out
+     */
+    public void testAwaitNanos_Timeout() {
+	final Mutex sync = new Mutex();	
+        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
+        try {
+            sync.acquire(1);
+            long t = c.awaitNanos(100);
+            assertTrue(t <= 0);
+            sync.release(1);
+        }
+        catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     *  Timed await without a signal times out
+     */
+    public void testAwait_Timeout() {
+	final Mutex sync = new Mutex();	
+        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
+        try {
+            sync.acquire(1);
+            assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            sync.release(1);
+        }
+        catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * awaitUntil without a signal times out
+     */
+    public void testAwaitUntil_Timeout() {
+	final Mutex sync = new Mutex();	
+        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
+        try {
+            sync.acquire(1);
+            java.util.Date d = new java.util.Date();
+            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
+            sync.release(1);
+        }
+        catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * await returns when signalled
+     */
+    public void testAwait() {
+	final Mutex sync = new Mutex();	
+        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
+	Thread t = new Thread(new Runnable() { 
+		public void run() {
+		    try {
+			sync.acquire(1);
+                        c.await();
+                        sync.release(1);
+		    }
+		    catch(InterruptedException e) {
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            sync.acquire(1);
+            c.signal();
+            sync.release(1);
+            t.join(SHORT_DELAY_MS);
+            assertFalse(t.isAlive());
+        }
+        catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+
+
+    /**
+     * hasWaiters throws NPE if null
+     */
+    public void testHasWaitersNPE() {
+	final Mutex sync = new Mutex();
+        try {
+            sync.hasWaiters(null);
+            shouldThrow();
+        } catch (NullPointerException success) {
+        } catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * getWaitQueueLength throws NPE if null
+     */
+    public void testGetWaitQueueLengthNPE() {
+	final Mutex sync = new Mutex();
+        try {
+            sync.getWaitQueueLength(null);
+            shouldThrow();
+        } catch (NullPointerException success) {
+        } catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+
+    /**
+     * getWaitingThreads throws NPE if null
+     */
+    public void testGetWaitingThreadsNPE() {
+	final Mutex sync = new Mutex();
+        try {
+            sync.getWaitingThreads(null);
+            shouldThrow();
+        } catch (NullPointerException success) {
+        } catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+
+    /**
+     * hasWaiters throws IAE if not owned
+     */
+    public void testHasWaitersIAE() {
+	final Mutex sync = new Mutex();
+        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
+	final Mutex sync2 = new Mutex();
+        try {
+            sync2.hasWaiters(c);
+            shouldThrow();
+        } catch (IllegalArgumentException success) {
+        } catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * hasWaiters throws IMSE if not synced
+     */
+    public void testHasWaitersIMSE() {
+	final Mutex sync = new Mutex();
+        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
+        try {
+            sync.hasWaiters(c);
+            shouldThrow();
+        } catch (IllegalMonitorStateException success) {
+        } catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+
+    /**
+     * getWaitQueueLength throws IAE if not owned
+     */
+    public void testGetWaitQueueLengthIAE() {
+	final Mutex sync = new Mutex();
+        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
+	final Mutex sync2 = new Mutex();
+        try {
+            sync2.getWaitQueueLength(c);
+            shouldThrow();
+        } catch (IllegalArgumentException success) {
+        } catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * getWaitQueueLength throws IMSE if not synced
+     */
+    public void testGetWaitQueueLengthIMSE() {
+	final Mutex sync = new Mutex();
+        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
+        try {
+            sync.getWaitQueueLength(c);
+            shouldThrow();
+        } catch (IllegalMonitorStateException success) {
+        } catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+
+    /**
+     * getWaitingThreads throws IAE if not owned
+     */
+    public void testGetWaitingThreadsIAE() {
+	final Mutex sync = new Mutex();	
+        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
+	final Mutex sync2 = new Mutex();	
+        try {
+            sync2.getWaitingThreads(c);
+            shouldThrow();
+        } catch (IllegalArgumentException success) {
+        } catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * getWaitingThreads throws IMSE if not synced
+     */
+    public void testGetWaitingThreadsIMSE() {
+	final Mutex sync = new Mutex();	
+        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
+        try {
+            sync.getWaitingThreads(c);
+            shouldThrow();
+        } catch (IllegalMonitorStateException success) {
+        } catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+
+
+    /**
+     * hasWaiters returns true when a thread is waiting, else false
+     */
+    public void testHasWaiters() {
+	final Mutex sync = new Mutex();	
+        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
+	Thread t = new Thread(new Runnable() { 
+		public void run() {
+		    try {
+			sync.acquire(1);
+                        threadAssertFalse(sync.hasWaiters(c));
+                        threadAssertEquals(0, sync.getWaitQueueLength(c));
+                        c.await();
+                        sync.release(1);
+		    }
+		    catch(InterruptedException e) {
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            sync.acquire(1);
+            assertTrue(sync.hasWaiters(c));
+            assertEquals(1, sync.getWaitQueueLength(c));
+            c.signal();
+            sync.release(1);
+            Thread.sleep(SHORT_DELAY_MS);
+            sync.acquire(1);
+            assertFalse(sync.hasWaiters(c));
+            assertEquals(0, sync.getWaitQueueLength(c));
+            sync.release(1);
+            t.join(SHORT_DELAY_MS);
+            assertFalse(t.isAlive());
+        }
+        catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * getWaitQueueLength returns number of waiting threads
+     */
+    public void testGetWaitQueueLength() {
+	final Mutex sync = new Mutex();	
+        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
+	Thread t1 = new Thread(new Runnable() { 
+		public void run() {
+		    try {
+			sync.acquire(1);
+                        threadAssertFalse(sync.hasWaiters(c));
+                        threadAssertEquals(0, sync.getWaitQueueLength(c));
+                        c.await();
+                        sync.release(1);
+		    }
+		    catch(InterruptedException e) {
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+
+	Thread t2 = new Thread(new Runnable() { 
+		public void run() {
+		    try {
+			sync.acquire(1);
+                        threadAssertTrue(sync.hasWaiters(c));
+                        threadAssertEquals(1, sync.getWaitQueueLength(c));
+                        c.await();
+                        sync.release(1);
+		    }
+		    catch(InterruptedException e) {
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+
+        try {
+            t1.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            t2.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            sync.acquire(1);
+            assertTrue(sync.hasWaiters(c));
+            assertEquals(2, sync.getWaitQueueLength(c));
+            c.signalAll();
+            sync.release(1);
+            Thread.sleep(SHORT_DELAY_MS);
+            sync.acquire(1);
+            assertFalse(sync.hasWaiters(c));
+            assertEquals(0, sync.getWaitQueueLength(c));
+            sync.release(1);
+            t1.join(SHORT_DELAY_MS);
+            t2.join(SHORT_DELAY_MS);
+            assertFalse(t1.isAlive());
+            assertFalse(t2.isAlive());
+        }
+        catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * getWaitingThreads returns only and all waiting threads
+     */
+    public void testGetWaitingThreads() {
+	final Mutex sync = new Mutex();	
+        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
+	Thread t1 = new Thread(new Runnable() { 
+		public void run() {
+		    try {
+			sync.acquire(1);
+                        threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
+                        c.await();
+                        sync.release(1);
+		    }
+		    catch(InterruptedException e) {
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+
+	Thread t2 = new Thread(new Runnable() { 
+		public void run() {
+		    try {
+			sync.acquire(1);
+                        threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
+                        c.await();
+                        sync.release(1);
+		    }
+		    catch(InterruptedException e) {
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+
+        try {
+            sync.acquire(1);
+            assertTrue(sync.getWaitingThreads(c).isEmpty());
+            sync.release(1);
+            t1.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            t2.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            sync.acquire(1);
+            assertTrue(sync.hasWaiters(c));
+            assertTrue(sync.getWaitingThreads(c).contains(t1));
+            assertTrue(sync.getWaitingThreads(c).contains(t2));
+            c.signalAll();
+            sync.release(1);
+            Thread.sleep(SHORT_DELAY_MS);
+            sync.acquire(1);
+            assertFalse(sync.hasWaiters(c));
+            assertTrue(sync.getWaitingThreads(c).isEmpty());
+            sync.release(1);
+            t1.join(SHORT_DELAY_MS);
+            t2.join(SHORT_DELAY_MS);
+            assertFalse(t1.isAlive());
+            assertFalse(t2.isAlive());
+        }
+        catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+
+
+    /**
+     * awaitUninterruptibly doesn't abort on interrupt
+     */
+    public void testAwaitUninterruptibly() {
+	final Mutex sync = new Mutex();	
+        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
+	Thread t = new Thread(new Runnable() { 
+		public void run() {
+                    sync.acquire(1);
+                    c.awaitUninterruptibly();
+                    sync.release(1);
+		}
+	    });
+
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            t.interrupt();
+            sync.acquire(1);
+            c.signal();
+            sync.release(1);
+            assert(t.isInterrupted());
+            t.join(SHORT_DELAY_MS);
+            assertFalse(t.isAlive());
+        }
+        catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * await is interruptible
+     */
+    public void testAwait_Interrupt() {
+	final Mutex sync = new Mutex();	
+        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
+	Thread t = new Thread(new Runnable() { 
+		public void run() {
+		    try {
+			sync.acquire(1);
+                        c.await();
+                        sync.release(1);
+                        threadShouldThrow();
+		    }
+		    catch(InterruptedException success) {
+                    }
+		}
+	    });
+
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            t.interrupt();
+            t.join(SHORT_DELAY_MS);
+            assertFalse(t.isAlive());
+        }
+        catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * awaitNanos is interruptible
+     */
+    public void testAwaitNanos_Interrupt() {
+	final Mutex sync = new Mutex();	
+        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
+	Thread t = new Thread(new Runnable() { 
+		public void run() {
+		    try {
+			sync.acquire(1);
+                        c.awaitNanos(1000 * 1000 * 1000); // 1 sec
+                        sync.release(1);
+                        threadShouldThrow();
+		    }
+		    catch(InterruptedException success) {
+                    }
+		}
+	    });
+
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            t.interrupt();
+            t.join(SHORT_DELAY_MS);
+            assertFalse(t.isAlive());
+        }
+        catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * awaitUntil is interruptible
+     */
+    public void testAwaitUntil_Interrupt() {
+	final Mutex sync = new Mutex();	
+        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
+	Thread t = new Thread(new Runnable() { 
+		public void run() {
+		    try {
+			sync.acquire(1);
+                        java.util.Date d = new java.util.Date();
+                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
+                        sync.release(1);
+                        threadShouldThrow();
+		    }
+		    catch(InterruptedException success) {
+                    }
+		}
+	    });
+
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            t.interrupt();
+            t.join(SHORT_DELAY_MS);
+            assertFalse(t.isAlive());
+        }
+        catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * signalAll wakes up all threads
+     */
+    public void testSignalAll() {
+	final Mutex sync = new Mutex();	
+        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
+	Thread t1 = new Thread(new Runnable() { 
+		public void run() {
+		    try {
+			sync.acquire(1);
+                        c.await();
+                        sync.release(1);
+		    }
+		    catch(InterruptedException e) {
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+
+	Thread t2 = new Thread(new Runnable() { 
+		public void run() {
+		    try {
+			sync.acquire(1);
+                        c.await();
+                        sync.release(1);
+		    }
+		    catch(InterruptedException e) {
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+
+        try {
+            t1.start();
+            t2.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            sync.acquire(1);
+            c.signalAll();
+            sync.release(1);
+            t1.join(SHORT_DELAY_MS);
+            t2.join(SHORT_DELAY_MS);
+            assertFalse(t1.isAlive());
+            assertFalse(t2.isAlive());
+        }
+        catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+
+    /**
+     * toString indicates current state
+     */
+    public void testToString() {
+        Mutex sync = new Mutex();
+        String us = sync.toString();
+        assertTrue(us.indexOf("State = 0") >= 0);
+        sync.acquire(1);
+        String ls = sync.toString();
+        assertTrue(ls.indexOf("State = 1") >= 0);
+    }
+
+    /**
+     * A serialized AQS deserializes with current state
+     */
+    public void testSerialization() {
+        Mutex l = new Mutex();
+        l.acquire(1);
+        assertTrue(l.isHeldExclusively());
+
+        try {
+            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
+            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
+            out.writeObject(l);
+            out.close();
+
+            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
+            Mutex r = (Mutex) in.readObject();
+            assertTrue(r.isHeldExclusively());
+        } catch(Exception e){
+            e.printStackTrace();
+            unexpectedException();
+        }
+    }
+
+
+    /**
+     * tryReleaseShared setting state changes getState
+     */
+    public void testGetStateWithReleaseShared() {
+	final BooleanLatch l = new BooleanLatch();
+	assertFalse(l.isSignalled());
+	l.releaseShared(0);
+	assertTrue(l.isSignalled());
+    }
+
+    /**
+     * releaseShared has no effect when already signalled
+     */
+    public void testReleaseShared() {
+	final BooleanLatch l = new BooleanLatch();
+	assertFalse(l.isSignalled());
+	l.releaseShared(0);
+	assertTrue(l.isSignalled());
+	l.releaseShared(0);
+	assertTrue(l.isSignalled());
+    }
+
+    /**
+     * acquireSharedInterruptibly returns after release, but not before
+     */
+    public void testAcquireSharedInterruptibly() {
+	final BooleanLatch l = new BooleanLatch();
+
+	Thread t = new Thread(new Runnable() {
+		public void run() {
+		    try {
+                        threadAssertFalse(l.isSignalled());
+			l.acquireSharedInterruptibly(0);
+                        threadAssertTrue(l.isSignalled());
+		    } catch(InterruptedException e){
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+	try {
+            t.start();
+            assertFalse(l.isSignalled());
+            Thread.sleep(SHORT_DELAY_MS);
+            l.releaseShared(0);
+            assertTrue(l.isSignalled());
+            t.join();
+        } catch (InterruptedException e){
+            unexpectedException();
+        }
+    }
+    
+
+    /**
+     * acquireSharedTimed returns after release
+     */
+    public void testAsquireSharedTimed() {
+	final BooleanLatch l = new BooleanLatch();
+
+	Thread t = new Thread(new Runnable() {
+		public void run() {
+		    try {
+                        threadAssertFalse(l.isSignalled());
+			threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000));
+                        threadAssertTrue(l.isSignalled());
+
+		    } catch(InterruptedException e){
+                        threadUnexpectedException();
+                    }
+		}
+	    });
+	try {
+            t.start();
+            assertFalse(l.isSignalled());
+            Thread.sleep(SHORT_DELAY_MS);
+            l.releaseShared(0);
+            assertTrue(l.isSignalled());
+            t.join();
+        } catch (InterruptedException e){
+            unexpectedException();
+        }
+    }
+    
+    /**
+     * acquireSharedInterruptibly throws IE if interrupted before released
+     */
+    public void testAcquireSharedInterruptibly_InterruptedException() {
+        final BooleanLatch l = new BooleanLatch();
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        threadAssertFalse(l.isSignalled());
+                        l.acquireSharedInterruptibly(0);
+                        threadShouldThrow();
+                    } catch(InterruptedException success){}
+                }
+            });
+	t.start();
+	try {
+            assertFalse(l.isSignalled());
+            t.interrupt();
+            t.join();
+        } catch (InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * acquireSharedTimed throws IE if interrupted before released
+     */
+    public void testAcquireSharedNanos_InterruptedException() {
+        final BooleanLatch l = new BooleanLatch();
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        threadAssertFalse(l.isSignalled());
+                        l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000);
+                        threadShouldThrow();                        
+                    } catch(InterruptedException success){}
+                }
+            });
+        t.start();
+        try {
+            Thread.sleep(SHORT_DELAY_MS);
+            assertFalse(l.isSignalled());
+            t.interrupt();
+            t.join();
+        } catch (InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * acquireSharedTimed times out if not released before timeout
+     */
+    public void testAcquireSharedNanos_Timeout() {
+        final BooleanLatch l = new BooleanLatch();
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        threadAssertFalse(l.isSignalled());
+                        threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000));
+                    } catch(InterruptedException ie){
+                        threadUnexpectedException();
+                    }
+                }
+            });
+        t.start();
+        try {
+            Thread.sleep(SHORT_DELAY_MS);
+            assertFalse(l.isSignalled());
+            t.join();
+        } catch (InterruptedException e){
+            unexpectedException();
+        }
+    }
+
+    
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AbstractQueuedSynchronizerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ArrayBlockingQueueTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ArrayBlockingQueueTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ArrayBlockingQueueTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ArrayBlockingQueueTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,1044 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/licenses/publicdomain
+ * Other contributors include Andrew Wright, Jeffrey Hayes, 
+ * Pat Fisher, Mike Judd. 
+ */
+
+
+import junit.framework.*;
+import java.util.*;
+import java.util.concurrent.*;
+import java.io.*;
+
+public class ArrayBlockingQueueTest extends JSR166TestCase {
+    public static void main(String[] args) {
+	junit.textui.TestRunner.run (suite());	
+    }
+    public static Test suite() {
+	return new TestSuite(ArrayBlockingQueueTest.class);
+    }
+
+    /**
+     * Create a queue of given size containing consecutive
+     * Integers 0 ... n.
+     */
+    private ArrayBlockingQueue populatedQueue(int n) {
+        ArrayBlockingQueue q = new ArrayBlockingQueue(n);
+        assertTrue(q.isEmpty());
+	for(int i = 0; i < n; i++)
+	    assertTrue(q.offer(new Integer(i)));
+        assertFalse(q.isEmpty());
+        assertEquals(0, q.remainingCapacity());
+	assertEquals(n, q.size());
+        return q;
+    }
+ 
+    /**
+     * A new queue has the indicated capacity
+     */
+    public void testConstructor1() {
+        assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity());
+    }
+
+    /**
+     * Constructor throws IAE if  capacity argument nonpositive
+     */
+    public void testConstructor2() {
+        try {
+            ArrayBlockingQueue q = new ArrayBlockingQueue(0);
+            shouldThrow();
+        }
+        catch (IllegalArgumentException success) {}
+    }
+
+    /**
+     * Initializing from null Collection throws NPE
+     */
+    public void testConstructor3() {
+        try {
+            ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
+            shouldThrow();
+        }
+        catch (NullPointerException success) {}
+    }
+
+    /**
+     * Initializing from Collection of null elements throws NPE
+     */
+    public void testConstructor4() {
+        try {
+            Integer[] ints = new Integer[SIZE];
+            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
+            shouldThrow();
+        }
+        catch (NullPointerException success) {}
+    }
+
+    /**
+     * Initializing from Collection with some null elements throws NPE
+     */
+    public void testConstructor5() {
+        try {
+            Integer[] ints = new Integer[SIZE];
+            for (int i = 0; i < SIZE-1; ++i)
+                ints[i] = new Integer(i);
+            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
+            shouldThrow();
+        }
+        catch (NullPointerException success) {}
+    }
+
+    /**
+     * Initializing from too large collection throws IAE
+     */
+    public void testConstructor6() {
+        try {
+            Integer[] ints = new Integer[SIZE];
+            for (int i = 0; i < SIZE; ++i)
+                ints[i] = new Integer(i);
+            ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
+            shouldThrow();
+        }
+        catch (IllegalArgumentException success) {}
+    }
+
+    /**
+     * Queue contains all elements of collection used to initialize
+     */
+    public void testConstructor7() {
+        try {
+            Integer[] ints = new Integer[SIZE];
+            for (int i = 0; i < SIZE; ++i)
+                ints[i] = new Integer(i);
+            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
+            for (int i = 0; i < SIZE; ++i)
+                assertEquals(ints[i], q.poll());
+        }
+        finally {}
+    }
+
+    /**
+     * Queue transitions from empty to full when elements added
+     */
+    public void testEmptyFull() {
+        ArrayBlockingQueue q = new ArrayBlockingQueue(2);
+        assertTrue(q.isEmpty());
+        assertEquals(2, q.remainingCapacity());
+        q.add(one);
+        assertFalse(q.isEmpty());
+        q.add(two);
+        assertFalse(q.isEmpty());
+        assertEquals(0, q.remainingCapacity());
+        assertFalse(q.offer(three));
+    }
+
+    /**
+     * remainingCapacity decreases on add, increases on remove
+     */
+    public void testRemainingCapacity() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+        for (int i = 0; i < SIZE; ++i) {
+            assertEquals(i, q.remainingCapacity());
+            assertEquals(SIZE-i, q.size());
+            q.remove();
+        }
+        for (int i = 0; i < SIZE; ++i) {
+            assertEquals(SIZE-i, q.remainingCapacity());
+            assertEquals(i, q.size());
+            q.add(new Integer(i));
+        }
+    }
+
+    /**
+     *  offer(null) throws NPE
+     */
+    public void testOfferNull() {
+	try {
+            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
+            q.offer(null);
+            shouldThrow();
+        } catch (NullPointerException success) { }   
+    }
+
+    /**
+     *  add(null) throws NPE
+     */
+    public void testAddNull() {
+	try {
+            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
+            q.add(null);
+            shouldThrow();
+        } catch (NullPointerException success) { }   
+    }
+
+    /**
+     * Offer succeeds if not full; fails if full
+     */
+    public void testOffer() {
+        ArrayBlockingQueue q = new ArrayBlockingQueue(1);
+        assertTrue(q.offer(zero));
+        assertFalse(q.offer(one));
+    }
+
+    /**
+     * add succeeds if not full; throws ISE if full
+     */
+    public void testAdd() {
+	try {
+            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
+            for (int i = 0; i < SIZE; ++i) {
+                assertTrue(q.add(new Integer(i)));
+            }
+            assertEquals(0, q.remainingCapacity());
+            q.add(new Integer(SIZE));
+        } catch (IllegalStateException success){
+	}   
+    }
+
+    /**
+     *  addAll(null) throws NPE
+     */
+    public void testAddAll1() {
+        try {
+            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
+            q.addAll(null);
+            shouldThrow();
+        }
+        catch (NullPointerException success) {}
+    }
+
+    /**
+     * addAll(this) throws IAE
+     */
+    public void testAddAllSelf() {
+        try {
+            ArrayBlockingQueue q = populatedQueue(SIZE);
+            q.addAll(q);
+            shouldThrow();
+        }
+        catch (IllegalArgumentException success) {}
+    }
+
+
+    /**
+     *  addAll of a collection with null elements throws NPE
+     */
+    public void testAddAll2() {
+        try {
+            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
+            Integer[] ints = new Integer[SIZE];
+            q.addAll(Arrays.asList(ints));
+            shouldThrow();
+        }
+        catch (NullPointerException success) {}
+    }
+    /**
+     * addAll of a collection with any null elements throws NPE after
+     * possibly adding some elements
+     */
+    public void testAddAll3() {
+        try {
+            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
+            Integer[] ints = new Integer[SIZE];
+            for (int i = 0; i < SIZE-1; ++i)
+                ints[i] = new Integer(i);
+            q.addAll(Arrays.asList(ints));
+            shouldThrow();
+        }
+        catch (NullPointerException success) {}
+    }
+    /**
+     * addAll throws ISE if not enough room
+     */
+    public void testAddAll4() {
+        try {
+            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
+            Integer[] ints = new Integer[SIZE];
+            for (int i = 0; i < SIZE; ++i)
+                ints[i] = new Integer(i);
+            q.addAll(Arrays.asList(ints));
+            shouldThrow();
+        }
+        catch (IllegalStateException success) {}
+    }
+    /**
+     * Queue contains all elements, in traversal order, of successful addAll
+     */
+    public void testAddAll5() {
+        try {
+            Integer[] empty = new Integer[0];
+            Integer[] ints = new Integer[SIZE];
+            for (int i = 0; i < SIZE; ++i)
+                ints[i] = new Integer(i);
+            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
+            assertFalse(q.addAll(Arrays.asList(empty)));
+            assertTrue(q.addAll(Arrays.asList(ints)));
+            for (int i = 0; i < SIZE; ++i)
+                assertEquals(ints[i], q.poll());
+        }
+        finally {}
+    }
+
+    /**
+     *  put(null) throws NPE
+     */
+     public void testPutNull() {
+	try {
+            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
+            q.put(null);
+            shouldThrow();
+        } 
+        catch (NullPointerException success){
+	}   
+        catch (InterruptedException ie) {
+	    unexpectedException();
+        }
+     }
+
+    /**
+     * all elements successfully put are contained
+     */
+     public void testPut() {
+         try {
+             ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
+             for (int i = 0; i < SIZE; ++i) {
+                 Integer I = new Integer(i);
+                 q.put(I);
+                 assertTrue(q.contains(I));
+             }
+             assertEquals(0, q.remainingCapacity());
+         }
+        catch (InterruptedException ie) {
+	    unexpectedException();
+        }
+    }
+
+    /**
+     * put blocks interruptibly if full
+     */
+    public void testBlockingPut() {
+        final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    int added = 0;
+                    try {
+                        for (int i = 0; i < SIZE; ++i) {
+                            q.put(new Integer(i));
+                            ++added;
+                        }
+                        q.put(new Integer(SIZE));
+                        threadShouldThrow();
+                    } catch (InterruptedException ie){
+                        threadAssertEquals(added, SIZE);
+                    }   
+                }});
+        try { 
+            t.start();
+           Thread.sleep(MEDIUM_DELAY_MS); 
+           t.interrupt();
+           t.join();
+        }
+        catch (InterruptedException ie) {
+	    unexpectedException();
+        }
+    }
+
+    /**
+     * put blocks waiting for take when full
+     */
+    public void testPutWithTake() {
+        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    int added = 0;
+                    try {
+                        q.put(new Object());
+                        ++added;
+                        q.put(new Object());
+                        ++added;
+                        q.put(new Object());
+                        ++added;
+                        q.put(new Object());
+                        ++added;
+			threadShouldThrow();
+                    } catch (InterruptedException e){
+                        threadAssertTrue(added >= 2);
+                    }
+                }
+            });
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            q.take();
+            t.interrupt();
+            t.join();
+        } catch (Exception e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * timed offer times out if full and elements not taken
+     */
+    public void testTimedOffer() {
+        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        q.put(new Object());
+                        q.put(new Object());
+                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
+                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
+			threadShouldThrow();
+                    } catch (InterruptedException success){}
+                }
+            });
+        
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            t.interrupt();
+            t.join();
+        } catch (Exception e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * take retrieves elements in FIFO order
+     */
+    public void testTake() {
+	try {
+            ArrayBlockingQueue q = populatedQueue(SIZE);
+            for (int i = 0; i < SIZE; ++i) {
+                assertEquals(i, ((Integer)q.take()).intValue());
+            }
+        } catch (InterruptedException e){
+	    unexpectedException();
+	}   
+    }
+
+    /**
+     * take blocks interruptibly when empty
+     */
+    public void testTakeFromEmpty() {
+        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        q.take();
+			threadShouldThrow();
+                    } catch (InterruptedException success){ }                
+                }
+            });
+        try {
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            t.interrupt();
+            t.join();
+        } catch (Exception e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * Take removes existing elements until empty, then blocks interruptibly
+     */
+    public void testBlockingTake() {
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        ArrayBlockingQueue q = populatedQueue(SIZE);
+                        for (int i = 0; i < SIZE; ++i) {
+                            threadAssertEquals(i, ((Integer)q.take()).intValue());
+                        }
+                        q.take();
+                        threadShouldThrow();
+                    } catch (InterruptedException success){
+                    }   
+                }});
+        try { 
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS); 
+            t.interrupt();
+            t.join();
+        }
+        catch (InterruptedException ie) {
+	    unexpectedException();
+        }
+    }
+
+
+    /**
+     * poll succeeds unless empty
+     */
+    public void testPoll() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+        for (int i = 0; i < SIZE; ++i) {
+            assertEquals(i, ((Integer)q.poll()).intValue());
+        }
+	assertNull(q.poll());
+    }
+
+    /**
+     * timed pool with zero timeout succeeds when non-empty, else times out
+     */
+    public void testTimedPoll0() {
+        try {
+            ArrayBlockingQueue q = populatedQueue(SIZE);
+            for (int i = 0; i < SIZE; ++i) {
+                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
+            }
+            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
+        } catch (InterruptedException e){
+	    unexpectedException();
+	}   
+    }
+
+    /**
+     * timed pool with nonzero timeout succeeds when non-empty, else times out
+     */
+    public void testTimedPoll() {
+        try {
+            ArrayBlockingQueue q = populatedQueue(SIZE);
+            for (int i = 0; i < SIZE; ++i) {
+                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
+            }
+            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+        } catch (InterruptedException e){
+	    unexpectedException();
+	}   
+    }
+
+    /**
+     * Interrupted timed poll throws InterruptedException instead of
+     * returning timeout status
+     */
+    public void testInterruptedTimedPoll() {
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        ArrayBlockingQueue q = populatedQueue(SIZE);
+                        for (int i = 0; i < SIZE; ++i) {
+                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
+                        }
+                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+                    } catch (InterruptedException success){
+                    }   
+                }});
+        try { 
+            t.start();
+            Thread.sleep(SHORT_DELAY_MS); 
+            t.interrupt();
+            t.join();
+        }
+        catch (InterruptedException ie) {
+	    unexpectedException();
+        }
+    }
+
+    /**
+     *  timed poll before a delayed offer fails; after offer succeeds;
+     *  on interruption throws
+     */
+    public void testTimedPollWithOffer() {
+        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
+                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
+			threadShouldThrow();
+                    } catch (InterruptedException success) { }                
+                }
+            });
+        try {
+            t.start();
+            Thread.sleep(SMALL_DELAY_MS);
+            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+            t.interrupt();
+            t.join();
+        } catch (Exception e){
+            unexpectedException();
+        }
+    }  
+
+
+    /**
+     * peek returns next element, or null if empty
+     */
+    public void testPeek() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+        for (int i = 0; i < SIZE; ++i) {
+            assertEquals(i, ((Integer)q.peek()).intValue());
+            q.poll();
+            assertTrue(q.peek() == null ||
+                       i != ((Integer)q.peek()).intValue());
+        }
+	assertNull(q.peek());
+    }
+
+    /**
+     * element returns next element, or throws NSEE if empty
+     */
+    public void testElement() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+        for (int i = 0; i < SIZE; ++i) {
+            assertEquals(i, ((Integer)q.element()).intValue());
+            q.poll();
+        }
+        try {
+            q.element();
+            shouldThrow();
+        }
+        catch (NoSuchElementException success) {}
+    }
+
+    /**
+     * remove removes next element, or throws NSEE if empty
+     */
+    public void testRemove() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+        for (int i = 0; i < SIZE; ++i) {
+            assertEquals(i, ((Integer)q.remove()).intValue());
+        }
+        try {
+            q.remove();
+            shouldThrow();
+        } catch (NoSuchElementException success){
+	}   
+    }
+
+    /**
+     * remove(x) removes x and returns true if present
+     */
+    public void testRemoveElement() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+        for (int i = 1; i < SIZE; i+=2) {
+            assertTrue(q.remove(new Integer(i)));
+        }
+        for (int i = 0; i < SIZE; i+=2) {
+            assertTrue(q.remove(new Integer(i)));
+            assertFalse(q.remove(new Integer(i+1)));
+        }
+        assertTrue(q.isEmpty());
+    }
+	
+    /**
+     * contains(x) reports true when elements added but not yet removed
+     */
+    public void testContains() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+        for (int i = 0; i < SIZE; ++i) {
+            assertTrue(q.contains(new Integer(i)));
+            q.poll();
+            assertFalse(q.contains(new Integer(i)));
+        }
+    }
+
+    /**
+     * clear removes all elements
+     */
+    public void testClear() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+        q.clear();
+        assertTrue(q.isEmpty());
+        assertEquals(0, q.size());
+        assertEquals(SIZE, q.remainingCapacity());
+        q.add(one);
+        assertFalse(q.isEmpty());
+        q.clear();
+        assertTrue(q.isEmpty());
+    }
+
+    /**
+     * containsAll(c) is true when c contains a subset of elements
+     */
+    public void testContainsAll() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+        ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE);
+        for (int i = 0; i < SIZE; ++i) {
+            assertTrue(q.containsAll(p));
+            assertFalse(p.containsAll(q));
+            p.add(new Integer(i));
+        }
+        assertTrue(p.containsAll(q));
+    }
+
+    /**
+     * retainAll(c) retains only those elements of c and reports true if changed
+     */
+    public void testRetainAll() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+        ArrayBlockingQueue p = populatedQueue(SIZE);
+        for (int i = 0; i < SIZE; ++i) {
+            boolean changed = q.retainAll(p);
+            if (i == 0)
+                assertFalse(changed);
+            else
+                assertTrue(changed);
+
+            assertTrue(q.containsAll(p));
+            assertEquals(SIZE-i, q.size());
+            p.remove();
+        }
+    }
+
+    /**
+     * removeAll(c) removes only those elements of c and reports true if changed
+     */
+    public void testRemoveAll() {
+        for (int i = 1; i < SIZE; ++i) {
+            ArrayBlockingQueue q = populatedQueue(SIZE);
+            ArrayBlockingQueue p = populatedQueue(i);
+            assertTrue(q.removeAll(p));
+            assertEquals(SIZE-i, q.size());
+            for (int j = 0; j < i; ++j) {
+                Integer I = (Integer)(p.remove());
+                assertFalse(q.contains(I));
+            }
+        }
+    }
+
+    /**
+     *  toArray contains all elements
+     */
+    public void testToArray() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+	Object[] o = q.toArray();
+	try {
+	for(int i = 0; i < o.length; i++)
+	    assertEquals(o[i], q.take());
+	} catch (InterruptedException e){
+	    unexpectedException();
+	}    
+    }
+
+    /**
+     * toArray(a) contains all elements
+     */
+    public void testToArray2() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+	Integer[] ints = new Integer[SIZE];
+	ints = (Integer[])q.toArray(ints);
+	try {
+	    for(int i = 0; i < ints.length; i++)
+		assertEquals(ints[i], q.take());
+	} catch (InterruptedException e){
+	    unexpectedException();
+	}    
+    }
+
+    /**
+     * toArray(null) throws NPE
+     */
+    public void testToArray_BadArg() {
+	try {
+            ArrayBlockingQueue q = populatedQueue(SIZE);
+	    Object o[] = q.toArray(null);
+	    shouldThrow();
+	} catch(NullPointerException success){}
+    }
+
+    /**
+     * toArray with incompatible array type throws CCE
+     */
+    public void testToArray1_BadArg() {
+	try {
+            ArrayBlockingQueue q = populatedQueue(SIZE);
+	    Object o[] = q.toArray(new String[10] );
+	    shouldThrow();
+	} catch(ArrayStoreException  success){}
+    }
+
+    
+    /**
+     * iterator iterates through all elements
+     */
+    public void testIterator() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+	Iterator it = q.iterator();
+	try {
+	    while(it.hasNext()){
+		assertEquals(it.next(), q.take());
+	    }
+	} catch (InterruptedException e){
+	    unexpectedException();
+	}    
+    }
+
+    /**
+     * iterator.remove removes current element
+     */
+    public void testIteratorRemove () {
+        final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
+        q.add(two);
+        q.add(one);
+        q.add(three);
+
+        Iterator it = q.iterator();
+        it.next();
+        it.remove();
+        
+        it = q.iterator();
+        assertEquals(it.next(), one);
+        assertEquals(it.next(), three);
+        assertFalse(it.hasNext());
+    }
+
+    /**
+     * iterator ordering is FIFO
+     */
+    public void testIteratorOrdering() {
+        final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
+        q.add(one);
+        q.add(two);
+        q.add(three);
+
+        assertEquals("queue should be full", 0, q.remainingCapacity());
+
+        int k = 0;
+        for (Iterator it = q.iterator(); it.hasNext();) {
+            int i = ((Integer)(it.next())).intValue();
+            assertEquals(++k, i);
+        }
+        assertEquals(3, k);
+    }
+
+    /**
+     * Modifications do not cause iterators to fail
+     */
+    public void testWeaklyConsistentIteration () {
+        final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
+        q.add(one);
+        q.add(two);
+        q.add(three);
+        try {
+            for (Iterator it = q.iterator(); it.hasNext();) {
+                q.remove();
+                it.next();
+            }
+        }
+        catch (ConcurrentModificationException e) {
+            unexpectedException();
+        }
+        assertEquals(0, q.size());
+    }
+
+
+    /**
+     * toString contains toStrings of elements
+     */
+    public void testToString() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+        String s = q.toString();
+        for (int i = 0; i < SIZE; ++i) {
+            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
+        }
+    }        
+
+
+    /**
+     * offer transfers elements across Executor tasks
+     */
+    public void testOfferInExecutor() {
+        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
+        q.add(one);
+        q.add(two);
+        ExecutorService executor = Executors.newFixedThreadPool(2);
+        executor.execute(new Runnable() {
+            public void run() {
+                threadAssertFalse(q.offer(three));
+                try {
+                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
+                    threadAssertEquals(0, q.remainingCapacity());
+                }
+                catch (InterruptedException e) {
+                    threadUnexpectedException();
+                }
+            }
+        });
+
+        executor.execute(new Runnable() {
+            public void run() {
+                try {
+                    Thread.sleep(SMALL_DELAY_MS);
+                    threadAssertEquals(one, q.take());
+                }
+                catch (InterruptedException e) {
+                    threadUnexpectedException();
+                }
+            }
+        });
+        
+        joinPool(executor);
+
+    }
+
+    /**
+     * poll retrieves elements across Executor threads
+     */
+    public void testPollInExecutor() {
+        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
+        ExecutorService executor = Executors.newFixedThreadPool(2);
+        executor.execute(new Runnable() {
+            public void run() {
+                threadAssertNull(q.poll());
+                try {
+                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
+                    threadAssertTrue(q.isEmpty());
+                }
+                catch (InterruptedException e) {
+                    threadUnexpectedException();
+                }
+            }
+        });
+
+        executor.execute(new Runnable() {
+            public void run() {
+                try {
+                    Thread.sleep(SMALL_DELAY_MS);
+                    q.put(one);
+                }
+                catch (InterruptedException e) {
+                    threadUnexpectedException();
+                }
+            }
+        });
+        
+        joinPool(executor);
+    }
+
+    /**
+     * A deserialized serialized queue has same elements in same order
+     */
+    public void testSerialization() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+
+        try {
+            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
+            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
+            out.writeObject(q);
+            out.close();
+
+            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
+            ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
+            assertEquals(q.size(), r.size());
+            while (!q.isEmpty()) 
+                assertEquals(q.remove(), r.remove());
+        } catch(Exception e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * drainTo(null) throws NPE
+     */ 
+    public void testDrainToNull() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+        try {
+            q.drainTo(null);
+            shouldThrow();
+        } catch(NullPointerException success) {
+        }
+    }
+
+    /**
+     * drainTo(this) throws IAE
+     */ 
+    public void testDrainToSelf() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+        try {
+            q.drainTo(q);
+            shouldThrow();
+        } catch(IllegalArgumentException success) {
+        }
+    }
+
+    /**
+     * drainTo(c) empties queue into another collection c
+     */ 
+    public void testDrainTo() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+        ArrayList l = new ArrayList();
+        q.drainTo(l);
+        assertEquals(q.size(), 0);
+        assertEquals(l.size(), SIZE);
+        for (int i = 0; i < SIZE; ++i) 
+            assertEquals(l.get(i), new Integer(i));
+    }
+
+    /**
+     * drainTo empties full queue, unblocking a waiting put.
+     */ 
+    public void testDrainToWithActivePut() {
+        final ArrayBlockingQueue q = populatedQueue(SIZE);
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        q.put(new Integer(SIZE+1));
+                    } catch (InterruptedException ie){ 
+                        threadUnexpectedException();
+                    }
+                }
+            });
+        try {
+            t.start();
+            ArrayList l = new ArrayList();
+            q.drainTo(l);
+            assertTrue(l.size() >= SIZE);
+            for (int i = 0; i < SIZE; ++i) 
+                assertEquals(l.get(i), new Integer(i));
+            t.join();
+            assertTrue(q.size() + l.size() >= SIZE);
+        } catch(Exception e){
+            unexpectedException();
+        }
+    }
+
+    /**
+     * drainTo(null, n) throws NPE
+     */ 
+    public void testDrainToNullN() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+        try {
+            q.drainTo(null, 0);
+            shouldThrow();
+        } catch(NullPointerException success) {
+        }
+    }
+
+    /**
+     * drainTo(this, n) throws IAE
+     */ 
+    public void testDrainToSelfN() {
+        ArrayBlockingQueue q = populatedQueue(SIZE);
+        try {
+            q.drainTo(q, 0);
+            shouldThrow();
+        } catch(IllegalArgumentException success) {
+        }
+    }
+
+    /**
+     * drainTo(c, n) empties first max {n, size} elements of queue into c
+     */ 
+    public void testDrainToN() {
+        for (int i = 0; i < SIZE + 2; ++i) {
+            ArrayBlockingQueue q = populatedQueue(SIZE);
+            ArrayList l = new ArrayList();
+            q.drainTo(l, i);
+            int k = (i < SIZE)? i : SIZE;
+            assertEquals(q.size(), SIZE-k);
+            assertEquals(l.size(), k);
+            for (int j = 0; j < k; ++j) 
+                assertEquals(l.get(j), new Integer(j));
+        }
+    }
+
+
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ArrayBlockingQueueTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicBooleanTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicBooleanTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicBooleanTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicBooleanTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,144 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/licenses/publicdomain
+ * Other contributors include Andrew Wright, Jeffrey Hayes, 
+ * Pat Fisher, Mike Judd. 
+ */
+
+import junit.framework.*;
+import java.util.concurrent.atomic.*;
+import java.io.*;
+
+public class AtomicBooleanTest extends JSR166TestCase {
+    public static void main (String[] args) {
+        junit.textui.TestRunner.run (suite());
+    }
+    public static Test suite() {
+        return new TestSuite(AtomicBooleanTest.class);
+    }
+
+    /**
+     * constructor initializes to given value
+     */
+    public void testConstructor() {
+        AtomicBoolean ai = new AtomicBoolean(true);
+	assertEquals(true,ai.get());
+    }
+
+    /**
+     * default constructed initializes to false
+     */
+    public void testConstructor2() {
+        AtomicBoolean ai = new AtomicBoolean();
+	assertEquals(false,ai.get());
+    }
+
+    /**
+     * get returns the last value set
+     */
+    public void testGetSet() {
+        AtomicBoolean ai = new AtomicBoolean(true);
+	assertEquals(true,ai.get());
+	ai.set(false);
+	assertEquals(false,ai.get());
+	ai.set(true);
+	assertEquals(true,ai.get());
+	
+    }
+
+    /**
+     * compareAndSet succeeds in changing value if equal to expected else fails
+     */
+    public void testCompareAndSet() {
+        AtomicBoolean ai = new AtomicBoolean(true);
+	assertTrue(ai.compareAndSet(true,false));
+	assertEquals(false,ai.get());
+	assertTrue(ai.compareAndSet(false,false));
+	assertEquals(false,ai.get());
+	assertFalse(ai.compareAndSet(true,false));
+	assertFalse((ai.get()));
+	assertTrue(ai.compareAndSet(false,true));
+	assertEquals(true,ai.get());
+    }
+
+    /**
+     * compareAndSet in one thread enables another waiting for value
+     * to succeed
+     */
+    public void testCompareAndSetInMultipleThreads() {
+        final AtomicBoolean ai = new AtomicBoolean(true);
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    while(!ai.compareAndSet(false, true)) Thread.yield();
+                }});
+        try {
+            t.start();
+            assertTrue(ai.compareAndSet(true, false));
+            t.join(LONG_DELAY_MS);
+            assertFalse(t.isAlive());
+        }
+        catch(Exception e) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * repeated weakCompareAndSet succeeds in changing value when equal
+     * to expected 
+     */
+    public void testWeakCompareAndSet() {
+        AtomicBoolean ai = new AtomicBoolean(true);
+	while(!ai.weakCompareAndSet(true,false));
+	assertEquals(false,ai.get());
+	while(!ai.weakCompareAndSet(false,false));
+        assertEquals(false,ai.get());
+        while(!ai.weakCompareAndSet(false,true));
+	assertEquals(true,ai.get());
+    }
+
+    /**
+     * getAndSet returns previous value and sets to given value
+     */
+    public void testGetAndSet() {
+        AtomicBoolean ai = new AtomicBoolean(true);
+	assertEquals(true,ai.getAndSet(false));
+	assertEquals(false,ai.getAndSet(false));
+	assertEquals(false,ai.getAndSet(true));
+	assertEquals(true,ai.get());
+    }
+
+    /**
+     * a deserialized serialized atomic holds same value
+     */
+    public void testSerialization() {
+        AtomicBoolean l = new AtomicBoolean();
+
+        try {
+            l.set(true);
+            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
+            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
+            out.writeObject(l);
+            out.close();
+
+            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
+            AtomicBoolean r = (AtomicBoolean) in.readObject();
+            assertEquals(l.get(), r.get());
+        } catch(Exception e){
+            e.printStackTrace();
+            unexpectedException();
+        }
+    }
+
+    /**
+     * toString returns current value.
+     */ 
+    public void testToString() {
+        AtomicBoolean ai = new AtomicBoolean(); 
+        assertEquals(ai.toString(), Boolean.toString(false));
+        ai.set(true);
+        assertEquals(ai.toString(), Boolean.toString(true));
+    }
+
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicBooleanTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicIntegerArrayTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicIntegerArrayTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicIntegerArrayTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicIntegerArrayTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,338 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/licenses/publicdomain
+ * Other contributors include Andrew Wright, Jeffrey Hayes, 
+ * Pat Fisher, Mike Judd. 
+ */
+
+import junit.framework.*;
+import java.util.concurrent.atomic.*;
+import java.io.*;
+import java.util.*;
+
+public class AtomicIntegerArrayTest extends JSR166TestCase {
+
+    public static void main (String[] args) {
+        junit.textui.TestRunner.run (suite());
+    }
+    public static Test suite() {
+        return new TestSuite(AtomicIntegerArrayTest.class);
+    }
+
+
+    /**
+     * constructor creates array of given size with all elements zero
+     */
+    public void testConstructor() {
+        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
+        for (int i = 0; i < SIZE; ++i) 
+            assertEquals(0,ai.get(i));
+    }
+
+    /**
+     * constructor with null array throws NPE
+     */
+    public void testConstructor2NPE() {
+        try {
+            int[] a = null;
+            AtomicIntegerArray ai = new AtomicIntegerArray(a);
+        } catch (NullPointerException success) {
+        } catch (Exception ex) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * constructor with array is of same size and has all elements
+     */
+    public void testConstructor2() {
+        int[] a = { 17, 3, -42, 99, -7};
+        AtomicIntegerArray ai = new AtomicIntegerArray(a);
+        assertEquals(a.length, ai.length());
+        for (int i = 0; i < a.length; ++i) 
+            assertEquals(a[i], ai.get(i));
+    }
+
+    /**
+     * get and set for out of bound indices throw IndexOutOfBoundsException
+     */
+    public void testIndexing(){
+        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
+        try {
+            ai.get(SIZE);
+        } catch(IndexOutOfBoundsException success){
+        }
+        try {
+            ai.get(-1);
+        } catch(IndexOutOfBoundsException success){
+        }
+        try {
+            ai.set(SIZE, 0);
+        } catch(IndexOutOfBoundsException success){
+        }
+        try {
+            ai.set(-1, 0);
+        } catch(IndexOutOfBoundsException success){
+        }
+    }
+
+    /**
+     * get returns the last value set at index
+     */
+    public void testGetSet() {
+        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); 
+        for (int i = 0; i < SIZE; ++i) {
+            ai.set(i, 1);
+            assertEquals(1,ai.get(i));
+            ai.set(i, 2);
+            assertEquals(2,ai.get(i));
+            ai.set(i, -3);
+            assertEquals(-3,ai.get(i));
+        }
+    }
+
+    /**
+     * compareAndSet succeeds in changing value if equal to expected else fails
+     */
+    public void testCompareAndSet() {
+        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); 
+        for (int i = 0; i < SIZE; ++i) {
+            ai.set(i, 1);
+            assertTrue(ai.compareAndSet(i, 1,2));
+            assertTrue(ai.compareAndSet(i, 2,-4));
+            assertEquals(-4,ai.get(i));
+            assertFalse(ai.compareAndSet(i, -5,7));
+            assertFalse((7 == ai.get(i)));
+            assertTrue(ai.compareAndSet(i, -4,7));
+            assertEquals(7,ai.get(i));
+        }
+    }
+
+    /**
+     * compareAndSet in one thread enables another waiting for value
+     * to succeed
+     */
+    public void testCompareAndSetInMultipleThreads() {
+        final AtomicIntegerArray a = new AtomicIntegerArray(1);
+        a.set(0, 1);
+        Thread t = new Thread(new Runnable() {
+                public void run() {
+                    while(!a.compareAndSet(0, 2, 3)) Thread.yield();
+                }});
+        try {
+            t.start();
+            assertTrue(a.compareAndSet(0, 1, 2));
+            t.join(LONG_DELAY_MS);
+            assertFalse(t.isAlive());
+            assertEquals(a.get(0), 3);
+        }
+        catch(Exception e) {
+            unexpectedException();
+        }
+    }
+
+    /**
+     * repeated weakCompareAndSet succeeds in changing value when equal
+     * to expected 
+     */
+    public void testWeakCompareAndSet() {
+        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); 
+        for (int i = 0; i < SIZE; ++i) {
+            ai.set(i, 1);
+            while(!ai.weakCompareAndSet(i, 1,2));
+            while(!ai.weakCompareAndSet(i, 2,-4));
+            assertEquals(-4,ai.get(i));
+            while(!ai.weakCompareAndSet(i, -4,7));
+            assertEquals(7,ai.get(i));
+        }
+    }
+
+    /**
+     *  getAndSet returns previous value and sets to given value at given index
+     */
+    public void testGetAndSet() {
+        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); 
+        for (int i = 0; i < SIZE; ++i) {
+            ai.set(i, 1);
+            assertEquals(1,ai.getAndSet(i,0));
+            assertEquals(0,ai.getAndSet(i,-10));
+            assertEquals(-10,ai.getAndSet(i,1));
+        }
+    }
+
+    /**
+     *  getAndAdd returns previous value and adds given value
+     */
+    public void testGetAndAdd() {
+        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); 
+        for (int i = 0; i < SIZE; ++i) {
+            ai.set(i, 1);
+            assertEquals(1,ai.getAndAdd(i,2));
+            assertEquals(3,ai.get(i));
+            assertEquals(3,ai.getAndAdd(i,-4));
+            assertEquals(-1,ai.get(i));
+        }
+    }
+
+    /**
+     * getAndDecrement returns previous value and decrements
+     */
+    public void testGetAndDecrement() {
+        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); 
+        for (int i = 0; i < SIZE; ++i) {
+            ai.set(i, 1);
+            assertEquals(1,ai.getAndDecrement(i));
+            assertEquals(0,ai.getAndDecrement(i));
+            assertEquals(-1,ai.getAndDecrement(i));
+        }
+    }
+
+    /**
+     * getAndIncrement returns previous value and increments
+     */
+    public void testGetAndIncrement() {
+        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); 
+        for (int i = 0; i < SIZE; ++i) {
+            ai.set(i, 1);
+            assertEquals(1,ai.getAndIncrement(i));
+            assertEquals(2,ai.get(i));
+            ai.set(i,-2);
+            assertEquals(-2,ai.getAndIncrement(i));
+            assertEquals(-1,ai.getAndIncrement(i));
+            assertEquals(0,ai.getAndIncrement(i));
+            assertEquals(1,ai.get(i));
+        }
+    }
+
+    /**
+     *  addAndGet adds given value to current, and returns current value
+     */
+    public void testAddAndGet() {
+        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); 
+        for (int i = 0; i < SIZE; ++i) {
+            ai.set(i, 1);
+            assertEquals(3,ai.addAndGet(i,2));
+            assertEquals(3,ai.get(i));
+            assertEquals(-1,ai.addAndGet(i,-4));
+            assertEquals(-1,ai.get(i));
+        }
+    }
+
+    /**
+     * decrementAndGet decrements and returns current value
+     */
+    public void testDecrementAndGet() {
+        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); 
+        for (int i = 0; i < SIZE; ++i) {
+            ai.set(i, 1);
+            assertEquals(0,ai.decrementAndGet(i));
+            assertEquals(-1,ai.decrementAndGet(i));
+            assertEquals(-2,ai.decrementAndGet(i));
+            assertEquals(-2,ai.get(i));
+        }
+    }
+
+    /**
+     *  incrementAndGet increments and returns current value
+     */
+    public void testIncrementAndGet() {
+        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); 
+        for (int i = 0; i < SIZE; ++i) {
+            ai.set(i, 1);
+            assertEquals(2,ai.incrementAndGet(i));
+            assertEquals(2,ai.get(i));
+            ai.set(i, -2);
+            assertEquals(-1,ai.incrementAndGet(i));
+            assertEquals(0,ai.incrementAndGet(i));
+            assertEquals(1,ai.incrementAndGet(i));
+            assertEquals(1,ai.get(i));
+        }
+    }
+
+    static final int COUNTDOWN = 100000;
+    
+    class Counter implements Runnable {
+        final AtomicIntegerArray ai;
+        volatile int counts;
+        Counter(AtomicIntegerArray a) { ai = a; }
+        public void run() {
+            for (;;) {
+                boolean done = true;
+                for (int i = 0; i < ai.length(); ++i) {
+                    int v = ai.get(i);
+                    threadAssertTrue(v >= 0);
+                    if (v != 0) {
+                        done = false;
+                        if (ai.compareAndSet(i, v, v-1))
+                            ++counts;
+                    }
+                }
+                if (done)
+                    break;
+            }
+        }
+    }
+
+    /**
+     * Multiple threads using same array of counters successfully
+     * update a number of times equal to total count
+     */
+    public void testCountingInMultipleThreads() {
+        try {
+            final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); 
+            for (int i = 0; i < SIZE; ++i) 
+                ai.set(i, COUNTDOWN);
+            Counter c1 = new Counter(ai);
+            Counter c2 = new Counter(ai);
+            Thread t1 = new Thread(c1);
+            Thread t2 = new Thread(c2);
+            t1.start();
+            t2.start();
+            t1.join();
+            t2.join();
+            assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
+        }
+        catch(InterruptedException ie) {
+            unexpectedException();
+        }
+    }
+
+
+    /**
+     * a deserialized serialized array holds same values
+     */
+    public void testSerialization() {
+        AtomicIntegerArray l = new AtomicIntegerArray(SIZE); 
+        for (int i = 0; i < SIZE; ++i) 
+            l.set(i, -i);
+
+        try {
+            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
+            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
+            out.writeObject(l);
+            out.close();
+
+            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
+            AtomicIntegerArray r = (AtomicIntegerArray) in.readObject();
+            for (int i = 0; i < SIZE; ++i) {
+                assertEquals(l.get(i), r.get(i));
+            }
+        } catch(Exception e){
+            e.printStackTrace();
+            unexpectedException();
+        }
+    }
+
+
+    /**
+     * toString returns current value.
+     */ 
+    public void testToString() {
+        int[] a = { 17, 3, -42, 99, -7};
+        AtomicIntegerArray ai = new AtomicIntegerArray(a);
+        assertEquals(Arrays.toString(a), ai.toString());
+    }
+
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicIntegerArrayTest.java
------------------------------------------------------------------------------
    svn:eol-style = native