|
| 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 java.util.stream.Stream; |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.params.ParameterizedTest; |
| 11 | +import org.junit.jupiter.params.provider.Arguments; |
| 12 | +import org.junit.jupiter.params.provider.MethodSource; |
| 13 | + |
| 14 | +class WordBoggleTest { |
| 15 | + private char[][] board; |
| 16 | + |
| 17 | + @BeforeEach |
| 18 | + void setup() { |
| 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 | + |
| 32 | + @ParameterizedTest |
| 33 | + @MethodSource("provideTestCases") |
| 34 | + void testBoggleBoard(String[] words, List<String> expectedWords, String testDescription) { |
| 35 | + List<String> result = WordBoggle.boggleBoard(board, words); |
| 36 | + assertEquals(expectedWords.size(), result.size(), "Test failed for: " + testDescription); |
| 37 | + assertTrue(expectedWords.containsAll(result), "Test failed for: " + testDescription); |
| 38 | + } |
| 39 | + |
| 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")); |
| 43 | + } |
| 44 | + |
| 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); |
| 51 | + } |
| 52 | + |
| 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")); |
| 55 | + } |
| 56 | +} |
0 commit comments