Add oving 1

This commit is contained in:
Andreas Omholt Olsen
2026-01-09 11:53:16 +01:00
parent 08ff4c2ba2
commit 2556a8f5eb
21 changed files with 1155 additions and 1 deletions

View File

@@ -0,0 +1,55 @@
package oving1;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class AccountTest {
private double delta = 1e-8;
private Account account;
@BeforeEach
public void setUp() {
account = new Account();
}
@Test
@DisplayName("Constructor")
public void testConstructor() {
assertEquals(0.0, account.getBalance(), delta, "Wrong balance for newly created account");
}
@Test
@DisplayName("Deposit")
public void testDeposit() {
account.deposit(100);
assertEquals(100.0, account.getBalance(), delta, "Wrong balance after depositing");
}
@Test
@DisplayName("Negative deposit")
public void testNegativeDeposit() {
account.deposit(-50);
assertEquals(0.0, account.getBalance(), delta,
"Wrong balance after making negative deposit");
}
@Test
@DisplayName("Adding interest")
public void testAddInterest() {
account.setInterestRate(5);
assertEquals(0, account.getBalance(), delta, "Wrong balance after updating interest rate");
assertEquals(5, account.getInterestRate(), delta,
"Wrong interest rate after updating interest rate");
account.deposit(100);
assertEquals(100, account.getBalance(), delta, "Wrong balance after depositing");
account.addInterest();
assertEquals(105, account.getBalance(), delta, "Wrong balance after adding interest");
}
}

View File

@@ -0,0 +1,69 @@
package oving1;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class DigitTest {
private String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
@Test
@DisplayName("Constructor")
public void testConstructor() {
Digit digit = new Digit(10);
assertEquals(0, digit.getValue(), "Wrong value when using constructor");
assertEquals(10, digit.getBase(), "Wrong base when using constructor");
}
@Test
@DisplayName("Increment value")
public void testIncrementedValue() {
for (int base = 2; base < 17; base++) {
Digit digit = new Digit(base);
assertEquals(0, digit.getValue(), "Wrong value for created digit");
int i = 1;
while (i < base) {
boolean result = digit.increment();
assertEquals(i, digit.getValue(), "The value was not incremented correctly");
assertFalse(result,
"Increment should return false when the value is less than the base");
i++;
}
boolean result = digit.increment();
assertEquals(0, digit.getValue(),
"The value was not reset to 0 when it became equal to the base");
assertTrue(result, "Increment should return true when the value is reset to 0");
}
}
@Test
@DisplayName("Increment value and convert to string")
public void testIncrementedToString() {
for (int base = 2; base < 17; base++) {
Digit digit = new Digit(base);
assertEquals("0", digit.toString(), "Wrong string representation");
int i = 1;
while (i < base) {
boolean result = digit.increment();
assertEquals(String.valueOf(digits.charAt(i)), digit.toString(),
"Wrong string representation");
assertFalse(result,
"Increment should return false when the value is less than the base");
i++;
}
boolean result = digit.increment();
assertEquals("0", digit.toString(), "Wrong string representation");
assertTrue(result, "Increment should return true when the value is reset to 0");
}
}
}

View File

@@ -0,0 +1,137 @@
package oving1;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class LineEditorTest {
private LineEditor lineEditor;
private void checkEditorContent(String s) {
assertEquals(s, lineEditor.toString(), "Wrong value returned by toString()");
int pos = s.indexOf('|');
assertEquals(s.substring(0, pos) + s.substring(pos + 1), lineEditor.getText(),
"Wrong text value returned");
assertEquals(pos, lineEditor.getInsertionIndex(), "Wrong insertion index");
}
@BeforeEach
public void setUp() {
lineEditor = new LineEditor();
}
@Test
@DisplayName("Constructor")
public void testContstructor() {
this.checkEditorContent("|");
}
@Test
@DisplayName("Setter methods")
public void testSetters() {
lineEditor.setText("Hello World!");
this.checkEditorContent("|Hello World!");
lineEditor.setInsertionIndex(5);
this.checkEditorContent("Hello| World!");
}
@Test
@DisplayName("Insert string at end")
public void testInsertStringAtEnd() {
lineEditor.insertString("");
this.checkEditorContent("|");
lineEditor.insertString("Java");
this.checkEditorContent("Java|");
lineEditor.insertString(" er gøy!");
this.checkEditorContent("Java er gøy!|");
}
@Test
@DisplayName("Insert string in the middle")
public void testInsertStringMiddle() {
lineEditor.setText("Javagøy!");
lineEditor.setInsertionIndex(4);
lineEditor.insertString(" er ");
this.checkEditorContent("Java er |gøy!");
}
@Test
@DisplayName("Insert string at the begginning")
public void testInsertStringAtBeginning() {
lineEditor.setText("er gøy!");
lineEditor.setInsertionIndex(0);
lineEditor.insertString("Java ");
this.checkEditorContent("Java |er gøy!");
}
@Test
@DisplayName("Move left")
public void testLeft() {
lineEditor.left();
this.checkEditorContent("|");
lineEditor.setText("J");
lineEditor.setInsertionIndex(1);
this.checkEditorContent("J|");
lineEditor.left();
this.checkEditorContent("|J");
}
@Test
@DisplayName("Move right")
public void testRight() {
lineEditor.right();
this.checkEditorContent("|");
lineEditor.setText("J");
lineEditor.setInsertionIndex(0);
this.checkEditorContent("|J");
lineEditor.right();
this.checkEditorContent("J|");
}
@Test
@DisplayName("Delete left")
public void testDeleteLeft() {
lineEditor.deleteLeft();
this.checkEditorContent("|");
lineEditor.insertString("J");
lineEditor.deleteLeft();
this.checkEditorContent("|");
lineEditor.insertString("Java");
lineEditor.setInsertionIndex(2);
this.checkEditorContent("Ja|va");
lineEditor.deleteLeft();
this.checkEditorContent("J|va");
}
@Test
@DisplayName("Delete right")
public void testDeleteRight() {
lineEditor.deleteRight();
this.checkEditorContent("|");
lineEditor.insertString("J");
lineEditor.setInsertionIndex(0);
lineEditor.deleteRight();
this.checkEditorContent("|");
lineEditor.insertString("Java");
lineEditor.setInsertionIndex(2);
this.checkEditorContent("Ja|va");
lineEditor.deleteRight();
this.checkEditorContent("Ja|a");
}
}

View File

@@ -0,0 +1,101 @@
package oving1;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class LocationTest {
private Location loc;
/**
* Check that the position of {@link #loc} is equal to the parameters.
*
* @param x Expected x position
* @param y Expected y position
*/
private void checkPos(int x, int y) {
assertEquals(x, loc.getX(), "Wrong x coordinate");
assertEquals(y, loc.getY(), "Wrong y coordinate");
}
@BeforeEach
public void beforeEach() {
loc = new Location();
}
@Test
@DisplayName("Constructor")
public void testConstructor() {
this.checkPos(0, 0);
}
@Test
@DisplayName("Move up")
public void testUp() {
loc.up();
this.checkPos(0, -1);
loc.up();
this.checkPos(0, -2);
}
@Test
@DisplayName("Move down")
public void testDown() {
loc.down();
this.checkPos(0, 1);
loc.down();
this.checkPos(0, 2);
}
@Test
@DisplayName("Move left")
public void testLeft() {
loc.left();
this.checkPos(-1, 0);
loc.left();
this.checkPos(-2, 0);
}
@Test
@DisplayName("Move right")
public void testRight() {
loc.right();
this.checkPos(1, 0);
loc.right();
this.checkPos(2, 0);
}
@Test
@DisplayName("Move multiple directions")
public void testComplexMovement() {
loc.right();
this.checkPos(1, 0);
loc.down();
this.checkPos(1, 1);
loc.right();
this.checkPos(2, 1);
loc.down();
this.checkPos(2, 2);
loc.left();
this.checkPos(1, 2);
loc.up();
this.checkPos(1, 1);
loc.up();
this.checkPos(1, 0);
loc.left();
this.checkPos(0, 0);
}
}

View File

@@ -0,0 +1,31 @@
package oving1;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static oving1.RectangleTest.assertValues;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class RectangleExtraTest {
@Test
@DisplayName("Intersecting rectangles")
public void testIntersection() {
Rectangle rect1 = new Rectangle(0, 0, 5, 5);
Rectangle rect2 = new Rectangle(3, 3, 7, 7);
Rectangle intersection = rect1.intersection(rect2);
assertValues(intersection, 3, 3, 5, 5, 2, 2, " for intersecting rectangle");
assertTrue(rect1.intersects(rect2), "Wrong value from #intersects(Rectangle)");
}
@Test
@DisplayName("Non-intersecting rectangles")
public void testNonIntersection() {
Rectangle rect1 = new Rectangle(-3, -3, 0, 0);
Rectangle rect2 = new Rectangle(3, 3, 7, 7);
Rectangle intersection = rect1.intersection(rect2);
assertNull(intersection, "Intersection of two non-intersecting rectangles should be null");
assertFalse(rect1.intersects(rect2), "Wrong value from #intersects(Rectangle)");
}
}

View File

@@ -0,0 +1,271 @@
package oving1;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class RectangleTest {
// Used in RectangleExtraTest as well
/**
* Compares all values in a given {@link Rectangle} to a set of expected values.
*
* @param rect The rectangle to check
* @param minX The expected minimum x value of rect
* @param minY The expected minimum y value of rect
* @param maxX The expected maximum x value of rect
* @param maxY The expected maximum y value of rect
* @param width The expected width of rect
* @param height The expected height of rect
*/
public static void assertValues(Rectangle rect, int minX, int minY, int maxX, int maxY,
int width, int height, String suffix) {
assertEquals(minX, rect.getMinX(), "Wrong minX " + suffix);
assertEquals(minY, rect.getMinY(), "Wrong minY " + suffix);
assertEquals(maxX, rect.getMaxX(), "Wrong maxX " + suffix);
assertEquals(maxY, rect.getMaxY(), "Wrong maxY " + suffix);
assertEquals(width, rect.getWidth(), "Wrong width " + suffix);
assertEquals(height, rect.getHeight(), "Wrong height " + suffix);
}
/**
* Check that a rectangle is empty.
*
* @param rect The rectangle to check
*/
private static void assertEmpty(Rectangle rect) {
assertTrue(rect.isEmpty(), "Expected rectangle to be empty!");
assertTrue(rect.getWidth() == 0 || rect.getHeight() == 0,
"Empty rectangle should either have a width or height of 0!");
}
@Test
@DisplayName("Empty rectangle")
public void testEmpty() {
// Test creating empty rectangle
Rectangle rect1 = new Rectangle(0, 0, 0, 0);
assertEmpty(rect1);
// Test creating empty rectangle with only 0 height
Rectangle rect2 = new Rectangle(3, 2, 1, 2);
assertEmpty(rect2);
// Test creating empty rectangle with only 0 width
Rectangle rect3 = new Rectangle(3, 1, 3, 2);
assertEmpty(rect3);
}
@Test
@DisplayName("Rectangle constructor")
public void testConstructor() {
// Simple test
Rectangle rect1 = new Rectangle(0, 0, 1, 2);
RectangleTest.assertValues(rect1, 0, 0, 1, 2, 1, 2, "when testing constructor");
// Test providing points in opposite order
Rectangle rect2 = new Rectangle(1, 2, 0, 0);
RectangleTest.assertValues(rect2, 0, 0, 1, 2, 1, 2, "when testing constructor");
// Test negative values
Rectangle rect3 = new Rectangle(3, 3, -1, 5);
RectangleTest.assertValues(rect3, -1, 3, 3, 5, 4, 2, "when testing constructor");
}
private void testAdd(Rectangle rect, int x, int y, boolean expected) {
assertEquals(expected, rect.add(x, y),
"Wrong value returned when adding (" + x + ", " + y + ") to " + rect);
assertFalse(rect.isEmpty(), "Rectangle should not be empty after adding a point!");
assertTrue(rect.contains(x, y),
"Rectangle should contain the point that has been just added!");
}
@Test
@DisplayName("Adding point to rectangle")
public void testAddXY() {
int x1 = 13;
int y1 = -27;
int x2 = -11;
int y2 = 23;
int x3 = 15;
int y3 = 33;
Rectangle rect = new Rectangle(x1, y1, x2, y2);
// Add (x3, y3) and check that rect is updated accordingly
this.testAdd(rect, x3, y3, true);
int minX1X2 = Math.min(x1, x2), minY1Y2 = Math.min(y1, y2);
int maxX1X2 = Math.max(x1, x2), maxY1Y2 = Math.max(y1, y2);
int minX1X2X3 = Math.min(minX1X2, x3), minY1Y2Y3 = Math.min(minY1Y2, y3);
int maxX1X2X3 = Math.max(maxX1X2, x3), maxY1Y2Y3 = Math.max(maxY1Y2, y3);
RectangleTest.assertValues(rect, minX1X2X3, minY1Y2Y3, maxX1X2X3, maxY1Y2Y3,
maxX1X2X3 - minX1X2X3, maxY1Y2Y3 - minY1Y2Y3,
"when adding point that is not in rectangle");
}
@Test
@DisplayName("Adding point already in rectangle")
public void testAddSameXY() {
int x1 = 13;
int y1 = -27;
int x2 = -11;
int y2 = 23;
int x3 = 15;
int y3 = 33;
int minX1X2 = Math.min(x1, x2);
int minY1Y2 = Math.min(y1, y2);
int maxX1X2 = Math.max(x1, x2);
int maxY1Y2 = Math.max(y1, y2);
int minX1X2X3 = Math.min(minX1X2, x3);
int minY1Y2Y3 = Math.min(minY1Y2, y3);
int maxX1X2X3 = Math.max(maxX1X2, x3);
int maxY1Y2Y3 = Math.max(maxY1Y2, y3);
Rectangle rect = new Rectangle(x1, y1, x2, y2);
// Add (x3, y3) and check that rect is updated accordingly
this.testAdd(rect, x3, y3, true);
RectangleTest.assertValues(rect, minX1X2X3, minY1Y2Y3, maxX1X2X3, maxY1Y2Y3,
maxX1X2X3 - minX1X2X3, maxY1Y2Y3 - minY1Y2Y3,
"when adding point that is not in rectangle");
// Add (x3, y3) again and check that all state is the same, with false response
this.testAdd(rect, x3, y3, false);
RectangleTest.assertValues(rect, minX1X2X3, minY1Y2Y3, maxX1X2X3, maxY1Y2Y3,
maxX1X2X3 - minX1X2X3, maxY1Y2Y3 - minY1Y2Y3,
"when adding point that is in rectangle");
}
@Test
@DisplayName("Adding other rectangle")
public void testAddRectangle() {
// Add a point to this.rect
int x1 = 13;
int y1 = -27;
int x2 = -11;
int y2 = 23;
int x3 = 15;
int y3 = 33;
int minX1X2 = Math.min(x1, x2);
int minY1Y2 = Math.min(y1, y2);
int maxX1X2 = Math.max(x1, x2);
int maxY1Y2 = Math.max(y1, y2);
int minX1X3 = Math.min(x1, x3);
int minY1Y3 = Math.min(y1, y3);
int maxX1X3 = Math.max(x1, x3);
int maxY1Y3 = Math.max(y1, y3);
int minX1X2X3 = Math.min(minX1X2, x3);
int minY1Y2Y3 = Math.min(minY1Y2, y3);
int maxX1X2X3 = Math.max(maxX1X2, x3);
int maxY1Y2Y3 = Math.max(maxY1Y2, y3);
int widthX1X2X3 = maxX1X2X3 - minX1X2X3;
int heightX1X2X3 = maxY1Y2Y3 - minY1Y2Y3;
// Create a rectangle and fill it with some points. Assert that this rect is correct
Rectangle rect = new Rectangle(x1, y1, x2, y2);
RectangleTest.assertValues(rect, minX1X2, minY1Y2, maxX1X2, maxY1Y2, maxX1X2 - minX1X2,
maxY1Y2 - minY1Y2, "when creating new rectangle");
// Create another rectangle and fill it with some points. Assert that this rect is correct
Rectangle rect2 = new Rectangle(x1, y1, x3, y3);
RectangleTest.assertValues(rect2, minX1X3, minY1Y3, maxX1X3, maxY1Y3, maxX1X3 - minX1X3,
maxY1Y3 - minY1Y3, "when creating new rectangle");
// Add rect to this.rect, and check correctness
assertTrue(rect.add(rect2));
RectangleTest.assertValues(rect, minX1X2X3, minY1Y2Y3, maxX1X2X3, maxY1Y2Y3, widthX1X2X3,
heightX1X2X3, "when adding another rectangle");
}
@Test
@DisplayName("Adding rectangle to itself")
public void testAddSameRectangle() {
int x1 = 13;
int y1 = -27;
int x2 = -11;
int y2 = 23;
int width = Math.abs(x1 - x2);
int height = Math.abs(y1 - y2);
int minX1X2 = Math.min(x1, x2);
int minY1Y2 = Math.min(y1, y2);
int maxX1X2 = Math.max(x1, x2);
int maxY1Y2 = Math.max(y1, y2);
// Create a rectangle and fill it with some points. Assert that this rect is correct
Rectangle rect = new Rectangle(x1, y1, x2, y2);
RectangleTest.assertValues(rect, minX1X2, minY1Y2, maxX1X2, maxY1Y2, width, height,
"when creating new rectangle");
// Add rectangle to itself and check that state stays the same
assertFalse(rect.add(rect),
"Expected no change when adding equal rectangle using #add(Rectangle)");
RectangleTest.assertValues(rect, minX1X2, minY1Y2, maxX1X2, maxY1Y2, width, height,
"when adding the same rectangle to itself");
}
@Test
@DisplayName("Rectangle union")
public void testUnion() {
int x1 = 13;
int y1 = -27;
int x2 = -11;
int y2 = 23;
int x3 = 15;
int y3 = 33;
int x4 = 17;
int y4 = -33;
int minX1X2 = Math.min(x1, x2);
int minY1Y2 = Math.min(y1, y2);
int maxX1X2 = Math.max(x1, x2);
int maxY1Y2 = Math.max(y1, y2);
int minX3X4 = Math.min(x3, x4);
int minY3Y4 = Math.min(y3, y4);
int maxX3X4 = Math.max(x3, x4);
int maxY3Y4 = Math.max(y3, y4);
// Create two rectangles and check correctness
Rectangle rect1 = new Rectangle(x1, y1, x2, y2);
RectangleTest.assertValues(rect1, minX1X2, minY1Y2, maxX1X2, maxY1Y2, maxX1X2 - minX1X2,
maxY1Y2 - minY1Y2, "when creating new rectangle");
Rectangle rect2 = new Rectangle(x3, y3, x4, y4);
RectangleTest.assertValues(rect2, minX3X4, minY3Y4, maxX3X4, maxY3Y4, maxX3X4 - minX3X4,
maxY3Y4 - minY3Y4, "when creating new rectangle");
// Take the union (both ways), and check that both are the same
int minX = Math.min(minX1X2, minX3X4);
int minY = Math.min(minY1Y2, minY3Y4);
int maxX = Math.max(maxX1X2, maxX3X4);
int maxY = Math.max(maxY1Y2, maxY3Y4);
Rectangle union1 = rect1.union(rect2);
RectangleTest.assertValues(union1, minX, minY, maxX, maxY, maxX - minX, maxY - minY,
"when calling #union with another rectangle");
RectangleTest.assertValues(rect1, minX1X2, minY1Y2, maxX1X2, maxY1Y2, maxX1X2 - minX1X2,
maxY1Y2 - minY1Y2,
"Values changed when calling #union with another rectangle! Make sure to not "
+ "create a new Rectangle and not modify the current ones!");
RectangleTest.assertValues(rect2, minX3X4, minY3Y4, maxX3X4, maxY3Y4, maxX3X4 - minX3X4,
maxY3Y4 - minY3Y4,
"Values changed when calling #union with another rectangle! Make sure to not "
+ "create a new Rectangle and not modify the current ones!");
Rectangle union2 = rect2.union(rect1);
RectangleTest.assertValues(union2, minX, minY, maxX, maxY, maxX - minX, maxY - minY,
"when calling #union with another rectangle");
RectangleTest.assertValues(rect1, minX1X2, minY1Y2, maxX1X2, maxY1Y2, maxX1X2 - minX1X2,
maxY1Y2 - minY1Y2,
"Values changed when calling #union with another rectangle! Make sure to not "
+ "create a new Rectangle and not modify the current ones!");
RectangleTest.assertValues(rect2, minX3X4, minY3Y4, maxX3X4, maxY3Y4, maxX3X4 - minX3X4,
maxY3Y4 - minY3Y4,
"Values changed when calling #union with another rectangle! Make sure to not "
+ "create a new Rectangle and not modify the current ones!");
}
}

View File

@@ -0,0 +1,153 @@
package oving1;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
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("Constructor")
public void testConstructor() {
assertFalse(stopWatch.isStarted(), "Stopwatch should not be started");
assertFalse(stopWatch.isStopped(), "Stopwatch should not be stopped");
assertEquals(0, stopWatch.getTicks(), "Wrong ticks returned");
assertEquals(-1, stopWatch.getTime(), "Time should be -1 when not started");
assertEquals(-1, stopWatch.getLapTime(), "Lap time should be -1 when not started");
assertEquals(-1, stopWatch.getLastLapTime(), "Last lap time should be -1 when not started");
}
@Test
@DisplayName("Tick without starting")
public void testTicksWithoutStart() {
stopWatch.tick(1);
assertEquals(-1, stopWatch.getTime(), "Time should be -1 when not started");
assertEquals(1, stopWatch.getTicks(), "Ticks should be 1 after calling #tick(1)");
stopWatch.tick(4);
assertEquals(-1, stopWatch.getTime(), "Time should be -1 when not started");
assertEquals(5, stopWatch.getTicks(), "Ticks should be 5 after calling #tick(4)");
}
@Test
@DisplayName("Tick, start and stop 1")
public void testStartTickStop() {
stopWatch.start();
assertEquals(0, stopWatch.getTime(), "Time should be 0 when just started");
assertEquals(0, stopWatch.getTicks(), "Ticks should be 0 when #tick() has not been called");
assertTrue(stopWatch.isStarted(), "Should be started after calling #start()");
assertFalse(stopWatch.isStopped(), "Should not be stopped before calling #stop()");
stopWatch.tick(3);
assertEquals(3, stopWatch.getTime(),
"Time should be 3 when started and #tick(3) has been called");
assertEquals(3, stopWatch.getTicks(), "Ticks should be 3 when #tick(3) has been called");
assertTrue(stopWatch.isStarted(), "Should be started after calling #start()");
assertFalse(stopWatch.isStopped(), "Should not be stopped before calling #stop()");
stopWatch.tick(5);
assertEquals(8, stopWatch.getTime(),
"Time should be 8 when started and #tick(5) has been called");
assertEquals(8, stopWatch.getTicks(), "Ticks should be 8 when #tick(5) has been called");
assertTrue(stopWatch.isStarted(), "Should be started after calling #start()");
assertFalse(stopWatch.isStopped(), "Should not be stopped before calling #stop()");
stopWatch.stop();
assertEquals(8, stopWatch.getTime(), "Time should be 8 after #stop() has been called");
assertEquals(8, stopWatch.getTicks(), "Ticks should be 8 after #stop() has been called");
assertTrue(stopWatch.isStarted(), "Should be started even after #stop() has been called");
assertTrue(stopWatch.isStopped(), "Should be stopped after calling #stop()");
}
@Test
@DisplayName("Tick, start and stop 2")
public void testTickStartTickStopTick() {
stopWatch.tick(2);
assertEquals(-1, stopWatch.getTime(), "Time should be -1 when not started");
assertEquals(2, stopWatch.getTicks(), "Ticks should be 2 when #tick(2) has been called");
assertFalse(stopWatch.isStarted(), "Stopwatch should not be started");
assertFalse(stopWatch.isStopped(), "Stopwatch should not be stopped");
stopWatch.start();
assertEquals(0, stopWatch.getTime(), "Time should be 0 when just started");
assertEquals(2, stopWatch.getTicks(), "Ticks should be 2 after #start() has been called");
assertTrue(stopWatch.isStarted(), "Should be started after calling #start()");
assertFalse(stopWatch.isStopped(), "Should not be stopped before calling #stop()");
stopWatch.tick(3);
assertEquals(3, stopWatch.getTime(),
"Time should be 3 when started and #tick(3) has been called");
assertEquals(5, stopWatch.getTicks(), "Ticks should be 5 when #tick(3) has been called");
assertTrue(stopWatch.isStarted(), "Should be started after calling #tick(3)");
assertFalse(stopWatch.isStopped(), "Should not be stopped before calling #tick(3)");
stopWatch.tick(5);
assertEquals(8, stopWatch.getTime(),
"Time should be 8 when started and #tick(5) has been called");
assertEquals(10, stopWatch.getTicks(), "Ticks should be 10 when #tick(5) has been called");
assertTrue(stopWatch.isStarted(), "Should be started after calling #tick(5)");
assertFalse(stopWatch.isStopped(), "Should not be stopped before calling #tick(5)");
stopWatch.stop();
assertEquals(8, stopWatch.getTime(), "Time should be 8 after #stop() has been called");
assertEquals(10, stopWatch.getTicks(), "Ticks should be 10 after #stop() has been called");
assertTrue(stopWatch.isStarted(), "Should be started even after #stop() has been called");
assertTrue(stopWatch.isStopped(), "Should be stopped after calling #stop()");
stopWatch.tick(3);
assertEquals(8, stopWatch.getTime(),
"Time should be 8 after #tick(3) has been called while stopped");
assertEquals(13, stopWatch.getTicks(),
"Ticks should be 13 when #tick(3) has been called while stopped");
assertTrue(stopWatch.isStarted(),
"Should be started even after #tick() has been called while stopped");
assertTrue(stopWatch.isStopped(), "Should be stopped after calling #tick() while stopped");
}
@Test
@DisplayName("Lap times")
public void testLaps() {
stopWatch.start();
assertEquals(0, stopWatch.getTime(), "Time should be 0 when just started");
assertEquals(0, stopWatch.getLapTime(), "Lap time should be 0 when just started");
assertEquals(-1, stopWatch.getLastLapTime(),
"Last lap time should be -1 when there is no previous lap");
stopWatch.tick(3);
assertEquals(3, stopWatch.getTime(), "Time should be 3 after #tick(3) has been called");
assertEquals(3, stopWatch.getLapTime(),
"Lap time should be 3 after calling #tick(3) while started");
assertEquals(-1, stopWatch.getLastLapTime(),
"Last lap time should be -1 when there is no previous lap");
stopWatch.lap();
assertEquals(3, stopWatch.getTime(), "Time should still be 3 after starting a new lap");
assertEquals(0, stopWatch.getLapTime(), "Current lap time should be 0 when just started");
assertEquals(3, stopWatch.getLastLapTime(),
"Last lap time should be 3 when we just started a new lap");
stopWatch.tick(5);
assertEquals(8, stopWatch.getTime(), "Time should be 8 after #tick(5) has been called");
assertEquals(5, stopWatch.getLapTime(),
"Current lap time should be 5 after calling #tick(5)");
assertEquals(3, stopWatch.getLastLapTime(),
"Last lap time should be 3 even after time passes");
stopWatch.stop();
assertEquals(8, stopWatch.getTime(), "Time should be 8 after stopping");
assertEquals(0, stopWatch.getLapTime(), "Current lap time should be 0 when stopped");
assertEquals(5, stopWatch.getLastLapTime(),
"Last lap should be the lap time of the current lap when stopping");
}
}

View File

@@ -0,0 +1,57 @@
package oving1;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class UpOrDownCounterTest {
@Test
@DisplayName("Count up")
public void testCountUp() {
UpOrDownCounter counter = new UpOrDownCounter(1, 5);
assertEquals(1, counter.getCounter(),
"Counter value should be equal to starting value when created");
for (int i = 2; i < 5; i++) {
boolean result = counter.count();
assertEquals(i, counter.getCounter(), "Wrong counter value");
assertTrue(result, "#count() should return true while within legal values");
}
boolean result = counter.count();
assertEquals(5, counter.getCounter(), "Wrong counter value");
assertFalse(result, "#count() should return false when we cannot count further");
result = counter.count();
assertEquals(5, counter.getCounter(),
"Counter value should not change when counting beyond legal values");
assertFalse(result, "#count() should return false when we cannot count further");
}
@Test
@DisplayName("Count down")
public void testCountDown() {
UpOrDownCounter counter = new UpOrDownCounter(1, -5);
assertEquals(1, counter.getCounter(),
"Counter value should be equal to starting value when created");
for (int i = 0; i > -5; i--) {
boolean result = counter.count();
assertEquals(i, counter.getCounter(), "Wrong counter value");
assertTrue(result, "#count() should return true while within legal values");
}
boolean result = counter.count();
assertEquals(-5, counter.getCounter(), "Wrong counter value");
assertFalse(result, "#count() should return false when we cannot count further");
result = counter.count();
assertEquals(-5, counter.getCounter(),
"Counter value should not change when counting beyond legal values");
assertFalse(result, "#count() should return false when we cannot count further");
}
}