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");
}
/**
* 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) {
StringBuilder phonetic = new StringBuilder();
for (char c : text.toUpperCase().toCharArray()) {
if (Character.isWhitespace(c)) {
continue;
}
phonetic.append(PHONETIC_MAP.getOrDefault(c, String.valueOf(c))).append(" ");
}
return phonetic.toString().trim();

View File

@ -2,20 +2,26 @@ package com.thealgorithms.conversions;
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 {
@Test
public void testTextToPhonetic() {
assertEquals("Alpha Bravo", PhoneticAlphabetConverter.textToPhonetic("AB"));
assertEquals("Alpha Bravo Charlie", PhoneticAlphabetConverter.textToPhonetic("ABC"));
assertEquals("Alpha One Bravo Two Charlie Three", PhoneticAlphabetConverter.textToPhonetic("A1B2C3"));
assertEquals("Hotel Echo Lima Lima Oscar", PhoneticAlphabetConverter.textToPhonetic("Hello"));
assertEquals("One Two Three", PhoneticAlphabetConverter.textToPhonetic("123"));
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",
PhoneticAlphabetConverter.textToPhonetic("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"));
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",
PhoneticAlphabetConverter.textToPhonetic("abcdefghijklmnopqrstuvwxyz0123456789"));
@ParameterizedTest
@CsvSource({
"'AB', 'Alpha Bravo'", "'ABC', 'Alpha Bravo Charlie'", "'A1B2C3', 'Alpha One Bravo Two Charlie Three'", "'Hello', 'Hotel Echo Lima Lima Oscar'", "'123', 'One Two Three'",
"'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'",
"'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'",
"'', ''", // Empty string case
"'A B C', 'Alpha Bravo Charlie'", // String with spaces
"'A@B#C', 'Alpha @ Bravo # Charlie'", // Special characters
"'A B C 123', 'Alpha Bravo Charlie One Two Three'", // Mixed letters, digits, and spaces
"'a b c', 'Alpha Bravo Charlie'", // Lowercase letters with spaces
"'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));
}
}