Ejemplo n.º 1
0
        public void TestTrigFunction()
        {
            Assert.AreEqual(3 + Math.Sin(30) - 1, CalculatorParser.Resolve("3+sin(30)-1"));
            Assert.AreEqual(Math.Sin(330), CalculatorParser.Resolve("sin(330)"));
            Assert.AreEqual(Math.Sin(-330), CalculatorParser.Resolve("sin(-330)"));

            Assert.AreEqual(3 + Math.Cos(30) - 1 * 5, CalculatorParser.Resolve("3+cos(30)-1*5"));
            Assert.AreEqual(Math.Cos(300), CalculatorParser.Resolve("cos(300)"));
            Assert.AreEqual(Math.Cos(-300), CalculatorParser.Resolve("cos(-300)"));
        }
 public void TestMissingOperand()
 {
     try
     {
         CalculatorParser.Resolve("1+");
         Assert.Fail();
     }
     catch (Exception ex)
     {
         Assert.AreEqual("Invalid expression", ex.Message);
     }
 }
 public void TestAdvancedExpression()
 {
     try
     {
         CalculatorParser.Resolve("5+4-3*2/1");
         Assert.IsTrue(true);
     }
     catch (Exception)
     {
         Assert.Fail();
     }
 }
 public void TestSimpleExpression()
 {
     try
     {
         CalculatorParser.Resolve("1+1");
         Assert.IsTrue(true);
     }
     catch (Exception)
     {
         Assert.Fail();
     }
 }
 public void TestInvalidCharacter()
 {
     try
     {
         CalculatorParser.Resolve("1+O");
         Assert.Fail();
     }
     catch (Exception ex)
     {
         Assert.AreEqual("Unknown term: 'O'", ex.Message);
     }
 }
Ejemplo n.º 6
0
 public void TestOrderOfOperations()
 {
     Assert.AreEqual(5 + 3 * 6, CalculatorParser.Resolve("5+3*6"));
     Assert.AreEqual(7 * 3 - 10 / 2, CalculatorParser.Resolve("7*3-10/2"));
     Assert.AreEqual(4 * 6 / 3, CalculatorParser.Resolve("4*6/3"));       // (4*6)/3
     Assert.AreEqual(4 * 5 + 4 * 3, CalculatorParser.Resolve("4*5+4*3")); // (4*5) + (4*3)
     Assert.AreEqual(Math.Pow(4, 2) / 2, CalculatorParser.Resolve("4^2/2"));
     Assert.AreEqual(7 - 4 + 6 - 2, CalculatorParser.Resolve("7-4+6-2"));
     Assert.AreEqual(5 + 10 * 5, CalculatorParser.Resolve("5+10*5"));       // 5+(10*5)
     Assert.AreEqual(8 + Math.Pow(5, 2) - 9, CalculatorParser.Resolve("8+5^2-9"));
     Assert.AreEqual(6 * 5 / 10 - 7, CalculatorParser.Resolve("6*5/10-7")); // (6*5) / (10-7)
     Assert.AreEqual(8 - 5 * Math.Pow(2, 2), CalculatorParser.Resolve("8-5*2^2"));
     Assert.AreEqual(2 + 3 * 5 - 4, CalculatorParser.Resolve("2+3*5-4"));
 }
Ejemplo n.º 7
0
        public void TestAngleModes()
        {
            Assert.AreEqual(CalculatorParser.Resolve("cos(sin(1))"), CalculatorParser.Resolve("cos(sin(1))", Utils.AngleMode.Rad));
            Assert.AreEqual(0.034899496702501, CalculatorParser.Resolve("sin(2)", Utils.AngleMode.Deg), 0.000000000000001);
            Assert.AreEqual(1.460139105621001, CalculatorParser.Resolve("atan(9)", Utils.AngleMode.Rad), 0.000000000000001);
            Assert.AreEqual(0.998026728428272, CalculatorParser.Resolve("cos(4)", Utils.AngleMode.Grad), 0.000000000000001);
            Assert.AreEqual(28.63625328291560, CalculatorParser.Resolve("tan(88)", Utils.AngleMode.Deg), 0.0000000000001);

            try
            {
                CalculatorParser.Resolve("asin(62)", Utils.AngleMode.Rad);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.AreEqual("Invalid input", ex.Message);
            }
        }
Ejemplo n.º 8
0
 public void TestAdvancedAddition()
 {
     Assert.AreEqual(15, CalculatorParser.Resolve("10+3+2"));
 }
Ejemplo n.º 9
0
 public void TestSimpleDivision()
 {
     Assert.AreEqual(5, CalculatorParser.Resolve("10/2"));
 }
Ejemplo n.º 10
0
 public void TestSimpleMultiplcation()
 {
     Assert.AreEqual(50, CalculatorParser.Resolve("10*5"));
 }
Ejemplo n.º 11
0
 public void TestSimpleSubtraction()
 {
     Assert.AreEqual(5, CalculatorParser.Resolve("15-10"));
 }
Ejemplo n.º 12
0
 public void TestSimpleAddition()
 {
     Assert.AreEqual(5, CalculatorParser.Resolve("3+2"));
 }
Ejemplo n.º 13
0
 public void TestAdvancedDivision()
 {
     Assert.AreEqual(5, CalculatorParser.Resolve("50/5/2"));
 }
Ejemplo n.º 14
0
 public void TestBrackets()
 {
     Assert.AreEqual((4 * 5) + (4 * Math.Sin(3)) / Math.Cos(2), CalculatorParser.Resolve("(4*5) + (4*sin(3)) / cos(2)"));
     Assert.AreEqual(1 + (2 - (Math.Pow(3, 2) * 4) / 10), CalculatorParser.Resolve("1+(2-((3^2)*4)/10)"));
 }
Ejemplo n.º 15
0
 public void TestAdvancedSubtraction()
 {
     Assert.AreEqual(5, CalculatorParser.Resolve("30-15-10"));
 }
Ejemplo n.º 16
0
 public void TestAdvancedMultiplcation()
 {
     Assert.AreEqual(50, CalculatorParser.Resolve("2*5*5"));
 }
Ejemplo n.º 17
0
 public void TestMathFunctions()
 {
     Assert.AreEqual(Math.Pow(10, 7491), CalculatorParser.Resolve("10 ^ (7491)"), 0.000000000000001);
 }
Ejemplo n.º 18
0
 public void TestExpression()
 {
     Assert.AreEqual(25, CalculatorParser.Resolve("50/5*2+5"));
 }
 public void TestEmptyString()
 {
     Assert.AreEqual(0, CalculatorParser.Resolve(""));
 }