Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/strings/AhoCorasickTest.java (Java)

Code before:
    @BeforeEach
    void setUp() {
        patterns = new String[] {"ACC", "ATC", "CAT", "GCG", "C", "T"};
        text = "GCATCG";
    }

    @Test
    void testSearch() {
        // Test searching for multiple patterns in the input text.
        Map<String, ArrayList<Integer>> positions = AhoCorasick.search(text, patterns);

        // Define expected results for each pattern
        int[][] expected = {
            {}, // "ACC" should not be found
            {2}, // "ATC" should be found at index 2
            {1}, // "CAT" should be found at index 1
            {}, // "GCG" should not be found
            {1, 4}, // "C" should be found at indices 1 and 4
            {3} // "T" should be found at index 3
        };

        // Check specific pattern occurrences
        for (int i = 0; i < patterns.length; i++) {
            assertIterableEquals(toList(expected[i]), positions.get(patterns[i]));
        }
    }

    @Test
    void testEmptyPatterns() {
        // Test searching with an empty pattern array.
        String[] emptyPatterns = {};
        Map<String, ArrayList<Integer>> positions = AhoCorasick.search(text, emptyPatterns);

        // Verify that no patterns are found
        for (String pattern : emptyPatterns) {
            assertTrue(positions.get(pattern).isEmpty());
        }
    }

    @Test
    void testPatternNotFound() {
        // Test searching for patterns that are not present in the input text.
        String[] patterns = {"XYZ", "123"};
        Map<String, ArrayList<Integer>> positions = AhoCorasick.search(text, patterns);

        // Verify that empty lists are found for patterns that are not in the text.
        for (String pattern : patterns) {
            assertEquals(positions.get(pattern).size(), 0);
        }
    }


Review comment (suggestion):
Please notice that this test could still pass, if the `search` method would return a map with some additional keys.

What do you think about the following:
```suggestion
    @Test
    void testSearch() {
        final var expected = Map.of("ACC", new ArrayList<>(Arrays.asList()), "ATC", new ArrayList<>(Arrays.asList(2)), "CAT", new ArrayList<>(Arrays.asList(1)), "GCG", new ArrayList<>(Arrays.asList()), "C", new ArrayList<>(Arrays.asList(1, 4)), "T", new ArrayList<>(Arrays.asList(3)));

        assertEquals(expected, AhoCorasick.search(text, patterns));
    }
```

In case of normal line lengths, the code above looks really nice (in my opinion).

Make sure to
```java
import java.util.Arrays;
```

Please apply above idea in other tests.

Code after:
 * This class contains test cases for the Aho-Corasick String Matching Algorithm.
 * The Aho-Corasick algorithm is used to efficiently find all occurrences of multiple
 * patterns in a given text.
 */
class AhoCorasickTest {
    private String[] patterns; // The array of patterns to search for
    private String text; // The input text to search within

    /**
     * This method sets up the test environment before each test case.
     * It initializes the patterns and text to be used for testing.
     */
    @BeforeEach
    void setUp() {
        patterns = new String[] {"ACC", "ATC", "CAT", "GCG", "C", "T"};
        text = "GCATCG";
    }

    /**
     * Test searching for multiple patterns in the input text.
     * The expected results are defined for each pattern.
     */
    @Test
    void testSearch() {
        // Define the expected results for each pattern
        final var expected = Map.of("ACC", new ArrayList<>(Arrays.asList()), "ATC", new ArrayList<>(Arrays.asList(2)), "CAT", new ArrayList<>(Arrays.asList(1)), "GCG", new ArrayList<>(Arrays.asList()), "C", new ArrayList<>(Arrays.asList(1, 4)), "T", new ArrayList<>(Arrays.asList(3)));
        assertEquals(expected, AhoCorasick.search(text, patterns));
    }

    /**
     * Test searching with an empty pattern array.
     * The result should be an empty map.
     */
    @Test
    void testEmptyPatterns() {
        // Define an empty pattern array
        final var emptyPatterns = new String[] {};
        assertTrue(AhoCorasick.search(text, emptyPatterns).isEmpty());
    }

    /**
     * Test searching for patterns that are not present in the input text.
     * The result should be an empty list for each pattern.
     */
    @Test
    void testPatternNotFound() {
        // Define patterns that are not present in the text
        final var searchPatterns = new String[] {"XYZ", "123"};
        final var expected = Map.of("XYZ", new ArrayList<Integer>(), "123", new ArrayList<Integer>());
        assertEquals(expected, AhoCorasick.search(text, searchPatterns));
    }