|
1 | 1 | package com.thealgorithms.strings;
|
2 | 2 |
|
3 |
| -import static org.junit.jupiter.api.Assertions.assertTrue; |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
4 | 5 |
|
5 |
| -import java.util.Arrays; |
6 | 6 | import java.util.List;
|
7 |
| -import org.junit.jupiter.api.Test; |
| 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; |
8 | 11 |
|
9 | 12 | public class LetterCombinationsOfPhoneNumberTest {
|
10 | 13 |
|
11 |
| - @Test |
12 |
| - public void letterCombinationsOfPhoneNumber() { |
13 |
| - LetterCombinationsOfPhoneNumber.generateNumberToCharMap(); |
14 |
| - |
15 |
| - // ** Test 1 ** |
16 |
| - // Input: digits = "" |
17 |
| - // Output: [] |
18 |
| - int[] numbers1 = {}; |
19 |
| - List<String> output1 = Arrays.asList(""); |
20 |
| - assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers1, numbers1.length, 0, "").equals(output1)); |
| 14 | + @ParameterizedTest |
| 15 | + @MethodSource("provideTestCases") |
| 16 | + public void testLetterCombinationsOfPhoneNumber(int[] numbers, List<String> expectedOutput) { |
| 17 | + assertEquals(expectedOutput, LetterCombinationsOfPhoneNumber.getCombinations(numbers)); |
| 18 | + } |
21 | 19 |
|
22 |
| - // ** Test 2 ** |
23 |
| - // Input: digits = "2" |
24 |
| - // Output: ["a","b","c"] |
25 |
| - int[] numbers2 = {2}; |
26 |
| - List<String> output2 = Arrays.asList("a", "b", "c"); |
27 |
| - assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers2, numbers2.length, 0, "").equals(output2)); |
| 20 | + @ParameterizedTest |
| 21 | + @MethodSource("wrongInputs") |
| 22 | + void throwsForWrongInput(int[] numbers) { |
| 23 | + assertThrows(IllegalArgumentException.class, () -> LetterCombinationsOfPhoneNumber.getCombinations(numbers)); |
| 24 | + } |
28 | 25 |
|
29 |
| - // ** Test 3 ** |
30 |
| - // Input: digits = "23" |
31 |
| - // Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"] |
32 |
| - int[] numbers3 = {2, 3}; |
33 |
| - List<String> output3 = Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"); |
34 |
| - assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers3, numbers3.length, 0, "").equals(output3)); |
| 26 | + private static Stream<Arguments> provideTestCases() { |
| 27 | + return Stream.of(Arguments.of(null, List.of("")), Arguments.of(new int[] {}, List.of("")), Arguments.of(new int[] {2}, List.of("a", "b", "c")), Arguments.of(new int[] {2, 3}, List.of("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf")), |
| 28 | + Arguments.of(new int[] {2, 3, 4}, List.of("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi")), |
| 29 | + Arguments.of(new int[] {3, 3}, List.of("dd", "de", "df", "ed", "ee", "ef", "fd", "fe", "ff")), Arguments.of(new int[] {8, 4}, List.of("tg", "th", "ti", "ug", "uh", "ui", "vg", "vh", "vi")), Arguments.of(new int[] {2, 0}, List.of("a ", "b ", "c ")), |
| 30 | + Arguments.of(new int[] {9, 2}, List.of("wa", "wb", "wc", "xa", "xb", "xc", "ya", "yb", "yc", "za", "zb", "zc")), Arguments.of(new int[] {0}, List.of(" ")), Arguments.of(new int[] {1}, List.of("")), Arguments.of(new int[] {2}, List.of("a", "b", "c")), |
| 31 | + Arguments.of(new int[] {1, 2, 0, 4}, List.of("a g", "a h", "a i", "b g", "b h", "b i", "c g", "c h", "c i"))); |
| 32 | + } |
35 | 33 |
|
36 |
| - // ** Test 4 ** |
37 |
| - // Input: digits = "234" |
38 |
| - // Output: ["adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", |
39 |
| - // "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", |
40 |
| - // "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"] |
41 |
| - int[] numbers4 = {2, 3, 4}; |
42 |
| - List<String> output4 = Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"); |
43 |
| - assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers4, numbers4.length, 0, "").equals(output4)); |
| 34 | + private static Stream<Arguments> wrongInputs() { |
| 35 | + return Stream.of(Arguments.of(new int[] {-1}), Arguments.of(new int[] {10}), Arguments.of(new int[] {2, 2, -1, 0}), Arguments.of(new int[] {0, 0, 0, 10})); |
44 | 36 | }
|
45 | 37 | }
|
0 commit comments