Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.pavelperc.gcd;
- import javafx.util.Pair;
- import org.junit.Test;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import static org.junit.Assert.*;
- public class GCDTest {
- GCD gcd = new GCD();
- @Test
- public void simplest() {
- assertEquals(gcd.gcd(6, 4), 2);
- assertEquals(gcd.gcd(6, 9), 3);
- assertEquals(gcd.gcd(10, 20), 10);
- assertEquals(gcd.gcd(10, 21), 1);
- }
- /** Generates all possible combinations of pairs from the list.*/
- public <T> List<Pair<T, T>> genPairs(List<T> list) {
- List<Pair<T, T>> combinations = new ArrayList<>();
- for (int i = 0; i < list.size(); i++) {
- for (int j = 0; j < list.size(); j++) {
- combinations.add(new Pair<>(list.get(i), list.get(j)));
- }
- }
- return combinations;
- }
- @Test
- public void resultIsDivisor() {
- List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 30, 22, 33, 25, 55, 60);
- genPairs(numbers).forEach(pair -> {
- int a = pair.getKey();
- int b = pair.getValue();
- int result = gcd.gcd(a, b);
- System.out.println(String.format("testing in resultIsDivisor: gcd(%d %d) = %d", a, b, result));
- assertEquals(a % result, 0);
- assertEquals(b % result, 0);
- });
- }
- @Test
- public void resultIsPositive() {
- List<Integer> numbers = Arrays.asList(-1, 1, 2, -3, 4, -5, 6, 7, 8, 9, 15);
- genPairs(numbers).forEach(pair -> {
- int a = pair.getKey();
- int b = pair.getValue();
- int result = gcd.gcd(a, b);
- System.out.println(String.format("testing in resultIsPositive: gcd(%d %d) = %d", a, b, result));
- assertTrue(result > 0);
- });
- }
- @Test
- public void divisorIsGreatest() {
- List<Integer> numbers = Arrays.asList(1, 2, -3, 4, -5, 6, 7, 8, 9, 20, 30, 22, 33, 25, 55, 60);
- genPairs(numbers).forEach(pair -> {
- int a = pair.getKey();
- int b = pair.getValue();
- int result = gcd.gcd(a, b);
- System.out.println(String.format("testing in divisorIsGreatest: gcd(%d %d) = %d", a, b, result));
- assertTrue(result > 0);
- assertEquals(a % result, 0);
- assertEquals(b % result, 0);
- for (int i = result + 1; i < Math.min(a, b); i++) {
- assertFalse(String.format("gcd(%d, %d) = %d. There is a number %d, where %d %% %d == 0 and %d %% %d == 0",
- a, b, result, i, a, i, b, i),
- (a % i == 0) && (b % i == 0));
- }
- });
- }
- @Test(timeout = 200)
- public void testBorders() {
- assertEquals(gcd.gcd(Integer.MAX_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
- assertEquals(gcd.gcd(Integer.MIN_VALUE, Integer.MIN_VALUE), Integer.MIN_VALUE);
- assertEquals(gcd.gcd(Integer.MIN_VALUE, 4), 4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement