Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add Unit Test for LargestPrimeDivisor #241

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.interviews</groupId>
<artifactId>interviews</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Interviews Project</name>

<!-- Java version -->
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<!-- Dependencies -->
<dependencies>
<!-- JUnit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<!-- Build configuration to use custom source directories -->
<build>
<sourceDirectory>${project.basedir}/uva</sourceDirectory>
<testSourceDirectory>${project.basedir}/test</testSourceDirectory>
<plugins>
<!-- Compiler plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>

<!-- Surefire plugin for running tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>
</project>
50 changes: 50 additions & 0 deletions test/LargestPrimeDivisorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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));
}
}