You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by dj...@apache.org on 2003/12/08 02:00:36 UTC

cvs commit: incubator-geronimo/modules/common/src/test/org/apache/geronimo/common StopWatchTest.java

djencks     2003/12/07 17:00:36

  Modified:    modules/common/src/java/org/apache/geronimo/common
                        StopWatch.java
               modules/common/src/test/org/apache/geronimo/common
                        StopWatchTest.java
  Log:
  fix a bug in average lap time, and provide more info on failures
  
  Revision  Changes    Path
  1.3       +40 -40    incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/StopWatch.java
  
  Index: StopWatch.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/StopWatch.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- StopWatch.java	30 Aug 2003 09:29:37 -0000	1.2
  +++ StopWatch.java	8 Dec 2003 01:00:36 -0000	1.3
  @@ -69,24 +69,24 @@
   {
       /** Total time */
       protected long total = 0;
  -    
  +
       /** Start time */
       protected long start = -1;
  -    
  +
       /** Stop time */
       protected long stop = -1;
  -    
  +
       /** The <i>lap</i> count */
       protected int count = 0;
  -    
  +
       /** Is the watch started */
       protected boolean running = false;
  -    
  +
       /**
        * Default constructor.
        */
       public StopWatch() {}
  -    
  +
       /**
        * Construct a StopWatch.
        *
  @@ -96,7 +96,7 @@
       {
           if (running) start();
       }
  -    
  +
       /**
        * Start the watch.
        *
  @@ -110,7 +110,7 @@
               running = true;
           }
       }
  -    
  +
       /**
        * Start the watch.
        */
  @@ -118,7 +118,7 @@
       {
           start(false);
       }
  -    
  +
       /**
        * Stop the watch.
        *
  @@ -127,7 +127,7 @@
       public long stop()
       {
           long lap = 0;
  -        
  +
           if (running) {
               count++;
               stop = System.currentTimeMillis();
  @@ -135,10 +135,10 @@
               total += lap;
               running = false;
           }
  -        
  +
           return lap;
       }
  -    
  +
       /**
        * Reset the watch.
        */
  @@ -150,7 +150,7 @@
           count = 0;
           running = false;
       }
  -    
  +
       /**
        * Get the <i>lap</i> count.
        *
  @@ -160,7 +160,7 @@
       {
           return count;
       }
  -    
  +
       /**
        * Get the elapsed <i>lap</i> time since the watch was started.
        *
  @@ -178,7 +178,7 @@
               return stop - start;
           }
       }
  -    
  +
       /**
        * Get the average <i>lap</i> time since the watch was started.
        *
  @@ -186,9 +186,9 @@
        */
       public long getAverageLapTime()
       {
  -        return (count == 0) ? 0 : getLapTime() / getLapCount();
  +        return (count == 0) ? 0 : total / getLapCount();
       }
  -    
  +
       /**
        * Get the elapsed time since the watch was created or last reset.
        *
  @@ -206,7 +206,7 @@
               return total;
           }
       }
  -    
  +
       /**
        * Check if the watch is running.
        *
  @@ -216,7 +216,7 @@
       {
           return running;
       }
  -    
  +
       /**
        * Convert to a duration value.
        */
  @@ -224,18 +224,18 @@
       {
           return new Duration(getTime());
       }
  -    
  +
       /**
        * Return a string representation.
        */
       public String toString()
       {
           StringBuffer buff = new StringBuffer();
  -        
  +
           if (running) {
               // the watch has not been stopped
               formatElapsedTime(buff, getTime());
  -            
  +
               // add the current lap time too if there is more than one lap
               if (count >= 1) {
                   buff.append(", count=").append(count);
  @@ -246,7 +246,7 @@
           else {
               // the watch has been stopped
               formatElapsedTime(buff, getTime());
  -            
  +
               // add extra info if there is more than one lap
               if (count > 1) {
                   buff.append(", count=").append(count);
  @@ -254,10 +254,10 @@
                   formatElapsedTime(buff, getAverageLapTime());
               }
           }
  -        
  +
           return buff.toString();
       }
  -    
  +
       private void formatElapsedTime(final StringBuffer buff, final long lapsed)
       {
           buff.append(new Duration(lapsed));
  @@ -275,52 +275,52 @@
           extends StopWatch
       {
           protected StopWatch watch;
  -        
  +
           public Wrapper(final StopWatch watch) {
               this.watch = watch;
           }
  -        
  +
           public void start(final boolean reset) {
               watch.start(reset);
           }
  -        
  +
           public void start() {
               watch.start();
           }
  -        
  +
           public long stop() {
               return watch.stop();
           }
  -        
  +
           public void reset() {
               watch.reset();
           }
  -        
  +
           public long getLapTime() {
               return watch.getLapTime();
           }
  -        
  +
           public long getAverageLapTime() {
               return watch.getAverageLapTime();
           }
  -        
  +
           public int getLapCount() {
               return watch.getLapCount();
           }
  -        
  +
           public long getTime() {
               return watch.getTime();
           }
  -        
  +
           public boolean isRunning() {
               return watch.isRunning();
           }
  -        
  +
           public Duration toDuration()
           {
               return watch.toDuration();
           }
  -        
  +
           public String toString() {
               return watch.toString();
           }
  @@ -370,12 +370,12 @@
               public synchronized boolean isRunning() {
                   return this.watch.isRunning();
               }
  -            
  +
               public synchronized Duration toDuration()
               {
                   return this.watch.toDuration();
               }
  -            
  +
               public synchronized String toString() {
                   return this.watch.toString();
               }
  
  
  
  1.2       +8 -7      incubator-geronimo/modules/common/src/test/org/apache/geronimo/common/StopWatchTest.java
  
  Index: StopWatchTest.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/common/src/test/org/apache/geronimo/common/StopWatchTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StopWatchTest.java	7 Dec 2003 02:54:12 -0000	1.1
  +++ StopWatchTest.java	8 Dec 2003 01:00:36 -0000	1.2
  @@ -90,7 +90,7 @@
           assertFalse(sw.isRunning());
   
           assertEquals(1, sw.getLapCount());
  -        assertTrue(sw.getLapTime() >= 1000L);
  +        assertTrue("Expected more than one second, not " + sw.getLapTime(), sw.getLapTime() >= 1000L);
           assertTrue(sw.getTime() >= 1000L);
           assertEquals(sw.getLapTime(), sw.getAverageLapTime());
       }
  @@ -110,7 +110,7 @@
           assertFalse(sw.isRunning());
   
           assertEquals(1, sw.getLapCount());
  -        assertTrue(sw.getLapTime() >= 1000L);
  +        assertTrue("Expected more than one second, not " + sw.getLapTime(), sw.getLapTime() >= 1000L);
           assertTrue(sw.getTime() >= 1000L);
           assertEquals(sw.getLapTime(), sw.getAverageLapTime());
       }
  @@ -130,7 +130,7 @@
           assertFalse(sw.isRunning());
   
           assertEquals(1, sw.getLapCount());
  -        assertTrue(sw.getLapTime() >= 1000L);
  +        assertTrue("Expected more than one second, not " + sw.getLapTime(), sw.getLapTime() >= 1000L);
           assertTrue(sw.getTime() >= 1000L);
           assertEquals(sw.getLapTime(), sw.getAverageLapTime());
       }
  @@ -146,7 +146,7 @@
           sw.stop();
   
           assertEquals(1, sw.getLapCount());
  -        assertTrue(sw.getLapTime() >= 1000L);
  +        assertTrue("Expected more than one second, not " + sw.getLapTime(), sw.getLapTime() >= 1000L);
           assertTrue(sw.getTime() >= 1000L);
           assertEquals(sw.getLapTime(), sw.getAverageLapTime());
   
  @@ -158,8 +158,9 @@
           sw.stop();
   
           assertEquals(2, sw.getLapCount());
  -        assertTrue(sw.getLapTime() >= 1000L);
  -        assertTrue(sw.getTime() >= 2000L);
  +        assertTrue("Expected more than one second, not " + sw.getLapTime(), sw.getLapTime() >= 1000L);
  +        assertTrue("Expected more than two seconds, not " + sw.getTime(), sw.getTime() >= 2000L);
  +        assertEquals(sw.getTime() / 2, sw.getAverageLapTime());
       }
   
       public void testReset() {