/*
 *
 * 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);
    }

    @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 stop() {
        assertThrows(IllegalStateException.class, () -> sw.stop());
        sw.start();
        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());
    }

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

    }

}
@@ -1475,38 +1475,101 @@
 dNanos() %3E 0L);%0A
+        sw.stop();%0A        assertTrue(sw.elapsedNanos() %3E 0L);%0A
     %7D%0A
-
 %0A    @Test%0A    v
@@ -1959,32 +1959,162 @@
 rted());%0A    %7D%0A%0A
+    @Test%0A    void startTwice() %7B%0A        sw.start();%0A        assertThrows(IllegalStateException.class, () -%3E sw.start());%0A    %7D%0A%0A
     @Test%0A    vo
@@ -2120,16 +2120,27 @@
 oid stop
+BeforeStart
 () %7B%0A   
@@ -2200,24 +2200,59 @@
 sw.stop());%0A
+    %7D%0A%0A    @Test%0A    void stop() %7B%0A
         sw.s
@@ -2277,32 +2277,180 @@
 .stop();%0A    %7D%0A%0A
+    @Test%0A    void stopTwice() %7B%0A        sw.start();%0A        sw.stop();%0A        assertThrows(IllegalStateException.class, () -%3E sw.stop());%0A    %7D%0A%0A%0A
     @Test%0A    vo
@@ -2908,31 +2908,344 @@
 Started());%0A
-
     %7D%0A%0A
+    @Test%0A    void toStringNotStarted() %7B%0A        final String actual = sw.toString();%0A        assertEquals(%220.00 ns%22, actual);%0A    %7D%0A%0A    @Test%0A    void toStringStarted() %7B%0A        sw.start();%0A        shortSleep();%0A        final String actual = sw.toString();%0A        assertNotEquals(%220.00 ns%22, actual);%0A    %7D%0A%0A%0A
     private 

improve code coverage