From ecaf8cd04852ea14ca10a1941a46a10e09efbbef Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Tue, 25 Jul 2023 18:12:49 +0000 Subject: [PATCH 1/6] tests: add `StackPostfixNotationTest` --- .../others/StackPostfixNotationTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java diff --git a/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java b/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java new file mode 100644 index 000000000000..b1a2fa397e2f --- /dev/null +++ b/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java @@ -0,0 +1,17 @@ +package com.thealgorithms.others; + +import static java.util.Map.entry; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Map; +import org.junit.jupiter.api.Test; + +public class StackPostfixNotationTest { + @Test + public void testGetWithNewObject() { + final Map testCases = Map.ofEntries(entry("1 1 +", 2), entry("2 3 *", 6), entry("6 2 /", 3), entry("5 2 + 3 *", 21)); + for (final var tc : testCases.entrySet()) { + assertEquals(tc.getValue(), StackPostfixNotation.postfixEvaluate(tc.getKey())); + } + } +} From 326b6feceefb33f01b821e37346c53d8f424cd8b Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Tue, 25 Jul 2023 18:42:36 +0000 Subject: [PATCH 2/6] tests: add testcase with single integer --- .../com/thealgorithms/others/StackPostfixNotationTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java b/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java index b1a2fa397e2f..af78c8c79fd7 100644 --- a/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java +++ b/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java @@ -8,8 +8,8 @@ public class StackPostfixNotationTest { @Test - public void testGetWithNewObject() { - final Map testCases = Map.ofEntries(entry("1 1 +", 2), entry("2 3 *", 6), entry("6 2 /", 3), entry("5 2 + 3 *", 21)); + public void testEvaluate() { + final Map testCases = Map.ofEntries(entry("1 1 +", 2), entry("2 3 *", 6), entry("6 2 /", 3), entry("5 2 + 3 *", 21), entry("-5", -5)); for (final var tc : testCases.entrySet()) { assertEquals(tc.getValue(), StackPostfixNotation.postfixEvaluate(tc.getKey())); } From b47bbf22cf33429a4a3b370da10a9a42b0f59b0e Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Tue, 25 Jul 2023 18:52:17 +0000 Subject: [PATCH 3/6] fix: handle empty input --- .../java/com/thealgorithms/others/StackPostfixNotation.java | 3 +++ .../com/thealgorithms/others/StackPostfixNotationTest.java | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/src/main/java/com/thealgorithms/others/StackPostfixNotation.java b/src/main/java/com/thealgorithms/others/StackPostfixNotation.java index 08b0f9f40273..0b7ea2156b8a 100644 --- a/src/main/java/com/thealgorithms/others/StackPostfixNotation.java +++ b/src/main/java/com/thealgorithms/others/StackPostfixNotation.java @@ -34,6 +34,9 @@ public static int postfixEvaluate(String exp) { } } tokens.close(); + if (s.size() != 1) { + throw new IllegalArgumentException("exp is not a proper postfix expression."); + } return s.pop(); } } diff --git a/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java b/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java index af78c8c79fd7..eb5f28320394 100644 --- a/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java +++ b/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java @@ -2,6 +2,7 @@ import static java.util.Map.entry; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.Map; import org.junit.jupiter.api.Test; @@ -14,4 +15,9 @@ public void testEvaluate() { assertEquals(tc.getValue(), StackPostfixNotation.postfixEvaluate(tc.getKey())); } } + + @Test + public void testIfEvaluateThrowsExceptionForEmptyInput() { + assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("")); + } } From 0a4b2993daafb12d4511d491008944ae60c64d14 Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Tue, 25 Jul 2023 18:59:57 +0000 Subject: [PATCH 4/6] fix: hanle more incorrect inputs --- .../com/thealgorithms/others/StackPostfixNotation.java | 3 ++- .../thealgorithms/others/StackPostfixNotationTest.java | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/StackPostfixNotation.java b/src/main/java/com/thealgorithms/others/StackPostfixNotation.java index 0b7ea2156b8a..3c185789faa4 100644 --- a/src/main/java/com/thealgorithms/others/StackPostfixNotation.java +++ b/src/main/java/com/thealgorithms/others/StackPostfixNotation.java @@ -28,7 +28,8 @@ public static int postfixEvaluate(String exp) { case "+" -> s.push(num1 + num2); case "-" -> s.push(num1 - num2); case "*" -> s.push(num1 * num2); - default -> s.push(num1 / num2); + case "/" -> s.push(num1 / num2); + default -> throw new IllegalArgumentException("exp contains an unknown operation."); } // "+", "-", "*", "/" } diff --git a/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java b/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java index eb5f28320394..5cb3706fd509 100644 --- a/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java +++ b/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java @@ -20,4 +20,14 @@ public void testEvaluate() { public void testIfEvaluateThrowsExceptionForEmptyInput() { assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("")); } + + @Test + public void testIfEvaluateThrowsExceptionForInproperInput() { + assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 3")); + } + + @Test + public void testIfEvaluateThrowsExceptionForInputWithUnknownOperation() { + assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 !")); + } } From 27d8267e8e5f1e942fb244b6938a15178aa2a070 Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Tue, 25 Jul 2023 19:03:49 +0000 Subject: [PATCH 5/6] refactor: make `StackPostfixNotation` a proper utility class --- .../thealgorithms/others/StackPostfixNotation.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/StackPostfixNotation.java b/src/main/java/com/thealgorithms/others/StackPostfixNotation.java index 3c185789faa4..c6d395cb04d5 100644 --- a/src/main/java/com/thealgorithms/others/StackPostfixNotation.java +++ b/src/main/java/com/thealgorithms/others/StackPostfixNotation.java @@ -1,18 +1,14 @@ package com.thealgorithms.others; -import java.util.*; +import java.util.Scanner; +import java.util.Stack; -public class StackPostfixNotation { - - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - String post = scanner.nextLine(); // Takes input with spaces in between eg. "1 21 +" - System.out.println(postfixEvaluate(post)); - scanner.close(); +public final class StackPostfixNotation { + private StackPostfixNotation() { } // Evaluates the given postfix expression string and returns the result. - public static int postfixEvaluate(String exp) { + public static int postfixEvaluate(final String exp) { Stack s = new Stack(); Scanner tokens = new Scanner(exp); From 98b1d085fdefb6f087f3e52c866bf46de2eb578d Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Tue, 25 Jul 2023 19:13:54 +0000 Subject: [PATCH 6/6] tests: add test case with `-` --- .../java/com/thealgorithms/others/StackPostfixNotationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java b/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java index 5cb3706fd509..4894b403ea06 100644 --- a/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java +++ b/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java @@ -10,7 +10,7 @@ public class StackPostfixNotationTest { @Test public void testEvaluate() { - final Map testCases = Map.ofEntries(entry("1 1 +", 2), entry("2 3 *", 6), entry("6 2 /", 3), entry("5 2 + 3 *", 21), entry("-5", -5)); + final Map 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)); for (final var tc : testCases.entrySet()) { assertEquals(tc.getValue(), StackPostfixNotation.postfixEvaluate(tc.getKey())); }