/** * MIT License * * Copyright (c) 2021 Exercism * Copyright (c) 2026 Networked Embedded Systems, University of Duisburg-Essen * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import java.util.List; import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework; import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework.Test; public class SplitSecondStopwatchTest { private static final int MAX_POINTS = 100; private static final int TEST_CASES = 29; private static final int SUCCESS_POINTS = MAX_POINTS / TEST_CASES; private static final int FAILURE_POINTS = 0; private final java.util.List testPoints = new java.util.ArrayList<>(); @Test(name = "new stopwatch starts in ready state") public void newStopwatchStartsInReadyState() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); assertEquals("ready", stopwatch.state()); }); } @Test(name = "new stopwatch's current lap has no elapsed time") public void newStopwatchCurrentLapHasNoElapsedTime() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); assertEquals("00:00:00", stopwatch.currentLap()); }); } @Test(name = "new stopwatch's total has no elapsed time") public void newStopwatchTotalHasNoElapsedTime() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); assertEquals("00:00:00", stopwatch.total()); }); } @Test(name = "new stopwatch does not have previous laps") public void newStopwatchDoesNotHavePreviousLaps() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); assertTrue(stopwatch.previousLaps().isEmpty()); }); } @Test(name = "start from ready state changes state to running") public void startFromReadyStateChangesStateToRunning() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); assertEquals("running", stopwatch.state()); }); } @Test(name = "start does not change previous laps") public void startDoesNotChangePreviousLaps() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); assertTrue(stopwatch.previousLaps().isEmpty()); }); } @Test(name = "start initiates time tracking for current lap") public void startInitiatesTimeTrackingForCurrentLap() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.advanceTime("00:00:05"); assertEquals("00:00:05", stopwatch.currentLap()); }); } @Test(name = "start initiates time tracking for total") public void startInitiatesTimeTrackingForTotal() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.advanceTime("00:00:23"); assertEquals("00:00:23", stopwatch.total()); }); } @Test(name = "start cannot be called from running state") public void startCannotBeCalledFromRunningState() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); try { stopwatch.start(); throw new AssertionError("Expected IllegalStateException"); } catch (IllegalStateException e) { if (!"cannot start an already running stopwatch".equals(e.getMessage())) { throw new AssertionError("Wrong message: " + e.getMessage()); } } }); } @Test(name = "stop from running state changes state to stopped") public void stopFromRunningStateChangesStateToStopped() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.stop(); assertEquals("stopped", stopwatch.state()); }); } @Test(name = "stop pauses time tracking for current lap") public void stopPausesTimeTrackingForCurrentLap() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.advanceTime("00:00:05"); stopwatch.stop(); stopwatch.advanceTime("00:00:08"); assertEquals("00:00:05", stopwatch.currentLap()); }); } @Test(name = "stop pauses time tracking for total") public void stopPausesTimeTrackingForTotal() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.advanceTime("00:00:13"); stopwatch.stop(); stopwatch.advanceTime("00:00:44"); assertEquals("00:00:13", stopwatch.total()); }); } @Test(name = "stop cannot be called from ready state") public void stopCannotBeCalledFromReadyState() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); try { stopwatch.stop(); throw new AssertionError("Expected IllegalStateException"); } catch (IllegalStateException e) { if (!"cannot stop a stopwatch that is not running".equals(e.getMessage())) { throw new AssertionError("Wrong message: " + e.getMessage()); } } }); } @Test(name = "stop cannot be called from stopped state") public void stopCannotBeCalledFromStoppedState() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.stop(); try { stopwatch.stop(); throw new AssertionError("Expected IllegalStateException"); } catch (IllegalStateException e) { if (!"cannot stop a stopwatch that is not running".equals(e.getMessage())) { throw new AssertionError("Wrong message: " + e.getMessage()); } } }); } @Test(name = "start from stopped state changes state to running") public void startFromStoppedStateChangesStateToRunning() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.stop(); stopwatch.start(); assertEquals("running", stopwatch.state()); }); } @Test(name = "start from stopped state resumes time tracking for current lap") public void startFromStoppedStateResumesTimeTrackingForCurrentLap() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.advanceTime("00:01:20"); stopwatch.stop(); stopwatch.advanceTime("00:00:20"); stopwatch.start(); stopwatch.advanceTime("00:00:08"); assertEquals("00:01:28", stopwatch.currentLap()); }); } @Test(name = "start from stopped state resumes time tracking for total") public void startFromStoppedStateResumesTimeTrackingForTotal() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.advanceTime("00:00:23"); stopwatch.stop(); stopwatch.advanceTime("00:00:44"); stopwatch.start(); stopwatch.advanceTime("00:00:09"); assertEquals("00:00:32", stopwatch.total()); }); } @Test(name = "lap adds current lap to previous laps") public void lapAddsCurrentLapToPreviousLaps() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.advanceTime("00:01:38"); stopwatch.lap(); assertTrue(stopwatch.previousLaps().containsAll(List.of("00:01:38"))); stopwatch.advanceTime("00:00:44"); stopwatch.lap(); assertTrue(stopwatch.previousLaps().containsAll(List.of("00:01:38", "00:00:44"))); }); } @Test(name = "lap resets current lap and resumes time tracking") public void lapResetsCurrentLapAndResumesTimeTracking() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.advanceTime("00:08:22"); stopwatch.lap(); assertEquals("00:00:00", stopwatch.currentLap()); stopwatch.advanceTime("00:00:15"); assertEquals("00:00:15", stopwatch.currentLap()); }); } @Test(name = "lap continues time tracking for total") public void lapContinuesTimeTrackingForTotal() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.advanceTime("00:00:22"); stopwatch.lap(); stopwatch.advanceTime("00:00:33"); assertEquals("00:00:55", stopwatch.total()); }); } @Test(name = "lap cannot be called from ready state") public void lapCannotBeCalledFromReadyState() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); try { stopwatch.lap(); throw new AssertionError("Expected IllegalStateException"); } catch (IllegalStateException e) { if (!"cannot lap a stopwatch that is not running".equals(e.getMessage())) { throw new AssertionError("Wrong message: " + e.getMessage()); } } }); } @Test(name = "lap cannot be called from stopped state") public void lapCannotBeCalledFromStoppedState() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.stop(); try { stopwatch.lap(); throw new AssertionError("Expected IllegalStateException"); } catch (IllegalStateException e) { if (!"cannot lap a stopwatch that is not running".equals(e.getMessage())) { throw new AssertionError("Wrong message: " + e.getMessage()); } } }); } @Test(name = "stop does not change previous laps") public void stopDoesNotChangePreviousLaps() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.advanceTime("00:11:22"); stopwatch.lap(); assertTrue(stopwatch.previousLaps().containsAll(List.of("00:11:22"))); stopwatch.stop(); assertTrue(stopwatch.previousLaps().containsAll(List.of("00:11:22"))); }); } @Test(name = "reset from stopped state changes state to ready") public void resetFromStoppedStateChangesStateToReady() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.stop(); stopwatch.reset(); assertEquals("ready", stopwatch.state()); }); } @Test(name = "reset resets current lap") public void resetResetsCurrentLap() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.advanceTime("00:00:10"); stopwatch.stop(); stopwatch.reset(); assertEquals("00:00:00", stopwatch.currentLap()); }); } @Test(name = "reset clears previous laps") public void resetClearsPreviousLaps() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.advanceTime("00:00:10"); stopwatch.lap(); stopwatch.advanceTime("00:00:20"); stopwatch.lap(); assertTrue(stopwatch.previousLaps().containsAll(List.of("00:00:10", "00:00:20"))); stopwatch.stop(); stopwatch.reset(); assertTrue(stopwatch.previousLaps().isEmpty()); }); } @Test(name = "reset cannot be called from ready state") public void resetCannotBeCalledFromReadyState() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); try { stopwatch.reset(); throw new AssertionError("Expected IllegalStateException"); } catch (IllegalStateException e) { if (!"cannot reset a stopwatch that is not stopped".equals(e.getMessage())) { throw new AssertionError("Wrong message: " + e.getMessage()); } } }); } @Test(name = "reset cannot be called from running state") public void resetCannotBeCalledFromRunningState() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); try { stopwatch.reset(); throw new AssertionError("Expected IllegalStateException"); } catch (IllegalStateException e) { if (!"cannot reset a stopwatch that is not stopped".equals(e.getMessage())) { throw new AssertionError("Wrong message: " + e.getMessage()); } } }); } @Test(name = "supports very long laps") public void supportsVeryLongLaps() { runTest(() -> { SplitSecondStopwatch stopwatch = new SplitSecondStopwatch(); stopwatch.start(); stopwatch.advanceTime("01:23:45"); assertEquals("01:23:45", stopwatch.currentLap()); stopwatch.lap(); assertTrue(stopwatch.previousLaps().containsAll(List.of("01:23:45"))); stopwatch.advanceTime("04:01:40"); assertEquals("04:01:40", stopwatch.currentLap()); assertEquals("05:25:25", stopwatch.total()); stopwatch.lap(); assertTrue(stopwatch.previousLaps().containsAll(List.of("01:23:45", "04:01:40"))); stopwatch.advanceTime("08:43:05"); assertEquals("08:43:05", stopwatch.currentLap()); assertEquals("14:08:30", stopwatch.total()); stopwatch.lap(); assertTrue(stopwatch.previousLaps().containsAll(List.of("01:23:45", "04:01:40", "08:43:05"))); }); } private void assertEquals(String expected, String actual) { if (!expected.equals(actual)) { throw new AssertionError("Expected: " + expected + " but was: " + actual); } } private void assertTrue(boolean condition) { if (!condition) { throw new AssertionError("Expected true but was false"); } } private void runTest(Runnable test) { try { test.run(); testPoints.add(SUCCESS_POINTS); } catch (AssertionError | Exception e) { TracingFramework.printError(e.getMessage()); testPoints.add(FAILURE_POINTS); } } public int getResult() { int sum = testPoints.stream().mapToInt(Integer::intValue).sum(); return sum / SUCCESS_POINTS == TEST_CASES ? MAX_POINTS : sum; } }