From f710b20f17a252e95ca66a9c9f17e6f3bebab4d1 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 13 Oct 2024 20:02:57 +0530 Subject: [PATCH 1/5] Add tests, remove `main` in `WordBoggle` --- .../com/thealgorithms/misc/WordBoggle.java | 31 ------- .../thealgorithms/misc/WordBoggleTest.java | 84 +++++++++++++++++++ 2 files changed, 84 insertions(+), 31 deletions(-) create mode 100644 src/test/java/com/thealgorithms/misc/WordBoggleTest.java diff --git a/src/main/java/com/thealgorithms/misc/WordBoggle.java b/src/main/java/com/thealgorithms/misc/WordBoggle.java index 3eb0dc95ffb5..8b629d68209b 100644 --- a/src/main/java/com/thealgorithms/misc/WordBoggle.java +++ b/src/main/java/com/thealgorithms/misc/WordBoggle.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 static List boggleBoard(char[][] board, String[] words) { return new ArrayList<>(finalWords); } - public static void main(String[] args) { - // Testcase - List 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 finalWords) { if (visited[i][j]) { return; diff --git a/src/test/java/com/thealgorithms/misc/WordBoggleTest.java b/src/test/java/com/thealgorithms/misc/WordBoggleTest.java new file mode 100644 index 000000000000..e9722e8b075a --- /dev/null +++ b/src/test/java/com/thealgorithms/misc/WordBoggleTest.java @@ -0,0 +1,84 @@ +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 org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class WordBoggleTest { + + private char[][] board; + private String[] words; + + @BeforeEach + public void setup() { + // Initialize the test board and words before each test + 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'}, + }; + + words = new String[] {"this", "is", "not", "a", "simple", "test", "boggle", "board", "REPEATED", "NOTRE_PEATED"}; + } + + @Test + public void testBoggleBoard_FindsAllWords() { + List expected = Arrays.asList("this", "is", "a", "simple", "board", "boggle", "NOTRE_PEATED"); + List result = WordBoggle.boggleBoard(board, words); + assertEquals(expected.size(), result.size()); + assertTrue(expected.containsAll(result)); + } + + @Test + public void testBoggleBoard_NoMatchingWords() { + // Test with words that don't exist on the board + String[] nonMatchingWords = {"xyz", "hello", "world"}; + List result = WordBoggle.boggleBoard(board, nonMatchingWords); + assertEquals(0, result.size()); + } + + @Test + public void testBoggleBoard_EmptyBoard() { + // Test with an empty board + char[][] emptyBoard = new char[0][0]; + List result = WordBoggle.boggleBoard(emptyBoard, words); + assertEquals(0, result.size()); + } + + @Test + public void testBoggleBoard_EmptyWordsArray() { + // Test with an empty words array + String[] emptyWords = {}; + List result = WordBoggle.boggleBoard(board, emptyWords); + assertEquals(0, result.size()); + } + + @Test + public void testBoggleBoard_SingleCharacterWords() { + // Test with single-character words + String[] singleCharWords = {"a", "x", "o"}; + List expected = Arrays.asList("a", "o"); + List result = WordBoggle.boggleBoard(board, singleCharWords); + assertEquals(expected.size() + 1, result.size()); + } + + @Test + public void testBoggleBoard_DuplicateWordsInInput() { + // Test with duplicate words in the input array + String[] duplicateWords = {"this", "this", "board", "board"}; + List expected = Arrays.asList("this", "board"); + List result = WordBoggle.boggleBoard(board, duplicateWords); + assertEquals(expected.size(), result.size()); + assertTrue(expected.containsAll(result)); + } +} From 1b2112a4326e6b6f90cef11d7a07518b9ec7e19b Mon Sep 17 00:00:00 2001 From: Hardvan Date: Sun, 13 Oct 2024 14:33:15 +0000 Subject: [PATCH 2/5] Update directory --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 06307d4aca78..4177b363adb5 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -996,6 +996,7 @@ * [MirrorOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java) * [PalindromeSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromeSinglyLinkedListTest.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) From a99b4dbceb524ea39360ddc22d99faeb8d9f4bb0 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 13 Oct 2024 20:09:24 +0530 Subject: [PATCH 3/5] Fix --- src/test/java/com/thealgorithms/misc/WordBoggleTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/misc/WordBoggleTest.java b/src/test/java/com/thealgorithms/misc/WordBoggleTest.java index e9722e8b075a..a022931327fb 100644 --- a/src/test/java/com/thealgorithms/misc/WordBoggleTest.java +++ b/src/test/java/com/thealgorithms/misc/WordBoggleTest.java @@ -15,7 +15,6 @@ public class WordBoggleTest { @BeforeEach public void setup() { - // Initialize the test board and words before each test board = new char[][] { {'t', 'h', 'i', 's', 'i', 's', 'a'}, {'s', 'i', 'm', 'p', 'l', 'e', 'x'}, From 7a31147c05e058518810190dc5f731ea242a5eaa Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 13 Oct 2024 20:12:01 +0530 Subject: [PATCH 4/5] Fix --- .../java/com/thealgorithms/misc/WordBoggleTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/thealgorithms/misc/WordBoggleTest.java b/src/test/java/com/thealgorithms/misc/WordBoggleTest.java index a022931327fb..fced4e299624 100644 --- a/src/test/java/com/thealgorithms/misc/WordBoggleTest.java +++ b/src/test/java/com/thealgorithms/misc/WordBoggleTest.java @@ -31,7 +31,7 @@ public void setup() { } @Test - public void testBoggleBoard_FindsAllWords() { + public void testBoggleBoardFindsAllWords() { List expected = Arrays.asList("this", "is", "a", "simple", "board", "boggle", "NOTRE_PEATED"); List result = WordBoggle.boggleBoard(board, words); assertEquals(expected.size(), result.size()); @@ -39,7 +39,7 @@ public void testBoggleBoard_FindsAllWords() { } @Test - public void testBoggleBoard_NoMatchingWords() { + public void testBoggleBoardNoMatchingWords() { // Test with words that don't exist on the board String[] nonMatchingWords = {"xyz", "hello", "world"}; List result = WordBoggle.boggleBoard(board, nonMatchingWords); @@ -47,7 +47,7 @@ public void testBoggleBoard_NoMatchingWords() { } @Test - public void testBoggleBoard_EmptyBoard() { + public void testBoggleBoardEmptyBoard() { // Test with an empty board char[][] emptyBoard = new char[0][0]; List result = WordBoggle.boggleBoard(emptyBoard, words); @@ -55,7 +55,7 @@ public void testBoggleBoard_EmptyBoard() { } @Test - public void testBoggleBoard_EmptyWordsArray() { + public void testBoggleBoardEmptyWordsArray() { // Test with an empty words array String[] emptyWords = {}; List result = WordBoggle.boggleBoard(board, emptyWords); @@ -63,7 +63,7 @@ public void testBoggleBoard_EmptyWordsArray() { } @Test - public void testBoggleBoard_SingleCharacterWords() { + public void testBoggleBoardSingleCharacterWords() { // Test with single-character words String[] singleCharWords = {"a", "x", "o"}; List expected = Arrays.asList("a", "o"); @@ -72,7 +72,7 @@ public void testBoggleBoard_SingleCharacterWords() { } @Test - public void testBoggleBoard_DuplicateWordsInInput() { + public void testBoggleBoardDuplicateWordsInInput() { // Test with duplicate words in the input array String[] duplicateWords = {"this", "this", "board", "board"}; List expected = Arrays.asList("this", "board"); From 19b2352dda5b855f1e3bc5b75b37e018f738dfaf Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Mon, 14 Oct 2024 08:32:54 +0530 Subject: [PATCH 5/5] Fix --- .../thealgorithms/misc/WordBoggleTest.java | 71 ++++++------------- 1 file changed, 22 insertions(+), 49 deletions(-) diff --git a/src/test/java/com/thealgorithms/misc/WordBoggleTest.java b/src/test/java/com/thealgorithms/misc/WordBoggleTest.java index fced4e299624..2c79ec796565 100644 --- a/src/test/java/com/thealgorithms/misc/WordBoggleTest.java +++ b/src/test/java/com/thealgorithms/misc/WordBoggleTest.java @@ -5,16 +5,17 @@ import java.util.Arrays; import java.util.List; +import java.util.stream.Stream; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -public class WordBoggleTest { +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; - private String[] words; @BeforeEach - public void setup() { + void setup() { board = new char[][] { {'t', 'h', 'i', 's', 'i', 's', 'a'}, {'s', 'i', 'm', 'p', 'l', 'e', 'x'}, @@ -26,58 +27,30 @@ public void setup() { {'N', 'O', 'T', 'R', 'E', '_', 'P'}, {'x', 'x', 'D', 'E', 'T', 'A', 'E'}, }; - - words = new String[] {"this", "is", "not", "a", "simple", "test", "boggle", "board", "REPEATED", "NOTRE_PEATED"}; } - @Test - public void testBoggleBoardFindsAllWords() { - List expected = Arrays.asList("this", "is", "a", "simple", "board", "boggle", "NOTRE_PEATED"); + @ParameterizedTest + @MethodSource("provideTestCases") + void testBoggleBoard(String[] words, List expectedWords, String testDescription) { List result = WordBoggle.boggleBoard(board, words); - assertEquals(expected.size(), result.size()); - assertTrue(expected.containsAll(result)); - } - - @Test - public void testBoggleBoardNoMatchingWords() { - // Test with words that don't exist on the board - String[] nonMatchingWords = {"xyz", "hello", "world"}; - List result = WordBoggle.boggleBoard(board, nonMatchingWords); - assertEquals(0, result.size()); - } - - @Test - public void testBoggleBoardEmptyBoard() { - // Test with an empty board - char[][] emptyBoard = new char[0][0]; - List result = WordBoggle.boggleBoard(emptyBoard, words); - assertEquals(0, result.size()); + assertEquals(expectedWords.size(), result.size(), "Test failed for: " + testDescription); + assertTrue(expectedWords.containsAll(result), "Test failed for: " + testDescription); } - @Test - public void testBoggleBoardEmptyWordsArray() { - // Test with an empty words array - String[] emptyWords = {}; - List result = WordBoggle.boggleBoard(board, emptyWords); - assertEquals(0, result.size()); + private static Stream 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")); } - @Test - public void testBoggleBoardSingleCharacterWords() { - // Test with single-character words - String[] singleCharWords = {"a", "x", "o"}; - List expected = Arrays.asList("a", "o"); - List result = WordBoggle.boggleBoard(board, singleCharWords); - assertEquals(expected.size() + 1, result.size()); + @ParameterizedTest + @MethodSource("provideSpecialCases") + void testBoggleBoardSpecialCases(char[][] specialBoard, String[] words, List expectedWords, String testDescription) { + List result = WordBoggle.boggleBoard(specialBoard, words); + assertEquals(expectedWords.size(), result.size(), "Test failed for: " + testDescription); + assertTrue(expectedWords.containsAll(result), "Test failed for: " + testDescription); } - @Test - public void testBoggleBoardDuplicateWordsInInput() { - // Test with duplicate words in the input array - String[] duplicateWords = {"this", "this", "board", "board"}; - List expected = Arrays.asList("this", "board"); - List result = WordBoggle.boggleBoard(board, duplicateWords); - assertEquals(expected.size(), result.size()); - assertTrue(expected.containsAll(result)); + private static Stream provideSpecialCases() { + return Stream.of(Arguments.of(new char[0][0], new String[] {"this", "is", "a", "test"}, List.of(), "Empty board")); } }