Files
jack/clock/ClockEqualTest.java

153 lines
5.8 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 ClockEqualTest {
private static final int MAX_POINTS = 100;
private static final int TEST_CASES = 16;
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 = "clocks with same time")
public void clocksWithSameTimeAreEqual() {
runTest(() -> assertEquals(new Clock(15, 37), new Clock(15, 37)));
}
@Test(name = "clocks a minute apart")
public void clocksAMinuteApartAreNotEqual() {
runTest(() -> assertNotEquals(new Clock(15, 36), new Clock(15, 37)));
}
@Test(name = "clocks an hour apart")
public void clocksAnHourApartAreNotEqual() {
runTest(() -> assertNotEquals(new Clock(14, 37), new Clock(15, 37)));
}
@Test(name = "clocks with hour overflow")
public void clocksWithHourOverflow() {
runTest(() -> assertEquals(new Clock(34, 37), new Clock(10, 37)));
}
@Test(name = "clocks with hour overflow by several days")
public void clocksWithHourOverflowBySeveralDays() {
runTest(() -> assertEquals(new Clock(99, 11), new Clock(3, 11)));
}
@Test(name = "clocks with negative hour")
public void clocksWithNegateHour() {
runTest(() -> assertEquals(new Clock(-2, 40), new Clock(22, 40)));
}
@Test(name = "clocks with negative hour that wraps")
public void clocksWithNegativeHourThatWraps() {
runTest(() -> assertEquals(new Clock(-31, 3), new Clock(17, 3)));
}
@Test(name = "clocks with negative hour that wraps multiple times")
public void clocksWithNegativeHourThatWrapsMultipleTimes() {
runTest(() -> assertEquals(new Clock(-83, 49), new Clock(13, 49)));
}
@Test(name = "clocks with minute overflow")
public void clocksWithMinuteOverflow() {
runTest(() -> assertEquals(new Clock(0, 1441), new Clock(0, 1)));
}
@Test(name = "clocks with minute overflow by several days")
public void clocksWithMinuteOverflowBySeveralDays() {
runTest(() -> assertEquals(new Clock(2, 4322), new Clock(2, 2)));
}
@Test(name = "clocks with negative minute")
public void clocksWithNegativeMinutes() {
runTest(() -> assertEquals(new Clock(3, -20), new Clock(2, 40)));
}
@Test(name = "clocks with negative minute that wraps")
public void clocksWithNegativeMinutesThatWraps() {
runTest(() -> assertEquals(new Clock(5, -1490), new Clock(4, 10)));
}
@Test(name = "clocks with negative minute that wraps multiple times")
public void clocksWithNegativeMinutesThatWrapsMultipleTimes() {
runTest(() -> assertEquals(new Clock(6, -4305), new Clock(6, 15)));
}
@Test(name = "clocks with negative hours and minutes")
public void clocksWithNegativeHoursAndMinutes() {
runTest(() -> assertEquals(new Clock(-12, -268), new Clock(7, 32)));
}
@Test(name = "clocks with negative hours and minutes that wrap")
public void clocksWithNegativeHoursAndMinutesThatWrap() {
runTest(() -> assertEquals(new Clock(-54, -11513), new Clock(18, 7)));
}
@Test(name = "full clock and zeroed clock")
public void clocksWithFullClockAndZeroedClockAreEqual() {
runTest(() -> assertEquals(new Clock(24, 0), new Clock(0, 0)));
}
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(Object expected, Object actual) {
if (expected == null && actual == null)
return;
if (expected == null || !expected.equals(actual)) {
throw new AssertionError("Expected: " + expected + " but was: " + actual);
}
}
private void assertNotEquals(Object first, Object second) {
if (first == null && second == null) {
throw new AssertionError("Expected not equals but both were null");
}
if (first != null && first.equals(second)) {
throw new AssertionError("Expected not equals but both were equal: " + first);
}
}
public int getResult() {
int sum = testPoints.stream().mapToInt(Integer::intValue).sum();
return sum / SUCCESS_POINTS == TEST_CASES ? MAX_POINTS : sum;
}
}