mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 09:06:51 +08:00
Add tests, remove main
in WordBoggle
(#5782)
This commit is contained in:
@ -1021,6 +1021,7 @@
|
||||
* [PalindromeSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromeSinglyLinkedListTest.java)
|
||||
* [RangeInSortedArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/RangeInSortedArrayTest.java)
|
||||
* [TwoSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/TwoSumProblemTest.java)
|
||||
* [WordBoggleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/WordBoggleTest.java)
|
||||
* others
|
||||
* [ArrayLeftRotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java)
|
||||
* [ArrayRightRotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayRightRotationTest.java)
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.thealgorithms.misc;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@ -32,36 +31,6 @@ public final class WordBoggle {
|
||||
return new ArrayList<>(finalWords);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Testcase
|
||||
List<String> ans = new ArrayList<>(Arrays.asList("a", "boggle", "this", "NOTRE_PEATED", "is", "simple", "board"));
|
||||
assert (boggleBoard(
|
||||
new char[][] {
|
||||
{'t', 'h', 'i', 's', 'i', 's', 'a'},
|
||||
{'s', 'i', 'm', 'p', 'l', 'e', 'x'},
|
||||
{'b', 'x', 'x', 'x', 'x', 'e', 'b'},
|
||||
{'x', 'o', 'g', 'g', 'l', 'x', 'o'},
|
||||
{'x', 'x', 'x', 'D', 'T', 'r', 'a'},
|
||||
{'R', 'E', 'P', 'E', 'A', 'd', 'x'},
|
||||
{'x', 'x', 'x', 'x', 'x', 'x', 'x'},
|
||||
{'N', 'O', 'T', 'R', 'E', '_', 'P'},
|
||||
{'x', 'x', 'D', 'E', 'T', 'A', 'E'},
|
||||
},
|
||||
new String[] {
|
||||
"this",
|
||||
"is",
|
||||
"not",
|
||||
"a",
|
||||
"simple",
|
||||
"test",
|
||||
"boggle",
|
||||
"board",
|
||||
"REPEATED",
|
||||
"NOTRE_PEATED",
|
||||
})
|
||||
.equals(ans));
|
||||
}
|
||||
|
||||
public static void explore(int i, int j, char[][] board, TrieNode trieNode, boolean[][] visited, Set<String> finalWords) {
|
||||
if (visited[i][j]) {
|
||||
return;
|
||||
|
56
src/test/java/com/thealgorithms/misc/WordBoggleTest.java
Normal file
56
src/test/java/com/thealgorithms/misc/WordBoggleTest.java
Normal file
@ -0,0 +1,56 @@
|
||||
package com.thealgorithms.misc;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
class WordBoggleTest {
|
||||
private char[][] board;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
board = new char[][] {
|
||||
{'t', 'h', 'i', 's', 'i', 's', 'a'},
|
||||
{'s', 'i', 'm', 'p', 'l', 'e', 'x'},
|
||||
{'b', 'x', 'x', 'x', 'x', 'e', 'b'},
|
||||
{'x', 'o', 'g', 'g', 'l', 'x', 'o'},
|
||||
{'x', 'x', 'x', 'D', 'T', 'r', 'a'},
|
||||
{'R', 'E', 'P', 'E', 'A', 'd', 'x'},
|
||||
{'x', 'x', 'x', 'x', 'x', 'x', 'x'},
|
||||
{'N', 'O', 'T', 'R', 'E', '_', 'P'},
|
||||
{'x', 'x', 'D', 'E', 'T', 'A', 'E'},
|
||||
};
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestCases")
|
||||
void testBoggleBoard(String[] words, List<String> expectedWords, String testDescription) {
|
||||
List<String> result = WordBoggle.boggleBoard(board, words);
|
||||
assertEquals(expectedWords.size(), result.size(), "Test failed for: " + testDescription);
|
||||
assertTrue(expectedWords.containsAll(result), "Test failed for: " + testDescription);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> provideTestCases() {
|
||||
return Stream.of(Arguments.of(new String[] {"this", "is", "not", "a", "simple", "test", "boggle", "board", "REPEATED", "NOTRE_PEATED"}, Arrays.asList("this", "is", "a", "simple", "board", "boggle", "NOTRE_PEATED"), "All words"),
|
||||
Arguments.of(new String[] {"xyz", "hello", "world"}, List.of(), "No matching words"), Arguments.of(new String[] {}, List.of(), "Empty words array"), Arguments.of(new String[] {"this", "this", "board", "board"}, Arrays.asList("this", "board"), "Duplicate words in input"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideSpecialCases")
|
||||
void testBoggleBoardSpecialCases(char[][] specialBoard, String[] words, List<String> expectedWords, String testDescription) {
|
||||
List<String> result = WordBoggle.boggleBoard(specialBoard, words);
|
||||
assertEquals(expectedWords.size(), result.size(), "Test failed for: " + testDescription);
|
||||
assertTrue(expectedWords.containsAll(result), "Test failed for: " + testDescription);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> provideSpecialCases() {
|
||||
return Stream.of(Arguments.of(new char[0][0], new String[] {"this", "is", "a", "test"}, List.of(), "Empty board"));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user