Skip to content

Commit 7d51c7f

Browse files
Add additional edge cases to GCD unit tests (#7174)
* Add additional edge cases to GCD unit tests * Rename GCD test methods and add descriptive names * Fix formatting in GCD tests --------- Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
1 parent 2911a7b commit 7d51c7f

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

src/test/java/com/thealgorithms/maths/GCDTest.java

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,77 @@
66
public class GCDTest {
77

88
@Test
9-
void test1() {
9+
void testNegativeAndZeroThrowsException() {
1010
Assertions.assertThrows(ArithmeticException.class, () -> GCD.gcd(-1, 0));
1111
}
1212

1313
@Test
14-
void test2() {
14+
void testPositiveAndNegativeThrowsException() {
1515
Assertions.assertThrows(ArithmeticException.class, () -> GCD.gcd(10, -2));
1616
}
1717

1818
@Test
19-
void test3() {
19+
void testBothNegativeThrowsException() {
2020
Assertions.assertThrows(ArithmeticException.class, () -> GCD.gcd(-5, -3));
2121
}
2222

2323
@Test
24-
void test4() {
25-
Assertions.assertEquals(GCD.gcd(0, 2), 2);
24+
void testZeroAndPositiveReturnsPositive() {
25+
Assertions.assertEquals(2, GCD.gcd(0, 2));
2626
}
2727

2828
@Test
29-
void test5() {
30-
Assertions.assertEquals(GCD.gcd(10, 0), 10);
29+
void testPositiveAndZeroReturnsPositive() {
30+
Assertions.assertEquals(10, GCD.gcd(10, 0));
3131
}
3232

3333
@Test
34-
void test6() {
35-
Assertions.assertEquals(GCD.gcd(1, 0), 1);
34+
void testOneAndZeroReturnsOne() {
35+
Assertions.assertEquals(1, GCD.gcd(1, 0));
3636
}
3737

3838
@Test
39-
void test7() {
40-
Assertions.assertEquals(GCD.gcd(9, 6), 3);
39+
void testTwoPositiveNumbers() {
40+
Assertions.assertEquals(3, GCD.gcd(9, 6));
4141
}
4242

4343
@Test
44-
void test8() {
45-
Assertions.assertEquals(GCD.gcd(48, 18, 30, 12), 6);
44+
void testMultipleArgumentsGcd() {
45+
Assertions.assertEquals(6, GCD.gcd(48, 18, 30, 12));
4646
}
4747

4848
@Test
49-
void testArrayGcd1() {
50-
Assertions.assertEquals(GCD.gcd(new int[] {9, 6}), 3);
49+
void testArrayInputGcd() {
50+
Assertions.assertEquals(3, GCD.gcd(new int[] {9, 6}));
5151
}
5252

5353
@Test
54-
void testArrayGcd2() {
55-
Assertions.assertEquals(GCD.gcd(new int[] {2 * 3 * 5 * 7, 2 * 5 * 5 * 5, 2 * 5 * 11, 5 * 5 * 5 * 13}), 5);
54+
void testArrayWithCommonFactor() {
55+
Assertions.assertEquals(5, GCD.gcd(new int[] {2 * 3 * 5 * 7, 2 * 5 * 5 * 5, 2 * 5 * 11, 5 * 5 * 5 * 13}));
5656
}
5757

5858
@Test
59-
void testArrayGcdForEmptyInput() {
60-
Assertions.assertEquals(GCD.gcd(new int[] {}), 0);
59+
void testEmptyArrayReturnsZero() {
60+
Assertions.assertEquals(0, GCD.gcd(new int[] {}));
61+
}
62+
63+
@Test
64+
void testSameNumbers() {
65+
Assertions.assertEquals(7, GCD.gcd(7, 7));
66+
}
67+
68+
@Test
69+
void testPrimeNumbersHaveGcdOne() {
70+
Assertions.assertEquals(1, GCD.gcd(13, 17));
71+
}
72+
73+
@Test
74+
void testSingleElementArrayReturnsElement() {
75+
Assertions.assertEquals(42, GCD.gcd(new int[] {42}));
76+
}
77+
78+
@Test
79+
void testLargeNumbers() {
80+
Assertions.assertEquals(12, GCD.gcd(123456, 789012));
6181
}
6282
}

0 commit comments

Comments
 (0)