From 67c4d79b69b250cc131342b908be1d5aceef2d04 Mon Sep 17 00:00:00 2001 From: Atharv Patil Date: Sun, 31 Aug 2025 23:28:59 +0530 Subject: [PATCH] circular-buffer, clock, collatz-conjecture --- .../src/test/java/CircularBufferTest.java | 15 +++++++++++++ .../clock/src/test/java/ClockAddTest.java | 17 +++++++++++++++ .../src/test/java/ClockCreationTest.java | 21 +++++++++++++++++++ .../clock/src/test/java/ClockEqualTest.java | 17 +++++++++++++++ .../src/test/java/CollatzCalculatorTest.java | 7 +++++++ 5 files changed, 77 insertions(+) diff --git a/exercises/practice/circular-buffer/src/test/java/CircularBufferTest.java b/exercises/practice/circular-buffer/src/test/java/CircularBufferTest.java index 1596b3535..6ecbc1930 100644 --- a/exercises/practice/circular-buffer/src/test/java/CircularBufferTest.java +++ b/exercises/practice/circular-buffer/src/test/java/CircularBufferTest.java @@ -2,11 +2,13 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; public class CircularBufferTest { @Test + @DisplayName("reading empty buffer should fail") public void readingFromEmptyBufferShouldThrowException() { CircularBuffer buffer = new CircularBuffer<>(1); @@ -17,6 +19,7 @@ public void readingFromEmptyBufferShouldThrowException() { @Disabled("Remove to run test") @Test + @DisplayName("can read an item just written") public void canReadItemJustWritten() throws BufferIOException { CircularBuffer buffer = new CircularBuffer<>(1); @@ -26,6 +29,7 @@ public void canReadItemJustWritten() throws BufferIOException { @Disabled("Remove to run test") @Test + @DisplayName("each item may only be read once") public void canReadItemOnlyOnce() throws BufferIOException { CircularBuffer buffer = new CircularBuffer<>(1); @@ -39,6 +43,7 @@ public void canReadItemOnlyOnce() throws BufferIOException { @Disabled("Remove to run test") @Test + @DisplayName("items are read in the order they are written") public void readsItemsInOrderWritten() throws BufferIOException { CircularBuffer buffer = new CircularBuffer<>(2); @@ -50,6 +55,7 @@ public void readsItemsInOrderWritten() throws BufferIOException { @Disabled("Remove to run test") @Test + @DisplayName("full buffer can't be written to") public void fullBufferCantBeWrittenTo() throws BufferIOException { CircularBuffer buffer = new CircularBuffer<>(1); @@ -62,6 +68,7 @@ public void fullBufferCantBeWrittenTo() throws BufferIOException { @Disabled("Remove to run test") @Test + @DisplayName("a read frees up capacity for another write") public void readFreesUpSpaceForWrite() throws BufferIOException { CircularBuffer buffer = new CircularBuffer<>(1); @@ -73,6 +80,7 @@ public void readFreesUpSpaceForWrite() throws BufferIOException { @Disabled("Remove to run test") @Test + @DisplayName("read position is maintained even across multiple writes") public void maintainsReadPositionAcrossWrites() throws BufferIOException { CircularBuffer buffer = new CircularBuffer<>(3); @@ -86,6 +94,7 @@ public void maintainsReadPositionAcrossWrites() throws BufferIOException { @Disabled("Remove to run test") @Test + @DisplayName("items cleared out of buffer can't be read") public void cantReadClearedItems() throws BufferIOException { CircularBuffer buffer = new CircularBuffer<>(1); @@ -99,6 +108,7 @@ public void cantReadClearedItems() throws BufferIOException { @Disabled("Remove to run test") @Test + @DisplayName("clear frees up capacity for another write") public void clearFreesUpCapacity() throws BufferIOException { CircularBuffer buffer = new CircularBuffer<>(1); @@ -110,6 +120,7 @@ public void clearFreesUpCapacity() throws BufferIOException { @Disabled("Remove to run test") @Test + @DisplayName("clear does nothing on empty buffer") public void clearDoesNothingOnEmptyBuffer() throws BufferIOException { CircularBuffer buffer = new CircularBuffer<>(1); @@ -120,6 +131,7 @@ public void clearDoesNothingOnEmptyBuffer() throws BufferIOException { @Disabled("Remove to run test") @Test + @DisplayName("overwrite acts like write on non-full buffer") public void overwriteActsLikeWriteOnNonFullBuffer() throws BufferIOException { CircularBuffer buffer = new CircularBuffer<>(2); @@ -131,6 +143,7 @@ public void overwriteActsLikeWriteOnNonFullBuffer() throws BufferIOException { @Disabled("Remove to run test") @Test + @DisplayName("overwrite replaces the oldest item on full buffer") public void overwriteRemovesOldestElementOnFullBuffer() throws BufferIOException { CircularBuffer buffer = new CircularBuffer<>(2); @@ -143,6 +156,7 @@ public void overwriteRemovesOldestElementOnFullBuffer() throws BufferIOException @Disabled("Remove to run test") @Test + @DisplayName("overwrite replaces the oldest item remaining in buffer following a read") public void overwriteDoesntRemoveAnAlreadyReadElement() throws BufferIOException { CircularBuffer buffer = new CircularBuffer<>(3); @@ -159,6 +173,7 @@ public void overwriteDoesntRemoveAnAlreadyReadElement() throws BufferIOException @Disabled("Remove to run test") @Test + @DisplayName("initial clear does not affect wrapping around") public void initialClearDoesNotAffectWrappingAround() throws BufferIOException { CircularBuffer buffer = new CircularBuffer<>(2); diff --git a/exercises/practice/clock/src/test/java/ClockAddTest.java b/exercises/practice/clock/src/test/java/ClockAddTest.java index 34c22e06b..1a82b687e 100644 --- a/exercises/practice/clock/src/test/java/ClockAddTest.java +++ b/exercises/practice/clock/src/test/java/ClockAddTest.java @@ -1,4 +1,5 @@ import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -7,6 +8,7 @@ public class ClockAddTest { @Disabled("Remove to run test") @Test + @DisplayName("add minutes") public void addMinutes() { Clock clock = new Clock(10, 0); clock.add(3); @@ -16,6 +18,7 @@ public void addMinutes() { @Disabled("Remove to run test") @Test + @DisplayName("add no minutes") public void addNoMinutes() { Clock clock = new Clock(6, 41); clock.add(0); @@ -25,6 +28,7 @@ public void addNoMinutes() { @Disabled("Remove to run test") @Test + @DisplayName("add to next hour") public void addToNextHour() { Clock clock = new Clock(0, 45); clock.add(40); @@ -34,6 +38,7 @@ public void addToNextHour() { @Disabled("Remove to run test") @Test + @DisplayName("add more than one hour") public void addMoreThanOneHour() { Clock clock = new Clock(10, 0); clock.add(61); @@ -43,6 +48,7 @@ public void addMoreThanOneHour() { @Disabled("Remove to run test") @Test + @DisplayName("add more than two hours with carry") public void addMoreThanTwoHoursWithCarry() { Clock clock = new Clock(0, 45); clock.add(160); @@ -52,6 +58,7 @@ public void addMoreThanTwoHoursWithCarry() { @Disabled("Remove to run test") @Test + @DisplayName("add across midnight") public void addAcrossMidnight() { Clock clock = new Clock(23, 59); clock.add(2); @@ -61,6 +68,7 @@ public void addAcrossMidnight() { @Disabled("Remove to run test") @Test + @DisplayName("add more than one day (1500 min = 25 hrs)") public void addMoreThanOneDay() { Clock clock = new Clock(5, 32); clock.add(1500); @@ -70,6 +78,7 @@ public void addMoreThanOneDay() { @Disabled("Remove to run test") @Test + @DisplayName("add more than two days") public void addMoreThanTwoDays() { Clock clock = new Clock(1, 1); clock.add(3500); @@ -79,6 +88,7 @@ public void addMoreThanTwoDays() { @Disabled("Remove to run test") @Test + @DisplayName("subtract minutes") public void subtractMinutes() { Clock clock = new Clock(10, 3); clock.add(-3); @@ -88,6 +98,7 @@ public void subtractMinutes() { @Disabled("Remove to run test") @Test + @DisplayName("subtract to previous hour") public void subtractToPreviousHour() { Clock clock = new Clock(10, 3); clock.add(-30); @@ -97,6 +108,7 @@ public void subtractToPreviousHour() { @Disabled("Remove to run test") @Test + @DisplayName("subtract more than an hour") public void subtractMoreThanAnHour() { Clock clock = new Clock(10, 3); clock.add(-70); @@ -106,6 +118,7 @@ public void subtractMoreThanAnHour() { @Disabled("Remove to run test") @Test + @DisplayName("subtract across midnight") public void subtractAcrossMidnight() { Clock clock = new Clock(0, 3); clock.add(-4); @@ -115,6 +128,7 @@ public void subtractAcrossMidnight() { @Disabled("Remove to run test") @Test + @DisplayName("subtract more than two hours") public void subtractMoreThanTwoHours() { Clock clock = new Clock(0, 0); clock.add(-160); @@ -124,6 +138,7 @@ public void subtractMoreThanTwoHours() { @Disabled("Remove to run test") @Test + @DisplayName("subtract more than two hours with borrow") public void subtractMoreThanTwoHoursWithBorrow() { Clock clock = new Clock(6, 15); clock.add(-160); @@ -133,6 +148,7 @@ public void subtractMoreThanTwoHoursWithBorrow() { @Disabled("Remove to run test") @Test + @DisplayName("subtract more than one day (1500 min = 25 hrs)") public void subtractMoreThanOneDay() { Clock clock = new Clock(5, 32); clock.add(-1500); @@ -142,6 +158,7 @@ public void subtractMoreThanOneDay() { @Disabled("Remove to run test") @Test + @DisplayName("subtract more than two days") public void subtractMoreThanTwoDays() { Clock clock = new Clock(2, 20); clock.add(-3000); diff --git a/exercises/practice/clock/src/test/java/ClockCreationTest.java b/exercises/practice/clock/src/test/java/ClockCreationTest.java index 84f68d60d..674d1fee7 100644 --- a/exercises/practice/clock/src/test/java/ClockCreationTest.java +++ b/exercises/practice/clock/src/test/java/ClockCreationTest.java @@ -1,4 +1,5 @@ import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -6,120 +7,140 @@ public class ClockCreationTest { @Test + @DisplayName("on the hour") public void canPrintTimeOnTheHour() { assertThat(new Clock(8, 0).toString()).isEqualTo("08:00"); } @Disabled("Remove to run test") @Test + @DisplayName("past the hour") public void canPrintTimeWithMinutes() { assertThat(new Clock(11, 9).toString()).isEqualTo("11:09"); } @Disabled("Remove to run test") @Test + @DisplayName("midnight is zero hours") public void midnightPrintsAsZero() { assertThat(new Clock(24, 0).toString()).isEqualTo("00:00"); } @Disabled("Remove to run test") @Test + @DisplayName("hour rolls over") public void hourRollsOver() { assertThat(new Clock(25, 0).toString()).isEqualTo("01:00"); } @Disabled("Remove to run test") @Test + @DisplayName("hour rolls over continuously") public void hourRollsOverContinuously() { assertThat(new Clock(100, 0).toString()).isEqualTo("04:00"); } @Disabled("Remove to run test") @Test + @DisplayName("sixty minutes is next hour") public void sixtyMinutesIsNextHour() { assertThat(new Clock(1, 60).toString()).isEqualTo("02:00"); } @Disabled("Remove to run test") @Test + @DisplayName("minutes roll over") public void minutesRollOver() { assertThat(new Clock(0, 160).toString()).isEqualTo("02:40"); } @Disabled("Remove to run test") @Test + @DisplayName("minutes roll over continuously") public void minutesRollOverContinuously() { assertThat(new Clock(0, 1723).toString()).isEqualTo("04:43"); } @Disabled("Remove to run test") @Test + @DisplayName("hour and minutes roll over") public void hourAndMinutesRollOver() { assertThat(new Clock(25, 160).toString()).isEqualTo("03:40"); } @Disabled("Remove to run test") @Test + @DisplayName("hour and minutes roll over continuously") public void hourAndMinutesRollOverContinuously() { assertThat(new Clock(201, 3001).toString()).isEqualTo("11:01"); } @Disabled("Remove to run test") @Test + @DisplayName("hour and minutes roll over to exactly midnight") public void hourAndMinutesRollOverToExactlyMidnight() { assertThat(new Clock(72, 8640).toString()).isEqualTo("00:00"); } @Disabled("Remove to run test") @Test + @DisplayName("negative hour") public void negativeHour() { assertThat(new Clock(-1, 15).toString()).isEqualTo("23:15"); } @Disabled("Remove to run test") @Test + @DisplayName("negative hour rolls over") public void negativeHourRollsOver() { assertThat(new Clock(-25, 0).toString()).isEqualTo("23:00"); } @Disabled("Remove to run test") @Test + @DisplayName("negative hour rolls over continuously") public void negativeHourRollsOverContinuously() { assertThat(new Clock(-91, 0).toString()).isEqualTo("05:00"); } @Disabled("Remove to run test") @Test + @DisplayName("negative minutes") public void negativeMinutes() { assertThat(new Clock(1, -40).toString()).isEqualTo("00:20"); } @Disabled("Remove to run test") @Test + @DisplayName("negative minutes roll over") public void negativeMinutesRollOver() { assertThat(new Clock(1, -160).toString()).isEqualTo("22:20"); } @Disabled("Remove to run test") @Test + @DisplayName("negative minutes roll over continuously") public void negativeMinutesRollOverContinuously() { assertThat(new Clock(1, -4820).toString()).isEqualTo("16:40"); } @Disabled("Remove to run test") @Test + @DisplayName("negative sixty minutes is previous hour") public void negativeSixtyMinutesIsPreviousHour() { assertThat(new Clock(2, -60).toString()).isEqualTo("01:00"); } @Disabled("Remove to run test") @Test + @DisplayName("negative hour and minutes both roll over") public void negativeHourAndMinutesBothRollOver() { assertThat(new Clock(-25, -160).toString()).isEqualTo("20:20"); } @Disabled("Remove to run test") @Test + @DisplayName("negative hour and minutes both roll over continuously") public void negativeHourAndMinutesBothRollOverContinuously() { assertThat(new Clock(-121, -5810).toString()).isEqualTo("22:10"); } diff --git a/exercises/practice/clock/src/test/java/ClockEqualTest.java b/exercises/practice/clock/src/test/java/ClockEqualTest.java index 0ec041f71..d73687773 100644 --- a/exercises/practice/clock/src/test/java/ClockEqualTest.java +++ b/exercises/practice/clock/src/test/java/ClockEqualTest.java @@ -1,4 +1,5 @@ import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -7,6 +8,7 @@ public class ClockEqualTest { @Disabled("Remove to run test") @Test + @DisplayName("clocks with same time") public void clocksWithSameTimeAreEqual() { assertThat(new Clock(15, 37)) .isEqualTo(new Clock(15, 37)); @@ -14,6 +16,7 @@ public void clocksWithSameTimeAreEqual() { @Disabled("Remove to run test") @Test + @DisplayName("clocks a minute apart") public void clocksAMinuteApartAreNotEqual() { assertThat(new Clock(15, 36)) .isNotEqualTo(new Clock(15, 37)); @@ -21,6 +24,7 @@ public void clocksAMinuteApartAreNotEqual() { @Disabled("Remove to run test") @Test + @DisplayName("clocks an hour apart") public void clocksAnHourApartAreNotEqual() { assertThat(new Clock(14, 37)) .isNotEqualTo(new Clock(15, 37)); @@ -28,6 +32,7 @@ public void clocksAnHourApartAreNotEqual() { @Disabled("Remove to run test") @Test + @DisplayName("clocks with hour overflow") public void clocksWithHourOverflow() { assertThat(new Clock(34, 37)) .isEqualTo(new Clock(10, 37)); @@ -35,6 +40,7 @@ public void clocksWithHourOverflow() { @Disabled("Remove to run test") @Test + @DisplayName("clocks with hour overflow by several days") public void clocksWithHourOverflowBySeveralDays() { assertThat(new Clock(99, 11)) .isEqualTo(new Clock(3, 11)); @@ -42,6 +48,7 @@ public void clocksWithHourOverflowBySeveralDays() { @Disabled("Remove to run test") @Test + @DisplayName("clocks with negative hour") public void clocksWithNegateHour() { assertThat(new Clock(-2, 40)) .isEqualTo(new Clock(22, 40)); @@ -49,6 +56,7 @@ public void clocksWithNegateHour() { @Disabled("Remove to run test") @Test + @DisplayName("clocks with negative hour that wraps") public void clocksWithNegativeHourThatWraps() { assertThat(new Clock(-31, 3)) .isEqualTo(new Clock(17, 3)); @@ -56,6 +64,7 @@ public void clocksWithNegativeHourThatWraps() { @Disabled("Remove to run test") @Test + @DisplayName("clocks with negative hour that wraps multiple times") public void clocksWithNegativeHourThatWrapsMultipleTimes() { assertThat(new Clock(-83, 49)) .isEqualTo(new Clock(13, 49)); @@ -63,6 +72,7 @@ public void clocksWithNegativeHourThatWrapsMultipleTimes() { @Disabled("Remove to run test") @Test + @DisplayName("clocks with minute overflow") public void clocksWithMinuteOverflow() { assertThat(new Clock(0, 1441)) .isEqualTo(new Clock(0, 1)); @@ -70,6 +80,7 @@ public void clocksWithMinuteOverflow() { @Disabled("Remove to run test") @Test + @DisplayName("clocks with minute overflow by several days") public void clocksWithMinuteOverflowBySeveralDays() { assertThat(new Clock(2, 4322)) .isEqualTo(new Clock(2, 2)); @@ -77,6 +88,7 @@ public void clocksWithMinuteOverflowBySeveralDays() { @Disabled("Remove to run test") @Test + @DisplayName("clocks with negative minute") public void clocksWithNegativeMinutes() { assertThat(new Clock(3, -20)) .isEqualTo(new Clock(2, 40)); @@ -84,6 +96,7 @@ public void clocksWithNegativeMinutes() { @Disabled("Remove to run test") @Test + @DisplayName("clocks with negative minute that wraps") public void clocksWithNegativeMinutesThatWraps() { assertThat(new Clock(5, -1490)) .isEqualTo(new Clock(4, 10)); @@ -91,6 +104,7 @@ public void clocksWithNegativeMinutesThatWraps() { @Disabled("Remove to run test") @Test + @DisplayName("clocks with negative minute that wraps multiple times") public void clocksWithNegativeMinutesThatWrapsMultipleTimes() { assertThat(new Clock(6, -4305)) .isEqualTo(new Clock(6, 15)); @@ -98,6 +112,7 @@ public void clocksWithNegativeMinutesThatWrapsMultipleTimes() { @Disabled("Remove to run test") @Test + @DisplayName("clocks with negative hours and minutes") public void clocksWithNegativeHoursAndMinutes() { assertThat(new Clock(-12, -268)) .isEqualTo(new Clock(7, 32)); @@ -105,6 +120,7 @@ public void clocksWithNegativeHoursAndMinutes() { @Disabled("Remove to run test") @Test + @DisplayName("clocks with negative hours and minutes that wrap") public void clocksWithNegativeHoursAndMinutesThatWrap() { assertThat(new Clock(-54, -11513)) .isEqualTo(new Clock(18, 7)); @@ -112,6 +128,7 @@ public void clocksWithNegativeHoursAndMinutesThatWrap() { @Disabled("Remove to run test") @Test + @DisplayName("full clock and zeroed clock") public void clocksWithFullClockAndZeroedClockAreEqual() { assertThat(new Clock(24, 0)) .isEqualTo(new Clock(0, 0)); diff --git a/exercises/practice/collatz-conjecture/src/test/java/CollatzCalculatorTest.java b/exercises/practice/collatz-conjecture/src/test/java/CollatzCalculatorTest.java index 708d608dc..c095ffdf6 100644 --- a/exercises/practice/collatz-conjecture/src/test/java/CollatzCalculatorTest.java +++ b/exercises/practice/collatz-conjecture/src/test/java/CollatzCalculatorTest.java @@ -1,4 +1,5 @@ import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -9,30 +10,35 @@ public class CollatzCalculatorTest { private CollatzCalculator collatzCalculator = new CollatzCalculator(); @Test + @DisplayName("zero steps for one") public void testZeroStepsRequiredWhenStartingFrom1() { assertThat(collatzCalculator.computeStepCount(1)).isEqualTo(0); } @Disabled("Remove to run test") @Test + @DisplayName("divide if even") public void testCorrectNumberOfStepsWhenAllStepsAreDivisions() { assertThat(collatzCalculator.computeStepCount(16)).isEqualTo(4); } @Disabled("Remove to run test") @Test + @DisplayName("even and odd steps") public void testCorrectNumberOfStepsWhenBothStepTypesAreNeeded() { assertThat(collatzCalculator.computeStepCount(12)).isEqualTo(9); } @Disabled("Remove to run test") @Test + @DisplayName("large number of even and odd steps") public void testAVeryLargeInput() { assertThat(collatzCalculator.computeStepCount(1000000)).isEqualTo(152); } @Disabled("Remove to run test") @Test + @DisplayName("zero is an error") public void testZeroIsConsideredInvalidInput() { assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> collatzCalculator.computeStepCount(0)) @@ -41,6 +47,7 @@ public void testZeroIsConsideredInvalidInput() { @Disabled("Remove to run test") @Test + @DisplayName("negative value is an error") public void testNegativeIntegerIsConsideredInvalidInput() { assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> collatzCalculator.computeStepCount(-15))