Skip to content

Commit 3ed8561

Browse files
authored
test: GCDRecursion (#5361)
1 parent 07dbc51 commit 3ed8561

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.CsvSource;
9+
import org.junit.jupiter.params.provider.ValueSource;
10+
11+
public class GCDRecursionTest {
12+
13+
@ParameterizedTest
14+
@CsvSource({"7, 5, 1", "9, 12, 3", "18, 24, 6", "36, 60, 12"})
15+
void testGcdPositiveNumbers(int a, int b, int expectedGcd) {
16+
assertEquals(expectedGcd, GCDRecursion.gcd(a, b));
17+
}
18+
19+
@ParameterizedTest
20+
@CsvSource({"0, 5, 5", "8, 0, 8"})
21+
void testGcdOneZero(int a, int b, int expectedGcd) {
22+
assertEquals(expectedGcd, GCDRecursion.gcd(a, b));
23+
}
24+
25+
@Test
26+
void testGcdBothZero() {
27+
assertEquals(0, GCDRecursion.gcd(0, 0));
28+
}
29+
30+
@ParameterizedTest
31+
@ValueSource(ints = {-5, -15})
32+
void testGcdNegativeNumbers(int negativeValue) {
33+
assertThrows(ArithmeticException.class, () -> GCDRecursion.gcd(negativeValue, 15));
34+
assertThrows(ArithmeticException.class, () -> GCDRecursion.gcd(15, negativeValue));
35+
}
36+
37+
@ParameterizedTest
38+
@CsvSource({"5, 5, 5", "8, 8, 8"})
39+
void testGcdWithSameNumbers(int a, int b, int expectedGcd) {
40+
assertEquals(expectedGcd, GCDRecursion.gcd(a, b));
41+
}
42+
43+
@ParameterizedTest
44+
@CsvSource({"7, 13, 1", "11, 17, 1"})
45+
void testGcdWithPrimeNumbers(int a, int b, int expectedGcd) {
46+
assertEquals(expectedGcd, GCDRecursion.gcd(a, b));
47+
}
48+
}

0 commit comments

Comments
 (0)