Add oving 4

This commit is contained in:
Andreas Omholt Olsen
2026-02-02 10:57:55 +01:00
parent 37b0f931ce
commit 7dd68c1ed8
41 changed files with 1702 additions and 0 deletions
@@ -0,0 +1,111 @@
package oving4.stopwatch;
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 org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class StopWatchManagerTest {
private StopWatchManager manager;
@BeforeEach
public void setUp() {
manager = new StopWatchManager();
}
@Test
@DisplayName("Create new StopWatch")
public void testNewStopWatch() {
StopWatch sw1 = manager.newStopWatch("SW1");
StopWatch sw2 = manager.newStopWatch("SW2");
assertEquals(sw1, manager.getStopWatch("SW1"));
assertEquals(sw2, manager.getStopWatch("SW2"));
assertThrows(IllegalArgumentException.class, () -> {
manager.newStopWatch(null);
}, "Name cannot be null");
assertThrows(IllegalArgumentException.class, () -> {
manager.newStopWatch("SW1");
}, "Name already exists");
}
@Test
@DisplayName("Ticker")
public void testTicks() {
StopWatch sw1 = manager.newStopWatch("SW1");
StopWatch sw2 = manager.newStopWatch("SW2");
manager.tick(1);
assertEquals(1, sw1.getTicks());
assertEquals(1, sw2.getTicks());
manager.tick(4);
assertEquals(5, sw1.getTicks());
assertEquals(5, sw2.getTicks());
}
@Test
@DisplayName("Remove StopWatches")
public void testRemoveStopWatches() {
assertEquals(0, manager.getAllWatches().size());
StopWatch sw1 = manager.newStopWatch("SW1");
assertEquals(1, manager.getAllWatches().size());
assertEquals(sw1, manager.getStopWatch("SW1"));
StopWatch sw2 = manager.newStopWatch("SW2");
assertEquals(2, manager.getAllWatches().size());
assertEquals(sw1, manager.getStopWatch("SW1"));
assertEquals(sw2, manager.getStopWatch("SW2"));
manager.removeStopWatch("SW1");
assertEquals(1, manager.getAllWatches().size());
assertEquals(null, manager.getStopWatch("SW1"));
manager.removeStopWatch("SW2");
assertEquals(0, manager.getAllWatches().size());
assertEquals(null, manager.getStopWatch("SW1"));
assertEquals(null, manager.getStopWatch("SW2"));
}
@Test
@DisplayName("Starting and stopping StopWatches")
public void testStartedStoppedWatches() {
assertEquals(0, manager.getStartedWatches().size());
manager.newStopWatch("SW1").start();
assertEquals(1, manager.getStartedWatches().size());
assertEquals(0, manager.getStoppedWatches().size());
assertTrue(manager.getStartedWatches().contains(manager.getStopWatch("SW1")));
assertTrue(manager.getStopWatch("SW1").isStarted());
manager.newStopWatch("SW2").start();
assertEquals(2, manager.getStartedWatches().size());
assertEquals(0, manager.getStoppedWatches().size());
assertTrue(manager.getStartedWatches().contains(manager.getStopWatch("SW1")));
assertTrue(manager.getStopWatch("SW1").isStarted());
assertFalse(manager.getStopWatch("SW1").isStopped());
assertTrue(manager.getStartedWatches().contains(manager.getStopWatch("SW2")));
assertTrue(manager.getStopWatch("SW2").isStarted());
assertFalse(manager.getStopWatch("SW2").isStopped());
manager.getStopWatch("SW2").stop();
assertEquals(1, manager.getStoppedWatches().size());
assertFalse(manager.getStoppedWatches().contains(manager.getStopWatch("SW1")));
assertFalse(manager.getStopWatch("SW1").isStopped());
assertTrue(manager.getStoppedWatches().contains(manager.getStopWatch("SW2")));
assertTrue(manager.getStopWatch("SW2").isStopped());
manager.getStopWatch("SW1").stop();
assertEquals(2, manager.getStoppedWatches().size());
assertTrue(manager.getStoppedWatches().contains(manager.getStopWatch("SW1")));
assertTrue(manager.getStopWatch("SW1").isStopped());
assertTrue(manager.getStoppedWatches().contains(manager.getStopWatch("SW2")));
assertTrue(manager.getStopWatch("SW2").isStopped());
}
}
@@ -0,0 +1,159 @@
package oving4.stopwatch;
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 org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class StopWatchTest {
private StopWatch stopWatch;
@BeforeEach
public void beforeEach() {
stopWatch = new StopWatch();
}
@Test
@DisplayName("Check that a newly created StopWatch object has correct values")
public void testConstructor() {
assertFalse(stopWatch.isStarted());
assertFalse(stopWatch.isStopped());
assertEquals(0, stopWatch.getTicks());
assertEquals(-1, stopWatch.getTime());
assertEquals(-1, stopWatch.getLapTime());
assertEquals(-1, stopWatch.getLastLapTime());
}
@Test
@DisplayName("Check that tick() without start does not change the time")
public void testTicksWithoutStart() {
stopWatch.tick(1);
assertEquals(-1, stopWatch.getTime());
assertEquals(1, stopWatch.getTicks());
stopWatch.tick(4);
assertEquals(-1, stopWatch.getTime());
assertEquals(5, stopWatch.getTicks());
}
@Test
@DisplayName("Start and stop the StopWatch and check that the time is correct")
public void testStartTickStop() {
stopWatch.start();
assertEquals(0, stopWatch.getTime());
assertEquals(0, stopWatch.getTicks());
assertTrue(stopWatch.isStarted());
assertFalse(stopWatch.isStopped());
assertThrows(IllegalStateException.class, () -> {
stopWatch.start();
}, "Cannot start an already running StopWatch");
stopWatch.tick(3);
assertEquals(3, stopWatch.getTime());
assertEquals(3, stopWatch.getTicks());
assertTrue(stopWatch.isStarted());
assertFalse(stopWatch.isStopped());
stopWatch.tick(5);
assertEquals(8, stopWatch.getTime());
assertEquals(8, stopWatch.getTicks());
assertTrue(stopWatch.isStarted());
assertFalse(stopWatch.isStopped());
stopWatch.stop();
assertEquals(8, stopWatch.getTime());
assertEquals(8, stopWatch.getTicks());
assertTrue(stopWatch.isStarted());
assertTrue(stopWatch.isStopped());
assertThrows(IllegalStateException.class, () -> {
stopWatch.stop();
}, "Cannot stop a StopWatch that is already stopped");
}
@Test
@DisplayName("Start and stop the StopWatch, and call tick() while it is not started")
public void testTickStartTickStopTick() {
stopWatch.tick(2);
assertEquals(-1, stopWatch.getTime());
assertEquals(2, stopWatch.getTicks());
assertFalse(stopWatch.isStarted());
assertFalse(stopWatch.isStopped());
stopWatch.start();
assertEquals(0, stopWatch.getTime());
assertEquals(2, stopWatch.getTicks());
assertTrue(stopWatch.isStarted());
assertFalse(stopWatch.isStopped());
stopWatch.tick(3);
assertEquals(3, stopWatch.getTime());
assertEquals(5, stopWatch.getTicks());
assertTrue(stopWatch.isStarted());
assertFalse(stopWatch.isStopped());
stopWatch.tick(5);
assertEquals(8, stopWatch.getTime());
assertEquals(10, stopWatch.getTicks());
assertTrue(stopWatch.isStarted());
assertFalse(stopWatch.isStopped());
stopWatch.stop();
assertEquals(8, stopWatch.getTime());
assertEquals(10, stopWatch.getTicks());
assertTrue(stopWatch.isStarted());
assertTrue(stopWatch.isStopped());
stopWatch.tick(3);
assertEquals(8, stopWatch.getTime());
assertEquals(13, stopWatch.getTicks());
assertTrue(stopWatch.isStarted());
assertTrue(stopWatch.isStopped());
assertThrows(IllegalArgumentException.class, () -> {
stopWatch.tick(-1);
}, "Time should not be able to go backward");
}
@Test
@DisplayName("Check that laps work as expected")
public void testLaps() {
assertThrows(IllegalStateException.class, () -> {
stopWatch.lap();
}, "Should not be able to start a new lap without starting the StopWatch");
stopWatch.start();
assertEquals(0, stopWatch.getTime());
assertEquals(0, stopWatch.getLapTime());
assertEquals(-1, stopWatch.getLastLapTime());
stopWatch.tick(3);
assertEquals(3, stopWatch.getTime());
assertEquals(3, stopWatch.getLapTime());
assertEquals(-1, stopWatch.getLastLapTime());
stopWatch.lap();
assertEquals(3, stopWatch.getTime());
assertEquals(0, stopWatch.getLapTime());
assertEquals(3, stopWatch.getLastLapTime());
stopWatch.tick(5);
assertEquals(8, stopWatch.getTime());
assertEquals(5, stopWatch.getLapTime());
assertEquals(3, stopWatch.getLastLapTime());
stopWatch.stop();
assertEquals(8, stopWatch.getTime());
assertEquals(0, stopWatch.getLapTime());
assertEquals(5, stopWatch.getLastLapTime());
assertThrows(IllegalStateException.class, () -> {
stopWatch.lap();
}, "Should not be able to start a new lap with a stopped StopWatch");
}
}