Skip to content

Commit

Permalink
Corrected bug with commas in input
Browse files Browse the repository at this point in the history
- Remove all commas before parsing starts
- Added unit test to check for a number entry with commas
- Added test for a number to the power of zero (should be 1)
- Added tests for import and export with scientific numbers on stack
  • Loading branch information
frossm committed May 31, 2023
1 parent c7debf0 commit 761c52b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.fross</groupId>
<artifactId>rpncalc</artifactId>
<version>5.0.8</version>
<version>5.0.9</version>
<packaging>jar</packaging>

<name>rpncalc</name>
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/fross/rpncalc/CommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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();

Expand Down
20 changes: 12 additions & 8 deletions src/test/java/org/fross/rpncalc/CommandParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/fross/rpncalc/MathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}

/**
Expand Down
20 changes: 12 additions & 8 deletions src/test/java/org/fross/rpncalc/StackOperationsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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();
Expand Down Expand Up @@ -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");
}
Expand All @@ -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
Expand Down

0 comments on commit 761c52b

Please sign in to comment.