Skip to content

Commit 9c497b1

Browse files
committed
Testes mdc
1 parent b0fbd2a commit 9c497b1

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.example;
2+
3+
public class MathUtil {
4+
static int mdc(int a, int b){
5+
a = Math.abs(a);
6+
b = Math.abs(b);
7+
8+
int maior = Math.max(a,b);
9+
b = Math.min(a,b);
10+
a = maior;
11+
12+
if(b>0 && a%b == 0){
13+
return b;
14+
}
15+
16+
if(b == 0){
17+
return Math.abs(a);
18+
}
19+
20+
21+
22+
return mdc(a-b, b);
23+
24+
}
25+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.example;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class MathUtilTest {
8+
@Test //anotação -> rotulam
9+
void testMdcP1BImpar(){
10+
int a = 6, b = 3;
11+
int esperado = 3;
12+
int obtido = MathUtil.mdc(a, b);
13+
14+
assertEquals(esperado, obtido);
15+
}
16+
17+
@Test
18+
void testMdcP1BPar(){
19+
int a = 6, b = 2;
20+
int esperado = 2;
21+
int obtido = MathUtil.mdc(a, b);
22+
23+
assertEquals(esperado, obtido);
24+
}
25+
26+
@Test
27+
void testMdcP3(){
28+
int a = -6;
29+
int b = 0;
30+
int esperado = 6;
31+
int obtido = MathUtil.mdc(a, b);
32+
assertEquals(esperado, obtido);
33+
}
34+
35+
@Test
36+
void testMdcP5(){
37+
int a = 6, b = 2;
38+
int esperado = MathUtil.mdc(b,a);
39+
int obtido = MathUtil.mdc(a, b);
40+
41+
assertEquals(esperado, obtido);
42+
}
43+
44+
@Test
45+
void testMdcP7(){
46+
int a = 6, b = 2;
47+
int esperado = MathUtil.mdc(-a, b);
48+
int obtido = MathUtil.mdc(a, -b);
49+
50+
assertEquals(esperado, obtido);
51+
}
52+
53+
@Test
54+
void testMdcP8(){
55+
int a = 6;
56+
int esperado = 6;
57+
int obtido = MathUtil.mdc(a,a);
58+
59+
assertEquals(esperado, obtido);
60+
}
61+
62+
63+
@Test
64+
void testMdcP8Negativo(){
65+
int a = -6;
66+
int esperado = 6;
67+
int obtido = MathUtil.mdc(a,a);
68+
69+
assertEquals(esperado, obtido);
70+
}
71+
72+
@Test
73+
void testMdcP12DoisPrimos(){
74+
int p = 7, a = p;
75+
int esperado = p;
76+
int obtido = MathUtil.mdc(p,a);
77+
78+
assertEquals(esperado, obtido);
79+
}
80+
81+
@Test
82+
void testMdcP12UmPrimo(){
83+
int p = 7, a = 2;
84+
int esperado = 1;
85+
int obtido = MathUtil.mdc(p,a);
86+
87+
assertEquals(esperado, obtido);
88+
}
89+
90+
@Test
91+
void testMdcCasoGeral30e12(){
92+
int a = 30, b = 12;
93+
int esperado = 6;
94+
int obtido = MathUtil.mdc(a,b);
95+
96+
assertEquals(esperado, obtido);
97+
}
98+
99+
@Test
100+
void testMdcCasoGeral12e9(){
101+
int a = 12, b = 9;
102+
int esperado = 3;
103+
int obtido = MathUtil.mdc(a,b);
104+
105+
assertEquals(esperado, obtido);
106+
}
107+
108+
109+
110+
}

0 commit comments

Comments
 (0)