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 testInitialState() {
     41     assertFalse(stopwatch.isRunning());
     42     assertEquals(0, stopwatch.elapsedTime(NANOSECONDS));
     43   }
     44 
     45   public void testStart() {
     46     assertSame(stopwatch, stopwatch.start());
     47     assertTrue(stopwatch.isRunning());
     48   }
     49 
     50   public void testStart_whileRunning() {
     51     stopwatch.start();
     52     try {
     53       stopwatch.start();
     54       fail();
     55     } catch (IllegalStateException expected) {
     56     }
     57     assertTrue(stopwatch.isRunning());
     58   }
     59 
     60   public void testStop() {
     61     stopwatch.start();
     62     assertSame(stopwatch, stopwatch.stop());
     63     assertFalse(stopwatch.isRunning());
     64   }
     65 
     66   public void testStop_new() {
     67     try {
     68       stopwatch.stop();
     69       fail();
     70     } catch (IllegalStateException expected) {
     71     }
     72     assertFalse(stopwatch.isRunning());
     73   }
     74 
     75   public void testStop_alreadyStopped() {
     76     stopwatch.start();
     77     stopwatch.stop();
     78     try {
     79       stopwatch.stop();
     80       fail();
     81     } catch (IllegalStateException expected) {
     82     }
     83     assertFalse(stopwatch.isRunning());
     84   }
     85 
     86   public void testReset_new() {
     87     ticker.advance(1);
     88     stopwatch.reset();
     89     assertFalse(stopwatch.isRunning());
     90     ticker.advance(2);
     91     assertEquals(0, stopwatch.elapsedTime(NANOSECONDS));
     92     stopwatch.start();
     93     ticker.advance(3);
     94     assertEquals(3, stopwatch.elapsedTime(NANOSECONDS));
     95   }
     96 
     97   public void testReset_whileRunning() {
     98     ticker.advance(1);
     99     stopwatch.start();
    100     assertEquals(0, stopwatch.elapsedTime(NANOSECONDS));
    101     ticker.advance(2);
    102     assertEquals(2, stopwatch.elapsedTime(NANOSECONDS));
    103     stopwatch.reset();
    104     assertFalse(stopwatch.isRunning());
    105     ticker.advance(3);
    106     assertEquals(0, stopwatch.elapsedTime(NANOSECONDS));
    107   }
    108 
    109   public void testElapsedTime_whileRunning() {
    110     ticker.advance(78);
    111     stopwatch.start();
    112     assertEquals(0, stopwatch.elapsedTime(NANOSECONDS));
    113 
    114     ticker.advance(345);
    115     assertEquals(345, stopwatch.elapsedTime(NANOSECONDS));
    116   }
    117 
    118   public void testElapsedTime_notRunning() {
    119     ticker.advance(1);
    120     stopwatch.start();
    121     ticker.advance(4);
    122     stopwatch.stop();
    123     ticker.advance(9);
    124     assertEquals(4, stopwatch.elapsedTime(NANOSECONDS));
    125   }
    126 
    127   public void testElapsedTime_multipleSegments() {
    128     stopwatch.start();
    129     ticker.advance(9);
    130     stopwatch.stop();
    131 
    132     ticker.advance(16);
    133 
    134     stopwatch.start();
    135     assertEquals(9, stopwatch.elapsedTime(NANOSECONDS));
    136     ticker.advance(25);
    137     assertEquals(34, stopwatch.elapsedTime(NANOSECONDS));
    138 
    139     stopwatch.stop();
    140     ticker.advance(36);
    141     assertEquals(34, stopwatch.elapsedTime(NANOSECONDS));
    142   }
    143 
    144   public void testElapsedTime_micros() {
    145     stopwatch.start();
    146     ticker.advance(999);
    147     assertEquals(0, stopwatch.elapsedTime(MICROSECONDS));
    148     ticker.advance(1);
    149     assertEquals(1, stopwatch.elapsedTime(MICROSECONDS));
    150   }
    151 
    152   public void testElapsedTime_millis() {
    153     stopwatch.start();
    154     ticker.advance(999999);
    155     assertEquals(0, stopwatch.elapsedTime(MILLISECONDS));
    156     ticker.advance(1);
    157     assertEquals(1, stopwatch.elapsedTime(MILLISECONDS));
    158   }
    159 
    160   public void testElapsedMillis() {
    161     stopwatch.start();
    162     ticker.advance(999999);
    163     assertEquals(0, stopwatch.elapsedMillis());
    164     ticker.advance(1);
    165     assertEquals(1, stopwatch.elapsedMillis());
    166   }
    167 
    168   public void testElapsedMillis_whileRunning() {
    169     ticker.advance(78000000);
    170     stopwatch.start();
    171     assertEquals(0, stopwatch.elapsedMillis());
    172 
    173     ticker.advance(345000000);
    174     assertEquals(345, stopwatch.elapsedMillis());
    175   }
    176 
    177   public void testElapsedMillis_notRunning() {
    178     ticker.advance(1000000);
    179     stopwatch.start();
    180     ticker.advance(4000000);
    181     stopwatch.stop();
    182     ticker.advance(9000000);
    183     assertEquals(4, stopwatch.elapsedMillis());
    184   }
    185 
    186   public void testElapsedMillis_multipleSegments() {
    187     stopwatch.start();
    188     ticker.advance(9000000);
    189     stopwatch.stop();
    190 
    191     ticker.advance(16000000);
    192 
    193     stopwatch.start();
    194     assertEquals(9, stopwatch.elapsedMillis());
    195     ticker.advance(25000000);
    196     assertEquals(34, stopwatch.elapsedMillis());
    197 
    198     stopwatch.stop();
    199     ticker.advance(36000000);
    200     assertEquals(34, stopwatch.elapsedMillis());
    201   }
    202 
    203   @GwtIncompatible("String.format()")
    204   public void testToString() {
    205     stopwatch.start();
    206     assertEquals("0.000 ns", stopwatch.toString());
    207     ticker.advance(1);
    208     assertEquals("1.000 ns", stopwatch.toString());
    209     ticker.advance(998);
    210     assertEquals("999.0 ns", stopwatch.toString());
    211     ticker.advance(1);
    212     assertEquals("1.000 \u03bcs", stopwatch.toString());
    213     ticker.advance(1);
    214     assertEquals("1.001 \u03bcs", stopwatch.toString());
    215     ticker.advance(8998);
    216     assertEquals("9.999 \u03bcs", stopwatch.toString());
    217     stopwatch.reset();
    218     stopwatch.start();
    219     ticker.advance(1234567);
    220     assertEquals("1.235 ms", stopwatch.toString());
    221     stopwatch.reset();
    222     stopwatch.start();
    223     ticker.advance(5000000000L);
    224     assertEquals("5.000 s", stopwatch.toString());
    225   }
    226 
    227   @GwtIncompatible("GWT is at millisecond granularity")
    228   public void testDefault() {
    229     // By default System.nanoTime() is used as the time source
    230     long value = new Stopwatch().start().elapsedTime(NANOSECONDS);
    231     assertTrue(value > 0);
    232     // There isn't much else we can test about this
    233   }
    234 }
    235