public double PowerReal(PowerExpression node, List <object> parameters) { double leftOperand = _interpreter.DispatchReal(node.Children[0], parameters); double rightOperand = _interpreter.DispatchReal(node.Children[1], parameters); return(Math.Pow(leftOperand, rightOperand)); }
private PowerExpression GetPowerExpression(TypeEnum left, TypeEnum right) { ExpressionNode leftNode = GetLiteral(left); ExpressionNode rightNode = GetLiteral(right); var node = new PowerExpression(leftNode, rightNode, 0, 0); return(node); }
public int PowerInteger(PowerExpression node, List <Object> parameters) { int leftOperand = _interpreter.DispatchInt(node.Children[0], parameters); int rightOperand = _interpreter.DispatchInt(node.Children[1], parameters); if (rightOperand < 0) { throw new NegativeExponentException(node); } return(Pow(leftOperand, rightOperand)); }
public void PowerInteger_TwoIntsButNegativeExponent_ThrowsException(int input1, int input2) { IntegerLiteralExpression realLit1 = new IntegerLiteralExpression(input1, 1, 1); IntegerLiteralExpression realLit2 = new IntegerLiteralExpression(input2, 2, 2); PowerExpression powExpr = new PowerExpression(realLit1, realLit2, 1, 1); IInterpreterInteger parent = Substitute.For <IInterpreterInteger>(); parent.DispatchInt(realLit1, Arg.Any <List <object> >()).Returns(input1); parent.DispatchInt(realLit2, Arg.Any <List <object> >()).Returns(input2); IntegerHelper realHelper = SetUpHelper(parent); int res = realHelper.PowerInteger(powExpr, new List <object>()); }
public void PowerReal_TwoReals_ReturnsCorrectResult(double input1, double input2, double expected) { RealLiteralExpression realLit1 = new RealLiteralExpression(input1, 1, 1); RealLiteralExpression realLit2 = new RealLiteralExpression(input2, 2, 2); PowerExpression powExpr = new PowerExpression(realLit1, realLit2, 1, 1); IInterpreterReal parent = Substitute.For <IInterpreterReal>(); parent.DispatchReal(realLit1, Arg.Any <List <object> >()).Returns(input1); parent.DispatchReal(realLit2, Arg.Any <List <object> >()).Returns(input2); RealHelper realHelper = SetUpHelper(parent); double res = realHelper.PowerReal(powExpr, new List <object>()); Assert.AreEqual(expected, res); }
public void PowerInteger_TwoInts_ReturnsCorrectResult(int input1, int input2, int expected) { IntegerLiteralExpression realLit1 = new IntegerLiteralExpression(input1, 1, 1); IntegerLiteralExpression realLit2 = new IntegerLiteralExpression(input2, 2, 2); PowerExpression powExpr = new PowerExpression(realLit1, realLit2, 1, 1); IInterpreterInteger parent = Substitute.For <IInterpreterInteger>(); parent.DispatchInt(realLit1, Arg.Any <List <object> >()).Returns(input1); parent.DispatchInt(realLit2, Arg.Any <List <object> >()).Returns(input2); IntegerHelper realHelper = SetUpHelper(parent); int res = realHelper.PowerInteger(powExpr, new List <object>()); Assert.AreEqual(expected, res); }
public void DispatchReal_PowerAndObjectList_CorrectValueReturned() { double expected = 17; PowerExpression input1 = new PowerExpression(null, null, 0, 0); List <Object> input2 = new List <Object>() { 23, 2.334, null }; IRealHelper rhelper = Substitute.For <IRealHelper>(); Interpreter interpreter = Utilities.GetIntepreterOnlyWith(rhelper); rhelper.PowerReal(Arg.Any <PowerExpression>(), Arg.Any <List <Object> >()).Returns(expected); double res = interpreter.DispatchReal(input1, input2); Assert.AreEqual(expected, res); }
public void DispatchReal_PowerAndObjectList_CorrectListPassed() { List <Object> expected = new List <Object>() { 23, 2.334, null }; PowerExpression input1 = new PowerExpression(null, null, 0, 0); IRealHelper rhelper = Substitute.For <IRealHelper>(); Interpreter interpreter = Utilities.GetIntepreterOnlyWith(rhelper); List <Object> res = null; rhelper.PowerReal(Arg.Any <PowerExpression>(), Arg.Do <List <Object> >(x => res = x)); interpreter.DispatchReal(input1, expected); res.Should().BeEquivalentTo(expected); }
public void Dispatch_INonIdentifierAndStringList_CorrectIdentifierExpPassed() { PowerExpression expected = new PowerExpression(null, null, 1, 1); PowerExpression input1 = expected; List <string> input2 = new List <string>() { "id" }; IReferenceHelper helper = Substitute.For <IReferenceHelper>(); ReferenceHandler refHandler = new ReferenceHandler(helper); INonIdentifierExpression res = null; helper.VisitNonIdentifier(Arg.Do <INonIdentifierExpression>(x => res = x), Arg.Any <List <string> >()); refHandler.Dispatch(input1, input2); res.Should().BeEquivalentTo(expected); }
private static RawExpression Exp1(char **input) { RawExpression e = Exp0(input); while (true) { if (Char(input, '^')) { e = new PowerExpression { Left = e, Right = Exp0(input), }; } else { break; } } return(e); }
void IExpressionVisitor.Visit(PowerExpression expression) { expression.LeftChildExpression.AcceptInternal(this); expression.RightChildExpression.AcceptInternal(this); generator.Call(powerMethod); }