• 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.annotations.GwtIncompatible;
25 import com.google.common.testing.FakeTicker;
26 import java.time.Duration;
27 import junit.framework.TestCase;
28 
29 /**
30  * Unit test for {@link Stopwatch}.
31  *
32  * @author Kevin Bourrillion
33  */
34 @GwtCompatible
35 public class StopwatchTest extends TestCase {
36 
37   private final FakeTicker ticker = new FakeTicker();
38   private final Stopwatch stopwatch = new Stopwatch(ticker);
39 
testCreateStarted()40   public void testCreateStarted() {
41     Stopwatch startedStopwatch = Stopwatch.createStarted();
42     assertTrue(startedStopwatch.isRunning());
43   }
44 
testCreateUnstarted()45   public void testCreateUnstarted() {
46     Stopwatch unstartedStopwatch = Stopwatch.createUnstarted();
47     assertFalse(unstartedStopwatch.isRunning());
48     assertEquals(0, unstartedStopwatch.elapsed(NANOSECONDS));
49   }
50 
testInitialState()51   public void testInitialState() {
52     assertFalse(stopwatch.isRunning());
53     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
54   }
55 
testStart()56   public void testStart() {
57     assertSame(stopwatch, stopwatch.start());
58     assertTrue(stopwatch.isRunning());
59   }
60 
testStart_whileRunning()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 
testStop()71   public void testStop() {
72     stopwatch.start();
73     assertSame(stopwatch, stopwatch.stop());
74     assertFalse(stopwatch.isRunning());
75   }
76 
testStop_new()77   public void testStop_new() {
78     try {
79       stopwatch.stop();
80       fail();
81     } catch (IllegalStateException expected) {
82     }
83     assertFalse(stopwatch.isRunning());
84   }
85 
testStop_alreadyStopped()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 
testReset_new()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 
testReset_whileRunning()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 
testElapsed_whileRunning()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 
testElapsed_notRunning()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 
testElapsed_multipleSegments()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 
testElapsed_micros()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 
testElapsed_millis()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   @GwtIncompatible
testElapsed_duration()172   public void testElapsed_duration() {
173     stopwatch.start();
174     ticker.advance(999999);
175     assertEquals(Duration.ofNanos(999999), stopwatch.elapsed());
176     ticker.advance(1);
177     assertEquals(Duration.ofMillis(1), stopwatch.elapsed());
178   }
179 
testToString()180   public void testToString() {
181     stopwatch.start();
182     assertEquals("0.000 ns", stopwatch.toString());
183     ticker.advance(1);
184     assertEquals("1.000 ns", stopwatch.toString());
185     ticker.advance(998);
186     assertEquals("999.0 ns", stopwatch.toString());
187     ticker.advance(1);
188     assertEquals("1.000 \u03bcs", stopwatch.toString());
189     ticker.advance(1);
190     assertEquals("1.001 \u03bcs", stopwatch.toString());
191     ticker.advance(8998);
192     assertEquals("9.999 \u03bcs", stopwatch.toString());
193     stopwatch.reset();
194     stopwatch.start();
195     ticker.advance(1234567);
196     assertEquals("1.235 ms", stopwatch.toString());
197     stopwatch.reset();
198     stopwatch.start();
199     ticker.advance(5000000000L);
200     assertEquals("5.000 s", stopwatch.toString());
201     stopwatch.reset();
202     stopwatch.start();
203     ticker.advance((long) (1.5 * 60 * 1000000000L));
204     assertEquals("1.500 min", stopwatch.toString());
205     stopwatch.reset();
206     stopwatch.start();
207     ticker.advance((long) (2.5 * 60 * 60 * 1000000000L));
208     assertEquals("2.500 h", stopwatch.toString());
209     stopwatch.reset();
210     stopwatch.start();
211     ticker.advance((long) (7.25 * 24 * 60 * 60 * 1000000000L));
212     assertEquals("7.250 d", stopwatch.toString());
213   }
214 }
215