|
| 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