Skip to content

Add tests, remove main in WordBoggle #5782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 0 additions & 31 deletions src/main/java/com/thealgorithms/misc/WordBoggle.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,36 +31,6 @@ public static List<String> boggleBoard(char[][] board, String[] words) {
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;
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/com/thealgorithms/misc/WordBoggleTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}