• 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 com.google.common.base.ReflectionFreeAssertThrows.assertThrows;
20 import static java.util.concurrent.TimeUnit.MICROSECONDS;
21 import static java.util.concurrent.TimeUnit.MILLISECONDS;
22 import static java.util.concurrent.TimeUnit.NANOSECONDS;
23 
24 import com.google.common.annotations.GwtCompatible;
25 import com.google.common.annotations.J2ktIncompatible;
26 import com.google.common.testing.FakeTicker;
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     assertThrows(IllegalStateException.class, stopwatch::start);
64     assertTrue(stopwatch.isRunning());
65   }
66 
testStop()67   public void testStop() {
68     stopwatch.start();
69     assertSame(stopwatch, stopwatch.stop());
70     assertFalse(stopwatch.isRunning());
71   }
72 
testStop_new()73   public void testStop_new() {
74     assertThrows(IllegalStateException.class, stopwatch::stop);
75     assertFalse(stopwatch.isRunning());
76   }
77 
testStop_alreadyStopped()78   public void testStop_alreadyStopped() {
79     stopwatch.start();
80     stopwatch.stop();
81     assertThrows(IllegalStateException.class, stopwatch::stop);
82     assertFalse(stopwatch.isRunning());
83   }
84 
testReset_new()85   public void testReset_new() {
86     ticker.advance(1);
87     stopwatch.reset();
88     assertFalse(stopwatch.isRunning());
89     ticker.advance(2);
90     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
91     stopwatch.start();
92     ticker.advance(3);
93     assertEquals(3, stopwatch.elapsed(NANOSECONDS));
94   }
95 
testReset_whileRunning()96   public void testReset_whileRunning() {
97     ticker.advance(1);
98     stopwatch.start();
99     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
100     ticker.advance(2);
101     assertEquals(2, stopwatch.elapsed(NANOSECONDS));
102     stopwatch.reset();
103     assertFalse(stopwatch.isRunning());
104     ticker.advance(3);
105     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
106   }
107 
testElapsed_whileRunning()108   public void testElapsed_whileRunning() {
109     ticker.advance(78);
110     stopwatch.start();
111     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
112 
113     ticker.advance(345);
114     assertEquals(345, stopwatch.elapsed(NANOSECONDS));
115   }
116 
testElapsed_notRunning()117   public void testElapsed_notRunning() {
118     ticker.advance(1);
119     stopwatch.start();
120     ticker.advance(4);
121     stopwatch.stop();
122     ticker.advance(9);
123     assertEquals(4, stopwatch.elapsed(NANOSECONDS));
124   }
125 
testElapsed_multipleSegments()126   public void testElapsed_multipleSegments() {
127     stopwatch.start();
128     ticker.advance(9);
129     stopwatch.stop();
130 
131     ticker.advance(16);
132 
133     stopwatch.start();
134     assertEquals(9, stopwatch.elapsed(NANOSECONDS));
135     ticker.advance(25);
136     assertEquals(34, stopwatch.elapsed(NANOSECONDS));
137 
138     stopwatch.stop();
139     ticker.advance(36);
140     assertEquals(34, stopwatch.elapsed(NANOSECONDS));
141   }
142 
testElapsed_micros()143   public void testElapsed_micros() {
144     stopwatch.start();
145     ticker.advance(999);
146     assertEquals(0, stopwatch.elapsed(MICROSECONDS));
147     ticker.advance(1);
148     assertEquals(1, stopwatch.elapsed(MICROSECONDS));
149   }
150 
testElapsed_millis()151   public void testElapsed_millis() {
152     stopwatch.start();
153     ticker.advance(999999);
154     assertEquals(0, stopwatch.elapsed(MILLISECONDS));
155     ticker.advance(1);
156     assertEquals(1, stopwatch.elapsed(MILLISECONDS));
157   }
158 
159   @J2ktIncompatible // TODO(b/259213718): Switch J2kt to String.format("%.4g") once that's supported
testToString()160   public void testToString() {
161     stopwatch.start();
162     assertEquals("0.000 ns", stopwatch.toString());
163     ticker.advance(1);
164     assertEquals("1.000 ns", stopwatch.toString());
165     ticker.advance(998);
166     assertEquals("999.0 ns", stopwatch.toString());
167     ticker.advance(1);
168     assertEquals("1.000 \u03bcs", stopwatch.toString());
169     ticker.advance(1);
170     assertEquals("1.001 \u03bcs", stopwatch.toString());
171     ticker.advance(8998);
172     assertEquals("9.999 \u03bcs", stopwatch.toString());
173     stopwatch.reset();
174     stopwatch.start();
175     ticker.advance(1234567);
176     assertEquals("1.235 ms", stopwatch.toString());
177     stopwatch.reset();
178     stopwatch.start();
179     ticker.advance(5000000000L);
180     assertEquals("5.000 s", stopwatch.toString());
181     stopwatch.reset();
182     stopwatch.start();
183     ticker.advance((long) (1.5 * 60 * 1000000000L));
184     assertEquals("1.500 min", stopwatch.toString());
185     stopwatch.reset();
186     stopwatch.start();
187     ticker.advance((long) (2.5 * 60 * 60 * 1000000000L));
188     assertEquals("2.500 h", stopwatch.toString());
189     stopwatch.reset();
190     stopwatch.start();
191     ticker.advance((long) (7.25 * 24 * 60 * 60 * 1000000000L));
192     assertEquals("7.250 d", stopwatch.toString());
193   }
194 }
195