|
| 1 | +package com.thealgorithms.strings; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | +import java.util.stream.Stream; |
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.Arguments; |
| 10 | +import org.junit.jupiter.params.provider.MethodSource; |
| 11 | + |
| 12 | +class TopKFrequentWordsTest { |
| 13 | + |
| 14 | + @ParameterizedTest |
| 15 | + @MethodSource("validTestCases") |
| 16 | + void testFindTopKFrequentWords(String[] words, int k, List<String> expected) { |
| 17 | + assertEquals(expected, TopKFrequentWords.findTopKFrequentWords(words, k)); |
| 18 | + } |
| 19 | + |
| 20 | + static Stream<Arguments> validTestCases() { |
| 21 | + return Stream.of(Arguments.of(new String[] {"i", "love", "leetcode", "i", "love", "coding"}, 2, List.of("i", "love")), Arguments.of(new String[] {"the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"}, 4, List.of("the", "is", "sunny", "day")), |
| 22 | + Arguments.of(new String[] {"bbb", "aaa", "bbb", "aaa", "ccc"}, 2, List.of("aaa", "bbb")), Arguments.of(new String[] {"one", "two", "three"}, 10, List.of("one", "three", "two")), Arguments.of(new String[] {}, 3, List.of()), Arguments.of(new String[] {"x", "x", "y"}, 0, List.of())); |
| 23 | + } |
| 24 | + |
| 25 | + @ParameterizedTest |
| 26 | + @MethodSource("invalidTestCases") |
| 27 | + void testFindTopKFrequentWordsInvalidInput(String[] words, int k) { |
| 28 | + assertThrows(IllegalArgumentException.class, () -> TopKFrequentWords.findTopKFrequentWords(words, k)); |
| 29 | + } |
| 30 | + |
| 31 | + static Stream<Arguments> invalidTestCases() { |
| 32 | + return Stream.of(Arguments.of((String[]) null, 1), Arguments.of(new String[] {"a", null, "b"}, 2), Arguments.of(new String[] {"a"}, -1)); |
| 33 | + } |
| 34 | +} |
0 commit comments