Advertisement
pavelperc

gcd_test

Oct 15th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. package com.pavelperc.gcd;
  2.  
  3. import javafx.util.Pair;
  4. import org.junit.Test;
  5.  
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.List;
  9.  
  10. import static org.junit.Assert.*;
  11.  
  12. public class GCDTest {
  13.    
  14.     GCD gcd = new GCD();
  15.    
  16.     @Test
  17.     public void simplest() {
  18.        
  19.         assertEquals(gcd.gcd(6, 4), 2);
  20.         assertEquals(gcd.gcd(6, 9), 3);
  21.         assertEquals(gcd.gcd(10, 20), 10);
  22.         assertEquals(gcd.gcd(10, 21), 1);
  23.     }
  24.    
  25.     /** Generates all possible combinations of pairs from the list.*/
  26.     public <T> List<Pair<T, T>> genPairs(List<T> list) {
  27.         List<Pair<T, T>> combinations = new ArrayList<>();
  28.    
  29.         for (int i = 0; i < list.size(); i++) {
  30.             for (int j = 0; j < list.size(); j++) {
  31.                 combinations.add(new Pair<>(list.get(i), list.get(j)));
  32.             }
  33.         }
  34.         return combinations;
  35.     }
  36.    
  37.     @Test
  38.     public void resultIsDivisor() {
  39.    
  40.         List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 30, 22, 33, 25, 55, 60);
  41.        
  42.         genPairs(numbers).forEach(pair -> {
  43.             int a = pair.getKey();
  44.             int b = pair.getValue();
  45.            
  46.             int result = gcd.gcd(a, b);
  47.             System.out.println(String.format("testing in resultIsDivisor: gcd(%d %d) = %d", a, b, result));
  48.             assertEquals(a % result, 0);
  49.             assertEquals(b % result, 0);
  50.         });
  51.        
  52.     }
  53.    
  54.     @Test
  55.     public void resultIsPositive() {
  56.         List<Integer> numbers = Arrays.asList(-1, 1, 2, -3, 4, -5, 6, 7, 8, 9, 15);
  57.        
  58.         genPairs(numbers).forEach(pair -> {
  59.             int a = pair.getKey();
  60.             int b = pair.getValue();
  61.            
  62.             int result = gcd.gcd(a, b);
  63.             System.out.println(String.format("testing in resultIsPositive: gcd(%d %d) = %d", a, b, result));
  64.             assertTrue(result > 0);
  65.            
  66.         });
  67.     }
  68.    
  69.     @Test
  70.     public void divisorIsGreatest() {
  71.         List<Integer> numbers = Arrays.asList(1, 2, -3, 4, -5, 6, 7, 8, 9, 20, 30, 22, 33, 25, 55, 60);
  72.        
  73.         genPairs(numbers).forEach(pair -> {
  74.             int a = pair.getKey();
  75.             int b = pair.getValue();
  76.            
  77.             int result = gcd.gcd(a, b);
  78.             System.out.println(String.format("testing in divisorIsGreatest: gcd(%d %d) = %d", a, b, result));
  79.            
  80.             assertTrue(result > 0);
  81.            
  82.             assertEquals(a % result, 0);
  83.             assertEquals(b % result, 0);
  84.            
  85.            
  86.             for (int i = result + 1; i < Math.min(a, b); i++) {
  87.                 assertFalse(String.format("gcd(%d, %d) = %d. There is a number %d, where %d %% %d == 0 and %d %% %d == 0",
  88.                         a, b, result, i, a, i, b, i),
  89.                         (a % i == 0) && (b % i == 0));
  90.             }
  91.            
  92.         });
  93.        
  94.     }
  95.    
  96.    
  97.     @Test(timeout = 200)
  98.     public void testBorders() {
  99.         assertEquals(gcd.gcd(Integer.MAX_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
  100.         assertEquals(gcd.gcd(Integer.MIN_VALUE, Integer.MIN_VALUE), Integer.MIN_VALUE);
  101.         assertEquals(gcd.gcd(Integer.MIN_VALUE, 4), 4);
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement