mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-27 06:23:08 +08:00
Add PrefixToInfix.java
new algorithm (#5552)
* Add `PrefixToInfix.java` new algorithm * Update directory * Fix clang error * Update directory * Fix comment * Add suggested changes --------- Co-authored-by: Hardvan <Hardvan@users.noreply.github.com> Co-authored-by: Alex Klymenko <alexanderklmn@gmail.com>
This commit is contained in:
@ -0,0 +1,44 @@
|
||||
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 PrefixToInfixTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideValidPrefixToInfixTestCases")
|
||||
void testValidPrefixToInfixConversion(String prefix, String expectedInfix) {
|
||||
assertEquals(expectedInfix, PrefixToInfix.getPrefixToInfix(prefix));
|
||||
}
|
||||
|
||||
static Stream<Arguments> provideValidPrefixToInfixTestCases() {
|
||||
return Stream.of(Arguments.of("A", "A"), // Single operand
|
||||
Arguments.of("+AB", "(A+B)"), // Addition
|
||||
Arguments.of("*+ABC", "((A+B)*C)"), // Addition and multiplication
|
||||
Arguments.of("-+A*BCD", "((A+(B*C))-D)"), // Mixed operators
|
||||
Arguments.of("/-A*BC+DE", "((A-(B*C))/(D+E))"), // Mixed operators
|
||||
Arguments.of("^+AB*CD", "((A+B)^(C*D))") // Mixed operators
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEmptyPrefixExpression() {
|
||||
assertEquals("", PrefixToInfix.getPrefixToInfix(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNullPrefixExpression() {
|
||||
assertThrows(NullPointerException.class, () -> PrefixToInfix.getPrefixToInfix(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMalformedPrefixExpression() {
|
||||
assertThrows(ArithmeticException.class, () -> PrefixToInfix.getPrefixToInfix("+ABC"));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user