Skip to content

Commit 19b2352

Browse files
committed
Fix
1 parent 7a31147 commit 19b2352

File tree

1 file changed

+22
-49
lines changed

1 file changed

+22
-49
lines changed

src/test/java/com/thealgorithms/misc/WordBoggleTest.java

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55

66
import java.util.Arrays;
77
import java.util.List;
8+
import java.util.stream.Stream;
89
import org.junit.jupiter.api.BeforeEach;
9-
import org.junit.jupiter.api.Test;
10-
11-
public class WordBoggleTest {
10+
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.Arguments;
12+
import org.junit.jupiter.params.provider.MethodSource;
1213

14+
class WordBoggleTest {
1315
private char[][] board;
14-
private String[] words;
1516

1617
@BeforeEach
17-
public void setup() {
18+
void setup() {
1819
board = new char[][] {
1920
{'t', 'h', 'i', 's', 'i', 's', 'a'},
2021
{'s', 'i', 'm', 'p', 'l', 'e', 'x'},
@@ -26,58 +27,30 @@ public void setup() {
2627
{'N', 'O', 'T', 'R', 'E', '_', 'P'},
2728
{'x', 'x', 'D', 'E', 'T', 'A', 'E'},
2829
};
29-
30-
words = new String[] {"this", "is", "not", "a", "simple", "test", "boggle", "board", "REPEATED", "NOTRE_PEATED"};
3130
}
3231

33-
@Test
34-
public void testBoggleBoardFindsAllWords() {
35-
List<String> expected = Arrays.asList("this", "is", "a", "simple", "board", "boggle", "NOTRE_PEATED");
32+
@ParameterizedTest
33+
@MethodSource("provideTestCases")
34+
void testBoggleBoard(String[] words, List<String> expectedWords, String testDescription) {
3635
List<String> result = WordBoggle.boggleBoard(board, words);
37-
assertEquals(expected.size(), result.size());
38-
assertTrue(expected.containsAll(result));
39-
}
40-
41-
@Test
42-
public void testBoggleBoardNoMatchingWords() {
43-
// Test with words that don't exist on the board
44-
String[] nonMatchingWords = {"xyz", "hello", "world"};
45-
List<String> result = WordBoggle.boggleBoard(board, nonMatchingWords);
46-
assertEquals(0, result.size());
47-
}
48-
49-
@Test
50-
public void testBoggleBoardEmptyBoard() {
51-
// Test with an empty board
52-
char[][] emptyBoard = new char[0][0];
53-
List<String> result = WordBoggle.boggleBoard(emptyBoard, words);
54-
assertEquals(0, result.size());
36+
assertEquals(expectedWords.size(), result.size(), "Test failed for: " + testDescription);
37+
assertTrue(expectedWords.containsAll(result), "Test failed for: " + testDescription);
5538
}
5639

57-
@Test
58-
public void testBoggleBoardEmptyWordsArray() {
59-
// Test with an empty words array
60-
String[] emptyWords = {};
61-
List<String> result = WordBoggle.boggleBoard(board, emptyWords);
62-
assertEquals(0, result.size());
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"));
6343
}
6444

65-
@Test
66-
public void testBoggleBoardSingleCharacterWords() {
67-
// Test with single-character words
68-
String[] singleCharWords = {"a", "x", "o"};
69-
List<String> expected = Arrays.asList("a", "o");
70-
List<String> result = WordBoggle.boggleBoard(board, singleCharWords);
71-
assertEquals(expected.size() + 1, result.size());
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);
7251
}
7352

74-
@Test
75-
public void testBoggleBoardDuplicateWordsInInput() {
76-
// Test with duplicate words in the input array
77-
String[] duplicateWords = {"this", "this", "board", "board"};
78-
List<String> expected = Arrays.asList("this", "board");
79-
List<String> result = WordBoggle.boggleBoard(board, duplicateWords);
80-
assertEquals(expected.size(), result.size());
81-
assertTrue(expected.containsAll(result));
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"));
8255
}
8356
}

0 commit comments

Comments
 (0)