Enhance docs, add more tests in PhoneticAlphabetConverter (#5943)

This commit is contained in:
Hardik Pawar
2024-10-23 12:11:48 +05:30
committed by GitHub
parent 757d10c277
commit 03fe106e32
2 changed files with 34 additions and 12 deletions

View File

@ -58,9 +58,25 @@ public final class PhoneticAlphabetConverter {
PHONETIC_MAP.put('9', "Nine"); PHONETIC_MAP.put('9', "Nine");
} }
/**
* Converts text to the NATO phonetic alphabet.
* Steps:
* 1. Convert the text to uppercase.
* 2. Iterate over each character in the text.
* 3. Get the phonetic equivalent of the character from the map.
* 4. Append the phonetic equivalent to the result.
* 5. Append a space to separate the phonetic equivalents.
* 6. Return the result.
*
* @param text the text to convert
* @return the NATO phonetic alphabet
*/
public static String textToPhonetic(String text) { public static String textToPhonetic(String text) {
StringBuilder phonetic = new StringBuilder(); StringBuilder phonetic = new StringBuilder();
for (char c : text.toUpperCase().toCharArray()) { for (char c : text.toUpperCase().toCharArray()) {
if (Character.isWhitespace(c)) {
continue;
}
phonetic.append(PHONETIC_MAP.getOrDefault(c, String.valueOf(c))).append(" "); phonetic.append(PHONETIC_MAP.getOrDefault(c, String.valueOf(c))).append(" ");
} }
return phonetic.toString().trim(); return phonetic.toString().trim();

View File

@ -2,20 +2,26 @@ package com.thealgorithms.conversions;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
public class PhoneticAlphabetConverterTest { public class PhoneticAlphabetConverterTest {
@Test @ParameterizedTest
public void testTextToPhonetic() { @CsvSource({
assertEquals("Alpha Bravo", PhoneticAlphabetConverter.textToPhonetic("AB")); "'AB', 'Alpha Bravo'", "'ABC', 'Alpha Bravo Charlie'", "'A1B2C3', 'Alpha One Bravo Two Charlie Three'", "'Hello', 'Hotel Echo Lima Lima Oscar'", "'123', 'One Two Three'",
assertEquals("Alpha Bravo Charlie", PhoneticAlphabetConverter.textToPhonetic("ABC")); "'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 'Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine'",
assertEquals("Alpha One Bravo Two Charlie Three", PhoneticAlphabetConverter.textToPhonetic("A1B2C3")); "'abcdefghijklmnopqrstuvwxyz0123456789', 'Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine'",
assertEquals("Hotel Echo Lima Lima Oscar", PhoneticAlphabetConverter.textToPhonetic("Hello")); "'', ''", // Empty string case
assertEquals("One Two Three", PhoneticAlphabetConverter.textToPhonetic("123")); "'A B C', 'Alpha Bravo Charlie'", // String with spaces
assertEquals("Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine", "'A@B#C', 'Alpha @ Bravo # Charlie'", // Special characters
PhoneticAlphabetConverter.textToPhonetic("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")); "'A B C 123', 'Alpha Bravo Charlie One Two Three'", // Mixed letters, digits, and spaces
assertEquals("Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine", "'a b c', 'Alpha Bravo Charlie'", // Lowercase letters with spaces
PhoneticAlphabetConverter.textToPhonetic("abcdefghijklmnopqrstuvwxyz0123456789")); "'123!@#', 'One Two Three ! @ #'", // Numbers with special characters
"'HELLO WORLD', 'Hotel Echo Lima Lima Oscar Whiskey Oscar Romeo Lima Delta'" // Words with space
})
public void
testTextToPhonetic(String input, String expectedOutput) {
assertEquals(expectedOutput, PhoneticAlphabetConverter.textToPhonetic(input));
} }
} }