From 750130c2bcf3079844f6c7d99de61e2f3f9b9939 Mon Sep 17 00:00:00 2001 From: Rudy Huyn Date: Thu, 9 May 2019 10:10:17 -0700 Subject: [PATCH] Accept exponential numbers without -/+ sign. (#270) * add exponential without sign support * Add unit tests * fix formatting * remove extra spaces * modify unit tests --- src/CalcViewModel/Common/CopyPasteManager.cpp | 2 +- .../StandardCalculatorViewModel.cpp | 17 ++++++++++++++--- .../CopyPasteManagerTest.cpp | 15 ++++++++++----- .../StandardViewModelUnitTests.cpp | 11 ++++++++++- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/CalcViewModel/Common/CopyPasteManager.cpp b/src/CalcViewModel/Common/CopyPasteManager.cpp index ffe91d1b..db2439f7 100644 --- a/src/CalcViewModel/Common/CopyPasteManager.cpp +++ b/src/CalcViewModel/Common/CopyPasteManager.cpp @@ -40,7 +40,7 @@ static const wstring c_uIntSuffixes = L"[uU]?[lL]{0,2}"; static const array standardModePatterns = { wregex(c_wspc + c_signedDecFloat + c_wspc) }; static const array scientificModePatterns = { wregex(L"(" + c_wspc + L"[-+]?)|(" + c_wspcLParenSigned + L")" + c_signedDecFloat + c_wspcRParens), - wregex(L"(" + c_wspc + L"[-+]?)|(" + c_wspcLParenSigned + L")" + c_signedDecFloat + L"[e]([+]|[-])+\\d+" + c_wspcRParens) + wregex(L"(" + c_wspc + L"[-+]?)|(" + c_wspcLParenSigned + L")" + c_signedDecFloat + L"e[+-]?\\d+" + c_wspcRParens) }; static const array, 4> programmerModePatterns = { { // Hex numbers like 5F, 4A0C, 0xa9, 0xFFull, 47CDh diff --git a/src/CalcViewModel/StandardCalculatorViewModel.cpp b/src/CalcViewModel/StandardCalculatorViewModel.cpp index 6acda106..0dc9865b 100644 --- a/src/CalcViewModel/StandardCalculatorViewModel.cpp +++ b/src/CalcViewModel/StandardCalculatorViewModel.cpp @@ -881,14 +881,25 @@ void StandardCalculatorViewModel::OnPaste(String ^ pastedString, ViewMode mode) } } - // Handle exponent and exponent sign (...e+... or ...e-...) + // Handle exponent and exponent sign (...e+... or ...e-... or ...e...) if (mappedNumOp == NumbersAndOperatorsEnum::Exp) { - ++it; - if (!(MapCharacterToButtonId(*it, canSendNegate) == NumbersAndOperatorsEnum::Add)) + //Check the following item + switch (MapCharacterToButtonId(*(it + 1), canSendNegate)) + { + case NumbersAndOperatorsEnum::Subtract: { Command cmdNegate = ConvertToOperatorsEnum(NumbersAndOperatorsEnum::Negate); m_standardCalculatorManager.SendCommand(cmdNegate); + ++it; + } + break; + case NumbersAndOperatorsEnum::Add: + { + //Nothing to do, skip to the next item + ++it; + } + break; } } diff --git a/src/CalculatorUnitTests/CopyPasteManagerTest.cpp b/src/CalculatorUnitTests/CopyPasteManagerTest.cpp index b079f3cc..3ca56f63 100644 --- a/src/CalculatorUnitTests/CopyPasteManagerTest.cpp +++ b/src/CalculatorUnitTests/CopyPasteManagerTest.cpp @@ -187,8 +187,13 @@ namespace CalculatorUnitTests m_CopyPasteManager.ExpressionRegExMatch(vector{ L"123", L"1e23" }, ViewMode::Standard, CategoryGroupType::Calculator, -1, -1)); VERIFY_IS_TRUE( - m_CopyPasteManager.ExpressionRegExMatch(vector{ L"1.23e+456" }, ViewMode::Scientific, CategoryGroupType::Calculator, -1, -1), - L"Verify operand only needs to match one pattern."); + m_CopyPasteManager.ExpressionRegExMatch( + vector{ L"1.23e+456", L"1.23e456", L".23e+456", L"123e-456", L"12e2", L"12e+2", L"12e-2", L"-12e2", L"-12e+2", L"-12e-2" }, + ViewMode::Scientific, + CategoryGroupType::Calculator, + -1, + -1), + L"Verify exponents are accepted in scientific mode."); VERIFY_IS_FALSE( m_CopyPasteManager.ExpressionRegExMatch( @@ -647,9 +652,9 @@ namespace CalculatorUnitTests "+(41213)", "-(432+3232)", "-(+(-3213)+(-2312))", - "-(-(432+3232))" }; - String ^ negativeInput[] = { L"1.2e23" /*unsigned exponent*/, - L"abcdef", + "-(-(432+3232))", + L"1.2e23"/*unsigned exponent*/ }; + String ^ negativeInput[] = { L"abcdef", L"xyz", L"ABab", L"e+234", diff --git a/src/CalculatorUnitTests/StandardViewModelUnitTests.cpp b/src/CalculatorUnitTests/StandardViewModelUnitTests.cpp index 4c6135d0..78493153 100644 --- a/src/CalculatorUnitTests/StandardViewModelUnitTests.cpp +++ b/src/CalculatorUnitTests/StandardViewModelUnitTests.cpp @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "pch.h" @@ -419,6 +419,12 @@ namespace CalculatorUnitTests m_viewModel->OnPaste("1.23e+10", ViewMode::Scientific); ValidateViewModelValueAndExpression("1" + m_decimalSeparator + "23e+10", ""); + m_viewModel->OnPaste("1.23e10", ViewMode::Scientific); + ValidateViewModelValueAndExpression("1" + m_decimalSeparator + "23e+10", ""); + + m_viewModel->OnPaste("135e10", ViewMode::Scientific); + ValidateViewModelValueAndExpression("135" + m_decimalSeparator + "e+10", ""); + //// Negative exponent m_viewModel->OnPaste("1.23e-10", ViewMode::Scientific); ValidateViewModelValueAndExpression("1" + m_decimalSeparator + "23e-10", ""); @@ -426,6 +432,9 @@ namespace CalculatorUnitTests //// Uppercase E (for exponent) m_viewModel->OnPaste("1.23E-10", ViewMode::Scientific); ValidateViewModelValueAndExpression("1" + m_decimalSeparator + "23e-10", ""); + + m_viewModel->OnPaste("135E10", ViewMode::Scientific); + ValidateViewModelValueAndExpression("135" + m_decimalSeparator + "e+10", ""); } // Verify Calculator CalculationResultAutomationName is set correctly