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