forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCDTest.java
More file actions
98 lines (83 loc) · 2.64 KB
/
GCDTest.java
File metadata and controls
98 lines (83 loc) · 2.64 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
/**
* Unit tests for GCD implementation.
*/
class GCDTest {
@Test
@DisplayName("GCD should handle zero and positive number")
void testZeroAndPositiveReturnsPositive() {
assertEquals(2, GCD.gcd(0, 2));
assertEquals(10, GCD.gcd(10, 0));
assertEquals(1, GCD.gcd(1, 0));
}
@Test
@DisplayName("GCD should work with negative inputs using absolute values")
void testNegativeInputsHandledCorrectly() {
assertEquals(1, GCD.gcd(-1, 0));
assertEquals(2, GCD.gcd(10, -2));
assertEquals(1, GCD.gcd(-5, -3));
assertEquals(3, GCD.gcd(-9, 6));
}
@Test
@DisplayName("GCD should return correct result for two positive numbers")
void testTwoPositiveNumbers() {
assertEquals(3, GCD.gcd(9, 6));
}
@Test
@DisplayName("GCD should return same number when both inputs are equal")
void testSameNumbers() {
assertEquals(7, GCD.gcd(7, 7));
}
@Test
@DisplayName("GCD of two prime numbers should be one")
void testPrimeNumbersHaveGcdOne() {
assertEquals(1, GCD.gcd(13, 17));
}
@Test
@DisplayName("GCD should handle multiple arguments")
void testMultipleArgumentsGcd() {
assertEquals(6, GCD.gcd(48, 18, 30, 12));
}
@Test
@DisplayName("GCD should handle array input")
void testArrayInputGcd() {
assertEquals(3, GCD.gcd(new int[] {9, 6}));
}
@Test
@DisplayName("GCD should handle array with common factor")
void testArrayWithCommonFactor() {
assertEquals(
5,
GCD.gcd(new int[] {
2 * 3 * 5 * 7,
2 * 5 * 5 * 5,
2 * 5 * 11,
5 * 5 * 5 * 13
})
);
}
@Test
@DisplayName("GCD of empty array should return zero")
void testEmptyArrayReturnsZero() {
assertEquals(0, GCD.gcd(new int[] {}));
}
@Test
@DisplayName("GCD of single-element array should return that element")
void testSingleElementArrayReturnsElement() {
assertEquals(42, GCD.gcd(new int[] {42}));
}
@Test
@DisplayName("GCD should handle large numbers")
void testLargeNumbers() {
assertEquals(12, GCD.gcd(123456, 789012));
}
@Test
@DisplayName("GCD should throw exception for null array")
void testNullArrayThrowsException() {
assertThrows(IllegalArgumentException.class, () -> GCD.gcd((int[]) null));
}
}