/
githubmirror
/
guava
Обзор
Документация
Войти
/
githubmirror
/
guava
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
android/guava-tests/test/com/google/common/base/StopwatchTest.java
209 строк
6 KB
cpovirk
Use JUnit's `assertThrows` instead of ours.
07 апр 2026, 16:15
07 апр 2026, 16:15
61e300d
Код
Авторство
О чём код?
/* * Copyright (C) 2008 The Guava Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.common.base; import static com.google.common.truth.Truth.assertThat; import static java.util.concurrent.TimeUnit.MICROSECONDS; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.NANOSECONDS; import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; import com.google.common.annotations.J2ktIncompatible; import com.google.common.testing.FakeTicker; import java.time.Duration; import junit.framework.TestCase; import org.jspecify.annotations.NullUnmarked; /** * Unit test for {@link Stopwatch}. * * @author Kevin Bourrillion */ @GwtCompatible @NullUnmarked public class StopwatchTest extends TestCase { private final FakeTicker ticker = new FakeTicker(); private final Stopwatch stopwatch = new Stopwatch(ticker); public void testCreateStarted() { Stopwatch startedStopwatch = Stopwatch.createStarted(); assertTrue(startedStopwatch.isRunning()); } public void testCreateUnstarted() { Stopwatch unstartedStopwatch = Stopwatch.createUnstarted(); assertFalse(unstartedStopwatch.isRunning()); assertEquals(0, unstartedStopwatch.elapsed(NANOSECONDS)); } public void testInitialState() { assertFalse(stopwatch.isRunning()); assertEquals(0, stopwatch.elapsed(NANOSECONDS)); } public void testStart() { assertThat(stopwatch.start()).isSameInstanceAs(stopwatch); assertTrue(stopwatch.isRunning()); } public void testStart_whileRunning() { stopwatch.start(); assertThrows(IllegalStateException.class, stopwatch::start); assertTrue(stopwatch.isRunning()); } public void testStop() { stopwatch.start(); assertThat(stopwatch.stop()).isSameInstanceAs(stopwatch); assertFalse(stopwatch.isRunning()); } public void testStop_new() { assertThrows(IllegalStateException.class, stopwatch::stop); assertFalse(stopwatch.isRunning()); } public void testStop_alreadyStopped() { stopwatch.start(); stopwatch.stop(); assertThrows(IllegalStateException.class, stopwatch::stop); assertFalse(stopwatch.isRunning()); } public void testReset_new() { ticker.advance(1); stopwatch.reset(); assertFalse(stopwatch.isRunning()); ticker.advance(2); assertEquals(0, stopwatch.elapsed(NANOSECONDS)); stopwatch.start(); ticker.advance(3); assertEquals(3, stopwatch.elapsed(NANOSECONDS)); } public void testReset_whileRunning() { ticker.advance(1); stopwatch.start(); assertEquals(0, stopwatch.elapsed(NANOSECONDS)); ticker.advance(2); assertEquals(2, stopwatch.elapsed(NANOSECONDS)); stopwatch.reset(); assertFalse(stopwatch.isRunning()); ticker.advance(3); assertEquals(0, stopwatch.elapsed(NANOSECONDS)); } public void testElapsed_whileRunning() { ticker.advance(78); stopwatch.start(); assertEquals(0, stopwatch.elapsed(NANOSECONDS)); ticker.advance(345); assertEquals(345, stopwatch.elapsed(NANOSECONDS)); } public void testElapsed_notRunning() { ticker.advance(1); stopwatch.start(); ticker.advance(4); stopwatch.stop(); ticker.advance(9); assertEquals(4, stopwatch.elapsed(NANOSECONDS)); } public void testElapsed_multipleSegments() { stopwatch.start(); ticker.advance(9); stopwatch.stop(); ticker.advance(16); stopwatch.start(); assertEquals(9, stopwatch.elapsed(NANOSECONDS)); ticker.advance(25); assertEquals(34, stopwatch.elapsed(NANOSECONDS)); stopwatch.stop(); ticker.advance(36); assertEquals(34, stopwatch.elapsed(NANOSECONDS)); } public void testElapsed_micros() { stopwatch.start(); ticker.advance(999); assertEquals(0, stopwatch.elapsed(MICROSECONDS)); ticker.advance(1); assertEquals(1, stopwatch.elapsed(MICROSECONDS)); } public void testElapsed_millis() { stopwatch.start(); ticker.advance(999999); assertEquals(0, stopwatch.elapsed(MILLISECONDS)); ticker.advance(1); assertEquals(1, stopwatch.elapsed(MILLISECONDS)); } @J2ktIncompatible // TODO(b/259213718): Switch J2kt to String.format("%.4g") once that's supported public void testToString() { stopwatch.start(); assertThat(stopwatch.toString()).isEqualTo("0.000 ns"); ticker.advance(1); assertThat(stopwatch.toString()).isEqualTo("1.000 ns"); ticker.advance(998); assertThat(stopwatch.toString()).isEqualTo("999.0 ns"); ticker.advance(1); assertThat(stopwatch.toString()).isEqualTo("1.000 \u03bcs"); ticker.advance(1); assertThat(stopwatch.toString()).isEqualTo("1.001 \u03bcs"); ticker.advance(8998); assertThat(stopwatch.toString()).isEqualTo("9.999 \u03bcs"); stopwatch.reset(); stopwatch.start(); ticker.advance(1234567); assertThat(stopwatch.toString()).isEqualTo("1.235 ms"); stopwatch.reset(); stopwatch.start(); ticker.advance(5000000000L); assertThat(stopwatch.toString()).isEqualTo("5.000 s"); stopwatch.reset(); stopwatch.start(); ticker.advance((long) (1.5 * 60 * 1000000000L)); assertThat(stopwatch.toString()).isEqualTo("1.500 min"); stopwatch.reset(); stopwatch.start(); ticker.advance((long) (2.5 * 60 * 60 * 1000000000L)); assertThat(stopwatch.toString()).isEqualTo("2.500 h"); stopwatch.reset(); stopwatch.start(); ticker.advance((long) (7.25 * 24 * 60 * 60 * 1000000000L)); assertThat(stopwatch.toString()).isEqualTo("7.250 d"); } @GwtIncompatible @J2ktIncompatible public void testElapsed_duration() { stopwatch.start(); ticker.advance(999999); assertEquals(Duration.ofNanos(999999), stopwatch.elapsed()); ticker.advance(1); assertEquals(Duration.ofMillis(1), stopwatch.elapsed()); } }