public void NumericParseBeginningPartialIndexTest() { var numeric = PolishNotation.ParseNumeric(" 123 ", 2); Assert.AreEqual(123, numeric.Numeric); Assert.AreEqual(7, numeric.NextIndex); }
public void NumericParseFromIndex0Test() { var numeric = PolishNotation.ParseNumeric("123", 0); Assert.AreEqual(123, numeric.Numeric); Assert.AreEqual(3, numeric.NextIndex); }
public void NumericParseWithTrailingSpaceTest() { var numeric = PolishNotation.ParseNumeric("123 ", 0); Assert.AreEqual(123, numeric.Numeric); Assert.AreEqual(6, numeric.NextIndex); }
public void OperatorAddParseWithTrailingSpaceTest() { var oper = PolishNotation.ParseOperator("+ ", 0); Assert.AreEqual('+', oper.Operator); Assert.AreEqual(4, oper.NextIndex); }
public void OperatorAddParseBeginningPartialIndexTest() { var oper = PolishNotation.ParseOperator(" + ", 2); Assert.AreEqual('+', oper.Operator); Assert.AreEqual(5, oper.NextIndex); }
public void OperatorAddParseFromIndex0Test() { var oper = PolishNotation.ParseOperator("+", 0); Assert.AreEqual('+', oper.Operator); Assert.AreEqual(1, oper.NextIndex); }
public void MinNumericParseTest() { var valueString = int.MinValue.ToString(); var numeric = PolishNotation.ParseNumeric(valueString, 0); Assert.AreEqual(int.MinValue, numeric.Numeric); Assert.AreEqual(valueString.Length, numeric.NextIndex); }
public void ReducePattern3Test() { var expr = PolishNotation.ParseExpression("* + 2 3 4", 0); var result = expr.Reduce(); Assert.AreEqual(20, result); }
public void ZeroNumericParseTest() { var valueString = "0"; var numeric = PolishNotation.ParseNumeric(valueString, 0); Assert.AreEqual(0, numeric.Numeric); Assert.AreEqual(valueString.Length, numeric.NextIndex); }
public void ReducePattern1Test() { var expr = PolishNotation.ParseExpression("+ - 123 456 * 789 111 ", 0); var result = expr.Reduce(); Assert.AreEqual(-333 + 87579, result); }
public void ExpressionParseSparseTest() { var expr = PolishNotation.ParseExpression("+ 123 456 ", 0); Assert.AreEqual('+', expr.Operator.Operator); Assert.AreEqual(123, ((NumericNode)expr.Left).Numeric); Assert.AreEqual(456, ((NumericNode)expr.Right).Numeric); Assert.AreEqual(15, expr.NextIndex); }
public void ExpressionParseWithRightNestedTest() { var expr = PolishNotation.ParseExpression("+ 123 - 456 789 ", 0); Assert.AreEqual('+', expr.Operator.Operator); Assert.AreEqual(123, ((NumericNode)expr.Left).Numeric); var right = (ExpressionNode)expr.Right; Assert.AreEqual('-', right.Operator.Operator); Assert.AreEqual(456, ((NumericNode)right.Left).Numeric); Assert.AreEqual(789, ((NumericNode)right.Right).Numeric); Assert.AreEqual(18, expr.NextIndex); }
public void ExpressionParseWithBothNestedTest() { var expr = PolishNotation.ParseExpression("+ - 123 456 * 789 111 ", 0); Assert.AreEqual('+', expr.Operator.Operator); var left = (ExpressionNode)expr.Left; Assert.AreEqual('-', left.Operator.Operator); Assert.AreEqual(123, ((NumericNode)left.Left).Numeric); Assert.AreEqual(456, ((NumericNode)left.Right).Numeric); var right = (ExpressionNode)expr.Right; Assert.AreEqual('*', right.Operator.Operator); Assert.AreEqual(789, ((NumericNode)right.Left).Numeric); Assert.AreEqual(111, ((NumericNode)right.Right).Numeric); Assert.AreEqual(23, expr.NextIndex); }
// what happens when clicking button = (while evaluating Polish Notation) private void evaluatePolishNotation_Click(object sender, EventArgs e) { if (hasFinished) { _screen = string.Empty; // clear the screen if it's finished _result = "0"; hasFinished = false; return; } try { { string expression = _screen.Replace(" ", string.Empty) .Replace(operatorAddButton.Text.Trim(), "+") .Replace(operatorSubtractButton.Text.Trim(), "-") .Replace(operatorMultiplyButton.Text.Trim(), "*") .Replace(operatorDivideButton.Text.Trim(), "/"); if (expression == string.Empty) { throw new Exception("Empty."); } int temp; if (!int.TryParse(expression[0].ToString(), out temp)) { if (!expression.StartsWith("(")) { expression = "0" + expression; } } if (!int.TryParse(expression[expression.Length - 1].ToString(), out temp)) { if (!expression.EndsWith(")")) { expression = expression + "0"; } } _screen = expression; } _result = PolishNotation.Evaluate(_screen); } catch (Exception exception) { _result = exception.Message; } finally { hasFinished = true; } }
public void SkipWhiteSpaceTest() { var index = PolishNotation.SkipWhiteSpace(" ", 2); Assert.AreEqual(5, index); }
public void SkipWhiteSpaceFromIndex0Test() { var index = PolishNotation.SkipWhiteSpace(" ", 0); Assert.AreEqual(3, index); }