<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.commons.lang3.time;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.junit.jupiter.api.Test;

/**
 * TestCase for StopWatch.
 */
public class StopWatchTest {

    //-----------------------------------------------------------------------
    @Test
    public void testStopWatchSimple() {
        final StopWatch watch = new StopWatch();
        watch.start();
        try {
            Thread.sleep(550);
        } catch (final InterruptedException ex) {
        }
        watch.stop();
        final long time = watch.getTime();
        assertEquals(time, watch.getTime());

        assertTrue(time >= 500);
        assertTrue(time < 700);

        watch.reset();
        assertEquals(0, watch.getTime());
    }

    @Test
    public void testStopWatchStatic() {
        final StopWatch watch = StopWatch.createStarted();
        assertTrue(watch.isStarted());
    }

    @Test
    public void testStopWatchSimpleGet() {
        final StopWatch watch = new StopWatch();
        assertEquals(0, watch.getTime());
        assertEquals("00:00:00.000", watch.toString());

        watch.start();
        try {
            Thread.sleep(500);
        } catch (final InterruptedException ex) {
        }
        assertTrue(watch.getTime() < 2000);
    }

    @Test
    public void testStopWatchGetWithTimeUnit() {
        // Create a mock StopWatch with a time of 2:59:01.999
        final StopWatch watch = createMockStopWatch(
                TimeUnit.HOURS.toNanos(2)
                        + TimeUnit.MINUTES.toNanos(59)
                        + TimeUnit.SECONDS.toNanos(1)
                        + TimeUnit.MILLISECONDS.toNanos(999));

        assertEquals(2L, watch.getTime(TimeUnit.HOURS));
        assertEquals(179L, watch.getTime(TimeUnit.MINUTES));
        assertEquals(10741L, watch.getTime(TimeUnit.SECONDS));
        assertEquals(10741999L, watch.getTime(TimeUnit.MILLISECONDS));
    }

    @Test
    public void testStopWatchSplit() {
        final StopWatch watch = new StopWatch();
        watch.start();
        try {
            Thread.sleep(550);
        } catch (final InterruptedException ex) {
        }
        watch.split();
        final long splitTime = watch.getSplitTime();
        final String splitStr = watch.toSplitString();
        try {
            Thread.sleep(550);
        } catch (final InterruptedException ex) {
        }
        watch.unsplit();
        try {
            Thread.sleep(550);
        } catch (final InterruptedException ex) {
        }
        watch.stop();
        final long totalTime = watch.getTime();

        assertEquals(splitStr.length(), 12, "Formatted split string not the correct length");
        assertTrue(splitTime >= 500);
        assertTrue(splitTime < 700);
        assertTrue(totalTime >= 1500);
        assertTrue(totalTime < 1900);
    }

    @Test
    public void testStopWatchSuspend() {
        final StopWatch watch = new StopWatch();
        watch.start();
        try {
            Thread.sleep(550);
        } catch (final InterruptedException ex) {
        }
        watch.suspend();
        final long suspendTime = watch.getTime();
        try {
            Thread.sleep(550);
        } catch (final InterruptedException ex) {
        }
        watch.resume();
        try {
            Thread.sleep(550);
        } catch (final InterruptedException ex) {
        }
        watch.stop();
        final long totalTime = watch.getTime();

        assertTrue(suspendTime >= 500);
        assertTrue(suspendTime < 700);
        assertTrue(totalTime >= 1000);
        assertTrue(totalTime < 1300);
    }

    @Test
    public void testLang315() {
        final StopWatch watch = new StopWatch();
        watch.start();
        try {
            Thread.sleep(200);
        } catch (final InterruptedException ex) {
        }
        watch.suspend();
        final long suspendTime = watch.getTime();
        try {
            Thread.sleep(200);
        } catch (final InterruptedException ex) {
        }
        watch.stop();
        final long totalTime = watch.getTime();
        assertEquals(suspendTime, totalTime);
    }

    // test bad states
    @Test
    public void testBadStates() {
        final StopWatch watch = new StopWatch();
        assertThrows(<commit_msg>Document empty block.<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.commons.lang3.time;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.junit.jupiter.api.Test;

/**
 * TestCase for StopWatch.
 */
public class StopWatchTest {

    //-----------------------------------------------------------------------
    @Test
    public void testStopWatchSimple() {
        final StopWatch watch = new StopWatch();
        watch.start();
        try {
            Thread.sleep(550);
        } catch (final InterruptedException ex) {
            // ignore
        }
        watch.stop();
        final long time = watch.getTime();
        assertEquals(time, watch.getTime());

        assertTrue(time >= 500);
        assertTrue(time < 700);

        watch.reset();
        assertEquals(0, watch.getTime());
    }

    @Test
    public void testStopWatchStatic() {
        final StopWatch watch = StopWatch.createStarted();
        assertTrue(watch.isStarted());
    }

    @Test
    public void testStopWatchSimpleGet() {
        final StopWatch watch = new StopWatch();
        assertEquals(0, watch.getTime());
        assertEquals("00:00:00.000", watch.toString());

        watch.start();
        try {
            Thread.sleep(500);
        } catch (final InterruptedException ex) {
            // ignore
        }
        assertTrue(watch.getTime() < 2000);
    }

    @Test
    public void testStopWatchGetWithTimeUnit() {
        // Create a mock StopWatch with a time of 2:59:01.999
        final StopWatch watch = createMockStopWatch(
                TimeUnit.HOURS.toNanos(2)
                        + TimeUnit.MINUTES.toNanos(59)
                        + TimeUnit.SECONDS.toNanos(1)
                        + TimeUnit.MILLISECONDS.toNanos(999));

        assertEquals(2L, watch.getTime(TimeUnit.HOURS));
        assertEquals(179L, watch.getTime(TimeUnit.MINUTES));
        assertEquals(10741L, watch.getTime(TimeUnit.SECONDS));
        assertEquals(10741999L, watch.getTime(TimeUnit.MILLISECONDS));
    }

    @Test
    public void testStopWatchSplit() {
        final StopWatch watch = new StopWatch();
        watch.start();
        try {
            Thread.sleep(550);
        } catch (final InterruptedException ex) {
            // ignore
        }
        watch.split();
        final long splitTime = watch.getSplitTime();
        final String splitStr = watch.toSplitString();
        try {
            Thread.sleep(550);
        } catch (final InterruptedException ex) {
            // ignore
        }
        watch.unsplit();
        try {
            Thread.sleep(550);
        } catch (final InterruptedException ex) {
            // ignore
        }
        watch.stop();
        final long totalTime = watch.getTime();

        assertEquals(splitStr.length(), 12, "Formatted split string not the correct length");
        assertTrue(splitTime >= 500);
        assertTrue(splitTime < 700);
        assertTrue(totalTime >= 1500);
        assertTrue(totalTime < 1900);
    }

    @Test
    public void testStopWatchSuspend() {
        final StopWatch watch = new StopWatch();
        watch.start();
        try {
            Thread.sleep(550);
        } catch (final InterruptedException ex) {
            // ignore
        }
        watch.suspend();
        final long suspendTime = watch.getTime();
        try {
            Thread.sleep(550);
        } catch (final InterruptedException ex) {
            // ignore
        }
        watch.resume();
        try {
            Thread.sleep(550);
        } catch (final InterruptedException ex) {
            // ignore
        }
        watch.stop();
        final long totalTime = watch.getTime();

        assertTrue(suspendTime >= 500);
        assertTrue(suspendTime < 700);
        assertTrue(totalTime >= 1000);
        assertTrue(totalTime < 1300);
    }

    @Test
    public void testLang315() {
        final StopWatch watch = new StopWatch();
        watch.start();
        try {
            Thread.sleep(200);
        } catch (final InterruptedException ex) {
            // ignore
        }
        watch.suspend();
        final long suspendTime = watch.getTime();
        try {
            Thread.sleep(200);
        } catch (final InterruptedException ex) {
            // ignore
        }
        watch.stop();
        final long totalTime = watch.getTime();
        assertEquals(suspendTime, totalTime);
    }

    // test bad states
    @Test
    public void testBadStates() {
        final StopWatch watch = new StopWatch();
        assertThrows(
{'fraction_non_alphanumeric': 0.1127976731503363, 'fraction_numerical': 0.022177785857116886, 'mean_word_length': 2.070033482142857, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7058563', 'n_tokens_mistral': 3039, 'n_tokens_neox': 2865, 'n_words': 777}