|
1 | 1 | package com.thealgorithms.stacks;
|
2 | 2 |
|
3 |
| -import static java.util.Map.entry; |
4 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
5 | 4 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
6 | 5 |
|
7 |
| -import java.util.Map; |
8 |
| -import org.junit.jupiter.api.Test; |
| 6 | +import java.util.stream.Stream; |
| 7 | +import org.junit.jupiter.params.ParameterizedTest; |
| 8 | +import org.junit.jupiter.params.provider.Arguments; |
| 9 | +import org.junit.jupiter.params.provider.MethodSource; |
9 | 10 |
|
10 | 11 | public class StackPostfixNotationTest {
|
11 |
| - @Test |
12 |
| - public void testEvaluate() { |
13 |
| - final Map<String, Integer> testCases = Map.ofEntries(entry("1 1 +", 2), entry("2 3 *", 6), entry("6 2 /", 3), entry("-5 -2 -", -3), entry("5 2 + 3 *", 21), entry("-5", -5)); |
14 |
| - for (final var tc : testCases.entrySet()) { |
15 |
| - assertEquals(tc.getValue(), StackPostfixNotation.postfixEvaluate(tc.getKey())); |
16 |
| - } |
17 |
| - } |
18 |
| - |
19 |
| - @Test |
20 |
| - public void testIfEvaluateThrowsExceptionForEmptyInput() { |
21 |
| - assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("")); |
22 |
| - } |
23 | 12 |
|
24 |
| - @Test |
25 |
| - public void testIfEvaluateThrowsExceptionForInproperInput() { |
26 |
| - assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 3")); |
| 13 | + @ParameterizedTest |
| 14 | + @MethodSource("provideValidTestCases") |
| 15 | + void testEvaluate(String expression, int expected) { |
| 16 | + assertEquals(expected, StackPostfixNotation.postfixEvaluate(expression)); |
27 | 17 | }
|
28 | 18 |
|
29 |
| - @Test |
30 |
| - public void testIfEvaluateThrowsExceptionForInputWithUnknownOperation() { |
31 |
| - assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 !")); |
| 19 | + static Stream<Arguments> provideValidTestCases() { |
| 20 | + return Stream.of(Arguments.of("1 1 +", 2), Arguments.of("2 3 *", 6), Arguments.of("6 2 /", 3), Arguments.of("-5 -2 -", -3), Arguments.of("5 2 + 3 *", 21), Arguments.of("-5", -5)); |
32 | 21 | }
|
33 | 22 |
|
34 |
| - @Test |
35 |
| - public void testIfEvaluateThrowsExceptionForInputWithTooFewArgsA() { |
36 |
| - assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("+")); |
| 23 | + @ParameterizedTest |
| 24 | + @MethodSource("provideInvalidTestCases") |
| 25 | + void testEvaluateThrowsException(String expression) { |
| 26 | + assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate(expression)); |
37 | 27 | }
|
38 | 28 |
|
39 |
| - @Test |
40 |
| - public void testIfEvaluateThrowsExceptionForInputWithTooFewArgsB() { |
41 |
| - assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("2 +")); |
| 29 | + static Stream<Arguments> provideInvalidTestCases() { |
| 30 | + return Stream.of(Arguments.of(""), Arguments.of("3 3 3"), Arguments.of("3 3 !"), Arguments.of("+"), Arguments.of("2 +")); |
42 | 31 | }
|
43 | 32 | }
|
0 commit comments