This commit is contained in:
Jordan Sinclair 2025-05-04 20:59:38 -05:00
commit d61327fb04
3 changed files with 99 additions and 3 deletions

View file

@ -163,7 +163,15 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam)
{ {
m_bRecord = true; m_bRecord = true;
m_input.Clear(); m_input.Clear();
CheckAndAddLastBinOpToHistory();
/*
* Account for scenarios where an equation includes any input after closing parenthesis - i.e. "(8)2=16".
* This prevents the calculator from ending an equation and adding to history prematurely.
*/
if (m_nLastCom != IDC_CLOSEP)
{
CheckAndAddLastBinOpToHistory();
}
} }
// Interpret digit keys. // Interpret digit keys.
@ -185,6 +193,38 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam)
return; return;
} }
// Check if the last command was a closing parenthesis
if (m_nLastCom == IDC_CLOSEP)
{
// Treat this as an implicit multiplication
m_nOpCode = IDC_MUL;
m_lastVal = m_currentVal;
// We need to clear any previous state from last calculation
m_holdVal = Rational(0);
m_bNoPrevEqu = true;
// Add the operand to history before adding the implicit multiplication
if (!m_HistoryCollector.FOpndAddedToHistory())
{
m_HistoryCollector.AddOpenBraceToHistory();
m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal);
m_HistoryCollector.AddCloseBraceToHistory();
}
// Add the implicit multiplication to history
m_HistoryCollector.AddBinOpToHistory(m_nOpCode, m_fIntegerMode);
m_bChangeOp = true;
m_nPrevOpCode = 0;
// Clear any pending operations in the precedence stack
while (m_precedenceOpCount > 0)
{
m_precedenceOpCount--;
m_nPrecOp[m_precedenceOpCount] = 0;
}
}
DisplayNum(); DisplayNum();
return; return;
@ -504,11 +544,13 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam)
{ {
wstring groupedString = GroupDigitsPerRadix(m_numberString, m_radix); wstring groupedString = GroupDigitsPerRadix(m_numberString, m_radix);
m_HistoryCollector.CompleteEquation(groupedString); m_HistoryCollector.CompleteEquation(groupedString);
m_lastVal = m_currentVal;
m_nPrevOpCode = 0;
m_precedenceOpCount = 0;
} }
m_bChangeOp = false; m_bChangeOp = false;
m_nPrevOpCode = 0;
break; break;
case IDC_OPENP: case IDC_OPENP:

View file

@ -4,6 +4,7 @@
using CalculatorUITestFramework; using CalculatorUITestFramework;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace CalculatorUITests namespace CalculatorUITests
{ {
@ -133,6 +134,47 @@ namespace CalculatorUITests
Assert.AreEqual("12", page.CalculatorResults.GetCalculatorResultText()); Assert.AreEqual("12", page.CalculatorResults.GetCalculatorResultText());
} }
[TestMethod]
[Priority(0)]
public void SmokeTest_CloseParenthesis()
{
/*
* TEST #1
*/
page.ScientificOperators.ParenthesisLeftButton.Click();
page.StandardOperators.NumberPad.Input(8);
page.ScientificOperators.ParenthesisRightButton.Click();
page.StandardOperators.NumberPad.Input(2);
page.StandardOperators.EqualButton.Click();
// Assert calculator & history results
Assert.AreEqual("16", page.CalculatorResults.GetCalculatorResultText());
Assert.AreEqual("(8) \x00D7 2=", page.CalculatorResults.GetCalculatorExpressionText());
var historyItems0 = page.HistoryPanel.GetAllHistoryListViewItems();
Assert.IsTrue(historyItems0[0].GetValue().Equals("16", StringComparison.InvariantCultureIgnoreCase));
Assert.IsTrue(historyItems0[0].GetExpression().Equals("(8) \x00D7 2=", StringComparison.InvariantCultureIgnoreCase));
/*
* TEST #2
*/
page.ScientificOperators.ParenthesisLeftButton.Click();
page.StandardOperators.NumberPad.Input(7);
page.StandardOperators.MultiplyButton.Click();
page.StandardOperators.NumberPad.Input(2);
page.ScientificOperators.ParenthesisRightButton.Click();
page.StandardOperators.NumberPad.Input(2);
page.StandardOperators.EqualButton.Click();
// Assert calculator & history results
Assert.AreEqual("28", page.CalculatorResults.GetCalculatorResultText());
Assert.AreEqual("(7 \x00D7 2) \x00D7 2=", page.CalculatorResults.GetCalculatorExpressionText());
var historyItems1 = page.HistoryPanel.GetAllHistoryListViewItems();
Assert.IsTrue(historyItems1[0].GetValue().Equals("28", StringComparison.InvariantCultureIgnoreCase));
Assert.IsTrue(historyItems1[0].GetExpression().Equals("(7 \x00D7 2) \x00D7 2=", StringComparison.InvariantCultureIgnoreCase));
}
[TestMethod] [TestMethod]
[Priority(0)] [Priority(0)]
public void SmokeTest_RadianAngleOperator() public void SmokeTest_RadianAngleOperator()

View file

@ -643,6 +643,18 @@ namespace CalculatorManagerTest
Command commands5[] = { Command::Command2, Command::CommandOPENP, Command::Command2, Command::CommandCLOSEP, Command commands5[] = { Command::Command2, Command::CommandOPENP, Command::Command2, Command::CommandCLOSEP,
Command::CommandADD, Command::CommandEQU, Command::CommandNULL }; Command::CommandADD, Command::CommandEQU, Command::CommandNULL };
TestDriver::Test(L"8", L"2 \x00D7 (2) + 4=", commands5, true, true); TestDriver::Test(L"8", L"2 \x00D7 (2) + 4=", commands5, true, true);
Command commands6[] = { Command::CommandOPENP, Command::Command8, Command::CommandCLOSEP, Command::Command2, Command::CommandEQU, Command::CommandNULL };
TestDriver::Test(L"16", L"(8) \x00D7 2=", commands6, true, true);
Command commands7[] = { Command::CommandOPENP, Command::Command7, Command::CommandMUL, Command::Command2,
Command::CommandCLOSEP, Command::Command2, Command::CommandEQU, Command::CommandNULL };
TestDriver::Test(L"28", L"(7 \x00D7 2) \x00D7 2=", commands7, true, true);
Command commands8[] = { Command::CommandOPENP, Command::Command7, Command::CommandMUL, Command::Command2,
Command::CommandCLOSEP, Command::Command2, Command::CommandEQU,
Command::CommandOPENP, Command::Command1, Command::Command4, Command::CommandCLOSEP, Command::Command2, Command::CommandEQU, Command::CommandNULL};
TestDriver::Test(L"28", L"(14) \x00D7 2=", commands8, true, true);
} }
void CalculatorManagerTest::CalculatorManagerTestScientificError() void CalculatorManagerTest::CalculatorManagerTestScientificError()