Files
jack/clock/ClockCreationTest.java

164 lines
5.9 KiB
Java

/**
* 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 de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework;
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework.Test;
public class ClockCreationTest {
private static final int MAX_POINTS = 100;
private static final int TEST_CASES = 20;
private static final int SUCCESS_POINTS = MAX_POINTS / TEST_CASES;
private static final int FAILURE_POINTS = 0;
private final java.util.List<Integer> testPoints = new java.util.ArrayList<>();
@Test(name = "on the hour")
public void canPrintTimeOnTheHour() {
runTest(() -> assertEquals("08:00", new Clock(8, 0).toString()));
}
@Test(name = "past the hour")
public void canPrintTimeWithMinutes() {
runTest(() -> assertEquals("11:09", new Clock(11, 9).toString()));
}
@Test(name = "midnight is zero hours")
public void midnightPrintsAsZero() {
runTest(() -> assertEquals("00:00", new Clock(24, 0).toString()));
}
@Test(name = "hour rolls over")
public void hourRollsOver() {
runTest(() -> assertEquals("01:00", new Clock(25, 0).toString()));
}
@Test(name = "hour rolls over continuously")
public void hourRollsOverContinuously() {
runTest(() -> assertEquals("04:00", new Clock(100, 0).toString()));
}
@Test(name = "sixty minutes is next hour")
public void sixtyMinutesIsNextHour() {
runTest(() -> assertEquals("02:00", new Clock(1, 60).toString()));
}
@Test(name = "minutes roll over")
public void minutesRollOver() {
runTest(() -> assertEquals("02:40", new Clock(0, 160).toString()));
}
@Test(name = "minutes roll over continuously")
public void minutesRollOverContinuously() {
runTest(() -> assertEquals("04:43", new Clock(0, 1723).toString()));
}
@Test(name = "hour and minutes roll over")
public void hourAndMinutesRollOver() {
runTest(() -> assertEquals("03:40", new Clock(25, 160).toString()));
}
@Test(name = "hour and minutes roll over continuously")
public void hourAndMinutesRollOverContinuously() {
runTest(() -> assertEquals("11:01", new Clock(201, 3001).toString()));
}
@Test(name = "hour and minutes roll over to exactly midnight")
public void hourAndMinutesRollOverToExactlyMidnight() {
runTest(() -> assertEquals("00:00", new Clock(72, 8640).toString()));
}
@Test(name = "negative hour")
public void negativeHour() {
runTest(() -> assertEquals("23:15", new Clock(-1, 15).toString()));
}
@Test(name = "negative hour rolls over")
public void negativeHourRollsOver() {
runTest(() -> assertEquals("23:00", new Clock(-25, 0).toString()));
}
@Test(name = "negative hour rolls over continuously")
public void negativeHourRollsOverContinuously() {
runTest(() -> assertEquals("05:00", new Clock(-91, 0).toString()));
}
@Test(name = "negative minutes")
public void negativeMinutes() {
runTest(() -> assertEquals("00:20", new Clock(1, -40).toString()));
}
@Test(name = "negative minutes roll over")
public void negativeMinutesRollOver() {
runTest(() -> assertEquals("22:20", new Clock(1, -160).toString()));
}
@Test(name = "negative minutes roll over continuously")
public void negativeMinutesRollOverContinuously() {
runTest(() -> assertEquals("16:40", new Clock(1, -4820).toString()));
}
@Test(name = "negative sixty minutes is previous hour")
public void negativeSixtyMinutesIsPreviousHour() {
runTest(() -> assertEquals("01:00", new Clock(2, -60).toString()));
}
@Test(name = "negative hour and minutes both roll over")
public void negativeHourAndMinutesBothRollOver() {
runTest(() -> assertEquals("20:20", new Clock(-25, -160).toString()));
}
@Test(name = "negative hour and minutes both roll over continuously")
public void negativeHourAndMinutesBothRollOverContinuously() {
runTest(() -> assertEquals("22:10", new Clock(-121, -5810).toString()));
}
private void runTest(Runnable body) {
try {
body.run();
testPoints.add(SUCCESS_POINTS);
} catch (Throwable t) {
TracingFramework.printError(t.getMessage());
testPoints.add(FAILURE_POINTS);
}
}
private void assertEquals(String expected, String actual) {
if (expected == null && actual == null)
return;
if (expected == null || !expected.equals(actual)) {
throw new AssertionError("Expected: " + expected + " but was: " + actual);
}
}
public int getResult() {
int sum = testPoints.stream().mapToInt(Integer::intValue).sum();
return sum / SUCCESS_POINTS == TEST_CASES ? MAX_POINTS : sum;
}
}