Add black box tests for queryBridgeWords.

This commit is contained in:
ulic-youthlic 2025-06-01 00:39:00 +08:00
parent 338d172893
commit 3f6c19ed28
3 changed files with 82 additions and 4 deletions

View file

@ -34,6 +34,8 @@ dependencies {
implementation(libs.guava)
implementation("org.jline:jline:3.29.0")
compileOnly("com.github.spotbugs:spotbugs-annotations:3.1.3")
}
// Apply a specific Java toolchain to ease working on different environments.

View file

@ -1,6 +1,5 @@
package fun.youthlic;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
@ -26,7 +25,6 @@ import org.jline.terminal.TerminalBuilder;
*/
@SuppressWarnings("ALL")
public class GraphCLI {
@SuppressFBWarnings({"DM_DEFAULT_ENCODING", "PATH_TRAVERSAL_IN"})
static String readFile(final String filename) {
String text = null;
try (BufferedReader inputReader = new BufferedReader(new FileReader(filename))) {
@ -255,7 +253,6 @@ class GraphCLIHelper {
return output.toString();
}
@SuppressFBWarnings({"PREDICTABLE_RANDOM", "DMI_RANDOM_USED_ONLY_ONCE"})
String generateNewText(final String inputText) {
final var output = new StringBuilder();
final var words = inputText.split("\\s+");
@ -332,7 +329,6 @@ class GraphCLIHelper {
return graph.computePageRank(word);
}
@SuppressFBWarnings("DM_DEFAULT_ENCODING")
String randomWalk() {
final var text = String.join(" ", graph.randomWalk());
final Path path = Paths.get("output.txt");

View file

@ -0,0 +1,80 @@
package fun.youthlic;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class GraphCLIHelperTest {
private final GraphCLIHelper graph = new GraphCLIHelper("B D E H A C D H A F D H A B G H A C G H A D G H");
@Test
void testWord1NotInGraph() {
var result = graph.queryBridgeWords("x", "a");
assertEquals("No x or a in the graph!", result);
}
@Test
void testWord2NotInGraph() {
String result = graph.queryBridgeWords("a", "x");
assertEquals("No a or x in the graph!", result);
}
@Test
void testBothWordsNotInGraph() {
String result = graph.queryBridgeWords("x", "y");
assertEquals("No x or y in the graph!", result);
}
@Test
void testNoBridgeWords() {
String result = graph.queryBridgeWords("a", "b");
assertEquals("No bridge words from a to b!", result);
}
@Test
void testSingleBridgeWord() {
String result = graph.queryBridgeWords("b", "e");
assertEquals("The bridge words from b to e are: d.", result);
}
@Test
void testTwoBridgeWords() {
String result = graph.queryBridgeWords("c", "h");
assertEquals("The bridge words from c to h are: g, and d.", result);
}
@Test
void testThreeBridgeWords() {
String result = graph.queryBridgeWords("a", "g");
assertEquals("The bridge words from a to g are: c, b, and d.", result);
}
@Test
void testSameWordInput() {
String result = graph.queryBridgeWords("a", "a");
assertEquals("No bridge words from a to a!", result);
}
@Test
void testWord1Empty() {
String result = graph.queryBridgeWords("", "a");
assertEquals("No or a in the graph!", result);
}
@Test
void testWord2Empty() {
String result = graph.queryBridgeWords("a", "");
assertEquals("No a or in the graph!", result);
}
@Test
void testBothWordsEmpty() {
String result = graph.queryBridgeWords("", "");
assertEquals("No or in the graph!", result);
}
@Test
void testCaseMismatch() {
String result = graph.queryBridgeWords("B", "E");
assertEquals("The bridge words from B to E are: d.", result);
}
}