diff --git a/pom.xml b/pom.xml index c276e8a..d9caf3f 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.fross rpncalc - 5.0.8 + 5.0.9 jar rpncalc diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index c96e392..202258f 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -1,5 +1,5 @@ name: rpncalc -version: '5.0.8' +version: '5.0.9' summary: The command line Reverse Polish Notation (RPN) calculator description: | RPNCalc is an easy to use command line based Reverse Polish diff --git a/src/main/java/org/fross/rpncalc/CommandParser.java b/src/main/java/org/fross/rpncalc/CommandParser.java index af6f89d..4bd3fc7 100644 --- a/src/main/java/org/fross/rpncalc/CommandParser.java +++ b/src/main/java/org/fross/rpncalc/CommandParser.java @@ -44,6 +44,9 @@ public class CommandParser { * @param cmdInputParam */ public static void Parse(StackObj calcStack, StackObj calcStack2, String cmdInput, String cmdInputCmd, String cmdInputParam) { + // Remove any commas in the command to allow for "1,234.00" + cmdInputCmd = cmdInputCmd.replaceAll(",", ""); + // Massive switch statement to process user input and call the correct functions switch (cmdInputCmd) { @@ -464,15 +467,15 @@ public static void Parse(StackObj calcStack, StackObj calcStack2, String cmdInpu } // Number entered, add to stack. - } else if (cmdInputCmd.replaceAll(" ", "").matches("^-?\\d*\\.?\\d*")) { + } else if (cmdInputCmd.matches("^-?\\d*\\.?\\d*")) { // Save current calcStack to the undoStack calcStack.saveUndo(); Output.debugPrint("Placing the number '" + cmdInputCmd + "' onto the stack"); - calcStack.push(new BigDecimal(cmdInputCmd.replaceAll(" ", ""))); + calcStack.push(new BigDecimal(cmdInputCmd)); // Handle numbers with a single operand at the end (a NumOp) - } else if (cmdInputCmd.replaceAll(" ", "").matches("^-?\\d*\\.?\\d*[Ee]?\\d*[\\*\\+\\-\\/\\^]")) { + } else if (cmdInputCmd.matches("^-?\\d*\\.?\\d*[Ee]?\\d*[\\*\\+\\-\\/\\^]")) { // Save current calcStack to the undoStack calcStack.saveUndo(); diff --git a/src/test/java/org/fross/rpncalc/CommandParserTest.java b/src/test/java/org/fross/rpncalc/CommandParserTest.java index c20f815..9caa0c1 100644 --- a/src/test/java/org/fross/rpncalc/CommandParserTest.java +++ b/src/test/java/org/fross/rpncalc/CommandParserTest.java @@ -61,18 +61,22 @@ void testNormalNumberEntry() { CommandParser.Parse(stk1, stk2, "123.321", "123.321", ""); assertEquals(123.321, stk1.peek().doubleValue()); assertEquals(3, stk1.size()); - + CommandParser.Parse(stk1, stk2, ".234", ".234", ""); assertEquals(.234, stk1.peek().doubleValue()); assertEquals(4, stk1.size()); - + CommandParser.Parse(stk1, stk2, "123.321e12", "123.321e12", ""); assertEquals("123.321E+12", stk1.peek().toEngineeringString()); assertEquals(5, stk1.size()); - + CommandParser.Parse(stk1, stk2, "-.1E44", "-.1E44", ""); assertEquals("-10E+42", stk1.peek().toEngineeringString()); assertEquals(6, stk1.size()); + + CommandParser.Parse(stk1, stk2, "123,456,321.321", "123,456,321.321", ""); + assertEquals("123456321.321", stk1.peek().toString()); + assertEquals(7, stk1.size()); } // Test fractional inputs @@ -161,27 +165,27 @@ void testNumOp() { assertEquals(1, stk1.size()); StackCommands.cmdRound(stk1, "4"); assertEquals("-1.0000", stk1.peek().toString()); - + CommandParser.Parse(stk1, stk2, "3.3E14*", "3.3E14*", ""); assertEquals(1, stk1.size()); StackCommands.cmdRound(stk1, "4"); assertEquals("-330000000000000.0000", stk1.peek().toString()); - + CommandParser.Parse(stk1, stk2, "2/", "2/", ""); assertEquals(1, stk1.size()); StackCommands.cmdRound(stk1, "4"); assertEquals("-165000000000000.0000", stk1.peek().toString()); - + CommandParser.Parse(stk1, stk2, "2.34e12/", "2.234e12/", ""); assertEquals(1, stk1.size()); StackCommands.cmdRound(stk1, "9"); assertEquals("-70.512820513", stk1.peek().toString()); - + CommandParser.Parse(stk1, stk2, ".02e2^", ".02e2^", ""); assertEquals(1, stk1.size()); StackCommands.cmdRound(stk1, "8"); assertEquals("4972.05785670", stk1.peek().toString()); - + } // Test the entry of a scientific notation number diff --git a/src/test/java/org/fross/rpncalc/MathTest.java b/src/test/java/org/fross/rpncalc/MathTest.java index 39ab19b..baa1cec 100644 --- a/src/test/java/org/fross/rpncalc/MathTest.java +++ b/src/test/java/org/fross/rpncalc/MathTest.java @@ -220,6 +220,13 @@ void testPower() { assertEquals("1.5241383936E+24", stk.peek().toEngineeringString()); assertEquals(1, stk.size()); + // Test #4 + stk.clear(); + stk.push(14345); + stk.push(0e2); + Math.Parse("^", stk); + assertEquals(BigDecimal.ONE, stk.peek()); + } /** diff --git a/src/test/java/org/fross/rpncalc/StackOperationsTest.java b/src/test/java/org/fross/rpncalc/StackOperationsTest.java index bb21228..17a6f75 100644 --- a/src/test/java/org/fross/rpncalc/StackOperationsTest.java +++ b/src/test/java/org/fross/rpncalc/StackOperationsTest.java @@ -34,6 +34,7 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.math.BigDecimal; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; @@ -181,18 +182,19 @@ void testLoadCommand() { * Test stack output to file */ @Test - void textExport() { + void testExport() { String testFileName = "target/rpncalc.export"; File testFile = new File(testFileName); - Double[] testValues = { -1.0123, 2.0234, 3.0345, -15.0456, 2.0567, 17.0678, 38.0789, 53.0891, 14.0123, 73.0234, 72.0345, 72.0456, 10.0567, 83.0678, - -60.0789, 76.0890, 59.090, 30.0234, -42.0345, 89.0456, 30.0567, 44.0678, -31.0789 }; + String[] testValues = { "-1.0123", "2.0234", "3.0345", "-15.0456", "-3.123e17", "2.123E8", "2.0567", "17.0678", "38.0789", "53.0891", "14.0123", + "73.0234", "72.0345", "72.0456", "10.0567", "83.0678", "-60.0789", "76.0890", "59.090", "30.0234", "-42.0345", "89.0456", "4.56e19", "30.0567", + "44.0678", "-31.0789" }; // Build the StackObject StackObj stk = new StackObj(); for (int i = 0; i < testValues.length; i++) { stk.push(testValues[i]); } - assertEquals(23, stk.size()); + assertEquals(26, stk.size()); // Delete the testFile if it exists try { @@ -216,7 +218,7 @@ void textExport() { // Test that read-from-file value = test value for (int i = 0; i < testValues.length; i++) { - assertEquals(testValues[i].toString(), linesRead.get(i)); + assertTrue(new BigDecimal(testValues[i]).compareTo(new BigDecimal(linesRead.get(i))) == 0); } } else { throw new IOException(); @@ -247,13 +249,15 @@ void textExport() { @Test void testImport() { String testFileName = "target/rpncalc.import"; - Double[] testValues = { 2.34, 6.78, -2.11, 0.0, 12.12345, 4.44, -54.223, 100.001, 11.23 }; + String[] testValues = { "-1.0123", "2.0234", "3.0345", "-15.0456", "-3.123e17", "2.123E8", "2.0567", "17.0678", "38.0789", "53.0891", "14.0123", + "73.0234", "72.0345", "72.0456", "10.0567", "83.0678", "-60.0789", "76.0890", "59.090", "30.0234", "-42.0345", "89.0456", "4.56e19", "30.0567", + "44.0678", "-31.0789" }; // Create a test file try { FileWriter fw = new FileWriter(new File(testFileName.toLowerCase())); - // Loop through the test values and compare + // Loop through the test values write them to disk for (int i = 0; i < testValues.length; i++) { fw.write(testValues[i] + "\n"); } @@ -271,7 +275,7 @@ void testImport() { // Verify the import values match the file data for (int i = 0; i < testValues.length; i++) { - assertEquals(testValues[i], stk.get(i).doubleValue()); + assertTrue(new BigDecimal(testValues[i]).compareTo(stk.get(i)) == 0); } // Delete the test import file