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

Code before:
        assertEquals(6, res.size());
    }

    @Test
    void testSearch() {
        // Note - Indexing is zero based

        // Test that the first pattern "ACC" is not found in the text
        assertEquals(0, res.get(0).size());

        // Test that the second pattern "ATC" is found at index 2
        assertEquals(2, res.get(1).get(0));

        // Test that the third pattern "CAT" is found at index 1
        assertEquals(1, res.get(2).get(0));

        // Test that the fourth pattern "GCG" is not found in the text
        assertEquals(0, res.get(3).size());

        // Test that the fifth pattern "C" is found at indices 1 and 4
        assertEquals(2, res.get(4).size());
        assertEquals(1, res.get(4).get(0));
        assertEquals(4, res.get(4).get(1));

        // Test that the sixth pattern "T" is found at index 3
        assertEquals(3, res.get(5).get(0));
    }

    @Test
    void testRepeatCountOfWords() {
        ArrayList<Integer> countOfWords = ahoCorasick.getRepeatCountOfWords();

        // Test that the counts match the expected values
        assertEquals(0, countOfWords.get(0)); // "ACC" appears zero times
        assertEquals(1, countOfWords.get(1)); // "ATC" appears once
        assertEquals(1, countOfWords.get(2)); // "CAT" appears once
        assertEquals(0, countOfWords.get(3)); // "GCG" does not appear
        assertEquals(2, countOfWords.get(4)); // "C" appears twice
        assertEquals(1, countOfWords.get(5)); // "T" appears once
    }
}


Review comment (suggestion):
What do you think about more _direct test_ i.e. defining a variable `expected` and just checking if `res` and `expected` are equal.

Code after:
    /**
     * 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
    void testPatternAtBeginning() {
        // Define patterns that start at the beginning of the text
        final var searchPatterns = new String[] {"GC", "GCA", "GCAT"};
        final var expected = Map.of("GC", new ArrayList<Integer>(Arrays.asList(0)), "GCA", new ArrayList<Integer>(Arrays.asList(0)), "GCAT", new ArrayList<Integer>(Arrays.asList(0)));
        assertEquals(expected, AhoCorasick.search(text, searchPatterns));
    }

    /**
     * Test searching for patterns that end at the end of the input text.
     * The expected positions are 4, 3, and 2 for the patterns.
     */
    @Test