/** * 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.lang.reflect.Method; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.annotation.ElementType; public class ClockEqualMain { @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") @Disabled public void clocksAMinuteApartAreNotEqual() { runTest(() -> assertNotEquals(new Clock(15, 36), new Clock(15, 37))); } @Test(name = "clocks an hour apart") @Disabled public void clocksAnHourApartAreNotEqual() { runTest(() -> assertNotEquals(new Clock(14, 37), new Clock(15, 37))); } @Test(name = "clocks with hour overflow") @Disabled public void clocksWithHourOverflow() { runTest(() -> assertEquals(new Clock(34, 37), new Clock(10, 37))); } @Test(name = "clocks with hour overflow by several days") @Disabled public void clocksWithHourOverflowBySeveralDays() { runTest(() -> assertEquals(new Clock(99, 11), new Clock(3, 11))); } @Test(name = "clocks with negative hour") @Disabled public void clocksWithNegateHour() { runTest(() -> assertEquals(new Clock(-2, 40), new Clock(22, 40))); } @Test(name = "clocks with negative hour that wraps") @Disabled public void clocksWithNegativeHourThatWraps() { runTest(() -> assertEquals(new Clock(-31, 3), new Clock(17, 3))); } @Test(name = "clocks with negative hour that wraps multiple times") @Disabled public void clocksWithNegativeHourThatWrapsMultipleTimes() { runTest(() -> assertEquals(new Clock(-83, 49), new Clock(13, 49))); } @Test(name = "clocks with minute overflow") @Disabled public void clocksWithMinuteOverflow() { runTest(() -> assertEquals(new Clock(0, 1441), new Clock(0, 1))); } @Test(name = "clocks with minute overflow by several days") @Disabled public void clocksWithMinuteOverflowBySeveralDays() { runTest(() -> assertEquals(new Clock(2, 4322), new Clock(2, 2))); } @Test(name = "clocks with negative minute") @Disabled public void clocksWithNegativeMinutes() { runTest(() -> assertEquals(new Clock(3, -20), new Clock(2, 40))); } @Test(name = "clocks with negative minute that wraps") @Disabled public void clocksWithNegativeMinutesThatWraps() { runTest(() -> assertEquals(new Clock(5, -1490), new Clock(4, 10))); } @Test(name = "clocks with negative minute that wraps multiple times") @Disabled public void clocksWithNegativeMinutesThatWrapsMultipleTimes() { runTest(() -> assertEquals(new Clock(6, -4305), new Clock(6, 15))); } @Test(name = "clocks with negative hours and minutes") @Disabled public void clocksWithNegativeHoursAndMinutes() { runTest(() -> assertEquals(new Clock(-12, -268), new Clock(7, 32))); } @Test(name = "clocks with negative hours and minutes that wrap") @Disabled public void clocksWithNegativeHoursAndMinutesThatWrap() { runTest(() -> assertEquals(new Clock(-54, -11513), new Clock(18, 7))); } @Test(name = "full clock and zeroed clock") @Disabled public void clocksWithFullClockAndZeroedClockAreEqual() { runTest(() -> assertEquals(new Clock(24, 0), new Clock(0, 0))); } private void runTest(Runnable body) { try { body.run(); System.out.println("TEST PASSED"); } catch (Exception | AssertionError t) { System.out.println("TEST FAILED: " + t.getMessage()); } } 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 static void main(String[] args) { new ClockEqualMain().runTests(); } /** * Annotation to mark test methods. The name parameter is used to identify the * test in the output. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) private @interface Test { String name(); } /** * Annotation to mark test methods as disabled. Disabled tests will not be * executed when running the tests. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) private @interface Disabled { } private void runTests() { Method[] methods = this.getClass().getDeclaredMethods(); List allTestMethods = Arrays.asList(methods).stream().filter(m -> m.isAnnotationPresent(Test.class)) .collect(Collectors.toList()); List enabledTestMethods = allTestMethods.stream().filter(m -> !m.isAnnotationPresent(Disabled.class)) .collect(Collectors.toList()); System.out .println("Running " + enabledTestMethods.size() + " out of " + allTestMethods.size() + " tests... \n"); for (Method method : enabledTestMethods) { try { System.out.println("Running test: " + method.getAnnotation(Test.class).name()); method.invoke(this); } catch (Exception e) { System.out.println("Test " + method.getAnnotation(Test.class).name() + " failed with an unexpected exception: " + e.getCause()); } } System.out.println("Finished running tests."); if (allTestMethods.size() != enabledTestMethods.size()) { System.out.println("\nThe following tests are disabled and were not executed:"); for (Method method : allTestMethods) { if (method.isAnnotationPresent(Disabled.class)) { System.out.println("- " + method.getAnnotation(Test.class).name()); } } System.out.println( "To enable these tests, remove the @Disabled annotation from the corresponding test methods."); } } }