Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2008 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.base;
     18 
     19 import static java.util.concurrent.TimeUnit.MICROSECONDS;
     20 import static java.util.concurrent.TimeUnit.MILLISECONDS;
     21 import static java.util.concurrent.TimeUnit.NANOSECONDS;
     22 
     23 import com.google.common.annotations.GwtCompatible;
     24 import com.google.common.annotations.GwtIncompatible;
     25 import com.google.common.testing.FakeTicker;
     26 
     27 import junit.framework.TestCase;
     28 
     29 /**
     30  * Unit test for {@link Stopwatch}.
     31  *
     32  * @author Kevin Bourrillion
     33  */
     34 @GwtCompatible(emulated = true)
     35 public class StopwatchTest extends TestCase {
     36 
     37   private final FakeTicker ticker = new FakeTicker();
     38   private final Stopwatch stopwatch = new Stopwatch(ticker);
     39 
     40   public void testCreateStarted() {
     41     Stopwatch startedStopwatch = Stopwatch.createStarted();
     42     assertTrue(startedStopwatch.isRunning());
     43   }
     44 
     45   public void testCreateUnstarted() {
     46     Stopwatch unstartedStopwatch = Stopwatch.createUnstarted();
     47     assertFalse(unstartedStopwatch.isRunning());
     48     assertEquals(0, unstartedStopwatch.elapsed(NANOSECONDS));
     49   }
     50 
     51   public void testInitialState() {
     52     assertFalse(stopwatch.isRunning());
     53     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
     54   }
     55 
     56   public void testStart() {
     57     assertSame(stopwatch, stopwatch.start());
     58     assertTrue(stopwatch.isRunning());
     59   }
     60 
     61   public void testStart_whileRunning() {
     62     stopwatch.start();
     63     try {
     64       stopwatch.start();
     65       fail();
     66     } catch (IllegalStateException expected) {
     67     }
     68     assertTrue(stopwatch.isRunning());
     69   }
     70 
     71   public void testStop() {
     72     stopwatch.start();
     73     assertSame(stopwatch, stopwatch.stop());
     74     assertFalse(stopwatch.isRunning());
     75   }
     76 
     77   public void testStop_new() {
     78     try {
     79       stopwatch.stop();
     80       fail();
     81     } catch (IllegalStateException expected) {
     82     }
     83     assertFalse(stopwatch.isRunning());
     84   }
     85 
     86   public void testStop_alreadyStopped() {
     87     stopwatch.start();
     88     stopwatch.stop();
     89     try {
     90       stopwatch.stop();
     91       fail();
     92     } catch (IllegalStateException expected) {
     93     }
     94     assertFalse(stopwatch.isRunning());
     95   }
     96 
     97   public void testReset_new() {
     98     ticker.advance(1);
     99     stopwatch.reset();
    100     assertFalse(stopwatch.isRunning());
    101     ticker.advance(2);
    102     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
    103     stopwatch.start();
    104     ticker.advance(3);
    105     assertEquals(3, stopwatch.elapsed(NANOSECONDS));
    106   }
    107 
    108   public void testReset_whileRunning() {
    109     ticker.advance(1);
    110     stopwatch.start();
    111     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
    112     ticker.advance(2);
    113     assertEquals(2, stopwatch.elapsed(NANOSECONDS));
    114     stopwatch.reset();
    115     assertFalse(stopwatch.isRunning());
    116     ticker.advance(3);
    117     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
    118   }
    119 
    120   public void testElapsed_whileRunning() {
    121     ticker.advance(78);
    122     stopwatch.start();
    123     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
    124 
    125     ticker.advance(345);
    126     assertEquals(345, stopwatch.elapsed(NANOSECONDS));
    127   }
    128 
    129   public void testElapsed_notRunning() {
    130     ticker.advance(1);
    131     stopwatch.start();
    132     ticker.advance(4);
    133     stopwatch.stop();
    134     ticker.advance(9);
    135     assertEquals(4, stopwatch.elapsed(NANOSECONDS));
    136   }
    137 
    138   public void testElapsed_multipleSegments() {
    139     stopwatch.start();
    140     ticker.advance(9);
    141     stopwatch.stop();
    142 
    143     ticker.advance(16);
    144 
    145     stopwatch.start();
    146     assertEquals(9, stopwatch.elapsed(NANOSECONDS));
    147     ticker.advance(25);
    148     assertEquals(34, stopwatch.elapsed(NANOSECONDS));
    149 
    150     stopwatch.stop();
    151     ticker.advance(36);
    152     assertEquals(34, stopwatch.elapsed(NANOSECONDS));
    153   }
    154 
    155   public void testElapsed_micros() {
    156     stopwatch.start();
    157     ticker.advance(999);
    158     assertEquals(0, stopwatch.elapsed(MICROSECONDS));
    159     ticker.advance(1);
    160     assertEquals(1, stopwatch.elapsed(MICROSECONDS));
    161   }
    162 
    163   public void testElapsed_millis() {
    164     stopwatch.start();
    165     ticker.advance(999999);
    166     assertEquals(0, stopwatch.elapsed(MILLISECONDS));
    167     ticker.advance(1);
    168     assertEquals(1, stopwatch.elapsed(MILLISECONDS));
    169   }
    170 
    171   public void testElapsedMillis() {
    172     stopwatch.start();
    173     ticker.advance(999999);
    174     assertEquals(0, stopwatch.elapsed(MILLISECONDS));
    175     ticker.advance(1);
    176     assertEquals(1, stopwatch.elapsed(MILLISECONDS));
    177   }
    178 
    179   public void testElapsedMillis_whileRunning() {
    180     ticker.advance(78000000);
    181     stopwatch.start();
    182     assertEquals(0, stopwatch.elapsed(MILLISECONDS));
    183 
    184     ticker.advance(345000000);
    185     assertEquals(345, stopwatch.elapsed(MILLISECONDS));
    186   }
    187 
    188   public void testElapsedMillis_notRunning() {
    189     ticker.advance(1000000);
    190     stopwatch.start();
    191     ticker.advance(4000000);
    192     stopwatch.stop();
    193     ticker.advance(9000000);
    194     assertEquals(4, stopwatch.elapsed(MILLISECONDS));
    195   }
    196 
    197   public void testElapsedMillis_multipleSegments() {
    198     stopwatch.start();
    199     ticker.advance(9000000);
    200     stopwatch.stop();
    201 
    202     ticker.advance(16000000);
    203 
    204     stopwatch.start();
    205     assertEquals(9, stopwatch.elapsed(MILLISECONDS));
    206     ticker.advance(25000000);
    207     assertEquals(34, stopwatch.elapsed(MILLISECONDS));
    208 
    209     stopwatch.stop();
    210     ticker.advance(36000000);
    211     assertEquals(34, stopwatch.elapsed(MILLISECONDS));
    212   }
    213 
    214   @GwtIncompatible("String.format()")
    215   public void testToString() {
    216     stopwatch.start();
    217     assertEquals("0.000 ns", stopwatch.toString());
    218     ticker.advance(1);
    219     assertEquals("1.000 ns", stopwatch.toString());
    220     ticker.advance(998);
    221     assertEquals("999.0 ns", stopwatch.toString());
    222     ticker.advance(1);
    223     assertEquals("1.000 \u03bcs", stopwatch.toString());
    224     ticker.advance(1);
    225     assertEquals("1.001 \u03bcs", stopwatch.toString());
    226     ticker.advance(8998);
    227     assertEquals("9.999 \u03bcs", stopwatch.toString());
    228     stopwatch.reset();
    229     stopwatch.start();
    230     ticker.advance(1234567);
    231     assertEquals("1.235 ms", stopwatch.toString());
    232     stopwatch.reset();
    233     stopwatch.start();
    234     ticker.advance(5000000000L);
    235     assertEquals("5.000 s", stopwatch.toString());
    236   }
    237 
    238 }
    239