/*
 *
 * Copyright (c) 2006-2019, Speedment, Inc. All Rights Reserved.
 *
 * 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.speedment.common.benchmark;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;

import static java.lang.Thread.currentThread;
import static java.lang.Thread.sleep;
import static org.junit.jupiter.api.Assertions.*;

final class StopwatchTest {

    private Stopwatch sw;

    @BeforeEach
    void setup() {
        sw = Stopwatch.create();
    }

    @Test
    void elapsed() {
        sw.start();
        shortSleep();
        assertTrue(sw.elapsed(TimeUnit.NANOSECONDS) > 0L);
    }

    @Test
    void elapsedMillis() {
        sw.start();
        shortSleep();
        assertTrue(sw.elapsedMillis() > 0L);
    }

    @Test
    void elapsedNanos() {
        assertEquals(0L, sw.elapsedNanos());
        sw.start();
        shortSleep();
        assertTrue(sw.elapsedNanos() > 0L);
        sw.stop();
        assertTrue(sw.elapsedNanos() > 0L);
    }

    @Test
    void isStarted() {
        assertFalse(sw.isStarted());
        sw.start();
        assertTrue(sw.isStarted());
    }

    @Test
    void isStopped() {
        assertFalse(sw.isStopped());
        sw.start();
        assertFalse(sw.isStopped());
        sw.stop();
        assertTrue(sw.isStopped());
    }

    @Test
    void start() {
        sw.start();
        assertTrue(sw.isStarted());
    }

    @Test
    void startTwice() {
        sw.start();
        assertThrows(IllegalStateException.class, () -> sw.start());
    }

    @Test
    void stopBeforeStart() {
        assertThrows(IllegalStateException.class, () -> sw.stop());
    }

    @Test
    void stop() {
        assertDoesNotThrow(() -> {
            sw.start();
            sw.stop();
        });
    }

    @Test
    void stopTwice() {
        sw.start();
        sw.stop();
        assertThrows(IllegalStateException.class, () -> sw.stop());
    }


    @Test
    void reset() {
        sw.start();
        shortSleep();
        sw.stop();
        sw.reset();
        assertFalse(sw.isStarted());
        assertEquals(0L, sw.elapsedNanos());
    }

    @Test
    void createStarted() {
        final Stopwatch stopwatch = Stopwatch.createStarted();
        assertTrue(stopwatch.isStarted());
    }

    @Test
    void create() {
        final Stopwatch stopwatch = Stopwatch.create();
        assertFalse(stopwatch.isStarted());
    }

    @Test
    void toStringNotStarted() {
        final String actual = sw.toString();
        assertEquals("0.00 ns", actual);
    }

    @Test
    void toStringStarted() {
        sw.start();
        shortSleep();
        final String actual = sw.toString();
        assertNotEquals("0.00 ns", actual);
    }


    private void shortSleep() {
        try {
            Thread.sleep(10);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}
@@ -3290,159 +3290,920 @@
  %7D%0A%0A
-%0A    private void shortSleep() %7B%0A        try %7B%0A            Thread.sleep(10);%0A        %7D catch (Exception e) %7B%0A            throw new RuntimeException(e);
+    @Test%0A    void toStringNano() %7B%0A        sw.start();%0A        sleep(1);%0A        final String actual = sw.toString();%0A        assertNotNull(actual);%0A    %7D%0A%0A    @Test%0A    void toStringMicro() %7B%0A        sw.start();%0A        sleep(1 * 2_000);%0A        final String actual = sw.toString();%0A        assertNotNull(actual);%0A    %7D%0A%0A    @Test%0A    void toStringMilli() %7B%0A        sw.start();%0A        sleep(1 * 2_000_000);%0A        final String actual = sw.toString();%0A        assertNotNull(actual);%0A    %7D%0A%0A    @Test%0A    void toStringSecond() %7B%0A        sw.start();%0A        sleep(1 * 2_000_000_000);%0A        final String actual = sw.toString();%0A        assertNotNull(actual);%0A    %7D%0A%0A%0A%0A    private void shortSleep() %7B%0A        sleep(10 * 1_000_000);%0A    %7D%0A%0A    private void sleep(long durationNs) %7B%0A        final long until = System.nanoTime() + durationNs;%0A        while (System.nanoTime() %3C until) %7B%0A            /* spin wait */
 %0A   
@@ -4205,21 +4205,20 @@
 /%0A        %7D%0A
-%0A
     %7D%0A%0A%7D

Add tests