There is a code that, according to the Euclidean algorithm, finds the greatest common divisor.
import java.util.Scanner; /** * Created by user on 24.11.2015. * По данным двум числам 1<a,b<2*10^9 найдите их наибольший общий делитель. */ public class Euclid { public static int Euclid(int a, int b) { //конструктор if (a == 0 || b == 0) { //Рассматривается случай, когда одно из if (a == 0) { //делимых равно нулю return b; } else { return a; } } else { if (a > b) { return Euclid(a % b, b); //рекурсивно вызовется } // алгоритм, если будет остаток от деления большего if (b > a) { // числа на меньшее и наоборот return Euclid(a, b % a); } else return Euclid(a % b, b); } } public static void main(String[] args) { Scanner sc1 = new Scanner(System.in); //ввод с клавиатуры Scanner sc2 = new Scanner(System.in); int a = sc1.nextInt(); int b = sc2.nextInt(); System.out.println(Euclid(a,b)); } }
Despite the fact that the program works, I decided to practice practicing testing on it.
I am writing a test:
import org.junit.Test; import static org.junit.Assert.*; public class EuclidTest { @Test public void testEuclid() throws Exception { int result = new Euclid(234, 45); //в этой строке ошибка компиляции assertEquals(9, result, 1e-9); } }
In the line where I declare result
I get a compilation error.
When I rewrote the class, replacing the constructor with a method with a different name, everything went like clockwork. Related issues:
- Is it possible to cover the constructor with tests so as not to cause a compilation error?
- How to do it?