|
| 1 | +package com.thealgorithms.conversions; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import java.util.stream.Stream; |
| 7 | +import org.junit.jupiter.api.DisplayName; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.params.ParameterizedTest; |
| 10 | +import org.junit.jupiter.params.provider.CsvSource; |
| 11 | +import org.junit.jupiter.params.provider.MethodSource; |
| 12 | + |
| 13 | +class TimeConverterTest { |
| 14 | + |
| 15 | + @ParameterizedTest(name = "{0} {1} -> {2} {3}") |
| 16 | + @CsvSource({"60, seconds, minutes, 1", "120, seconds, minutes, 2", "2, minutes, seconds, 120", "2, hours, minutes, 120", "1, days, hours, 24", "1, weeks, days, 7", "1, months, days, 30.438", "1, years, days, 365.25", "3600, seconds, hours, 1", "86400, seconds, days, 1", |
| 17 | + "604800, seconds, weeks, 1", "31557600, seconds, years, 1"}) |
| 18 | + void |
| 19 | + testValidConversions(double value, String from, String to, double expected) { |
| 20 | + assertEquals(expected, TimeConverter.convertTime(value, from, to)); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + @DisplayName("Zero conversion returns zero") |
| 25 | + void testZeroValue() { |
| 26 | + assertEquals(0.0, TimeConverter.convertTime(0, "seconds", "hours")); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + @DisplayName("Same-unit conversion returns original value") |
| 31 | + void testSameUnitConversion() { |
| 32 | + assertEquals(123.456, TimeConverter.convertTime(123.456, "minutes", "minutes")); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + @DisplayName("Negative value throws exception") |
| 37 | + void testNegativeValue() { |
| 38 | + assertThrows(IllegalArgumentException.class, () -> TimeConverter.convertTime(-5, "seconds", "minutes")); |
| 39 | + } |
| 40 | + |
| 41 | + @ParameterizedTest |
| 42 | + @CsvSource({"lightyears, seconds", "minutes, centuries"}) |
| 43 | + void testInvalidUnits(String from, String to) { |
| 44 | + assertThrows(IllegalArgumentException.class, () -> TimeConverter.convertTime(10, from, to)); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + @DisplayName("Null unit throws exception") |
| 49 | + void testNullUnit() { |
| 50 | + assertThrows(IllegalArgumentException.class, () -> TimeConverter.convertTime(10, null, "seconds")); |
| 51 | + |
| 52 | + assertThrows(IllegalArgumentException.class, () -> TimeConverter.convertTime(10, "minutes", null)); |
| 53 | + |
| 54 | + assertThrows(IllegalArgumentException.class, () -> TimeConverter.convertTime(10, null, null)); |
| 55 | + } |
| 56 | + |
| 57 | + static Stream<org.junit.jupiter.params.provider.Arguments> roundTripCases() { |
| 58 | + return Stream.of(org.junit.jupiter.params.provider.Arguments.of(1.0, "hours", "minutes"), org.junit.jupiter.params.provider.Arguments.of(2.5, "days", "hours"), org.junit.jupiter.params.provider.Arguments.of(1000, "seconds", "minutes")); |
| 59 | + } |
| 60 | + |
| 61 | + @ParameterizedTest |
| 62 | + @MethodSource("roundTripCases") |
| 63 | + @DisplayName("Round-trip conversion returns original value") |
| 64 | + void testRoundTripConversion(double value, String from, String to) { |
| 65 | + double converted = TimeConverter.convertTime(value, from, to); |
| 66 | + double roundTrip = TimeConverter.convertTime(converted, to, from); |
| 67 | + assertEquals(Math.round(value * 1000.0) / 1000.0, Math.round(roundTrip * 1000.0) / 1000.0, 0.05); |
| 68 | + } |
| 69 | +} |
0 commit comments