refactor: PostfixToInfix (#5409)

* refactor: PostfixToInfix

* checkstyle: fix formatting

* refactor: add support for one character

---------

Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
Alex Klymenko
2024-08-27 15:03:26 +02:00
committed by GitHub
parent fc5a70edc9
commit 633b9d4112
2 changed files with 74 additions and 83 deletions

View File

@ -0,0 +1,33 @@
package com.thealgorithms.stacks;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
class PostfixToInfixTest {
@ParameterizedTest
@MethodSource("provideValidPostfixToInfixTestCases")
void testValidPostfixToInfixConversion(String postfix, String expectedInfix) {
assertEquals(expectedInfix, PostfixToInfix.getPostfixToInfix(postfix));
}
static Stream<Arguments> provideValidPostfixToInfixTestCases() {
return Stream.of(Arguments.of("A", "A"), Arguments.of("ABC+/", "(A/(B+C))"), Arguments.of("AB+CD+*", "((A+B)*(C+D))"), Arguments.of("AB+C+D+", "(((A+B)+C)+D)"), Arguments.of("ABCDE^*/-", "(A-(B/(C*(D^E))))"), Arguments.of("AB+CD^/E*FGH+-^", "((((A+B)/(C^D))*E)^(F-(G+H)))"));
}
@Test
void testEmptyPostfixExpression() {
assertEquals("", PostfixToInfix.getPostfixToInfix(""));
}
@Test
void testNullPostfixExpression() {
assertThrows(NullPointerException.class, () -> PostfixToInfix.getPostfixToInfix(null));
}
}