Beispiel #1
0
 public void GetMultipleOperationTypeAndRightResultWhenParseMultipleExpression()
 {
     const string expression = "6*3";
     IParser parser = new DefaultParser();
     var operation = parser.Parse(expression);
     Assert.IsInstanceOfType(operation, typeof(MultiplicateOperation));
     Assert.AreEqual(18, operation.Evaluate());
 }
Beispiel #2
0
        public void GetAddOperationTypeAndRightResultWhenParseExpressionWithAddAndMultipleOperations()
        {
            const string expression = "2+6*3";
            const string expression2 = "6*3+2";
            IParser parser = new DefaultParser();

            var operation = parser.Parse(expression);
            Assert.IsInstanceOfType(operation, typeof(AddOperation));
            Assert.AreEqual(20, operation.Evaluate());

            var operation2 = parser.Parse(expression2);
            Assert.IsInstanceOfType(operation2, typeof(AddOperation));
            Assert.AreEqual(20, operation2.Evaluate());
        }
Beispiel #3
0
        public void BracesInExpressionHaveHighestPriority()
        {
            const string expression = "(2+6)*3";
            const string expression2 = "6*(3+2)";
            IParser parser = new DefaultParser();

            var operation = parser.Parse(expression);
            Assert.IsInstanceOfType(operation, typeof(MultiplicateOperation));
            Assert.AreEqual(24, operation.Evaluate());

            var operation2 = parser.Parse(expression2);
            Assert.IsInstanceOfType(operation2, typeof(MultiplicateOperation));
            Assert.AreEqual(30, operation2.Evaluate());
        }
Beispiel #4
0
        public void AddAndSubtructOperationsCalulate()
        {
            const string expression = "(6-3-5)";
            const string expression2 = "6-(3-2)+5";
            const string expression3 = "(6+5)-(3-2)";
            const string expression4 = "6-(2*(3-2))";
            IParser parser = new DefaultParser();

            var operation = parser.Parse(expression);
            var operation2 = parser.Parse(expression2);
            var operation3 = parser.Parse(expression3);
            var operation4 = parser.Parse(expression4);

            Assert.AreEqual(-2, operation.Evaluate());
            Assert.AreEqual(10, operation2.Evaluate());
            Assert.AreEqual(10, operation3.Evaluate());
            Assert.AreEqual(4, operation4.Evaluate());
        }
Beispiel #5
0
        public void GetSubtractOperationTypeAndRightResultWhenParseSubtractExpression()
        {
            const string expression = "6-3";
            const string expression2 = "6-3-2";
            IParser parser = new DefaultParser();

            var operation = parser.Parse(expression);
            Assert.IsInstanceOfType(operation, typeof(SubtractOperation));
            Assert.AreEqual(3, operation.Evaluate());

            var operation2 = parser.Parse(expression2);
            Assert.IsInstanceOfType(operation2, typeof(SubtractOperation));
            Assert.AreEqual(1, operation2.Evaluate());
        }
Beispiel #6
0
        public void ParseAtomOperation()
        {
            const string expression = "63";
            IParser parser = new DefaultParser();

            var operation = parser.Parse(expression);
            Assert.IsInstanceOfType(operation, typeof(AtomOperation));
            Assert.AreEqual(63, operation.Evaluate());
        }
Beispiel #7
0
        public void MultipleAndDivideOperationsCalulate()
        {
            const string expression = "6*5/3";
            const string expression2 = "50/5/5";
            const string expression3 = "(4*4)-(3*3)";
            const string expression4 = "50/(5/5)";
            IParser parser = new DefaultParser();

            var operation = parser.Parse(expression);
            var operation2 = parser.Parse(expression2);
            var operation3 = parser.Parse(expression3);
            var operation4 = parser.Parse(expression4);

            Assert.AreEqual(10, operation.Evaluate());
            Assert.AreEqual(2, operation2.Evaluate());
            Assert.AreEqual(7, operation3.Evaluate());
            Assert.AreEqual(50, operation4.Evaluate());
        }