Skip to content

Commit f710b20

Browse files
committed
Add tests, remove main in WordBoggle
1 parent 596c614 commit f710b20

File tree

2 files changed

+84
-31
lines changed

2 files changed

+84
-31
lines changed

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

Lines changed: 0 additions & 31 deletions
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;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class WordBoggleTest {
12+
13+
private char[][] board;
14+
private String[] words;
15+
16+
@BeforeEach
17+
public void setup() {
18+
// Initialize the test board and words before each test
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+
words = new String[] {"this", "is", "not", "a", "simple", "test", "boggle", "board", "REPEATED", "NOTRE_PEATED"};
32+
}
33+
34+
@Test
35+
public void testBoggleBoard_FindsAllWords() {
36+
List<String> expected = Arrays.asList("this", "is", "a", "simple", "board", "boggle", "NOTRE_PEATED");
37+
List<String> result = WordBoggle.boggleBoard(board, words);
38+
assertEquals(expected.size(), result.size());
39+
assertTrue(expected.containsAll(result));
40+
}
41+
42+
@Test
43+
public void testBoggleBoard_NoMatchingWords() {
44+
// Test with words that don't exist on the board
45+
String[] nonMatchingWords = {"xyz", "hello", "world"};
46+
List<String> result = WordBoggle.boggleBoard(board, nonMatchingWords);
47+
assertEquals(0, result.size());
48+
}
49+
50+
@Test
51+
public void testBoggleBoard_EmptyBoard() {
52+
// Test with an empty board
53+
char[][] emptyBoard = new char[0][0];
54+
List<String> result = WordBoggle.boggleBoard(emptyBoard, words);
55+
assertEquals(0, result.size());
56+
}
57+
58+
@Test
59+
public void testBoggleBoard_EmptyWordsArray() {
60+
// Test with an empty words array
61+
String[] emptyWords = {};
62+
List<String> result = WordBoggle.boggleBoard(board, emptyWords);
63+
assertEquals(0, result.size());
64+
}
65+
66+
@Test
67+
public void testBoggleBoard_SingleCharacterWords() {
68+
// Test with single-character words
69+
String[] singleCharWords = {"a", "x", "o"};
70+
List<String> expected = Arrays.asList("a", "o");
71+
List<String> result = WordBoggle.boggleBoard(board, singleCharWords);
72+
assertEquals(expected.size() + 1, result.size());
73+
}
74+
75+
@Test
76+
public void testBoggleBoard_DuplicateWordsInInput() {
77+
// Test with duplicate words in the input array
78+
String[] duplicateWords = {"this", "this", "board", "board"};
79+
List<String> expected = Arrays.asList("this", "board");
80+
List<String> result = WordBoggle.boggleBoard(board, duplicateWords);
81+
assertEquals(expected.size(), result.size());
82+
assertTrue(expected.containsAll(result));
83+
}
84+
}

0 commit comments

Comments
 (0)