Skip to content

test: GCDRecursion #5361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/test/java/com/thealgorithms/maths/GCDRecursionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

public class GCDRecursionTest {

@ParameterizedTest
@CsvSource({"7, 5, 1", "9, 12, 3", "18, 24, 6", "36, 60, 12"})
void testGcdPositiveNumbers(int a, int b, int expectedGcd) {
assertEquals(expectedGcd, GCDRecursion.gcd(a, b));
}

@ParameterizedTest
@CsvSource({"0, 5, 5", "8, 0, 8"})
void testGcdOneZero(int a, int b, int expectedGcd) {
assertEquals(expectedGcd, GCDRecursion.gcd(a, b));
}

@Test
void testGcdBothZero() {
assertEquals(0, GCDRecursion.gcd(0, 0));
}

@ParameterizedTest
@ValueSource(ints = {-5, -15})
void testGcdNegativeNumbers(int negativeValue) {
assertThrows(ArithmeticException.class, () -> GCDRecursion.gcd(negativeValue, 15));
assertThrows(ArithmeticException.class, () -> GCDRecursion.gcd(15, negativeValue));
}

@ParameterizedTest
@CsvSource({"5, 5, 5", "8, 8, 8"})
void testGcdWithSameNumbers(int a, int b, int expectedGcd) {
assertEquals(expectedGcd, GCDRecursion.gcd(a, b));
}

@ParameterizedTest
@CsvSource({"7, 13, 1", "11, 17, 1"})
void testGcdWithPrimeNumbers(int a, int b, int expectedGcd) {
assertEquals(expectedGcd, GCDRecursion.gcd(a, b));
}
}