11package com .thealgorithms .maths ;
22
33import static org .junit .jupiter .api .Assertions .assertEquals ;
4+ import static org .junit .jupiter .api .Assertions .assertThrows ;
45
6+ import java .math .BigInteger ;
7+ import java .util .function .Supplier ;
58import java .util .stream .Stream ;
9+ import org .junit .jupiter .api .Test ;
610import org .junit .jupiter .params .ParameterizedTest ;
711import org .junit .jupiter .params .provider .Arguments ;
812import org .junit .jupiter .params .provider .MethodSource ;
@@ -16,6 +20,62 @@ void testLcm(int num1, int num2, int expected) {
1620 }
1721
1822 private static Stream <Arguments > provideTestCases () {
19- return Stream .of (Arguments .of (12 , 18 , 36 ), Arguments .of (5 , 10 , 10 ), Arguments .of (7 , 3 , 21 ), Arguments .of (21 , 6 , 42 ), Arguments .of (1 , 1 , 1 ), Arguments .of (8 , 12 , 24 ), Arguments .of (14 , 35 , 70 ), Arguments .of (15 , 25 , 75 ), Arguments .of (100 , 25 , 100 ), Arguments .of (0 , 10 , 0 ));
23+ return Stream .of (Arguments .of (12 , 18 , 36 ), Arguments .of (5 , 10 , 10 ), Arguments .of (7 , 3 , 21 ), Arguments .of (21 , 6 , 42 ), Arguments .of (1 , 1 , 1 ), Arguments .of (8 , 12 , 24 ), Arguments .of (14 , 35 , 70 ), Arguments .of (15 , 25 , 75 ), Arguments .of (100 , 25 , 100 ), Arguments .of (0 , 10 , 0 ), Arguments .of (10 , 0 , 0 ),
24+ Arguments .of (0 , 0 , 0 ), Arguments .of (-12 , 18 , 36 ), Arguments .of (12 , -18 , 36 ), Arguments .of (-12 , -18 , 36 ), Arguments .of (60_000 , 90_000 , 180_000 ));
25+ }
26+
27+ @ Test
28+ void testIntOverflowThrows () {
29+ assertThrows (ArithmeticException .class , () -> LeastCommonMultiple .lcm (50_000 , 50_001 ));
30+ assertThrows (ArithmeticException .class , () -> LeastCommonMultiple .lcm (Integer .MIN_VALUE , 1 ));
31+ assertThrows (ArithmeticException .class , () -> LeastCommonMultiple .lcm (1 , Integer .MIN_VALUE ));
32+ assertThrows (ArithmeticException .class , () -> LeastCommonMultiple .lcm (Integer .MIN_VALUE , 2 ));
33+ }
34+
35+ @ Test
36+ void testIntVarargs () {
37+ assertEquals (24 , LeastCommonMultiple .lcm (4 , 6 , 8 , 12 ));
38+ assertEquals (42 , LeastCommonMultiple .lcm (-6 , 14 , 21 ));
39+ assertEquals (7 , LeastCommonMultiple .lcm (new int [] {-7 }));
40+ assertEquals (0 , LeastCommonMultiple .lcm (1_000_000_000 , 1_000_000_001 , 0 ));
41+ Supplier <int []> nullIntArray = () -> null ;
42+ assertThrows (IllegalArgumentException .class , () -> LeastCommonMultiple .lcm (nullIntArray .get ()));
43+ assertThrows (IllegalArgumentException .class , () -> LeastCommonMultiple .lcm (new int [] {}));
44+ assertThrows (ArithmeticException .class , () -> LeastCommonMultiple .lcm (new int [] {Integer .MIN_VALUE }));
45+ }
46+
47+ @ Test
48+ void testLongLcm () {
49+ assertEquals (6_000_000_000_000L , LeastCommonMultiple .lcm (2_000_000_000_000L , -3_000_000_000_000L ));
50+ assertEquals (0L , LeastCommonMultiple .lcm (0L , Long .MAX_VALUE ));
51+ assertEquals (0L , LeastCommonMultiple .lcm (Long .MIN_VALUE , 0L ));
52+ assertEquals (60L , LeastCommonMultiple .lcm (12L , -15L , 20L ));
53+ assertEquals (9L , LeastCommonMultiple .lcm (new long [] {-9L }));
54+ assertEquals (0L , LeastCommonMultiple .lcm (4_000_000_000L , 4_000_000_001L , 0L ));
55+ assertThrows (ArithmeticException .class , () -> LeastCommonMultiple .lcm (4_000_000_000L , 4_000_000_001L ));
56+ assertThrows (ArithmeticException .class , () -> LeastCommonMultiple .lcm (Long .MIN_VALUE , 1L ));
57+ assertThrows (ArithmeticException .class , () -> LeastCommonMultiple .lcm (1L , Long .MIN_VALUE ));
58+ Supplier <long []> nullLongArray = () -> null ;
59+ assertThrows (IllegalArgumentException .class , () -> LeastCommonMultiple .lcm (nullLongArray .get ()));
60+ assertThrows (IllegalArgumentException .class , () -> LeastCommonMultiple .lcm (new long [] {}));
61+ assertThrows (ArithmeticException .class , () -> LeastCommonMultiple .lcm (new long [] {Long .MIN_VALUE }));
62+ }
63+
64+ @ Test
65+ void testBigIntegerLcm () {
66+ BigInteger a = BigInteger .valueOf (Long .MAX_VALUE );
67+ BigInteger b = BigInteger .valueOf (Long .MAX_VALUE - 1 );
68+ assertEquals (a .multiply (b ), LeastCommonMultiple .lcm (a , b ));
69+ assertEquals (BigInteger .ZERO , LeastCommonMultiple .lcm (BigInteger .ZERO , a ));
70+ assertEquals (BigInteger .ZERO , LeastCommonMultiple .lcm (a , BigInteger .ZERO ));
71+ assertEquals (BigInteger .ZERO , LeastCommonMultiple .lcm (BigInteger .ZERO , BigInteger .TEN , BigInteger .valueOf (15 )));
72+ assertEquals (BigInteger .valueOf (7 ), LeastCommonMultiple .lcm (new BigInteger [] {BigInteger .valueOf (-7 )}));
73+ assertEquals (BigInteger .valueOf (60 ), LeastCommonMultiple .lcm (BigInteger .valueOf (12 ), BigInteger .valueOf (-15 ), BigInteger .valueOf (20 )));
74+ Supplier <BigInteger []> nullBigArray = () -> null ;
75+ assertThrows (IllegalArgumentException .class , () -> LeastCommonMultiple .lcm ((BigInteger ) null , BigInteger .ONE ));
76+ assertThrows (IllegalArgumentException .class , () -> LeastCommonMultiple .lcm (BigInteger .ONE , (BigInteger ) null ));
77+ assertThrows (IllegalArgumentException .class , () -> LeastCommonMultiple .lcm (nullBigArray .get ()));
78+ assertThrows (IllegalArgumentException .class , () -> LeastCommonMultiple .lcm (new BigInteger [] {BigInteger .TEN , null }));
79+ assertThrows (IllegalArgumentException .class , () -> LeastCommonMultiple .lcm (new BigInteger [] {}));
2080 }
2181}
0 commit comments