Repository navigation
Expand file tree
/
Copy pathLeastCommonMultipleTest.java
More file actions
81 lines (72 loc) · 4.83 KB
/
Copy pathLeastCommonMultipleTest.java
File metadata and controls
81 lines (72 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.math.BigInteger;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
class LeastCommonMultipleTest {
@ParameterizedTest
@MethodSource("provideTestCases")
void testLcm(int num1, int num2, int expected) {
assertEquals(expected, LeastCommonMultiple.lcm(num1, num2));
}
private static Stream<Arguments> provideTestCases() {
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),
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));
}
@Test
void testIntOverflowThrows() {
assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(50_000, 50_001));
assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(Integer.MIN_VALUE, 1));
assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(1, Integer.MIN_VALUE));
assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(Integer.MIN_VALUE, 2));
}
@Test
void testIntVarargs() {
assertEquals(24, LeastCommonMultiple.lcm(4, 6, 8, 12));
assertEquals(42, LeastCommonMultiple.lcm(-6, 14, 21));
assertEquals(7, LeastCommonMultiple.lcm(new int[] {-7}));
assertEquals(0, LeastCommonMultiple.lcm(1_000_000_000, 1_000_000_001, 0));
Supplier<int[]> nullIntArray = () -> null;
assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm(nullIntArray.get()));
assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm(new int[] {}));
assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(new int[] {Integer.MIN_VALUE}));
}
@Test
void testLongLcm() {
assertEquals(6_000_000_000_000L, LeastCommonMultiple.lcm(2_000_000_000_000L, -3_000_000_000_000L));
assertEquals(0L, LeastCommonMultiple.lcm(0L, Long.MAX_VALUE));
assertEquals(0L, LeastCommonMultiple.lcm(Long.MIN_VALUE, 0L));
assertEquals(60L, LeastCommonMultiple.lcm(12L, -15L, 20L));
assertEquals(9L, LeastCommonMultiple.lcm(new long[] {-9L}));
assertEquals(0L, LeastCommonMultiple.lcm(4_000_000_000L, 4_000_000_001L, 0L));
assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(4_000_000_000L, 4_000_000_001L));
assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(Long.MIN_VALUE, 1L));
assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(1L, Long.MIN_VALUE));
Supplier<long[]> nullLongArray = () -> null;
assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm(nullLongArray.get()));
assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm(new long[] {}));
assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(new long[] {Long.MIN_VALUE}));
}
@Test
void testBigIntegerLcm() {
BigInteger a = BigInteger.valueOf(Long.MAX_VALUE);
BigInteger b = BigInteger.valueOf(Long.MAX_VALUE - 1);
assertEquals(a.multiply(b), LeastCommonMultiple.lcm(a, b));
assertEquals(BigInteger.ZERO, LeastCommonMultiple.lcm(BigInteger.ZERO, a));
assertEquals(BigInteger.ZERO, LeastCommonMultiple.lcm(a, BigInteger.ZERO));
assertEquals(BigInteger.ZERO, LeastCommonMultiple.lcm(BigInteger.ZERO, BigInteger.TEN, BigInteger.valueOf(15)));
assertEquals(BigInteger.valueOf(7), LeastCommonMultiple.lcm(new BigInteger[] {BigInteger.valueOf(-7)}));
assertEquals(BigInteger.valueOf(60), LeastCommonMultiple.lcm(BigInteger.valueOf(12), BigInteger.valueOf(-15), BigInteger.valueOf(20)));
Supplier<BigInteger[]> nullBigArray = () -> null;
assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm((BigInteger) null, BigInteger.ONE));
assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm(BigInteger.ONE, (BigInteger) null));
assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm(nullBigArray.get()));
assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm(new BigInteger[] {BigInteger.TEN, null}));
assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm(new BigInteger[] {}));
}
}