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