11package com .thealgorithms .conversions ;
22
3+ import static org .junit .jupiter .api .Assertions .*;
4+
5+ import java .util .stream .Stream ;
36import org .junit .jupiter .api .DisplayName ;
47import org .junit .jupiter .api .Test ;
58import org .junit .jupiter .params .ParameterizedTest ;
69import org .junit .jupiter .params .provider .CsvSource ;
710import org .junit .jupiter .params .provider .MethodSource ;
811
9- import java .util .stream .Stream ;
10-
11- import static org .junit .jupiter .api .Assertions .*;
12-
1312class TimeConverterTest {
1413
1514 @ ParameterizedTest (name = "{0} {1} -> {2} {3}" )
16- @ CsvSource ({
17- "60, seconds, minutes, 1" ,
18- "120, seconds, minutes, 2" ,
19- "2, minutes, seconds, 120" ,
20- "2, hours, minutes, 120" ,
21- "1, days, hours, 24" ,
22- "1, weeks, days, 7" ,
23- "1, months, days, 30.438" ,
24- "1, years, days, 365.25" ,
25- "3600, seconds, hours, 1" ,
26- "86400, seconds, days, 1" ,
27- "604800, seconds, weeks, 1" ,
28- "31557600, seconds, years, 1"
29- })
30- void testValidConversions (double value , String from , String to , double expected ) {
15+ @ 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" ,
16+ "604800, seconds, weeks, 1" , "31557600, seconds, years, 1" })
17+ void
18+ testValidConversions (double value , String from , String to , double expected ) {
3119 assertEquals (expected , TimeConverter .convertTime (value , from , to ));
3220 }
3321
@@ -46,25 +34,17 @@ void testSameUnitConversion() {
4634 @ Test
4735 @ DisplayName ("Negative value throws exception" )
4836 void testNegativeValue () {
49- assertThrows (IllegalArgumentException .class , () ->
50- TimeConverter .convertTime (-5 , "seconds" , "minutes" ));
37+ assertThrows (IllegalArgumentException .class , () -> TimeConverter .convertTime (-5 , "seconds" , "minutes" ));
5138 }
5239
5340 @ ParameterizedTest
54- @ CsvSource ({
55- "lightyears, seconds" ,
56- "minutes, centuries"
57- })
41+ @ CsvSource ({"lightyears, seconds" , "minutes, centuries" })
5842 void testInvalidUnits (String from , String to ) {
59- assertThrows (IllegalArgumentException .class , () ->
60- TimeConverter .convertTime (10 , from , to ));
43+ assertThrows (IllegalArgumentException .class , () -> TimeConverter .convertTime (10 , from , to ));
6144 }
6245
6346 static Stream <org .junit .jupiter .params .provider .Arguments > roundTripCases () {
64- return Stream .of (
65- org .junit .jupiter .params .provider .Arguments .of (1.0 , "hours" , "minutes" ),
66- org .junit .jupiter .params .provider .Arguments .of (2.5 , "days" , "hours" ),
67- org .junit .jupiter .params .provider .Arguments .of (1000 , "seconds" , "minutes" ));
47+ 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" ));
6848 }
6949
7050 @ ParameterizedTest
@@ -73,9 +53,6 @@ static Stream<org.junit.jupiter.params.provider.Arguments> roundTripCases() {
7353 void testRoundTripConversion (double value , String from , String to ) {
7454 double converted = TimeConverter .convertTime (value , from , to );
7555 double roundTrip = TimeConverter .convertTime (converted , to , from );
76- assertEquals (
77- Math .round (value * 1000.0 ) / 1000.0 ,
78- Math .round (roundTrip * 1000.0 ) / 1000.0 ,
79- 0.05 );
56+ assertEquals (Math .round (value * 1000.0 ) / 1000.0 , Math .round (roundTrip * 1000.0 ) / 1000.0 , 0.05 );
8057 }
8158}
0 commit comments