public void Add_Remove_Value_Fail() { ExpressionParser parser = new ExpressionParser(); parser.Values.Add("x", 5); // This parses the expression and the values // will be cached in the expression tree double value = parser.Parse("x+x+x"); Assert.AreEqual(15, value); // Remove value parser.Values.Remove("x"); bool exception = false; try { value = parser.Parse("x+x+x"); } catch(Exception ex) { exception = true; } if (!exception) Assert.Fail(); }
public void AllCultures_Correct_DecimalSeparator_Success() { CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures); ExpressionParser parser = new ExpressionParser(); foreach(var culture in cultures) { string decimalSeparator = culture.NumberFormat.NumberDecimalSeparator; try { parser.Culture = culture; } catch (ArgumentOutOfRangeException) { // If unsupported decimal or group separator, see documentation continue; } Assert.AreEqual( 4300.40, parser.Parse(String.Format(culture, "3300{0}3+2100{0}2-1100{0}1", decimalSeparator)), "Failed, Culture " + culture.Name ); } }
public void Serialize_Deserialize_Evaluate_38() { ExpressionParser parser = new ExpressionParser(); string func = "x+y+z"; double expected = 38; // Values parser.Values.Add("x", 5); parser.Values.Add("y", 10); parser.Values.Add("z", 23); // Parse double value = parser.Parse(func); Assert.AreEqual(expected, value); // Get expression Expression expression = parser.Expressions[func]; MemoryStream stream = new MemoryStream(); // Serialize expression.Save(stream); stream.Flush(); stream.Position = 0; // Deserialize expression = new Expression(); expression.Load(stream); value = parser.EvalExpression(expression); Assert.AreEqual(expected, value); }
static private string CheckAndAddRef(string Expression, info.lundin.math.ExpressionParser parser, Widget widget) { string expression = Expression; /*replace to values*/ ///add this. values pos expression = expression.Replace("this.X", $"{widget.Left}"); expression = expression.Replace("this.Y", $"{widget.Top}"); expression = expression.Replace("this.l", $"{widget.Left}"); expression = expression.Replace("this.t", $"{widget.Top}"); expression = expression.Replace("this.left", $"{widget.Left}"); expression = expression.Replace("this.top", $"{widget.Top}"); ///add this.size expression = expression.Replace("this.w", $"{widget.Width ?? 0}"); expression = expression.Replace("this.h", $"{widget.Height ?? 0}"); expression = expression.Replace("this.width", $"{widget.Width ?? 0}"); expression = expression.Replace("this.height", $"{widget.Height ?? 0}"); if (widget.Parent != null) { ///add parent values pos expression = expression.Replace("&.X", GerParrentValue(widget, "&.X")); expression = expression.Replace("&.Y", GerParrentValue(widget, "&.Y")); expression = expression.Replace("&.l", GerParrentValue(widget, "&.X")); expression = expression.Replace("&.t", GerParrentValue(widget, "&.Y")); expression = expression.Replace("&.left", GerParrentValue(widget, "&.X")); expression = expression.Replace("&.top", GerParrentValue(widget, "&.Y")); ///add parent values size expression = expression.Replace("&.w", GerParrentValue(widget, "&.w")); expression = expression.Replace("&.h", GerParrentValue(widget, "&.h")); expression = expression.Replace("&.width", GerParrentValue(widget, "&.w")); expression = expression.Replace("&.height", GerParrentValue(widget, "&.h")); } ///add window walues expression = expression.Replace("W.h", $"{MyraEnvironment.Game.Window.ClientBounds.Height}"); expression = expression.Replace("W.height", $"{MyraEnvironment.Game.Window.ClientBounds.Height}"); expression = expression.Replace("W.w", $"{MyraEnvironment.Game.Window.ClientBounds.Width}"); expression = expression.Replace("W.width", $"{MyraEnvironment.Game.Window.ClientBounds.Width}"); /// if (expression.Contains("[")) { Regex regex = new Regex(@"\[.+].\w*"); MatchCollection matches = regex.Matches(expression); foreach (var item in matches) { string Id = item.ToString().Split('[')[1].ToString().Split(']')[0].ToString(); var w = GetByID(Id, _widgets); var valueForReg = GetValue(item.ToString().Substring(item.ToString().IndexOf(']') + 1, 1), w.Bounds); expression = expression.Replace(item as string, $"{valueForReg ?? 0}"); } } return(expression); }
public void Parse_EmptyOrNUllExpression_Fail() { bool exception = false; var parser = new ExpressionParser(); try { parser.Parse(""); } catch (ParserException) { exception = true; } if (!exception) Assert.Fail("Null expression was allowed"); }
public static void Parse(Widget widget, List <Widget> widgets, int level = 0) { _widgets = widgets; #region Check ///check on depths if (level > DepthOfCalculations) { return; } ///check parent on dynamic layout if (widget.Parent != null && !widget.Parent.Layout2d.Nullable) { Parse(widget.Parent, widgets, ++level); } #endregion ///expresion parser info.lundin.math.ExpressionParser parser = new info.lundin.math.ExpressionParser(); parser.ImplicitMultiplication = false; #region Calculation /// if X exp not null if (widget.Layout2d.PositionXExpression != "NULL") { widget.Left = (int)parser.Parse(CheckAndAddRef(widget.Layout2d.PositionXExpression, parser, widget)); } /// if Y exp not null if (widget.Layout2d.PositionYExpression != "NULL") { widget.Top = (int)parser.Parse(CheckAndAddRef(widget.Layout2d.PositionYExpression, parser, widget)); } /// if H exp not null if (widget.Layout2d.SizeYExpression != "NULL") { widget.Height = (int)parser.Parse(CheckAndAddRef(widget.Layout2d.SizeYExpression, parser, widget)); } /// if W exp not null if (widget.Layout2d.SizeXExpression != "NULL") { widget.Width = (int)parser.Parse(CheckAndAddRef(widget.Layout2d.SizeXExpression, parser, widget)); } #endregion widget.Layout2d.Calculated = true; }
public void Parse_MissingParentheses_Fail() { bool exception = false; var parser = new ExpressionParser() { RequireParentheses = true }; try { parser.Parse("cos5"); } catch (ParserException) { exception = true; } if (!exception) Assert.Fail("Missing parentheses was wrongly allowed"); }
public void Add_Remove_Add_Value_Success() { ExpressionParser parser = new ExpressionParser(); parser.Values.Add("x", 5); // This parses the expression and the values // will be cached in the expression tree double value = parser.Parse("x+x+x"); Assert.AreEqual(15, value); // Remove value parser.Values.Remove("x"); // Add value with same variable parser.Values.Add("x", 10); value = parser.Parse("x+x+x"); Assert.AreEqual(30, value); }
public void AllCultures_Correct_GroupAndDecimalSeparator_Success() { CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures); ExpressionParser parser = new ExpressionParser(); foreach (var culture in cultures) { string separator = culture.NumberFormat.CurrencyGroupSeparator; string decimalSeparator = culture.NumberFormat.NumberDecimalSeparator; // At least one culture uses / for decimal separator, this is not allowed // and documented as not allowed. if (decimalSeparator == "/") continue; // At least one culture uses the same grouping and decimal separator, // this is NotFiniteNumberException allowed and documented if (separator == decimalSeparator) continue; try { parser.Culture = culture; } catch (ArgumentOutOfRangeException) { // If unsupported decimal or group separator, see documentation continue; } Assert.AreEqual( 2469134.46, parser.Parse(String.Format(culture, "1{0}234{0}567{1}23+1{0}234{0}567{1}23", separator, decimalSeparator)), "Failed, Culture " + culture.EnglishName ); } }
public void AllCultures_Correct_GroupSeparator_Success() { CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures); ExpressionParser parser = new ExpressionParser(); foreach (var culture in cultures) { string separator = culture.NumberFormat.CurrencyGroupSeparator; try { parser.Culture = culture; } catch (ArgumentOutOfRangeException) { // If unsupported decimal or group separator, see documentation continue; } Assert.AreEqual( 2469134, parser.Parse(String.Format(culture, "1{0}234{0}567+1{0}234{0}567", separator)), "Failed, Culture " + culture.EnglishName ); } }
public void Initialize() { parser = new ExpressionParser(); }
public void Parse_MissingVariables_Fail() { bool exception = false; var parser = new ExpressionParser(); try { parser.Parse("x+y"); } catch (ParserException) { exception = true; } if (!exception) Assert.Fail("Missing variables in table wrongly allowed"); }
public void Change_Value_Success() { ExpressionParser parser = new ExpressionParser(); parser.Values.Add("x", 5); // This parses the expression and the values // will be cached in the expression tree double value = parser.Parse("x+x+x"); Assert.AreEqual(15, value); // Change value parser.Values["x"].SetValue(10); value = parser.Parse("x+x+x"); Assert.AreEqual(30, value); }
public void Parse_UnbalancedParentheses_Fail() { bool exception = false; var parser = new ExpressionParser() { RequireParentheses = true }; try { parser.Parse("cos(5)+x-sin(pi"); } catch (ParserException) { exception = true; } if (!exception) Assert.Fail("Unbalanced parentheses wrongly allowed"); }
public List<Pointd> Calcular(string function, double X0, double XF, double Y0, int N) { var h = (XF - X0) / N; var k1 = 0.0; var k2 = 0.0; var k3 = 0.0; var k4 = 0.0; List<Pointd> list = new List<Pointd>(); list.Add(new Pointd(X0, Y0)); try { for (int i = 1; i <= N; i++) { var exp = new ExpressionParser(); exp.Values.Add("x", X0); exp.Values.Add("t", X0); exp.Values.Add("y", Y0); k1 = exp.Parse(function); var exp2 = new ExpressionParser(); exp2.Values.Add("x", X0 + (h / 2)); exp2.Values.Add("t", X0 + (h / 2)); exp2.Values.Add("y", Y0 + (h * (k1 / 2))); k2 = exp2.Parse(function); var exp3 = new ExpressionParser(); exp3.Values.Add("x", X0 + (h / 2)); exp3.Values.Add("t", X0 + (h / 2)); exp3.Values.Add("y", Y0 + (h * (k2 / 2))); k3 = exp3.Parse(function); var exp4 = new ExpressionParser(); exp4.Values.Add("x", X0 + (h)); exp4.Values.Add("t", X0 + (h)); exp4.Values.Add("y", Y0 + (h * (k3))); k4 = exp4.Parse(function); Y0 = Y0 + ((h / 6) * (k1 + (2 * k2) + (2 * k3) + k4)); X0 = X0 + h; list.Add(new Pointd(X0, Y0)); } return list; } catch (Exception) { return null; } }
public void Initialize() { parser = new ExpressionParser(); currencyDecimalSeparator = CultureInfo.InvariantCulture.NumberFormat.CurrencyDecimalSeparator; delta = 1.22460635382238E-16; }
public void CurrentCulture_Parse_Success() { CultureInfo culture = System.Threading.Thread.CurrentThread.CurrentCulture; ExpressionParser parser = new ExpressionParser(); parser.Culture = culture; string decimalSeparator = culture.NumberFormat.NumberDecimalSeparator; Assert.AreEqual(3, parser.Parse(String.Format("round(3{0}3)", decimalSeparator))); }
private void button1_Click(object sender, EventArgs e) { ExpressionParser oParser = new ExpressionParser(); oParser.RequireParentheses = chkParant.Checked; oParser.ImplicitMultiplication = chkImplicit.Checked; int iIterations = 1000; try { iIterations = Int32.Parse(textBox4.Text.Trim()); } catch { textBox4.Text = iIterations.ToString(); } foreach (Control c in valuePanel.Controls) { VariableValue varVal = c as VariableValue; if (varVal == null) continue; if (String.IsNullOrEmpty(varVal.Variable) || String.IsNullOrEmpty(varVal.Value)) continue; double val = 0; if (Double.TryParse(varVal.Value, out val)) { oParser.Values.Add(varVal.Variable, val); } else { oParser.Values.Add(varVal.Variable, varVal.Value); } } string sFunction = textBox1.Text.Trim(); double fResult = 0f; try { Cursor = Cursors.WaitCursor; Stopwatch watch = new Stopwatch(); watch.Start(); // Parse expression once fResult = oParser.Parse(sFunction); // Fetch parsed tree Expression expression = oParser.Expressions[sFunction]; // Iterate and evaluate tree for (int i = 0; i < iIterations; i++) { fResult = oParser.EvalExpression(expression); } watch.Stop(); textBox2.Text = "Result: " + fResult + "\r\n"; textBox2.Text += "Iterations: " + iIterations + "\r\n"; textBox2.Text += "Time consumed: " + watch.ElapsedMilliseconds + " milliseconds.\r\n\r\n"; try { Assembly oAssembly = Assembly.LoadFrom("info.lundin.math.dll"); textBox2.Text += String.Format("Assembly: {0}\r\n.NET Runtime: {1}", oAssembly.FullName, oAssembly.ImageRuntimeVersion); } catch { } } catch (Exception ex) { textBox2.Text = ex.Message; } finally { Cursor = Cursors.Default; } }
public List<Pointd> Calcular(string function, double X0, double XF, double Y0, int N) { try { var h = (XF - X0) / N; List<Pointd> list = new List<Pointd>(); list.Add(new Pointd(X0, Y0)); for (int i = 1; i <= N; i++) { var exp = new ExpressionParser(); exp.Values.Add("x", X0); exp.Values.Add("y", Y0); exp.Values.Add("t", X0); var result = exp.Parse(function); Y0 = Y0 + (h * result); X0 = X0 + (h); list.Add(new Pointd(X0, Y0)); } return list; } catch (Exception) { return null; } }