From 30dcc4c9de6c83b37ea3578267ee78027fea9f69 Mon Sep 17 00:00:00 2001 From: Saniya2701 <185913688+Saniya2701@users.noreply.github.com> Date: Wed, 17 Dec 2025 22:36:24 +0530 Subject: [PATCH 1/3] Add additional edge cases to GCD unit tests --- .../java/com/thealgorithms/maths/GCDTest.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/test/java/com/thealgorithms/maths/GCDTest.java b/src/test/java/com/thealgorithms/maths/GCDTest.java index bac3f8f7596c..a0d48e3a95e7 100644 --- a/src/test/java/com/thealgorithms/maths/GCDTest.java +++ b/src/test/java/com/thealgorithms/maths/GCDTest.java @@ -59,4 +59,28 @@ void testArrayGcd2() { void testArrayGcdForEmptyInput() { Assertions.assertEquals(GCD.gcd(new int[] {}), 0); } + + + + +@Test +void testSameNumbers() { + Assertions.assertEquals(GCD.gcd(7, 7), 7); +} + +@Test +void testPrimeNumbers() { + Assertions.assertEquals(GCD.gcd(13, 17), 1); +} + +@Test +void testSingleElementArray() { + Assertions.assertEquals(GCD.gcd(new int[] {42}), 42); +} + +@Test +void testLargeNumbers() { + Assertions.assertEquals(GCD.gcd(123456, 789012), 12); +} + } From 742bcc71642071df8f704f556e2cac55da617773 Mon Sep 17 00:00:00 2001 From: Saniya2701 <185913688+Saniya2701@users.noreply.github.com> Date: Thu, 18 Dec 2025 12:47:00 +0530 Subject: [PATCH 2/3] Rename GCD test methods and add descriptive names --- .../java/com/thealgorithms/maths/GCDTest.java | 82 ++++++++++--------- 1 file changed, 43 insertions(+), 39 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/GCDTest.java b/src/test/java/com/thealgorithms/maths/GCDTest.java index a0d48e3a95e7..d608dba91bc9 100644 --- a/src/test/java/com/thealgorithms/maths/GCDTest.java +++ b/src/test/java/com/thealgorithms/maths/GCDTest.java @@ -6,81 +6,85 @@ public class GCDTest { @Test - void test1() { + void testNegativeAndZeroThrowsException() { Assertions.assertThrows(ArithmeticException.class, () -> GCD.gcd(-1, 0)); } @Test - void test2() { + void testPositiveAndNegativeThrowsException() { Assertions.assertThrows(ArithmeticException.class, () -> GCD.gcd(10, -2)); } @Test - void test3() { + void testBothNegativeThrowsException() { Assertions.assertThrows(ArithmeticException.class, () -> GCD.gcd(-5, -3)); } @Test - void test4() { - Assertions.assertEquals(GCD.gcd(0, 2), 2); + void testZeroAndPositiveReturnsPositive() { + Assertions.assertEquals(2, GCD.gcd(0, 2)); } @Test - void test5() { - Assertions.assertEquals(GCD.gcd(10, 0), 10); + void testPositiveAndZeroReturnsPositive() { + Assertions.assertEquals(10, GCD.gcd(10, 0)); } @Test - void test6() { - Assertions.assertEquals(GCD.gcd(1, 0), 1); + void testOneAndZeroReturnsOne() { + Assertions.assertEquals(1, GCD.gcd(1, 0)); } @Test - void test7() { - Assertions.assertEquals(GCD.gcd(9, 6), 3); + void testTwoPositiveNumbers() { + Assertions.assertEquals(3, GCD.gcd(9, 6)); } @Test - void test8() { - Assertions.assertEquals(GCD.gcd(48, 18, 30, 12), 6); + void testMultipleArgumentsGcd() { + Assertions.assertEquals(6, GCD.gcd(48, 18, 30, 12)); } @Test - void testArrayGcd1() { - Assertions.assertEquals(GCD.gcd(new int[] {9, 6}), 3); + void testArrayInputGcd() { + Assertions.assertEquals(3, GCD.gcd(new int[] {9, 6})); } @Test - void testArrayGcd2() { - Assertions.assertEquals(GCD.gcd(new int[] {2 * 3 * 5 * 7, 2 * 5 * 5 * 5, 2 * 5 * 11, 5 * 5 * 5 * 13}), 5); + void testArrayWithCommonFactor() { + Assertions.assertEquals( + 5, + GCD.gcd(new int[] { + 2 * 3 * 5 * 7, + 2 * 5 * 5 * 5, + 2 * 5 * 11, + 5 * 5 * 5 * 13 + }) + ); } @Test - void testArrayGcdForEmptyInput() { - Assertions.assertEquals(GCD.gcd(new int[] {}), 0); + void testEmptyArrayReturnsZero() { + Assertions.assertEquals(0, GCD.gcd(new int[] {})); } - - - -@Test -void testSameNumbers() { - Assertions.assertEquals(GCD.gcd(7, 7), 7); -} - -@Test -void testPrimeNumbers() { - Assertions.assertEquals(GCD.gcd(13, 17), 1); -} + @Test + void testSameNumbers() { + Assertions.assertEquals(7, GCD.gcd(7, 7)); + } -@Test -void testSingleElementArray() { - Assertions.assertEquals(GCD.gcd(new int[] {42}), 42); -} + @Test + void testPrimeNumbersHaveGcdOne() { + Assertions.assertEquals(1, GCD.gcd(13, 17)); + } -@Test -void testLargeNumbers() { - Assertions.assertEquals(GCD.gcd(123456, 789012), 12); -} + @Test + void testSingleElementArrayReturnsElement() { + Assertions.assertEquals(42, GCD.gcd(new int[] {42})); + } + @Test + void testLargeNumbers() { + Assertions.assertEquals(12, GCD.gcd(123456, 789012)); + } } From 0bfbe5b0742e60ec549ffaf60e2846e4e8aa554d Mon Sep 17 00:00:00 2001 From: Saniya2701 <185913688+Saniya2701@users.noreply.github.com> Date: Thu, 18 Dec 2025 12:57:41 +0530 Subject: [PATCH 3/3] Fix formatting in GCD tests --- src/test/java/com/thealgorithms/maths/GCDTest.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/GCDTest.java b/src/test/java/com/thealgorithms/maths/GCDTest.java index d608dba91bc9..6bc870e94df9 100644 --- a/src/test/java/com/thealgorithms/maths/GCDTest.java +++ b/src/test/java/com/thealgorithms/maths/GCDTest.java @@ -52,15 +52,7 @@ void testArrayInputGcd() { @Test void testArrayWithCommonFactor() { - Assertions.assertEquals( - 5, - GCD.gcd(new int[] { - 2 * 3 * 5 * 7, - 2 * 5 * 5 * 5, - 2 * 5 * 11, - 5 * 5 * 5 * 13 - }) - ); + Assertions.assertEquals(5, GCD.gcd(new int[] {2 * 3 * 5 * 7, 2 * 5 * 5 * 5, 2 * 5 * 11, 5 * 5 * 5 * 13})); } @Test