public void BracketTest()
 {
     var parser = new ExpressionSerializer();
     var expressionTree = parser.Deserialize("2/{2*2}");
     var otherExpressionTree = parser.Deserialize("{2/2}*2");
     Assert.IsFalse(expressionTree.Equals(otherExpressionTree));
 }
        public void CloneTest()
        {
            var parser = new ExpressionSerializer();
            var expressionTree = parser.Deserialize("a*b");
            var complexExpressionTree = parser.Deserialize("sqrt(pi^2)+2-5*(3-2)+pi");
            var crazyComplexExpressionTree = parser.Deserialize("sqrt(pi^2)+(-5)*2^6/--2*sqrt(2*5^5)");
            var cloneExpressionTree = expressionTree.Clone();
            var cloneComplexExpressionTree = complexExpressionTree.Clone();
            var cloneCrazyComplexExpressionTree = crazyComplexExpressionTree.Clone();

            Assert.IsTrue(expressionTree.Equals(cloneExpressionTree));
            Assert.IsTrue(expressionTree == cloneExpressionTree);

            Assert.IsTrue(complexExpressionTree.Equals(cloneComplexExpressionTree));
            Assert.IsTrue(complexExpressionTree == cloneComplexExpressionTree);

            Assert.IsTrue(crazyComplexExpressionTree == cloneCrazyComplexExpressionTree);
            Assert.IsTrue(crazyComplexExpressionTree.Equals(cloneCrazyComplexExpressionTree));
        }
 public ExpressionModel(string expression, Action<ExpressionModel> onChange, params ExpressionRule[] rules)
 {
     selectionParent = null;
     selection = new List<ExpressionBase>();
     identities = new List<Identity>();
     serializer = new ExpressionSerializer();
     analyzer = new ExpressionAnalyzer();
     this.expression = serializer.Deserialize(expression);
     foreach (ExpressionRule rule in rules)
     {
         analyzer.Add(rule);
     }
     OnChanged = onChange;
     callOnChanged();
 }
Esempio n. 4
0
        /// <summary>
        /// Sets up the model to complete the level.
        /// </summary>
        /// <param name="level">The desired level.</param>
        /// <param name="category">The desired category.</param>
        public void SetLevel(int level, int category)
        {
            User.CurrentLevelIndex = level;
            User.CurrentCategoryIndex = category;
            ExpressionSerializer serializer = new ExpressionSerializer();
            int endValue = serializer.Deserialize(User.Categories[category][level].StarExpressions.Last()).Size;
            int startValue = serializer.Deserialize(User.Categories[category][level].StartExpression).Size;
            int currentValue = serializer.Deserialize(User.Categories[category][level].CurrentExpression).Size;
            ProgressBar = new ProgressbarStar(startValue, endValue, currentValue);
            StarExpressions = new List<ExpressionBase>();

            foreach (string starExpression in User.CurrentLevel.StarExpressions)
            {
                ExpressionBase starExpressionBase = serializer.Deserialize(starExpression);
                StarExpressions.Add(starExpressionBase);
                ProgressBar.Add(starExpressionBase.Size);
            }
            ExprModel = new ExpressionModel(User.Categories[category][level].CurrentExpression, (m) => onExpressionChanged(m),
                Rules.ExponentToProductRule, Rules.ProductToExponentRule,
                Rules.VariableWithNegativeExponent, Rules.ReverseVariableWithNegativeExponent, Rules.ExponentProduct,
                Rules.CommonPowerParenthesisRule, Rules.ReverseCommonPowerParenthesisRule, Rules.SplittingFractions, Rules.ProductParenthesis,
                Rules.ReverseProductParenthesis, Rules.ParenthesisPowerRule, Rules.FractionToProductRule, Rules.SquareRootRule,
                Rules.RemoveParenthesisRule, Rules.ProductOfConstantAndFraction, Rules.FactorizeUnaryMinus, Rules.FactorizationRule,
                Rules.MultiplyOneRule, Rules.AddFractionWithCommonDenominatorRule, Rules.RemoveNull, Rules.MultiplyByNull,
                Rules.CalculateVariadicRule, Rules.CalculateBinaryRule, Rules.MultiplyMinusRule, Rules.DivisionEqualsOneRule, Rules.ProductOfFractions, Rules.VariablesEqualNull);
            UpdateLevelData();
            onExpressionChanged(ExprModel);
        }
 public ExpressionBase expr(string expression)
 {
     ExpressionSerializer serializer = new ExpressionSerializer();
     return serializer.Deserialize(expression);
 }
        public void ExpressionSerializerDeserialize()
        {
            var parser = new ExpressionSerializer();
            var exp = parser.Deserialize("pi+1");

            Assert.IsInstanceOfType(exp, typeof(VariadicOperatorExpression), "Root is Operator");
            Assert.AreEqual(OperatorType.Add, (exp as VariadicOperatorExpression).Type, "Root operator is add");
            Assert.IsInstanceOfType((exp as VariadicOperatorExpression)[0], typeof(ConstantExpression), "Root.Left is Constant");
            Assert.AreEqual("Pi", ((exp as VariadicOperatorExpression)[0] as ConstantExpression).Value, "Root.Left is Pi");
            Assert.IsInstanceOfType((exp as VariadicOperatorExpression)[1], typeof(NumericExpression), "Root.Right is Numeric");
            Assert.AreEqual("1", ((exp as VariadicOperatorExpression)[1] as NumericExpression).Value, "Root.Right is 1");
        }
 public void ExpressionSerializerCalculate()
 {
     var parser = new ExpressionSerializer();
     Assert.AreEqual(2, parser.Deserialize("1+1").Calculate(), "1 + 1 = 2");
     Assert.AreEqual(5, parser.Deserialize("--5").Calculate(), "--5 = 5");
     Assert.AreEqual(2, parser.Deserialize("sqrt(-2^2)").Calculate(), "sqrt(-2^2) = 2");
     Assert.AreEqual(2, parser.Deserialize("sqrt(sqrt(16))").Calculate(), "sqrt(sqrt(16)) = 2");
     Assert.AreEqual(-Math.PI, parser.Deserialize("-pi").Calculate(), "-pi = " + -Math.PI);
     Assert.IsTrue(double.IsNaN(parser.Deserialize("sqrt(-1)").Calculate().Value), "sqrt(-1) = NaN");
     Assert.AreEqual(Math.Pow(Math.PI, 2), parser.Deserialize("pi^2").Calculate(), "pi^2 = " + Math.Pow(Math.PI, 2));
     Assert.AreEqual(4, parser.Deserialize("2^sqrt(4)").Calculate(), "2^sqrt(4) = 4");
     Assert.AreEqual(24, parser.Deserialize("sqrt(4)*sqrt(9)*sqrt(16)").Calculate(), "sqrt(4) * sqrt(9) * sqrt(16) = 24");
     Assert.AreEqual(-4, parser.Deserialize("-(2+2)").Calculate(), "-(2 + 2) = -4");
     Assert.AreEqual(24, parser.Deserialize("(2+2)*(3+3)").Calculate(), "(2 + 2) * (3 + 3) = 24");
     Assert.AreEqual(24, parser.Deserialize("-(2+2)*-(3+3)").Calculate(), "-(2 + 2) * -(3 + 3) = 24");
     Assert.AreEqual(20, parser.Deserialize("2*sqrt(4)*5").Calculate(), "2 * sqrt(4) * 5 = 20");
     Assert.AreEqual(-2, parser.Deserialize("-sqrt(4)").Calculate(), "-sqrt(2) = -2");
     Assert.IsTrue(double.IsInfinity(parser.Deserialize("1/0").Calculate().Value), "1/0 = Infinity");
     Assert.IsTrue(double.IsNaN(parser.Deserialize("0/0").Calculate().Value), "0/0 = NaN");
 }
        public void ExpressionSerializerDeserializeToSerialize()
        {
            var parser = new ExpressionSerializer();
            var exp = parser.Deserialize("1   +  1");

            Assert.AreEqual("{1}+{1}", parser.Serialize(exp), "'1   +  1' = '{1}+{1}'");
        }