harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hinde...@apache.org
Subject svn commit: r794678 [19/19] - in /harmony/enhanced/classlib/trunk/modules/concurrent/src: main/java/java/util/concurrent/ main/java/java/util/concurrent/atomic/ main/java/java/util/concurrent/locks/ test/java/
Date Thu, 16 Jul 2009 14:01:17 GMT
Modified: harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ReentrantReadWriteLockTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ReentrantReadWriteLockTest.java?rev=794678&r1=794677&r2=794678&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ReentrantReadWriteLockTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ReentrantReadWriteLockTest.java
Thu Jul 16 14:01:15 2009
@@ -126,7 +126,7 @@
     /**
      * getWriteHoldCount returns number of recursive holds
      */
-    public void testGetHoldCount() {
+    public void testGetWriteHoldCount() {
 	ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
 	for(int i = 1; i <= SIZE; i++) {
 	    lock.writeLock().lock();
@@ -137,7 +137,7 @@
 	    assertEquals(i-1,lock.getWriteHoldCount());
 	}
     }
-    
+
 
     /**
      * write-unlocking an unlocked lock throws IllegalMonitorStateException
@@ -169,7 +169,9 @@
         try {
             lock.writeLock().lock();
             t.start();
+            Thread.sleep(SHORT_DELAY_MS);
             t.interrupt();
+            Thread.sleep(SHORT_DELAY_MS);
             lock.writeLock().unlock();
             t.join();
         } catch(Exception e){
@@ -215,7 +217,9 @@
 	    });
         try {
             t.start();
+            Thread.sleep(SHORT_DELAY_MS);
             t.interrupt();
+            Thread.sleep(SHORT_DELAY_MS);
             lock.writeLock().unlock();
             t.join();
         } catch(Exception e){
@@ -376,6 +380,269 @@
         }
     } 
 
+    /**
+     * Read trylock succeeds if write locked by current thread
+     */
+    public void testReadHoldingWriteLock() { 
+	final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+	lock.writeLock().lock();
+        assertTrue(lock.readLock().tryLock());
+        lock.readLock().unlock();
+        lock.writeLock().unlock();
+    } 
+
+    /**
+     * Read lock succeeds if write locked by current thread even if
+     * other threads are waiting for readlock
+     */
+    public void testReadHoldingWriteLock2() { 
+	final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+	lock.writeLock().lock();
+	Thread t1 = new Thread(new Runnable() {
+                public void run() {
+                    lock.readLock().lock();
+                    lock.readLock().unlock();
+		}
+	    });
+	Thread t2 = new Thread(new Runnable() {
+                public void run() {
+                    lock.readLock().lock();
+                    lock.readLock().unlock();
+		}
+	    });
+
+        try {
+            t1.start();
+            t2.start();
+            lock.readLock().lock();
+            lock.readLock().unlock();
+            Thread.sleep(SHORT_DELAY_MS);
+            lock.readLock().lock();
+            lock.readLock().unlock();
+            lock.writeLock().unlock();
+            t1.join(MEDIUM_DELAY_MS);
+            t2.join(MEDIUM_DELAY_MS);
+            assertTrue(!t1.isAlive());
+            assertTrue(!t2.isAlive());
+           
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+    /**
+     *  Read lock succeeds if write locked by current thread even if
+     * other threads are waiting for writelock
+     */
+    public void testReadHoldingWriteLock3() { 
+	final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+	lock.writeLock().lock();
+	Thread t1 = new Thread(new Runnable() {
+                public void run() {
+                    lock.writeLock().lock();
+                    lock.writeLock().unlock();
+		}
+	    });
+	Thread t2 = new Thread(new Runnable() {
+                public void run() {
+                    lock.writeLock().lock();
+                    lock.writeLock().unlock();
+		}
+	    });
+
+        try {
+            t1.start();
+            t2.start();
+            lock.readLock().lock();
+            lock.readLock().unlock();
+            Thread.sleep(SHORT_DELAY_MS);
+            lock.readLock().lock();
+            lock.readLock().unlock();
+            lock.writeLock().unlock();
+            t1.join(MEDIUM_DELAY_MS);
+            t2.join(MEDIUM_DELAY_MS);
+            assertTrue(!t1.isAlive());
+            assertTrue(!t2.isAlive());
+           
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+
+    /**
+     *  Write lock succeeds if write locked by current thread even if
+     * other threads are waiting for writelock
+     */
+    public void testWriteHoldingWriteLock4() { 
+	final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+	lock.writeLock().lock();
+	Thread t1 = new Thread(new Runnable() {
+                public void run() {
+                    lock.writeLock().lock();
+                    lock.writeLock().unlock();
+		}
+	    });
+	Thread t2 = new Thread(new Runnable() {
+                public void run() {
+                    lock.writeLock().lock();
+                    lock.writeLock().unlock();
+		}
+	    });
+
+        try {
+            t1.start();
+            t2.start();
+            lock.writeLock().lock();
+            lock.writeLock().unlock();
+            Thread.sleep(SHORT_DELAY_MS);
+            lock.writeLock().lock();
+            lock.writeLock().unlock();
+            lock.writeLock().unlock();
+            t1.join(MEDIUM_DELAY_MS);
+            t2.join(MEDIUM_DELAY_MS);
+            assertTrue(!t1.isAlive());
+            assertTrue(!t2.isAlive());
+           
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+
+    /**
+     * Fair Read trylock succeeds if write locked by current thread
+     */
+    public void testReadHoldingWriteLockFair() { 
+	final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
+	lock.writeLock().lock();
+        assertTrue(lock.readLock().tryLock());
+        lock.readLock().unlock();
+        lock.writeLock().unlock();
+    } 
+
+    /**
+     * Fair Read lock succeeds if write locked by current thread even if
+     * other threads are waiting for readlock
+     */
+    public void testReadHoldingWriteLockFair2() { 
+	final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
+	lock.writeLock().lock();
+	Thread t1 = new Thread(new Runnable() {
+                public void run() {
+                    lock.readLock().lock();
+                    lock.readLock().unlock();
+		}
+	    });
+	Thread t2 = new Thread(new Runnable() {
+                public void run() {
+                    lock.readLock().lock();
+                    lock.readLock().unlock();
+		}
+	    });
+
+        try {
+            t1.start();
+            t2.start();
+            lock.readLock().lock();
+            lock.readLock().unlock();
+            Thread.sleep(SHORT_DELAY_MS);
+            lock.readLock().lock();
+            lock.readLock().unlock();
+            lock.writeLock().unlock();
+            t1.join(MEDIUM_DELAY_MS);
+            t2.join(MEDIUM_DELAY_MS);
+            assertTrue(!t1.isAlive());
+            assertTrue(!t2.isAlive());
+           
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+
+    /**
+     * Fair Read lock succeeds if write locked by current thread even if
+     * other threads are waiting for writelock
+     */
+    public void testReadHoldingWriteLockFair3() { 
+	final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
+	lock.writeLock().lock();
+	Thread t1 = new Thread(new Runnable() {
+                public void run() {
+                    lock.writeLock().lock();
+                    lock.writeLock().unlock();
+		}
+	    });
+	Thread t2 = new Thread(new Runnable() {
+                public void run() {
+                    lock.writeLock().lock();
+                    lock.writeLock().unlock();
+		}
+	    });
+
+        try {
+            t1.start();
+            t2.start();
+            lock.readLock().lock();
+            lock.readLock().unlock();
+            Thread.sleep(SHORT_DELAY_MS);
+            lock.readLock().lock();
+            lock.readLock().unlock();
+            lock.writeLock().unlock();
+            t1.join(MEDIUM_DELAY_MS);
+            t2.join(MEDIUM_DELAY_MS);
+            assertTrue(!t1.isAlive());
+            assertTrue(!t2.isAlive());
+           
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+
+    /**
+     * Fair Write lock succeeds if write locked by current thread even if
+     * other threads are waiting for writelock
+     */
+    public void testWriteHoldingWriteLockFair4() { 
+	final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
+	lock.writeLock().lock();
+	Thread t1 = new Thread(new Runnable() {
+                public void run() {
+                    lock.writeLock().lock();
+                    lock.writeLock().unlock();
+		}
+	    });
+	Thread t2 = new Thread(new Runnable() {
+                public void run() {
+                    lock.writeLock().lock();
+                    lock.writeLock().unlock();
+		}
+	    });
+
+        try {
+            t1.start();
+            t2.start();
+            Thread.sleep(SHORT_DELAY_MS);
+            assertTrue(lock.isWriteLockedByCurrentThread());
+            assertTrue(lock.getWriteHoldCount() == 1);
+            lock.writeLock().lock();
+            assertTrue(lock.getWriteHoldCount() == 2);
+            lock.writeLock().unlock();
+            lock.writeLock().lock();
+            lock.writeLock().unlock();
+            lock.writeLock().unlock();
+            t1.join(MEDIUM_DELAY_MS);
+            t2.join(MEDIUM_DELAY_MS);
+            assertTrue(!t1.isAlive());
+            assertTrue(!t2.isAlive());
+           
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
 
     /**
      * Read tryLock succeeds if readlocked but not writelocked
@@ -420,6 +687,50 @@
         }
     } 
 
+
+    /**
+     * Fair Read tryLock succeeds if readlocked but not writelocked
+     */
+    public void testTryLockWhenReadLockedFair() { 
+	final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
+	lock.readLock().lock();
+	Thread t = new Thread(new Runnable() {
+                public void run() {
+                    threadAssertTrue(lock.readLock().tryLock());
+                    lock.readLock().unlock();
+		}
+	    });
+        try {
+            t.start();
+            t.join();
+            lock.readLock().unlock();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
+    
+
+    /**
+     * Fair write tryLock fails when readlocked
+     */
+    public void testWriteTryLockWhenReadLockedFair() { 
+	final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
+	lock.readLock().lock();
+	Thread t = new Thread(new Runnable() {
+                public void run() {
+                    threadAssertFalse(lock.writeLock().tryLock());
+		}
+	    });
+        try {
+            t.start();
+            t.join();
+            lock.readLock().unlock();
+        } catch(Exception e){
+            unexpectedException();
+        }
+    } 
+
     
 
     /**
@@ -493,7 +804,9 @@
 	    });
         try {
             t.start();
+            Thread.sleep(SHORT_DELAY_MS);
             t.interrupt();
+            Thread.sleep(SHORT_DELAY_MS);
             t.join();
             lock.writeLock().unlock();
         } catch(Exception e){
@@ -523,6 +836,7 @@
 	    });
         try {
             t.start();
+            Thread.sleep(SHORT_DELAY_MS);
             t.interrupt();
             t.join();
             lock.writeLock().unlock();
@@ -591,7 +905,6 @@
         final Condition c = lock.writeLock().newCondition();
         try {
             lock.writeLock().lock();
-            assertFalse(c.await(10, TimeUnit.MILLISECONDS));
             lock.writeLock().unlock();
         }
         catch (Exception ex) {
@@ -608,7 +921,6 @@
         try {
             lock.writeLock().lock();
             java.util.Date d = new java.util.Date();
-            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
             lock.writeLock().unlock();
         }
         catch (Exception ex) {
@@ -649,32 +961,61 @@
         }
     }
 
+    /** A helper class for uninterruptible wait tests */
+    class UninterruptableThread extends Thread {
+        private Lock lock;
+        private Condition c;
+        
+        public volatile boolean canAwake = false;
+        public volatile boolean interrupted = false;
+        public volatile boolean lockStarted = false;
+        
+        public UninterruptableThread(Lock lock, Condition c) {
+            this.lock = lock;
+            this.c = c;
+        }
+        
+        public synchronized void run() {
+            lock.lock();
+            lockStarted = true;
+            
+            while (!canAwake) {
+                c.awaitUninterruptibly();
+            }
+            
+            interrupted = isInterrupted();
+            lock.unlock();
+        }
+    }
+
     /**
      * awaitUninterruptibly doesn't abort on interrupt
      */
     public void testAwaitUninterruptibly() {
-	final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();	
+        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
         final Condition c = lock.writeLock().newCondition();
-	Thread t = new Thread(new Runnable() { 
-		public void run() {
-                    lock.writeLock().lock();
-                    c.awaitUninterruptibly();
-                    lock.writeLock().unlock();
-		}
-	    });
+        UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c);
 
         try {
-            t.start();
-            Thread.sleep(SHORT_DELAY_MS);
-            t.interrupt();
+            thread.start();
+
+            while (!thread.lockStarted) {
+                Thread.sleep(100);
+            }
+
             lock.writeLock().lock();
-            c.signal();
-            lock.writeLock().unlock();
-            assert(t.isInterrupted());
-            t.join(SHORT_DELAY_MS);
-            assertFalse(t.isAlive());
-        }
-        catch (Exception ex) {
+            try {
+                thread.interrupt();
+                thread.canAwake = true;
+                c.signal();
+            } finally {
+                lock.writeLock().unlock();
+            }
+
+            thread.join();
+            assertTrue(thread.interrupted);
+            assertFalse(thread.isAlive());
+        } catch (Exception ex) {
             unexpectedException();
         }
     }

Modified: harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SynchronousQueueTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SynchronousQueueTest.java?rev=794678&r1=794677&r2=794678&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SynchronousQueueTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/SynchronousQueueTest.java
Thu Jul 16 14:01:15 2009
@@ -755,6 +755,7 @@
             while (!q.isEmpty()) 
                 assertEquals(q.remove(), r.remove());
         } catch(Exception e){
+            e.printStackTrace();
             unexpectedException();
         }
     }

Modified: harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ThreadPoolExecutorTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ThreadPoolExecutorTest.java?rev=794678&r1=794677&r2=794678&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ThreadPoolExecutorTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ThreadPoolExecutorTest.java
Thu Jul 16 14:01:15 2009
@@ -7,6 +7,7 @@
  */
 
 import java.util.concurrent.*;
+import java.util.concurrent.atomic.*;
 import junit.framework.*;
 import java.util.*;
 
@@ -36,6 +37,15 @@
         }
     }
 
+    static class FailingThreadFactory implements ThreadFactory{
+        int calls = 0;
+        public Thread newThread(Runnable r){
+            if (++calls > 1) return null;
+            return new Thread(r);
+        }   
+    }
+    
+
     /**
      *  execute successfully executes a runnable
      */
@@ -340,6 +350,9 @@
             assertSame(q, wq);
             assertFalse(wq.contains(tasks[0]));
             assertTrue(wq.contains(tasks[4]));
+            for (int i = 1; i < 5; ++i)
+                tasks[i].cancel(true);
+            p1.shutdownNow();
         } catch(Exception e) {
             unexpectedException();
         } finally {
@@ -1498,5 +1511,58 @@
         }
     }
 
+    /**
+     * Execution continues if there is at least one thread even if
+     * thread factory fails to create more
+     */
+    public void testFailingThreadFactory() {
+        ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
+        try {
+            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
+            for (int k = 0; k < 100; ++k) {
+                e.execute(new NoOpRunnable());
+            }
+            Thread.sleep(LONG_DELAY_MS);
+        } catch(Exception ex) {
+            unexpectedException();
+        } finally {
+            joinPool(e);
+        }
+    }
 
+    /**
+     * execute allows the same task to be submitted multiple times, even
+     * if rejected
+     */
+    public void testRejectedRecycledTask() {
+        final int nTasks = 1000;
+        final AtomicInteger nRun = new AtomicInteger(0);
+        final Runnable recycledTask = new Runnable() {
+                public void run() {
+                    nRun.getAndIncrement();
+                } };
+        final ThreadPoolExecutor p = 
+            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, 
+                                   new ArrayBlockingQueue(30));
+        try {
+            for (int i = 0; i < nTasks; ++i) {
+                for (;;) {
+                    try {
+                        p.execute(recycledTask);
+                        break;
+                    }
+                    catch (RejectedExecutionException ignore) {
+                    }
+                }
+            }
+            Thread.sleep(5000); // enough time to run all tasks
+            assertEquals(nRun.get(), nTasks);
+        } catch(Exception ex) {
+            ex.printStackTrace();
+            unexpectedException();
+        } finally {
+            p.shutdown();
+        }
+    }
+            
 }

Modified: harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/TimeUnitTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/TimeUnitTest.java?rev=794678&r1=794677&r2=794678&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/TimeUnitTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/TimeUnitTest.java Thu
Jul 16 14:01:15 2009
@@ -20,54 +20,96 @@
 	return new TestSuite(TimeUnitTest.class);
     }
 
+    // (loops to 88888 check increments at all time divisions.)
+
     /**
-     * convert correctly converts sample values across the four units
+     * convert correctly converts sample values across the units
      */
     public void testConvert() {
-        for (long t = 0; t < 10; ++t) {
+        for (long t = 0; t < 88888; ++t) {
+            assertEquals(t*60*60*24, 
+                         TimeUnit.SECONDS.convert(t, 
+                                                  TimeUnit.DAYS));
+            assertEquals(t*60*60, 
+                         TimeUnit.SECONDS.convert(t, 
+                                                  TimeUnit.HOURS));
+            assertEquals(t*60, 
+                         TimeUnit.SECONDS.convert(t, 
+                                                  TimeUnit.MINUTES));
             assertEquals(t, 
                          TimeUnit.SECONDS.convert(t, 
                                                   TimeUnit.SECONDS));
             assertEquals(t, 
-                         TimeUnit.SECONDS.convert(1000 * t, 
+                         TimeUnit.SECONDS.convert(1000L*t, 
                                                   TimeUnit.MILLISECONDS));
             assertEquals(t, 
-                         TimeUnit.SECONDS.convert(1000000 * t, 
+                         TimeUnit.SECONDS.convert(1000000L*t, 
                                                   TimeUnit.MICROSECONDS));
             assertEquals(t, 
-                         TimeUnit.SECONDS.convert(1000000000 * t, 
+                         TimeUnit.SECONDS.convert(1000000000L*t, 
                                                   TimeUnit.NANOSECONDS));
-            assertEquals(1000 * t, 
+
+
+            assertEquals(1000L*t*60*60*24, 
+                         TimeUnit.MILLISECONDS.convert(t, 
+                                                  TimeUnit.DAYS));
+            assertEquals(1000L*t*60*60, 
+                         TimeUnit.MILLISECONDS.convert(t, 
+                                                  TimeUnit.HOURS));
+            assertEquals(1000L*t*60, 
+                         TimeUnit.MILLISECONDS.convert(t, 
+                                                  TimeUnit.MINUTES));
+            assertEquals(1000L*t, 
                          TimeUnit.MILLISECONDS.convert(t, 
                                                   TimeUnit.SECONDS));
             assertEquals(t, 
                          TimeUnit.MILLISECONDS.convert(t, 
                                                   TimeUnit.MILLISECONDS));
             assertEquals(t, 
-                         TimeUnit.MILLISECONDS.convert(1000 * t, 
+                         TimeUnit.MILLISECONDS.convert(1000L*t, 
                                                   TimeUnit.MICROSECONDS));
             assertEquals(t, 
-                         TimeUnit.MILLISECONDS.convert(1000000 * t, 
+                         TimeUnit.MILLISECONDS.convert(1000000L*t, 
                                                   TimeUnit.NANOSECONDS));
-            assertEquals(1000000 * t, 
+
+            assertEquals(1000000L*t*60*60*24, 
+                         TimeUnit.MICROSECONDS.convert(t, 
+                                                  TimeUnit.DAYS));
+            assertEquals(1000000L*t*60*60, 
+                         TimeUnit.MICROSECONDS.convert(t, 
+                                                  TimeUnit.HOURS));
+            assertEquals(1000000L*t*60, 
+                         TimeUnit.MICROSECONDS.convert(t, 
+                                                  TimeUnit.MINUTES));
+            assertEquals(1000000L*t, 
                          TimeUnit.MICROSECONDS.convert(t, 
                                                   TimeUnit.SECONDS));
-            assertEquals(1000 * t, 
+            assertEquals(1000L*t, 
                          TimeUnit.MICROSECONDS.convert(t, 
                                                   TimeUnit.MILLISECONDS));
             assertEquals(t, 
                          TimeUnit.MICROSECONDS.convert(t, 
                                                   TimeUnit.MICROSECONDS));
             assertEquals(t, 
-                         TimeUnit.MICROSECONDS.convert(1000 * t, 
+                         TimeUnit.MICROSECONDS.convert(1000L*t, 
                                                   TimeUnit.NANOSECONDS));
-            assertEquals(1000000000 * t, 
+
+            assertEquals(1000000000L*t*60*60*24, 
+                         TimeUnit.NANOSECONDS.convert(t, 
+                                                  TimeUnit.DAYS));
+            assertEquals(1000000000L*t*60*60, 
+                         TimeUnit.NANOSECONDS.convert(t, 
+                                                  TimeUnit.HOURS));
+            assertEquals(1000000000L*t*60, 
+                         TimeUnit.NANOSECONDS.convert(t, 
+                                                  TimeUnit.MINUTES));
+            assertEquals(1000000000L*t, 
                          TimeUnit.NANOSECONDS.convert(t, 
                                                   TimeUnit.SECONDS));
-            assertEquals(1000000 * t, 
+            assertEquals(1000000L*t, 
                          TimeUnit.NANOSECONDS.convert(t, 
                                                   TimeUnit.MILLISECONDS));
-            assertEquals(1000 * t, 
+            assertEquals(1000L*t, 
                          TimeUnit.NANOSECONDS.convert(t, 
                                                   TimeUnit.MICROSECONDS));
             assertEquals(t, 
@@ -81,13 +123,18 @@
      * nanoseconds
      */
     public void testToNanos() {
-        for (long t = 0; t < 10; ++t) {
-            assertEquals(1000000000 * t, 
+        for (long t = 0; t < 88888; ++t) {
+            assertEquals(t*1000000000L*60*60*24, 
+                         TimeUnit.DAYS.toNanos(t));
+            assertEquals(t*1000000000L*60*60, 
+                         TimeUnit.HOURS.toNanos(t));
+            assertEquals(t*1000000000L*60, 
+                         TimeUnit.MINUTES.toNanos(t));
+            assertEquals(1000000000L*t, 
                          TimeUnit.SECONDS.toNanos(t));
-
-            assertEquals(1000000 * t, 
+            assertEquals(1000000L*t, 
                          TimeUnit.MILLISECONDS.toNanos(t));
-            assertEquals(1000 * t, 
+            assertEquals(1000L*t, 
                          TimeUnit.MICROSECONDS.toNanos(t));
             assertEquals(t, 
                          TimeUnit.NANOSECONDS.toNanos(t));
@@ -99,16 +146,21 @@
      * microseconds
      */
     public void testToMicros() {
-        for (long t = 0; t < 10; ++t) {
-            assertEquals(1000000 * t, 
+        for (long t = 0; t < 88888; ++t) {
+            assertEquals(t*1000000L*60*60*24, 
+                         TimeUnit.DAYS.toMicros(t));
+            assertEquals(t*1000000L*60*60, 
+                         TimeUnit.HOURS.toMicros(t));
+            assertEquals(t*1000000L*60, 
+                         TimeUnit.MINUTES.toMicros(t));
+            assertEquals(1000000L*t, 
                          TimeUnit.SECONDS.toMicros(t));
-
-            assertEquals(1000 * t, 
+            assertEquals(1000L*t, 
                          TimeUnit.MILLISECONDS.toMicros(t));
             assertEquals(t, 
                          TimeUnit.MICROSECONDS.toMicros(t));
             assertEquals(t, 
-                         TimeUnit.NANOSECONDS.toMicros(t * 1000));
+                         TimeUnit.NANOSECONDS.toMicros(t*1000L));
         }
     }
 
@@ -117,16 +169,21 @@
      * milliseconds
      */
     public void testToMillis() {
-        for (long t = 0; t < 10; ++t) {
-            assertEquals(1000 * t, 
+        for (long t = 0; t < 88888; ++t) {
+            assertEquals(t*1000L*60*60*24, 
+                         TimeUnit.DAYS.toMillis(t));
+            assertEquals(t*1000L*60*60, 
+                         TimeUnit.HOURS.toMillis(t));
+            assertEquals(t*1000L*60, 
+                         TimeUnit.MINUTES.toMillis(t));
+            assertEquals(1000L*t, 
                          TimeUnit.SECONDS.toMillis(t));
-
             assertEquals(t, 
                          TimeUnit.MILLISECONDS.toMillis(t));
             assertEquals(t, 
-                         TimeUnit.MICROSECONDS.toMillis(t * 1000));
+                         TimeUnit.MICROSECONDS.toMillis(t*1000L));
             assertEquals(t, 
-                         TimeUnit.NANOSECONDS.toMillis(t * 1000000));
+                         TimeUnit.NANOSECONDS.toMillis(t*1000000L));
         }
     }
 
@@ -135,20 +192,24 @@
      * seconds
      */
     public void testToSeconds() {
-        for (long t = 0; t < 10; ++t) {
+        for (long t = 0; t < 88888; ++t) {
+            assertEquals(t*60*60*24, 
+                         TimeUnit.DAYS.toSeconds(t));
+            assertEquals(t*60*60, 
+                         TimeUnit.HOURS.toSeconds(t));
+            assertEquals(t*60, 
+                         TimeUnit.MINUTES.toSeconds(t));
             assertEquals(t, 
                          TimeUnit.SECONDS.toSeconds(t));
-
             assertEquals(t, 
-                         TimeUnit.MILLISECONDS.toSeconds(t * 1000));
+                         TimeUnit.MILLISECONDS.toSeconds(t*1000L));
             assertEquals(t, 
-                         TimeUnit.MICROSECONDS.toSeconds(t * 1000000));
+                         TimeUnit.MICROSECONDS.toSeconds(t*1000000L));
             assertEquals(t, 
-                         TimeUnit.NANOSECONDS.toSeconds(t * 1000000000));
+                         TimeUnit.NANOSECONDS.toSeconds(t*1000000000L));
         }
     }
 
-
     /**
      * convert saturates positive too-large values to Long.MAX_VALUE 
      * and negative to LONG.MIN_VALUE
@@ -160,6 +221,25 @@
         assertEquals(Long.MIN_VALUE,
                      TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
                                                   TimeUnit.SECONDS));
+        assertEquals(Long.MAX_VALUE,
+                     TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
+                                                  TimeUnit.MINUTES));
+        assertEquals(Long.MIN_VALUE,
+                     TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
+                                                  TimeUnit.MINUTES));
+        assertEquals(Long.MAX_VALUE,
+                     TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
+                                                  TimeUnit.HOURS));
+        assertEquals(Long.MIN_VALUE,
+                     TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
+                                                  TimeUnit.HOURS));
+        assertEquals(Long.MAX_VALUE,
+                     TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
+                                                  TimeUnit.DAYS));
+        assertEquals(Long.MIN_VALUE,
+                     TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
+                                                  TimeUnit.DAYS));
+
     }
 
     /**



Mime
View raw message