Skip to content

Commit bcf4034

Browse files
authored
Add tests, remove main in WordBoggle (#5782)
1 parent 8d88349 commit bcf4034

File tree

3 files changed

+57
-31
lines changed

3 files changed

+57
-31
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,7 @@
10211021
* [PalindromeSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromeSinglyLinkedListTest.java)
10221022
* [RangeInSortedArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/RangeInSortedArrayTest.java)
10231023
* [TwoSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/TwoSumProblemTest.java)
1024+
* [WordBoggleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/WordBoggleTest.java)
10241025
* others
10251026
* [ArrayLeftRotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java)
10261027
* [ArrayRightRotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayRightRotationTest.java)

src/main/java/com/thealgorithms/misc/WordBoggle.java

-31
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.thealgorithms.misc;
22

33
import java.util.ArrayList;
4-
import java.util.Arrays;
54
import java.util.HashMap;
65
import java.util.HashSet;
76
import java.util.List;
@@ -32,36 +31,6 @@ public static List<String> boggleBoard(char[][] board, String[] words) {
3231
return new ArrayList<>(finalWords);
3332
}
3433

35-
public static void main(String[] args) {
36-
// Testcase
37-
List<String> ans = new ArrayList<>(Arrays.asList("a", "boggle", "this", "NOTRE_PEATED", "is", "simple", "board"));
38-
assert (boggleBoard(
39-
new char[][] {
40-
{'t', 'h', 'i', 's', 'i', 's', 'a'},
41-
{'s', 'i', 'm', 'p', 'l', 'e', 'x'},
42-
{'b', 'x', 'x', 'x', 'x', 'e', 'b'},
43-
{'x', 'o', 'g', 'g', 'l', 'x', 'o'},
44-
{'x', 'x', 'x', 'D', 'T', 'r', 'a'},
45-
{'R', 'E', 'P', 'E', 'A', 'd', 'x'},
46-
{'x', 'x', 'x', 'x', 'x', 'x', 'x'},
47-
{'N', 'O', 'T', 'R', 'E', '_', 'P'},
48-
{'x', 'x', 'D', 'E', 'T', 'A', 'E'},
49-
},
50-
new String[] {
51-
"this",
52-
"is",
53-
"not",
54-
"a",
55-
"simple",
56-
"test",
57-
"boggle",
58-
"board",
59-
"REPEATED",
60-
"NOTRE_PEATED",
61-
})
62-
.equals(ans));
63-
}
64-
6534
public static void explore(int i, int j, char[][] board, TrieNode trieNode, boolean[][] visited, Set<String> finalWords) {
6635
if (visited[i][j]) {
6736
return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.misc;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.stream.Stream;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.Arguments;
12+
import org.junit.jupiter.params.provider.MethodSource;
13+
14+
class WordBoggleTest {
15+
private char[][] board;
16+
17+
@BeforeEach
18+
void setup() {
19+
board = new char[][] {
20+
{'t', 'h', 'i', 's', 'i', 's', 'a'},
21+
{'s', 'i', 'm', 'p', 'l', 'e', 'x'},
22+
{'b', 'x', 'x', 'x', 'x', 'e', 'b'},
23+
{'x', 'o', 'g', 'g', 'l', 'x', 'o'},
24+
{'x', 'x', 'x', 'D', 'T', 'r', 'a'},
25+
{'R', 'E', 'P', 'E', 'A', 'd', 'x'},
26+
{'x', 'x', 'x', 'x', 'x', 'x', 'x'},
27+
{'N', 'O', 'T', 'R', 'E', '_', 'P'},
28+
{'x', 'x', 'D', 'E', 'T', 'A', 'E'},
29+
};
30+
}
31+
32+
@ParameterizedTest
33+
@MethodSource("provideTestCases")
34+
void testBoggleBoard(String[] words, List<String> expectedWords, String testDescription) {
35+
List<String> result = WordBoggle.boggleBoard(board, words);
36+
assertEquals(expectedWords.size(), result.size(), "Test failed for: " + testDescription);
37+
assertTrue(expectedWords.containsAll(result), "Test failed for: " + testDescription);
38+
}
39+
40+
private static Stream<Arguments> provideTestCases() {
41+
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"),
42+
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"));
43+
}
44+
45+
@ParameterizedTest
46+
@MethodSource("provideSpecialCases")
47+
void testBoggleBoardSpecialCases(char[][] specialBoard, String[] words, List<String> expectedWords, String testDescription) {
48+
List<String> result = WordBoggle.boggleBoard(specialBoard, words);
49+
assertEquals(expectedWords.size(), result.size(), "Test failed for: " + testDescription);
50+
assertTrue(expectedWords.containsAll(result), "Test failed for: " + testDescription);
51+
}
52+
53+
private static Stream<Arguments> provideSpecialCases() {
54+
return Stream.of(Arguments.of(new char[0][0], new String[] {"this", "is", "a", "test"}, List.of(), "Empty board"));
55+
}
56+
}

0 commit comments

Comments
 (0)