-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Expand file tree
/
Copy pathLargestPrimeDivisorTest.java
More file actions
50 lines (40 loc) · 1.36 KB
/
LargestPrimeDivisorTest.java
File metadata and controls
50 lines (40 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class LargestPrimeDivisorTest {
private String runLargestPrimeDivisor(String input) {
ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes());
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setIn(in);
System.setOut(new PrintStream(out));
// Gọi hàm main
LargestPrimeDivisor.main(new String[]{});
return out.toString().trim();
}
@Test
public void testSmallNumbers() {
String input = "2\n6\n100\n0\n";
String expectedOutput = "-1\n3\n5";
assertEquals(expectedOutput, runLargestPrimeDivisor(input));
}
@Test
public void testPrimeNumber() {
String input = "17\n0\n";
String expectedOutput = "-1";
assertEquals(expectedOutput, runLargestPrimeDivisor(input));
}
@Test
public void testLargeNumber() {
String input = "13195\n0\n";
String expectedOutput = "29"; // Ước số nguyên tố lớn nhất của 13195 là 29
assertEquals(expectedOutput, runLargestPrimeDivisor(input));
}
@Test
public void testNegativeNumbers() {
String input = "-100\n0\n";
String expectedOutput = "5";
assertEquals(expectedOutput, runLargestPrimeDivisor(input));
}
}