public void PropertyRuleDefaultCanXYZVaules() { var rule = new LessThan(RuleBaseClassesRoot.StartDateProperty, RuleBaseClassesRoot.EndDateProperty); Assert.IsFalse(rule.IsAsync); Assert.IsTrue(rule.CanRunOnServer); Assert.IsTrue(rule.CanRunInCheckRules); Assert.IsTrue(rule.CanRunAsAffectedProperty); }
public void TestLessThan() { var lt = new LessThan(new Constant("23"), new Constant("12")); Assert.IsFalse((bool) lt.Evaluate(new Reflection(this))); lt = new LessThan(new Constant("12"), new Constant("23")); Assert.IsTrue((bool) lt.Evaluate(new Reflection(this))); lt = new LessThan(new Constant("12"), new Constant("12")); Assert.IsFalse((bool) lt.Evaluate(new Reflection(this))); }
private static IStatement CreateIfStatement() { var condition = new LessThan(new Variable<int>("x"), new Number(0)); var consequence = new Assign<int>(new Variable<int>("a"), new Number(1)); var alternative = new Assign<int>(new Variable<int>("b"), new Number(1)); var ifStatement = new If(condition, consequence, alternative); return ifStatement; }
public void ThenResolveReturnsTrue() { var rightHandSide = new Mock<IValue>(); rightHandSide.Setup(x => x.GreaterThan("larger")).Returns(false); var subject = new LessThan("Value", rightHandSide.Object); var result = subject.Resolve(new TestObject { Value = "larger" }, new List<IExpression>()); Assert.That(result, Is.False); }
private static IStatement CreateWhileStatement() { // while (x < 5) { x = x + x } var condition = new LessThan(new Variable<int>("x"), new Number(5)); var body = new Assign<int>( new Variable<int>("x"), new Add(new Variable<int>("x"), new Variable<int>("x"))); var whileStatement = new While(condition, body); return whileStatement; }
public LessThan_Should() { LessThan = new LessThan(); }
private void Initialize() { var add = new Addition(); var sub = new Subtraction(); var mul = new Multiplication(); var div = new Division(); var mean = new Average(); var sin = new Sine(); var cos = new Cosine(); var tan = new Tangent(); var log = new Logarithm(); var exp = new Exponential(); var @if = new IfThenElse(); var gt = new GreaterThan(); var lt = new LessThan(); var and = new And(); var or = new Or(); var not = new Not(); var constant = new Constant(); constant.MinValue = -20; constant.MaxValue = 20; variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable(); var allSymbols = new List <Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp, @if, gt, lt, and, or, not, constant, variableSymbol }; var unaryFunctionSymbols = new List <Symbol>() { sin, cos, tan, log, exp, not }; var binaryFunctionSymbols = new List <Symbol>() { gt, lt }; var functionSymbols = new List <Symbol>() { add, sub, mul, div, mean, and, or }; foreach (var symb in allSymbols) { AddSymbol(symb); } foreach (var funSymb in functionSymbols) { SetSubtreeCount(funSymb, 1, 3); } foreach (var funSymb in unaryFunctionSymbols) { SetSubtreeCount(funSymb, 1, 1); } foreach (var funSymb in binaryFunctionSymbols) { SetSubtreeCount(funSymb, 2, 2); } SetSubtreeCount(@if, 3, 3); SetSubtreeCount(constant, 0, 0); SetSubtreeCount(variableSymbol, 0, 0); // allow each symbol as child of the start symbol foreach (var symb in allSymbols) { AddAllowedChildSymbol(StartSymbol, symb, 0); } // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0) foreach (var parent in allSymbols) { for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) { foreach (var child in allSymbols) { AddAllowedChildSymbol(parent, child, i); } } } }
public int CompareTo(FormattingRule other) { return(LessThan.CompareTo(other.LessThan)); }
public void LessThanCorrectlyConvertsToJson() { var lt = new LessThan <int>(10); lt.ToString().ShouldEqual(LessThanJson); }
public void TestLessThanException() { var exp = new LessThan(new Bool(true), new Bool(false)); TestException(exp); }
public void TestLessThanNumber() { var exp = new LessThan(new Number(10), new Number(10)); Test(exp, ResultType.Boolean); }
public void Visit(LessThan lt) { lt.Left.Accept(this); _sb.Append(" < "); lt.Right.Accept(this); }
public IEnumerable <Id> Visit(LessThan node) { return(VisitBinary(node)); }
protected override bool Visit(LessThan eq) => base.Visit(eq.Left) || base.Visit(eq.Right);
public double Execute(Operation operation, IFunctionRegistry functionRegistry, IConstantRegistry constantRegistry, IDictionary <string, double> variables) { if (operation == null) { throw new ArgumentNullException("operation"); } if (operation.GetType() == typeof(IntegerConstant)) { IntegerConstant constant = (IntegerConstant)operation; return(constant.Value); } else if (operation.GetType() == typeof(FloatingPointConstant)) { FloatingPointConstant constant = (FloatingPointConstant)operation; return(constant.Value); } else if (operation.GetType() == typeof(Variable)) { Variable variable = (Variable)operation; double value; bool variableFound = variables.TryGetValue(variable.Name, out value); if (variableFound) { return(value); } else { throw new VariableNotDefinedException(string.Format("The variable \"{0}\" used is not defined.", variable.Name)); } } else if (operation.GetType() == typeof(Multiplication)) { Multiplication multiplication = (Multiplication)operation; return(Execute(multiplication.Argument1, functionRegistry, constantRegistry, variables) * Execute(multiplication.Argument2, functionRegistry, constantRegistry, variables)); } else if (operation.GetType() == typeof(Addition)) { Addition addition = (Addition)operation; return(Execute(addition.Argument1, functionRegistry, constantRegistry, variables) + Execute(addition.Argument2, functionRegistry, constantRegistry, variables)); } else if (operation.GetType() == typeof(Subtraction)) { Subtraction addition = (Subtraction)operation; return(Execute(addition.Argument1, functionRegistry, constantRegistry, variables) - Execute(addition.Argument2, functionRegistry, constantRegistry, variables)); } else if (operation.GetType() == typeof(Division)) { Division division = (Division)operation; return(Execute(division.Dividend, functionRegistry, constantRegistry, variables) / Execute(division.Divisor, functionRegistry, constantRegistry, variables)); } else if (operation.GetType() == typeof(Modulo)) { Modulo division = (Modulo)operation; return(Execute(division.Dividend, functionRegistry, constantRegistry, variables) % Execute(division.Divisor, functionRegistry, constantRegistry, variables)); } else if (operation.GetType() == typeof(Exponentiation)) { Exponentiation exponentiation = (Exponentiation)operation; return(Math.Pow(Execute(exponentiation.Base, functionRegistry, constantRegistry, variables), Execute(exponentiation.Exponent, functionRegistry, constantRegistry, variables))); } else if (operation.GetType() == typeof(UnaryMinus)) { UnaryMinus unaryMinus = (UnaryMinus)operation; return(-Execute(unaryMinus.Argument, functionRegistry, constantRegistry, variables)); } else if (operation.GetType() == typeof(And)) { And and = (And)operation; var operation1 = Execute(and.Argument1, functionRegistry, constantRegistry, variables) != 0; var operation2 = Execute(and.Argument2, functionRegistry, constantRegistry, variables) != 0; return((operation1 && operation2) ? 1.0 : 0.0); } else if (operation.GetType() == typeof(Or)) { Or or = (Or)operation; var operation1 = Execute(or.Argument1, functionRegistry, constantRegistry, variables) != 0; var operation2 = Execute(or.Argument2, functionRegistry, constantRegistry, variables) != 0; return((operation1 || operation2) ? 1.0 : 0.0); } else if (operation.GetType() == typeof(LessThan)) { LessThan lessThan = (LessThan)operation; return((Execute(lessThan.Argument1, functionRegistry, constantRegistry, variables) < Execute(lessThan.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0); } else if (operation.GetType() == typeof(LessOrEqualThan)) { LessOrEqualThan lessOrEqualThan = (LessOrEqualThan)operation; return((Execute(lessOrEqualThan.Argument1, functionRegistry, constantRegistry, variables) <= Execute(lessOrEqualThan.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0); } else if (operation.GetType() == typeof(GreaterThan)) { GreaterThan greaterThan = (GreaterThan)operation; return((Execute(greaterThan.Argument1, functionRegistry, constantRegistry, variables) > Execute(greaterThan.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0); } else if (operation.GetType() == typeof(GreaterOrEqualThan)) { GreaterOrEqualThan greaterOrEqualThan = (GreaterOrEqualThan)operation; return((Execute(greaterOrEqualThan.Argument1, functionRegistry, constantRegistry, variables) >= Execute(greaterOrEqualThan.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0); } else if (operation.GetType() == typeof(Equal)) { Equal equal = (Equal)operation; return((Execute(equal.Argument1, functionRegistry, constantRegistry, variables) == Execute(equal.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0); } else if (operation.GetType() == typeof(NotEqual)) { NotEqual notEqual = (NotEqual)operation; return((Execute(notEqual.Argument1, functionRegistry, constantRegistry, variables) != Execute(notEqual.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0); } else if (operation.GetType() == typeof(Function)) { Function function = (Function)operation; FunctionInfo functionInfo = functionRegistry.GetFunctionInfo(function.FunctionName); double[] arguments = new double[functionInfo.IsDynamicFunc ? function.Arguments.Count : functionInfo.NumberOfParameters]; for (int i = 0; i < arguments.Length; i++) { arguments[i] = Execute(function.Arguments[i], functionRegistry, constantRegistry, variables); } return(Invoke(functionInfo.Function, arguments)); } else { throw new ArgumentException(string.Format("Unsupported operation \"{0}\".", operation.GetType().FullName), "operation"); } }
public void When_string_containment_equivalent_of_less_than_twice_is_asserted_and_actual_value_is_null_then_it_should_not_throw() { // Arrange string actual = null; string expectedSubstring = "XyZ"; // Act Action act = () => actual.Should().ContainEquivalentOf(expectedSubstring, LessThan.Twice()); // Assert act.Should().NotThrow(); }
public void When_string_containment_equivalent_of_less_than_is_asserted_and_actual_value_contains_the_expected_string_but_not_less_than_expected_times_it_should_throw() { // Arrange string actual = "abCDEBcDF"; string expectedSubstring = "Bcd"; // Act Action act = () => actual.Should().ContainEquivalentOf(expectedSubstring, LessThan.Times(2)); // Assert act.Should().Throw <XunitException>() .WithMessage("Expected * \"abCDEBcDF\" to contain equivalent of \"Bcd\" less than 2 times, but found it 2 times."); }
public void Visit(LessThan lt) { Visit(lt, OpCodes.Clt); }
//----------------------------------------------------------- public Type Visit(LessThan node) { VisitBinaryNumericOperator(".lt.", node); return(Type.LOGICAL); }
public void When_string_containment_equivalent_of_less_than_is_asserted_and_actual_value_contains_the_expected_string_less_than_expected_times_it_should_not_throw() { // Arrange string actual = "abCDEBcDF"; string expectedSubstring = "Bcd"; // Act Action act = () => actual.Should().ContainEquivalentOf(expectedSubstring, LessThan.Times(3)); // Assert act.Should().NotThrow(); }
private Expression GenerateMethodBody(Operation operation, ParameterExpression contextParameter, IFunctionRegistry functionRegistry, IDictionary <string, double> variables) { if (operation == null) { throw new ArgumentNullException("operation"); } if (operation.GetType() == typeof(IntegerConstant)) { IntegerConstant constant = (IntegerConstant)operation; double value = constant.Value; return(Expression.Constant(value, typeof(double))); } else if (operation.GetType() == typeof(FloatingPointConstant)) { FloatingPointConstant constant = (FloatingPointConstant)operation; return(Expression.Constant(constant.Value, typeof(double))); } else if (operation.GetType() == typeof(Variable)) { Variable variable = (Variable)operation; Func <string, FormulaContext, double> getVariableValueOrThrow = PrecompiledMethods.GetVariableValueOrThrow; return(Expression.Call(null, getVariableValueOrThrow.Method, Expression.Constant(variable.Name), contextParameter)); } else if (operation.GetType() == typeof(Multiplication)) { Multiplication multiplication = (Multiplication)operation; Expression argument1 = GenerateMethodBody(multiplication.Argument1, contextParameter, functionRegistry, variables); Expression argument2 = GenerateMethodBody(multiplication.Argument2, contextParameter, functionRegistry, variables); return(Expression.Multiply(argument1, argument2)); } else if (operation.GetType() == typeof(Addition)) { Addition addition = (Addition)operation; Expression argument1 = GenerateMethodBody(addition.Argument1, contextParameter, functionRegistry, variables); Expression argument2 = GenerateMethodBody(addition.Argument2, contextParameter, functionRegistry, variables); return(Expression.Add(argument1, argument2)); } else if (operation.GetType() == typeof(Subtraction)) { Subtraction addition = (Subtraction)operation; Expression argument1 = GenerateMethodBody(addition.Argument1, contextParameter, functionRegistry, variables); Expression argument2 = GenerateMethodBody(addition.Argument2, contextParameter, functionRegistry, variables); return(Expression.Subtract(argument1, argument2)); } else if (operation.GetType() == typeof(Division)) { Division division = (Division)operation; Expression dividend = GenerateMethodBody(division.Dividend, contextParameter, functionRegistry, variables); Expression divisor = GenerateMethodBody(division.Divisor, contextParameter, functionRegistry, variables); return(Expression.Divide(dividend, divisor)); } else if (operation.GetType() == typeof(Modulo)) { Modulo modulo = (Modulo)operation; Expression dividend = GenerateMethodBody(modulo.Dividend, contextParameter, functionRegistry, variables); Expression divisor = GenerateMethodBody(modulo.Divisor, contextParameter, functionRegistry, variables); return(Expression.Modulo(dividend, divisor)); } else if (operation.GetType() == typeof(Exponentiation)) { Exponentiation exponentation = (Exponentiation)operation; Expression @base = GenerateMethodBody(exponentation.Base, contextParameter, functionRegistry, variables); Expression exponent = GenerateMethodBody(exponentation.Exponent, contextParameter, functionRegistry, variables); return(Expression.Call(null, typeof(Math).GetMethod("Pow", new Type[] { typeof(double), typeof(double) }), @base, exponent)); } else if (operation.GetType() == typeof(UnaryMinus)) { UnaryMinus unaryMinus = (UnaryMinus)operation; Expression argument = GenerateMethodBody(unaryMinus.Argument, contextParameter, functionRegistry, variables); return(Expression.Negate(argument)); } else if (operation.GetType() == typeof(And)) { And and = (And)operation; Expression argument1 = Expression.NotEqual(GenerateMethodBody(and.Argument1, contextParameter, functionRegistry, variables), Expression.Constant(0.0)); Expression argument2 = Expression.NotEqual(GenerateMethodBody(and.Argument2, contextParameter, functionRegistry, variables), Expression.Constant(0.0)); return(Expression.Condition(Expression.And(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(Or)) { Or and = (Or)operation; Expression argument1 = Expression.NotEqual(GenerateMethodBody(and.Argument1, contextParameter, functionRegistry, variables), Expression.Constant(0.0)); Expression argument2 = Expression.NotEqual(GenerateMethodBody(and.Argument2, contextParameter, functionRegistry, variables), Expression.Constant(0.0)); return(Expression.Condition(Expression.Or(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(LessThan)) { LessThan lessThan = (LessThan)operation; Expression argument1 = GenerateMethodBody(lessThan.Argument1, contextParameter, functionRegistry, variables); Expression argument2 = GenerateMethodBody(lessThan.Argument2, contextParameter, functionRegistry, variables); return(Expression.Condition(Expression.LessThan(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(LessOrEqualThan)) { LessOrEqualThan lessOrEqualThan = (LessOrEqualThan)operation; Expression argument1 = GenerateMethodBody(lessOrEqualThan.Argument1, contextParameter, functionRegistry, variables); Expression argument2 = GenerateMethodBody(lessOrEqualThan.Argument2, contextParameter, functionRegistry, variables); return(Expression.Condition(Expression.LessThanOrEqual(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(GreaterThan)) { GreaterThan greaterThan = (GreaterThan)operation; Expression argument1 = GenerateMethodBody(greaterThan.Argument1, contextParameter, functionRegistry, variables); Expression argument2 = GenerateMethodBody(greaterThan.Argument2, contextParameter, functionRegistry, variables); return(Expression.Condition(Expression.GreaterThan(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(GreaterOrEqualThan)) { GreaterOrEqualThan greaterOrEqualThan = (GreaterOrEqualThan)operation; Expression argument1 = GenerateMethodBody(greaterOrEqualThan.Argument1, contextParameter, functionRegistry, variables); Expression argument2 = GenerateMethodBody(greaterOrEqualThan.Argument2, contextParameter, functionRegistry, variables); return(Expression.Condition(Expression.GreaterThanOrEqual(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(Equal)) { Equal equal = (Equal)operation; Expression argument1 = GenerateMethodBody(equal.Argument1, contextParameter, functionRegistry, variables); Expression argument2 = GenerateMethodBody(equal.Argument2, contextParameter, functionRegistry, variables); return(Expression.Condition(Expression.Equal(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(NotEqual)) { NotEqual notEqual = (NotEqual)operation; Expression argument1 = GenerateMethodBody(notEqual.Argument1, contextParameter, functionRegistry, variables); Expression argument2 = GenerateMethodBody(notEqual.Argument2, contextParameter, functionRegistry, variables); return(Expression.Condition(Expression.NotEqual(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(Function)) { Function function = (Function)operation; FunctionInfo functionInfo = functionRegistry.GetFunctionInfo(function.FunctionName); Type funcType; Type[] parameterTypes; Expression[] arguments; if (function.FunctionName == "isvardefined") { if (function.Arguments.Count != 1) { throw new ArgumentException("Invalid number of arguments."); } if (function.Arguments[0].GetType() == typeof(Variable)) { Variable variable = (Variable)function.Arguments[0]; double value; bool variableFound = variables.TryGetValue(variable.Name, out value); IntegerConstant newArgument = new IntegerConstant(variableFound ? 1 : 0); function.Arguments[0] = newArgument; } else if (function.Arguments[0].GetType() == typeof(IntegerConstant)) { IntegerConstant oldArgument = (IntegerConstant)function.Arguments[0]; IntegerConstant newArgument = new IntegerConstant(oldArgument.Value != 0 ? 1 : 0); function.Arguments[0] = newArgument; } else if (function.Arguments[0].GetType() == typeof(FloatingPointConstant)) { FloatingPointConstant oldArgument = (FloatingPointConstant)function.Arguments[0]; FloatingPointConstant newArgument = new FloatingPointConstant(oldArgument.Value != 0 ? 1 : 0); function.Arguments[0] = newArgument; } else { throw new ArgumentException("Invalid argument for isVarDefined."); } } if (functionInfo.IsDynamicFunc) { funcType = typeof(DynamicFunc <double, double>); parameterTypes = new Type[] { typeof(double[]) }; Expression[] arrayArguments = new Expression[function.Arguments.Count]; for (int i = 0; i < function.Arguments.Count; i++) { arrayArguments[i] = GenerateMethodBody(function.Arguments[i], contextParameter, functionRegistry, variables); } arguments = new Expression[1]; arguments[0] = NewArrayExpression.NewArrayInit(typeof(double), arrayArguments); } else { funcType = GetFuncType(functionInfo.NumberOfParameters); parameterTypes = (from i in Enumerable.Range(0, functionInfo.NumberOfParameters) select typeof(double)).ToArray(); arguments = new Expression[functionInfo.NumberOfParameters]; for (int i = 0; i < functionInfo.NumberOfParameters; i++) { arguments[i] = GenerateMethodBody(function.Arguments[i], contextParameter, functionRegistry, variables); } } Expression getFunctionRegistry = Expression.Property(contextParameter, "FunctionRegistry"); ParameterExpression functionInfoVariable = Expression.Variable(typeof(FunctionInfo)); Expression funcInstance; if (!functionInfo.IsOverWritable) { funcInstance = Expression.Convert( Expression.Property( Expression.Call( getFunctionRegistry, typeof(IFunctionRegistry).GetMethod("GetFunctionInfo", new Type[] { typeof(string) }), Expression.Constant(function.FunctionName)), "Function"), funcType); } else { funcInstance = Expression.Constant(functionInfo.Function, funcType); } return(Expression.Call( funcInstance, funcType.GetMethod("Invoke", parameterTypes), arguments)); } else { throw new ArgumentException(string.Format("Unsupported operation \"{0}\".", operation.GetType().FullName), "operation"); } }
protected override IDataFlowGraphExpressionNode Visit(LessThan eq) => VisitBinary(eq, YololBinaryOp.LessThan);
private Expression ParseRelationalExpression(TokenSet followers) { TokenSet followerOrRelational = followers|Parser.RelationalOperators; Expression result = this.ParseAdditiveExpression(followerOrRelational); while (Parser.RelationalOperators[this.currentToken]) { SourceLocationBuilder slb = new SourceLocationBuilder(result.SourceLocation); Token operatorToken = this.currentToken; this.GetNextToken(); Expression operand2 = this.ParseAdditiveExpression(followerOrRelational); slb.UpdateToSpan(operand2.SourceLocation); switch (operatorToken){ case Token.Equals: result = new Equality(result, operand2, slb); break; case Token.GreaterThan: result = new GreaterThan(result, operand2, slb); break; case Token.GreaterThanEqualTo: result = new GreaterThanOrEqual(result, operand2, slb); break; case Token.LessThan: result = new LessThan(result, operand2, slb); break; case Token.LessThanEqualTo: result = new LessThanOrEqual(result, operand2, slb); break; case Token.NotEqualTo: result = new NotEquality(result, operand2, slb); break; } } //^ assume followers[this.currentToken] || this.currentToken == Token.EndOfFile; return result; }
public void TestLessThanBoolNumberException() { var exp = new LessThan(new Bool(true), new Number(10)); TestBinaryException(exp); }
public virtual T Visit(LessThan node) { return(Visit((ComparisonBinaryExpression)node)); }
public virtual T Visit(LessThan lessThan) { return(VisitBinaryExpression(lessThan)); }
private static Activity HandleBinaryExpression <TLeft, TRight, TResult>(BinaryOperator op, TestExpression left, TestExpression right) { Activity we = null; InArgument <TLeft> leftArgument = (InArgument <TLeft>)TestExpression.GetInArgumentFromExpectedNode <TLeft>(left); leftArgument.EvaluationOrder = 0; InArgument <TRight> rightArgument = (InArgument <TRight>)TestExpression.GetInArgumentFromExpectedNode <TRight>(right); rightArgument.EvaluationOrder = 1; switch (op) { case BinaryOperator.Add: we = new Add <TLeft, TRight, TResult>() { Checked = false, Left = leftArgument, Right = rightArgument }; break; case BinaryOperator.And: we = new And <TLeft, TRight, TResult>() { Left = leftArgument, Right = rightArgument }; break; case BinaryOperator.AndAlso: we = new AndAlso() { Left = TestExpression.GetWorkflowElementFromExpectedNode <bool>(left), Right = TestExpression.GetWorkflowElementFromExpectedNode <bool>(right) }; break; case BinaryOperator.CheckedAdd: we = new Add <TLeft, TRight, TResult>() { Checked = true, Left = leftArgument, Right = rightArgument }; break; case BinaryOperator.CheckedMultiply: we = new Multiply <TLeft, TRight, TResult>() { Checked = true, Left = leftArgument, Right = rightArgument }; break; case BinaryOperator.CheckedSubtract: we = new Subtract <TLeft, TRight, TResult>() { Checked = true, Left = leftArgument, Right = rightArgument }; break; case BinaryOperator.Divide: we = new Divide <TLeft, TRight, TResult>() { Left = leftArgument, Right = rightArgument }; break; case BinaryOperator.Equal: we = new Equal <TLeft, TRight, TResult>() { Left = leftArgument, Right = rightArgument }; break; case BinaryOperator.GreaterThan: we = new GreaterThan <TLeft, TRight, TResult>() { Left = leftArgument, Right = rightArgument }; break; case BinaryOperator.GreaterThanOrEqual: we = new GreaterThanOrEqual <TLeft, TRight, TResult>() { Left = leftArgument, Right = rightArgument }; break; case BinaryOperator.LessThan: we = new LessThan <TLeft, TRight, TResult>() { Left = leftArgument, Right = rightArgument }; break; case BinaryOperator.LessThanOrEqual: we = new LessThanOrEqual <TLeft, TRight, TResult>() { Left = leftArgument, Right = rightArgument }; break; case BinaryOperator.Or: we = new Or <TLeft, TRight, TResult>() { Left = leftArgument, Right = rightArgument }; break; case BinaryOperator.Multiply: we = new Multiply <TLeft, TRight, TResult>() { Checked = false, Left = leftArgument, Right = rightArgument }; break; case BinaryOperator.NotEqual: we = new NotEqual <TLeft, TRight, TResult>() { Left = leftArgument, Right = rightArgument }; break; case BinaryOperator.OrElse: we = new OrElse() { Left = TestExpression.GetWorkflowElementFromExpectedNode <bool>(left), Right = TestExpression.GetWorkflowElementFromExpectedNode <bool>(right) }; break; case BinaryOperator.Subtract: we = new Subtract <TLeft, TRight, TResult>() { Checked = false, Left = leftArgument, Right = rightArgument }; break; default: throw new NotSupportedException(string.Format("Operator: {0} is unsupported", op.ToString())); } return(we); }
public void LessToString() { var exp = new LessThan(new Number(5), new Number(5)); Assert.Equal("5 < 5", exp.ToString(commonFormatter)); }
protected override IEnumerable <BaseStatement> Visit(LessThan eq) => Binary(eq, (a, b) => new LessThan(a, b));
protected override bool Visit(LessThan eq) => VisitBinary(eq);
public override object Visit(LessThan lessThan) { ValidateBinaryExpression(lessThan); return(base.Visit(lessThan)); }
private Expression GenerateMethodBody(Operation operation, ParameterExpression contextParameter, IFunctionRegistry functionRegistry) { if (operation == null) { throw new ArgumentNullException("operation"); } if (operation.GetType() == typeof(IntegerConstant)) { IntegerConstant constant = (IntegerConstant)operation; return(Expression.Convert(Expression.Constant(constant.Value, typeof(int)), typeof(double))); } else if (operation.GetType() == typeof(FloatingPointConstant)) { FloatingPointConstant constant = (FloatingPointConstant)operation; return(Expression.Constant(constant.Value, typeof(double))); } else if (operation.GetType() == typeof(Variable)) { Type contextType = typeof(FormulaContext); Type dictionaryType = typeof(IDictionary <string, double>); Variable variable = (Variable)operation; Expression getVariables = Expression.Property(contextParameter, "Variables"); ParameterExpression value = Expression.Variable(typeof(double), "value"); Expression variableFound = Expression.Call(getVariables, dictionaryType.GetRuntimeMethod("TryGetValue", new Type[] { typeof(string), typeof(double).MakeByRefType() }), Expression.Constant(variable.Name), value); Expression throwException = Expression.Throw( Expression.New(typeof(VariableNotDefinedException).GetConstructor(new Type[] { typeof(string) }), Expression.Constant(string.Format("The variable \"{0}\" used is not defined.", variable.Name)))); LabelTarget returnLabel = Expression.Label(typeof(double)); return(Expression.Block( new[] { value }, Expression.IfThenElse( variableFound, Expression.Return(returnLabel, value), throwException ), Expression.Label(returnLabel, Expression.Constant(0.0)) )); } else if (operation.GetType() == typeof(Multiplication)) { Multiplication multiplication = (Multiplication)operation; Expression argument1 = GenerateMethodBody(multiplication.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(multiplication.Argument2, contextParameter, functionRegistry); return(Expression.Multiply(argument1, argument2)); } else if (operation.GetType() == typeof(Addition)) { Addition addition = (Addition)operation; Expression argument1 = GenerateMethodBody(addition.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(addition.Argument2, contextParameter, functionRegistry); return(Expression.Add(argument1, argument2)); } else if (operation.GetType() == typeof(Subtraction)) { Subtraction addition = (Subtraction)operation; Expression argument1 = GenerateMethodBody(addition.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(addition.Argument2, contextParameter, functionRegistry); return(Expression.Subtract(argument1, argument2)); } else if (operation.GetType() == typeof(Division)) { Division division = (Division)operation; Expression dividend = GenerateMethodBody(division.Dividend, contextParameter, functionRegistry); Expression divisor = GenerateMethodBody(division.Divisor, contextParameter, functionRegistry); return(Expression.Divide(dividend, divisor)); } else if (operation.GetType() == typeof(Modulo)) { Modulo modulo = (Modulo)operation; Expression dividend = GenerateMethodBody(modulo.Dividend, contextParameter, functionRegistry); Expression divisor = GenerateMethodBody(modulo.Divisor, contextParameter, functionRegistry); return(Expression.Modulo(dividend, divisor)); } else if (operation.GetType() == typeof(Exponentiation)) { Exponentiation exponentation = (Exponentiation)operation; Expression @base = GenerateMethodBody(exponentation.Base, contextParameter, functionRegistry); Expression exponent = GenerateMethodBody(exponentation.Exponent, contextParameter, functionRegistry); return(Expression.Call(null, typeof(Math).GetRuntimeMethod("Pow", new Type[] { typeof(double), typeof(double) }), @base, exponent)); } else if (operation.GetType() == typeof(UnaryMinus)) { UnaryMinus unaryMinus = (UnaryMinus)operation; Expression argument = GenerateMethodBody(unaryMinus.Argument, contextParameter, functionRegistry); return(Expression.Negate(argument)); } else if (operation.GetType() == typeof(LessThan)) { LessThan lessThan = (LessThan)operation; Expression argument1 = GenerateMethodBody(lessThan.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(lessThan.Argument2, contextParameter, functionRegistry); return(Expression.Condition(Expression.LessThan(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(LessOrEqualThan)) { LessOrEqualThan lessOrEqualThan = (LessOrEqualThan)operation; Expression argument1 = GenerateMethodBody(lessOrEqualThan.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(lessOrEqualThan.Argument2, contextParameter, functionRegistry); return(Expression.Condition(Expression.LessThanOrEqual(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(GreaterThan)) { GreaterThan greaterThan = (GreaterThan)operation; Expression argument1 = GenerateMethodBody(greaterThan.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(greaterThan.Argument2, contextParameter, functionRegistry); return(Expression.Condition(Expression.GreaterThan(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(GreaterOrEqualThan)) { GreaterOrEqualThan greaterOrEqualThan = (GreaterOrEqualThan)operation; Expression argument1 = GenerateMethodBody(greaterOrEqualThan.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(greaterOrEqualThan.Argument2, contextParameter, functionRegistry); return(Expression.Condition(Expression.GreaterThanOrEqual(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(Equal)) { Equal equal = (Equal)operation; Expression argument1 = GenerateMethodBody(equal.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(equal.Argument2, contextParameter, functionRegistry); return(Expression.Condition(Expression.Equal(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(NotEqual)) { NotEqual notEqual = (NotEqual)operation; Expression argument1 = GenerateMethodBody(notEqual.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(notEqual.Argument2, contextParameter, functionRegistry); return(Expression.Condition(Expression.NotEqual(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(Function)) { Function function = (Function)operation; FunctionInfo functionInfo = functionRegistry.GetFunctionInfo(function.FunctionName); Type funcType; Type[] parameterTypes; Expression[] arguments; if (functionInfo.IsDynamicFunc) { funcType = typeof(DynamicFunc <double, double>); parameterTypes = new Type[] { typeof(double[]) }; Expression[] arrayArguments = new Expression[function.Arguments.Count]; for (int i = 0; i < function.Arguments.Count; i++) { arrayArguments[i] = GenerateMethodBody(function.Arguments[i], contextParameter, functionRegistry); } arguments = new Expression[1]; arguments[0] = NewArrayExpression.NewArrayInit(typeof(double), arrayArguments); } else { funcType = GetFuncType(functionInfo.NumberOfParameters); parameterTypes = (from i in Enumerable.Range(0, functionInfo.NumberOfParameters) select typeof(double)).ToArray(); arguments = new Expression[functionInfo.NumberOfParameters]; for (int i = 0; i < functionInfo.NumberOfParameters; i++) { arguments[i] = GenerateMethodBody(function.Arguments[i], contextParameter, functionRegistry); } } Expression getFunctionRegistry = Expression.Property(contextParameter, "FunctionRegistry"); ParameterExpression functionInfoVariable = Expression.Variable(typeof(FunctionInfo)); return(Expression.Block( new[] { functionInfoVariable }, Expression.Assign( functionInfoVariable, Expression.Call(getFunctionRegistry, typeof(IFunctionRegistry).GetRuntimeMethod("GetFunctionInfo", new Type[] { typeof(string) }), Expression.Constant(function.FunctionName)) ), Expression.Call( Expression.Convert(Expression.Property(functionInfoVariable, "Function"), funcType), funcType.GetRuntimeMethod("Invoke", parameterTypes), arguments))); } else { throw new ArgumentException(string.Format("Unsupported operation \"{0}\".", operation.GetType().FullName), "operation"); } }
public void SetUp() { testee = new LessThan<CustomEntity>("<"); }
public static IExpression Normalize(IExpression expression) { LogicalNot /*?*/ logicalNot = expression as LogicalNot; if (logicalNot != null) { IExpression operand = logicalNot.Operand; #region LogicalNot: ! LogicalNot /*?*/ operandAsLogicalNot = operand as LogicalNot; if (operandAsLogicalNot != null) { return(Normalize(operandAsLogicalNot.Operand)); } #endregion #region BinaryOperations: ==, !=, <, <=, >, >= BinaryOperation /*?*/ binOp = operand as BinaryOperation; if (binOp != null) { BinaryOperation /*?*/ result = null; if (binOp is IEquality) { result = new NotEquality(); } else if (binOp is INotEquality) { result = new Equality(); } else if (binOp is ILessThan) { result = new GreaterThanOrEqual(); } else if (binOp is ILessThanOrEqual) { result = new GreaterThan(); } else if (binOp is IGreaterThan) { result = new LessThanOrEqual(); } else if (binOp is IGreaterThanOrEqual) { result = new LessThan(); } if (result != null) { result.LeftOperand = Normalize(binOp.LeftOperand); result.RightOperand = Normalize(binOp.RightOperand); return(result); } } #endregion #region Conditionals: &&, || Conditional /*?*/ conditional = operand as Conditional; if (conditional != null) { if (ExpressionHelper.IsIntegralNonzero(conditional.ResultIfTrue) || ExpressionHelper.IsIntegralZero(conditional.ResultIfFalse)) { Conditional result = new Conditional(); LogicalNot not; //invert condition not = new LogicalNot(); not.Operand = conditional.Condition; result.Condition = Normalize(not); //invert false branch and switch with true branch not = new LogicalNot(); not.Operand = conditional.ResultIfFalse; result.ResultIfTrue = Normalize(not); //invert true branch and switch with false branch not = new LogicalNot(); not.Operand = conditional.ResultIfTrue; result.ResultIfFalse = Normalize(not); //return result.Type = conditional.Type; return(result); } } #endregion #region Constants: true, false CompileTimeConstant /*?*/ ctc = operand as CompileTimeConstant; if (ctc != null) { if (ExpressionHelper.IsIntegralNonzero(ctc)) //Is true { var val = SetBooleanFalse(ctc); if (val != null) { return(val); } else { return(expression); } } else if (ExpressionHelper.IsIntegralZero(ctc)) //Is false { var val = SetBooleanTrue(ctc); if (val != null) { return(val); } else { return(expression); } } } #endregion } return(expression); }
public virtual void Visit(LessThan node) { }
void Comparison(out Expression exp) { Expression second; BitOr(out exp); if (StartOf(3)) { switch (la.kind) { case 29: { Get(); break; } case 30: { Get(); break; } case 31: { Get(); break; } case 32: { Get(); break; } case 33: { Get(); break; } case 34: { Get(); break; } } Token tok = t; Comparison(out second); if (!ExpectInt(exp, tok, false)) { return; }; if (!ExpectInt(second, tok, true)) { return; }; if (tok.val == "<") { exp = new LessThan((TypedExpression<int>)exp, (TypedExpression<int>)second); } else if (tok.val == ">") { exp = new GreaterThan((TypedExpression<int>)exp, (TypedExpression<int>)second); } else if (tok.val == "<=") { exp = new LessThanOrEqual((TypedExpression<int>)exp, (TypedExpression<int>)second); } else if (tok.val == ">=") { exp = new GreaterThanOrEqual((TypedExpression<int>)exp, (TypedExpression<int>)second); } else if (tok.val == "==") { exp = new Equal((TypedExpression<int>)exp, (TypedExpression<int>)second); } else if (tok.val == "!=") { exp = new NotEqual((TypedExpression<int>)exp, (TypedExpression<int>)second); } } }
public void Visit(LessThan m) { Console.WriteLine("pop stack into a"); Console.WriteLine("pop stack into b"); Console.WriteLine("push a 1 if carry and z clear"); }
public void Visit(LessThan lt) { ResultIsBoolAndBothOperandsMustBeInt(lt); }
/// <summary> /// Analyzes the specified expression. /// </summary> /// <param name="exp">The expression.</param> /// <returns> /// The result of analysis. /// </returns> /// <exception cref="System.NotSupportedException">Always.</exception> public virtual TResult Analyze(LessThan exp) { throw new NotSupportedException(); }
public void Visit(LessThan lt) { // Nothing to do here... }
public void Execute(LessThan cmd) { _file.WriteLine($"{_indents}LessThan\tOp1:{cmd.Op1.Name},Op2:{cmd.Op2.Name},Result:{cmd.Result.Name}"); }
public void TestLessThanUndefined() { var exp = new LessThan(Variable.X, Variable.X); Test(exp, ResultType.Undefined); }
public void LessThanCorrectlyConvertsToJson() { var lt = new LessThan<int>(10); lt.ToString().ShouldEqual(LessThanJson); }
public void Evaluate_LessThan_Int_True_Test() { Expression left = CreateIntLiteral(1); Expression right = CreateIntLiteral(2); Expression lessThan = new LessThan(left, right, pos); Values.Bool value = ((Values.Bool)lessThan.Accept(evaluator)); Assert.IsTrue(value.GetValue()); }