• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.testing.FakeTicker;
25 import junit.framework.TestCase;
26 
27 /**
28  * Unit test for {@link Stopwatch}.
29  *
30  * @author Kevin Bourrillion
31  */
32 @GwtCompatible
33 public class StopwatchTest extends TestCase {
34 
35   private final FakeTicker ticker = new FakeTicker();
36   private final Stopwatch stopwatch = new Stopwatch(ticker);
37 
testCreateStarted()38   public void testCreateStarted() {
39     Stopwatch startedStopwatch = Stopwatch.createStarted();
40     assertTrue(startedStopwatch.isRunning());
41   }
42 
testCreateUnstarted()43   public void testCreateUnstarted() {
44     Stopwatch unstartedStopwatch = Stopwatch.createUnstarted();
45     assertFalse(unstartedStopwatch.isRunning());
46     assertEquals(0, unstartedStopwatch.elapsed(NANOSECONDS));
47   }
48 
testInitialState()49   public void testInitialState() {
50     assertFalse(stopwatch.isRunning());
51     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
52   }
53 
testStart()54   public void testStart() {
55     assertSame(stopwatch, stopwatch.start());
56     assertTrue(stopwatch.isRunning());
57   }
58 
testStart_whileRunning()59   public void testStart_whileRunning() {
60     stopwatch.start();
61     try {
62       stopwatch.start();
63       fail();
64     } catch (IllegalStateException expected) {
65     }
66     assertTrue(stopwatch.isRunning());
67   }
68 
testStop()69   public void testStop() {
70     stopwatch.start();
71     assertSame(stopwatch, stopwatch.stop());
72     assertFalse(stopwatch.isRunning());
73   }
74 
testStop_new()75   public void testStop_new() {
76     try {
77       stopwatch.stop();
78       fail();
79     } catch (IllegalStateException expected) {
80     }
81     assertFalse(stopwatch.isRunning());
82   }
83 
testStop_alreadyStopped()84   public void testStop_alreadyStopped() {
85     stopwatch.start();
86     stopwatch.stop();
87     try {
88       stopwatch.stop();
89       fail();
90     } catch (IllegalStateException expected) {
91     }
92     assertFalse(stopwatch.isRunning());
93   }
94 
testReset_new()95   public void testReset_new() {
96     ticker.advance(1);
97     stopwatch.reset();
98     assertFalse(stopwatch.isRunning());
99     ticker.advance(2);
100     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
101     stopwatch.start();
102     ticker.advance(3);
103     assertEquals(3, stopwatch.elapsed(NANOSECONDS));
104   }
105 
testReset_whileRunning()106   public void testReset_whileRunning() {
107     ticker.advance(1);
108     stopwatch.start();
109     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
110     ticker.advance(2);
111     assertEquals(2, stopwatch.elapsed(NANOSECONDS));
112     stopwatch.reset();
113     assertFalse(stopwatch.isRunning());
114     ticker.advance(3);
115     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
116   }
117 
testElapsed_whileRunning()118   public void testElapsed_whileRunning() {
119     ticker.advance(78);
120     stopwatch.start();
121     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
122 
123     ticker.advance(345);
124     assertEquals(345, stopwatch.elapsed(NANOSECONDS));
125   }
126 
testElapsed_notRunning()127   public void testElapsed_notRunning() {
128     ticker.advance(1);
129     stopwatch.start();
130     ticker.advance(4);
131     stopwatch.stop();
132     ticker.advance(9);
133     assertEquals(4, stopwatch.elapsed(NANOSECONDS));
134   }
135 
testElapsed_multipleSegments()136   public void testElapsed_multipleSegments() {
137     stopwatch.start();
138     ticker.advance(9);
139     stopwatch.stop();
140 
141     ticker.advance(16);
142 
143     stopwatch.start();
144     assertEquals(9, stopwatch.elapsed(NANOSECONDS));
145     ticker.advance(25);
146     assertEquals(34, stopwatch.elapsed(NANOSECONDS));
147 
148     stopwatch.stop();
149     ticker.advance(36);
150     assertEquals(34, stopwatch.elapsed(NANOSECONDS));
151   }
152 
testElapsed_micros()153   public void testElapsed_micros() {
154     stopwatch.start();
155     ticker.advance(999);
156     assertEquals(0, stopwatch.elapsed(MICROSECONDS));
157     ticker.advance(1);
158     assertEquals(1, stopwatch.elapsed(MICROSECONDS));
159   }
160 
testElapsed_millis()161   public void testElapsed_millis() {
162     stopwatch.start();
163     ticker.advance(999999);
164     assertEquals(0, stopwatch.elapsed(MILLISECONDS));
165     ticker.advance(1);
166     assertEquals(1, stopwatch.elapsed(MILLISECONDS));
167   }
168 
testToString()169   public void testToString() {
170     stopwatch.start();
171     assertEquals("0.000 ns", stopwatch.toString());
172     ticker.advance(1);
173     assertEquals("1.000 ns", stopwatch.toString());
174     ticker.advance(998);
175     assertEquals("999.0 ns", stopwatch.toString());
176     ticker.advance(1);
177     assertEquals("1.000 \u03bcs", stopwatch.toString());
178     ticker.advance(1);
179     assertEquals("1.001 \u03bcs", stopwatch.toString());
180     ticker.advance(8998);
181     assertEquals("9.999 \u03bcs", stopwatch.toString());
182     stopwatch.reset();
183     stopwatch.start();
184     ticker.advance(1234567);
185     assertEquals("1.235 ms", stopwatch.toString());
186     stopwatch.reset();
187     stopwatch.start();
188     ticker.advance(5000000000L);
189     assertEquals("5.000 s", stopwatch.toString());
190     stopwatch.reset();
191     stopwatch.start();
192     ticker.advance((long) (1.5 * 60 * 1000000000L));
193     assertEquals("1.500 min", stopwatch.toString());
194     stopwatch.reset();
195     stopwatch.start();
196     ticker.advance((long) (2.5 * 60 * 60 * 1000000000L));
197     assertEquals("2.500 h", stopwatch.toString());
198     stopwatch.reset();
199     stopwatch.start();
200     ticker.advance((long) (7.25 * 24 * 60 * 60 * 1000000000L));
201     assertEquals("7.250 d", stopwatch.toString());
202   }
203 }
204