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

Code before:
    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)));

        // Perform the search using the Aho-Corasick algorithm
        final var positions = AhoCorasick.search(text, patterns);

        // Compare the actual results with the expected results
        assertEquals(expected, positions);
    }

    /**
     * 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[] {};

        // Perform the search using the Aho-Corasick algorithm
        final var positions = AhoCorasick.search(text, emptyPatterns);

        // Verify that no patterns are found (empty map)
        assertTrue(positions.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

Review comment (suggestion):
There is no need for these comments.
```suggestion
        assertEquals(expected, AhoCorasick.search(text, patterns));
```

Code after:
    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));
    }

    /**
     * Test searching for patterns that start at the beginning of the input text.
     * The expected position for each pattern is 0.
     */
    @Test