public void TestCalculateCosineFunctionInterpreted() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Interpreted); double result = engine.Calculate("cos(41)"); Assert.AreEqual(Math.Cos(41.0), result); }
private static void BenchMarkCalculationEngineRandomFunctionBuild(CalculationEngine engine, List<string> functions, int numberOfTests) { Random random = new Random(); DateTime start = DateTime.Now; Parallel.ForEach(functions,(functionText)=> { Func<int, int, int, double> function = (Func<int, int, int, double>)engine.Formula(functionText) .Parameter("var1", DataType.Integer) .Parameter("var2", DataType.Integer) .Parameter("var3", DataType.Integer) .Result(DataType.FloatingPoint) .Build(); for (int i = 0; i < numberOfTests; i++) { function(random.Next(), random.Next(), random.Next()); } }); DateTime end = DateTime.Now; Console.WriteLine("Total duration: {0}", end - start); }
public void TestCalculateCosineFunctionCompiled() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled, true, false); double result = engine.Calculate("cos(41)"); Assert.AreEqual(Math.Cos(41.0), result); }
/// <summary> /// The main entry point for the application /// </summary> static void Main(string[] args) { try { Settings settings; if (!TryInitialize(out settings)) { Usage(); return; } var engine = new CalculationEngine(settings); engine.Run(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); Console.WriteLine("\n{0} | Exiting...", DateTime.Now); engine.Stop(); } catch (Exception ex) { Console.WriteLine(ex); Usage(); } }
public static void Register(CalculationEngine ce) { //ce.RegisterFunction("ASC Changes full-width (double-byte) English letters or katakana within a character string to half-width (single-byte) characters //ce.RegisterFunction("BAHTTEXT Converts a number to text, using the ß (baht) currency format ce.RegisterFunction("CHAR", 1, _Char); // Returns the character specified by the code number //ce.RegisterFunction("CLEAN Removes all nonprintable characters from text ce.RegisterFunction("CODE", 1, Code); // Returns a numeric code for the first character in a text string ce.RegisterFunction("CONCATENATE", 1, int.MaxValue, Concat); // Joins several text items into one text item //ce.RegisterFunction("DOLLAR Converts a number to text, using the $ (dollar) currency format //ce.RegisterFunction("EXACT Checks to see if two text values are identical ce.RegisterFunction("FIND", 2, 3, Find); //Finds one text value within another (case-sensitive) //ce.RegisterFunction("FIXED Formats a number as text with a fixed number of decimals //ce.RegisterFunction("JIS Changes half-width (single-byte) English letters or katakana within a character string to full-width (double-byte) characters ce.RegisterFunction("LEFT", 1, 2, Left); // LEFTB Returns the leftmost characters from a text value ce.RegisterFunction("LEN", 1, Len); //, Returns the number of characters in a text string ce.RegisterFunction("LOWER", 1, Lower); // Converts text to lowercase ce.RegisterFunction("MID", 3, Mid); // Returns a specific number of characters from a text string starting at the position you specify //ce.RegisterFunction("PHONETIC Extracts the phonetic (furigana) characters from a text string ce.RegisterFunction("PROPER", 1, Proper); // Capitalizes the first letter in each word of a text value ce.RegisterFunction("REPLACE", 4, Replace); // Replaces characters within text ce.RegisterFunction("REPT", 2, Rept); // Repeats text a given number of times ce.RegisterFunction("RIGHT", 1, 2, Right); // Returns the rightmost characters from a text value ce.RegisterFunction("SEARCH", 2, Search); // Finds one text value within another (not case-sensitive) ce.RegisterFunction("SUBSTITUTE", 3, 4, Substitute); // Substitutes new text for old text in a text string ce.RegisterFunction("T", 1, T); // Converts its arguments to text ce.RegisterFunction("TEXT", 2, _Text); // Formats a number and converts it to text ce.RegisterFunction("TRIM", 1, Trim); // Removes spaces from text ce.RegisterFunction("UPPER", 1, Upper); // Converts text to uppercase ce.RegisterFunction("VALUE", 1, Value); // Converts a text argument to a number }
public void TestCalculateFormula1() { CalculationEngine engine = new CalculationEngine(); double result = engine.Calculate("2+3"); Assert.AreEqual(5.0, result); }
public void TestUnaryMinus5Interpreted() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Interpreted, true, false); double result = engine.Calculate("-(1*2)^3"); Assert.AreEqual(-8.0, result); }
public void TestCalculationFormula1IntegersCompiled() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled); double result = engine.Calculate("2+3"); Assert.AreEqual(5.0, result); }
public void TestUnaryMinus4Compiled() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled, true, false); double result = engine.Calculate("5* -(1*2)"); Assert.AreEqual(-10.0, result); }
public void TestMultiplicationWithNegativeConstant() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled, true, false); double result = engine.Calculate("5*-100"); Assert.AreEqual(-500.0, result); }
public void TestCalculateLognFunctionCompiled() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled, true, false); double result = engine.Calculate("logn(14, 3)"); Assert.AreEqual(Math.Log(14.0, 3.0), result); }
public void TestBatchLoad() { // Test that we can add expressions in any order CalculationEngine engine = new CalculationEngine(); ExpressionContext context = new ExpressionContext(); int interest = 2; context.Variables.Add("interest", interest); BatchLoader loader = engine.CreateBatchLoader(); loader.Add("c", "a + b", context); loader.Add("a", "100 + interest", context); loader.Add("b", "a + 1 + a", context); // Test an expression with a reference in a string loader.Add("d", "\"str \\\" str\" + a + \"b\"", context); engine.BatchLoad(loader); int result = engine.GetResult<int>("b"); Assert.AreEqual((100 + interest) + 1 + (100 + interest), result); interest = 300; context.Variables["interest"] = interest; engine.Recalculate("a"); result = engine.GetResult<int>("b"); Assert.AreEqual((100 + interest) + 1 + (100 + interest), result); result = engine.GetResult<int>("c"); Assert.AreEqual((100 + interest) + 1 + (100 + interest) + (100 + interest), result); Assert.AreEqual("str \" str400b", engine.GetResult<string>("d")); }
public void TestCalculateSineFunctionInterpreted() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Interpreted); double result = engine.Calculate("sin(14)"); Assert.AreEqual(Math.Sin(14.0), result); }
/// <summary> /// Creates a new instance of the FormulaBuilder class. /// </summary> /// <param name="formulaText"> /// A calculation engine instance that can be used for interpreting and executing /// the formula. /// </param> internal FormulaBuilder(string formulaText, bool caseSensitive, CalculationEngine engine) { this.parameters = new List <ParameterInfo>(); this.constants = new Dictionary <string, double>(); this.formulaText = formulaText; this.engine = engine; this.caseSensitive = caseSensitive; }
public void TestFormulaBuilderDuplicateParameterName() { CalculationEngine engine = new CalculationEngine(); Func <int, double, double> function = (Func <int, double, double>)engine.Formula("var1+2") .Parameter("var1", DataType.Integer) .Parameter("var1", DataType.FloatingPoint) .Build(); }
public TimeIntervalBoundaryCondition(double startTime, double endTime, string function) { this.StartTime = startTime; this.EndTime = endTime; var engine = new CalculationEngine(); this.Function = engine.Build(function); }
/// <summary> /// Uses CalcEngine to calculate an expression derived from an inspector formula. /// </summary> /// <param name="expression">The expression used to calculate.</param> /// <returns>A new value based on the expression and time in this phase.</returns> private double Calculate(Expression expression) { // Must update the Value variable stored in the CalcEngine CalculationEngine.Variables[PARAMETER_NAME] = CurrentCurveTime; // Calculate the result of the expression return((double)CalculationEngine.Evaluate(expression)); }
public void TestCalculateModuloInterpreted() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Interpreted, false, false); double result = engine.Calculate("5 % 3.0"); Assert.AreEqual(2.0, result); }
public void TestExpressionArguments2() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture); double result = engine.Calculate("if(0.57 < (3000-500)/(1500-500), 10, 20)"); Assert.AreEqual(10, result); }
public void TestNestedFunctions() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture); double result = engine.Calculate("max(sin(67), cos(67))"); Assert.AreEqual(-0.517769799789505, Math.Round(result, 15)); }
public void TestCalculateFormulaVariableNotDefinedInterpreted() { Dictionary<string, double> variables = new Dictionary<string, double>(); variables.Add("var1", 2.5); CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Interpreted); double result = engine.Calculate("var1*var2", variables); }
public void TestComplicatedPrecedence2() { CalculationEngine engine = new CalculationEngine(); double result = engine.Calculate("1+2-3*4/sqrt(25)+6-7*8/9+0"); Assert.AreEqual(0.378, Math.Round(result, 3)); }
public IterationProcess(double TimeStep, int NumberOfIterations, bool isCombinedProblem) { this.isCombinedProblem = isCombinedProblem; var engine = new CalculationEngine(); this.TimeStep = TimeStep; this.NumberOfIterations = NumberOfIterations; }
public static void Register(CalculationEngine ce) { ce.RegisterFunction("AND" , 1 , int.MaxValue , And); ce.RegisterFunction("OR" , 1 , int.MaxValue , Or); ce.RegisterFunction("NOT" , 1 , Not); ce.RegisterFunction("IF" , 3 , If); ce.RegisterFunction("TRUE" , 0 , True); ce.RegisterFunction("FALSE" , 0 , False); }
public void addition_with_only_one_number_throws_an_invalid_input_exception() { //Arrange var calculator = new CalculationEngine(); var input = "2+"; //Assert Assert.Throws <InvalidInputException>(() => /*act*/ calculator.DoMath(input)); }
public void TestCalculateFormulaVariableNotDefinedCompiled() { Dictionary <string, double> variables = new Dictionary <string, double>(); variables.Add("var1", 2.5); CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled); double result = engine.Calculate("var1*var2", variables); }
public static void Main(string[] args) { CalculationEngine calculEngine = new CalculationEngine(); Console.WriteLine("Entrz un formule math"); String formule = Console.ReadLine(); Console.WriteLine(calculEngine.Calculate(formule)); Console.ReadLine(); }
public void TestFormulaBuilderInvalidParameterName() { AssertExtensions.ThrowsException <ArgumentException>(() => { CalculationEngine engine = new CalculationEngine(); Func <int, double, double> function = (Func <int, double, double>)engine.Formula("sin+2") .Parameter("sin", DataType.Integer) .Build(); }); }
public void adding_2_and_5_should_be_7() { var calculator = new CalculationEngine(); var input = "2+5"; var expectedResult = 7; var actualResult = calculator.DoMath(input); Assert.Equal(expectedResult, actualResult); }
public static double calculateY(string equation, double x) { CalculationEngine engine = new CalculationEngine(); Dictionary <string, double> variables = new Dictionary <string, double>(); variables.Add("x", x); double result = engine.Calculate(equation, variables); return(result); }
public void TestCustomFunctionCompiled() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled); engine.AddFunction("test", (a, b) => a + b); double result = engine.Calculate("test(2,3)"); Assert.AreEqual(5.0, result); }
public void TestUnaryMinus5Compiled() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled, true, false); double result = engine.Calculate("-(1*2)^3"); #if !NETCORE Assert.AreEqual(-8.0, result); #else Assert.Equal(-8.0, result); #endif }
public void TestVariableCaseNonFunc() { CalculationEngine engine = new CalculationEngine(); Dictionary <string, double> variables = new Dictionary <string, double>(); variables.Add("var1", 2); variables.Add("otherVariable", 4.2); double result = engine.Calculate("var1+2/(3*otherVariablE)", variables); }
public void TestCalculateFormula1() { CalculationEngine engine = new CalculationEngine(); double result = engine.Calculate("2+3"); #if !NETCORE Assert.AreEqual(5.0, result); #else Assert.Equal(5.0, result); #endif }
public void TestUnaryMinus3Interpreted() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Interpreted, true, false); double result = engine.Calculate("5*(-(1*2)*3)"); #if !NETCORE Assert.AreEqual(-30.0, result); #else Assert.Equal(-30.0, result); #endif }
public void TestNegativeConstant() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled, true, false); double result = engine.Calculate("-100"); #if !NETCORE Assert.AreEqual(-100.0, result); #else Assert.Equal(-100.0, result); #endif }
public void TestCalculationFormula1FloatingPointCompiled() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled); double result = engine.Calculate("2.0+3.0"); #if !NETCORE Assert.AreEqual(5.0, result); #else Assert.Equal(5.0, result); #endif }
public void TestCalculateLognFunctionInterpreted() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Interpreted, true, false); double result = engine.Calculate("logn(14, 3)"); #if !NETCORE Assert.AreEqual(Math.Log(14.0, 3.0), result); #else Assert.Equal(Math.Log(14.0, 3.0), result); #endif }
public void TestCalculateFormulaVariableNotDefinedCompiled() { Dictionary<string, double> variables = new Dictionary<string, double>(); variables.Add("var1", 2.5); AssertExtensions.ThrowsException<VariableNotDefinedException>(() => { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled); double result = engine.Calculate("var1*var2", variables); }); }
public void TestBuild() { CalculationEngine engine = new CalculationEngine(); Func<Dictionary<string, double>, double> function = engine.Build("var1+2*(3*age)"); Dictionary<string, double> variables = new Dictionary<string, double>(); variables.Add("var1", 2); variables.Add("age", 4); double result = function(variables); Assert.AreEqual(26.0, result); }
private static void BenchMarkCalculationEngine(CalculationEngine engine, string functionText) { DateTime start = DateTime.Now; for (int i = 0; i < NumberOfTests; i++) { engine.Calculate(functionText); } DateTime end = DateTime.Now; Console.WriteLine("Total duration: {0}", end - start); }
public void TestCalcEngineAtom() { // Test that calc engine atom reference work properly CalculationEngine engine = new CalculationEngine(); ExpressionContext context = new ExpressionContext(); engine.Add("a", "\"abc\"", context); engine.Add("b", "a.length", context); engine.Add("c", "a.startswith(\"a\")", context); int result = engine.GetResult<int>("b"); Assert.AreEqual("abc".Length, result); Assert.AreEqual(true, engine.GetResult<bool>("c")); }
private static void BenchMarkCalculationEngineFunctionBuild(CalculationEngine engine, string functionText) { DateTime start = DateTime.Now; Func<int, int, int, double> function = (Func<int, int, int, double>)engine.Formula(functionText) .Parameter("var1", DataType.Integer) .Parameter("var2", DataType.Integer) .Parameter("something", DataType.Integer) .Result(DataType.FloatingPoint) .Build(); Random random = new Random(); for (int i = 0; i < NumberOfTests; i++) { function(random.Next(), random.Next(), random.Next()); } DateTime end = DateTime.Now; Console.WriteLine("Total duration: {0}", end - start); }
public void TestBasic() { CalculationEngine ce = new CalculationEngine(); ExpressionContext context = new ExpressionContext(); VariableCollection variables = context.Variables; variables.Add("x", 100); ce.Add("a", "x * 2", context); variables.Add("y", 1); ce.Add("b", "a + y", context); ce.Add("c", "b * 2", context); ce.Recalculate("a"); int result = ce.GetResult<int>("c"); Assert.AreEqual(result, ((100 * 2) + 1) * 2); variables["x"] = 345; ce.Recalculate("a"); result = ce.GetResult<int>("c"); Assert.AreEqual(((345 * 2) + 1) * 2, result); }
private static void BenchMarkCalculationEngineRandomFunctionBuild(CalculationEngine engine, List<string> functions, int numberOfTests) { Random random = new Random(); DateTime start = DateTime.Now; List<Task> tasks = new List<Task>(); foreach (string functionText in functions) { Task task = new Task(() => { Func<int, int, int, double> function = (Func<int, int, int, double>)engine.Formula(functionText) .Parameter("var1", DataType.Integer) .Parameter("var2", DataType.Integer) .Parameter("var3", DataType.Integer) .Result(DataType.FloatingPoint) .Build(); for (int i = 0; i < numberOfTests; i++) { function(random.Next(), random.Next(), random.Next()); } }); tasks.Add(task); task.Start(); } Task.WaitAll(tasks.ToArray()); DateTime end = DateTime.Now; Console.WriteLine("Total duration: {0}", end - start); }
public void TestVariableNameCaseSensitivity() { Dictionary<string, double> variables = new Dictionary<string, double>(); variables.Add("blabla", 42.5); CalculationEngine engine = new CalculationEngine(); double result = engine.Calculate("2 * BlAbLa", variables); Assert.AreEqual(85.0, result); }
public void TestReservedVariableName() { AssertExtensions.ThrowsException<ArgumentException>(() => { Dictionary<string, double> variables = new Dictionary<string, double>(); variables.Add("pi", 2.0); CalculationEngine engine = new CalculationEngine(); double result = engine.Calculate("2 * pI", variables); }); }
public void TestPiMultiplication() { CalculationEngine engine = new CalculationEngine(); double result = engine.Calculate("2 * pI"); Assert.AreEqual(2 * Math.PI, result); }
public void TestFormulaBuilderInvalidParameterName() { AssertExtensions.ThrowsException<ArgumentException>(() => { CalculationEngine engine = new CalculationEngine(); Func<int, double, double> function = (Func<int, double, double>)engine.Formula("sin+2") .Parameter("sin", DataType.Integer) .Build(); }); }
public void TestFormulaBuilderDuplicateParameterName() { AssertExtensions.ThrowsException<ArgumentException>(() => { CalculationEngine engine = new CalculationEngine(); Func<int, double, double> function = (Func<int, double, double>)engine.Formula("var1+2") .Parameter("var1", DataType.Integer) .Parameter("var1", DataType.FloatingPoint) .Build(); }); }
public void TestFormulaBuilderCompiled() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled); Func<int, double, double> function = (Func<int, double, double>)engine.Formula("var1+2*(3*age)") .Parameter("var1", DataType.Integer) .Parameter("age", DataType.FloatingPoint) .Result(DataType.FloatingPoint) .Build(); double result = function(2, 4); Assert.AreEqual(26.0, result); }
public void TestExpressionArguments() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture); double result = engine.Calculate("ifless(0.57, (3000-500)/(1500-500), 10, 20)"); Assert.AreEqual(10, result); }
public void TestCustomFunctionInterpreted() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Interpreted, false, false); engine.AddFunction("test", (a, b) => a + b); double result = engine.Calculate("test(2,3)"); Assert.AreEqual(5.0, result); }
public void TestNegativeConstant() { CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled, true, false); double result = engine.Calculate("-100"); Assert.AreEqual(-100.0, result); }
public void TestCalculateFormulaWithVariables() { Dictionary<string, double> variables = new Dictionary<string, double>(); variables.Add("var1", 2.5); variables.Add("var2", 3.4); CalculationEngine engine = new CalculationEngine(); double result = engine.Calculate("var1*var2", variables); Assert.AreEqual(8.5, result); }