public void ConstNodeTest(int value) { var node = new ConstNode(value); var calculatedValue = node.CalculateValue(); calculatedValue.Should().Be(value); }
public void MultiplicateOperationNodeTest(int left, int right) { var leftNode = new ConstNode(left); var rightNode = new ConstNode(right); var node = new OperationNode(leftNode, rightNode, (a, b) => a * b); var calculatedValue = node.CalculateValue(); calculatedValue.Should().Be(left * right); }
public void DivideOperationNodeTest(int left, int right) { var leftNode = new ConstNode(left); var rightNode = new ConstNode(right); var node = new OperationNode(leftNode, rightNode, (a, b) => a / b); var calculatedValue = node.CalculateValue(); calculatedValue.Should().Be((double)left / right); }
public void LeftNestedExpressionTest(int a, int b, int c) { var aNode = new ConstNode(a); var bNode = new ConstNode(b); var cNode = new ConstNode(c); var leftNode = new OperationNode(aNode, bNode, (x, y) => x + y); var node = new OperationNode(leftNode, cNode, (x, y) => x*y); var calculatedValue = node.CalculateValue(); calculatedValue.Should().Be((a + b)*c); }
private IExpressionNode BuildRightNode(IList<string> expressionList, ref int index) { var leftValue = int.Parse(expressionList[index]); var leftNode = new ConstNode(leftValue); var operationSymbol = expressionList.Count > index + 1 ? expressionList[index + 1] : null; if (operationSymbol == null || operationSymbol == "+" || operationSymbol == "-") { index++; return leftNode; } var operation = _operationSymbol2Function[operationSymbol]; index += 2; var rightNode = BuildRightNode(expressionList, ref index); return new OperationNode(leftNode, rightNode, operation); }
public IExpressionNode BuildExpression(IList<string> expressionList) { var leftValue = int.Parse(expressionList[0]); IExpressionNode leftNode = new ConstNode(leftValue); var operationSymbol = expressionList.Count > 1 ? expressionList[1] : null; var index = 2; while (operationSymbol != null) { var rightNode = BuildRightNode(expressionList, ref index); var operation = _operationSymbol2Function[operationSymbol]; leftNode = new OperationNode(leftNode, rightNode, operation); operationSymbol = expressionList.Count > index ? expressionList[index] : null; index++; } return leftNode; }
public void RightLeftNestedExpressionTest(int a, int b, int c, int d) { var aNode = new ConstNode(a); var bNode = new ConstNode(b); var cNode = new ConstNode(c); var dNode = new ConstNode(d); var leftNode = new OperationNode(aNode, bNode, (x, y) => x + y); var rightNode = new OperationNode(cNode, dNode, (x, y) => x - y); var node = new OperationNode(leftNode, rightNode, (x, y) => x * y); var calculatedValue = node.CalculateValue(); calculatedValue.Should().Be((a + b) * (c - d)); }
public void SubstractOperationNodeTest(int left, int right) { var leftNode = new ConstNode(left); var rightNode = new ConstNode(right); var node = new OperationNode(leftNode, rightNode, (a, b) => a - b); var calculatedValue = node.CalculateValue(); calculatedValue.Should().Be(left - right); }