public void DefineOperation_BinaryAlreadyExists_ShouldThrow()
        {
            var calculator = new CalculatorEngine();

            calculator.DefineOperation("+", (x, y) => x + y);

            Assert.Throws <AlreadyExistsOperationException>(() =>
            {
                calculator.DefineOperation("+", (x, y) => x + y);
            });
        }
Beispiel #2
0
        public override double calculate(List <double> arguments, CalculatorEngine engine)
        {
            switch (arguments.Count)
            {
            case 1: return(random.NextDouble() * arguments[0]);

            case 2: return(arguments[0] + (arguments[1] - arguments[0]) * random.NextDouble());

            default: throw new ExpressionInvalidException("invalidRandomNumArgs");
            }
        }
Beispiel #3
0
 public override double calculate(List <double> arguments, CalculatorEngine engine)
 {
     foreach (double num in arguments)
     {
         if (num > 0)
         {
             return(1);
         }
     }
     return(0);
 }
        public void DefineOperation_TernaryAlreadyExists_ShouldThrow()
        {
            var calculator = new CalculatorEngine();

            calculator.DefineOperation("whatever", (x, y, z) => x + y + z);

            Assert.Throws <AlreadyExistsOperationException>(() =>
            {
                calculator.DefineOperation("whatever", (x, y, z) => x + y + z);
            });
        }
        public void PerformOperation_Ternary_ShouldCalculate()
        {
            var calculator = new CalculatorEngine();

            calculator.DefineOperation("T5", (x, y, z) => Math.Min(x, Math.Min(y, z)));

            var operation = new Operation("T5", new[] { 2.0, 3.0, 6.0 });
            var actual    = calculator.PerformOperation(operation);

            Assert.That(actual, Is.EqualTo(2.0).Within(0.001), "Incorrect calculation result");
        }
        public void PerformOperation_Binary_ShouldCalculate()
        {
            var calculator = new CalculatorEngine();

            calculator.DefineOperation("*", (x, y) => x * y);

            var operation = new Operation("*", new[] { 5.0, 3.0 });
            var actual    = calculator.PerformOperation(operation);

            Assert.That(actual, Is.EqualTo(15.0).Within(0.001), "Incorrect calculation result");
        }
        public void PerformOperation_Unary_ShouldCalculate()
        {
            var calculator = new CalculatorEngine();

            calculator.DefineOperation("++", x => x + 1);

            var operation = new Operation("++", new[] { 3.0 });

            var actual = calculator.PerformOperation(operation);

            Assert.That(actual, Is.EqualTo(4.0).Within(0.001), "Incorrect calculation result");
        }
        public void ShouldReturn7WhenDivide35To5()
        {
            //arrange
            var calculator = new CalculatorEngine();

            //act
            Int32 realExeption = calculator.Divide(35, 5);
            Int32 simulateExeption = 35 / 5;

            //assert
            Assert.AreEqual(realExeption, simulateExeption);
        }
        public void ShouldReturn15WhenMultiply5To3()
        {
            //arrange
            var calculator = new CalculatorEngine();

            //act
            Int32 realExeption = calculator.Multiply(5, 3);
            Int32 simulateExeption = 5 * 3;

            //assert
            Assert.AreEqual(realExeption, simulateExeption);
        }
        public void ShouldReturn2WhenSubstract7To5()
        {
            //arrange
            var calculator = new CalculatorEngine();

            //act
            Int32 realExeption = calculator.Substract(7, 5);
            Int32 simulateExeption = 7 - 5;

            //assert
            Assert.AreEqual(realExeption, simulateExeption);
        }
        public void ShouldReturn5WhenAdd2To3()
        {
            //arrange
            var calculator = new CalculatorEngine();

            //act
            Int32 realExeption = calculator.Add(2, 3);
            Int32 simulateExeption = 2 + 3;

            //assert
            Assert.AreEqual(realExeption,simulateExeption);
        }
Beispiel #12
0
 public override double calculate(List <double> arguments, CalculatorEngine engine)
 {
     if (arguments.Count > 4)
     {
         throw new ExpressionInvalidException("invalidTimeNumArgs");
     }
     while (arguments.Count < 4)
     {
         arguments.Add(0);
     }
     return(arguments[0] + arguments[1] / 24 + arguments[2] / 1440 + arguments[3] / 86400);
 }
Beispiel #13
0
 public override double calculate(List <double> arguments, CalculatorEngine engine)
 {
     if (arguments.Count > 3)
     {
         throw new ExpressionInvalidException("invalidIfNumArgs");
     }
     while (arguments.Count < 3)
     {
         arguments.Add(0);
     }
     return(arguments[0] > 0 ? arguments[1]: arguments[2]);
 }
        public void Calculate_BasicNegativeDivide_Test()
        {
            string firstOperand  = "-4";
            string secondOperand = "2";
            string expected      = "-2";
            string actual;

            CalculatorEngine engine = new CalculatorEngine();

            actual = engine.calculate("÷", firstOperand, secondOperand);
            Assert.AreEqual(expected, actual);
        }
        public void Calculate_DivideByZero_Test()
        {
            string firstOperand  = "4";
            string secondOperand = "0";
            string expected      = "E";
            string actual;

            CalculatorEngine engine = new CalculatorEngine();

            actual = engine.calculate("÷", firstOperand, secondOperand);
            Assert.AreEqual(expected, actual);
        }
        public void Calculate_BasicPlus_Test()
        {
            string firstOperand  = "1";
            string secondOperand = "2";
            string expected      = "3";
            string actual;

            CalculatorEngine engine = new CalculatorEngine();

            actual = engine.calculate("+", firstOperand, secondOperand);
            Assert.AreEqual(expected, actual);
        }
Beispiel #17
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            calculatorEngine = new CalculatorEngine();
            calculatorEngine.PropertyChanged += CalculatorPropertyChanged;

            var gridView = FindViewById <GridView> (Resource.Id.buttonGridView);

            gridView.Adapter = new ButtonAdapter(this, OnCalculatorKeyPress);
        }
Beispiel #18
0
 public override double calculate(List <double> arguments, CalculatorEngine engine)
 {
     if (arguments.Count < 2)
     {
         throw new ExpressionInvalidException("invalidComparisonNumArgs");
     }
     for (int i = 1; i < arguments.Count; i++)
     {
         if (arguments[i] != arguments[0])
         {
             return(0);
         }
     }
     return(1);
 }
        public void Calculate_TernaryParametersMismatch_ShouldThrow()
        {
            var calculator = new CalculatorEngine();

            calculator.DefineOperation("+", (x, y, z) => x + y + z);

            Assert.Throws <ParametersCountMismatchException>(() =>
            {
                calculator.PerformOperation(new Operation("+", new double[] { 1 }));
            });

            Assert.Throws <ParametersCountMismatchException>(() =>
            {
                calculator.PerformOperation(new Operation("+", new double[] { 1, 2 }));
            });
        }
Beispiel #20
0
        public override double calculate(List <double> arguments, CalculatorEngine engine)
        {
            double min     = 0;
            bool   isFirst = true;

            foreach (double num in arguments)
            {
                if (isFirst)
                {
                    isFirst = false;
                    min     = num;
                }
                else if (num < min)
                {
                    min = num;
                }
            }
            return(min);
        }
Beispiel #21
0
        public override double calculate(List <double> arguments, CalculatorEngine engine)
        {
            double total = 0;

            foreach (double argument in arguments)
            {
                total += argument;
            }
            double n = Math.Floor(total);

            if (n < 0)
            {
                throw new ExpressionInvalidException("invalidFactorialArg");
            }
            total = 1;
            for (double i = 1; i <= n; i += 1)
            {
                total *= i;
            }
            return(total);
        }
Beispiel #22
0
        public override double calculate(List <double> arguments, CalculatorEngine engine)
        {
            if (arguments.Count == 1)
            {
                double r = Math.Floor(Math.Abs(arguments[0]));
                return(r == 0 ? 1 : r);
            }
            double res = Math.Floor(Math.Abs(arguments[0]));

            for (int i = 1; i < arguments.Count; i++)
            {
                double n = Math.Floor(Math.Abs(arguments[i]));
                while (n != 0)
                {
                    double temp = n;
                    n   = mod(res, n);
                    res = temp;
                }
            }
            return(res);
        }
        public void Calculate_ArgumentOutOfRange_ShouldThrow()
        {
            var calculator = new CalculatorEngine();

            var sqrt = new Func <double, double>(
                x =>
            {
                if (x < 0)
                {
                    throw new ArgumentOutOfRangeException();
                }

                return(Math.Sqrt(x));
            });

            calculator.DefineOperation("sqrt", sqrt);

            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                calculator.PerformOperation(new Operation("sqrt", new double[] { -4 }));
            });
        }
Beispiel #24
0
        public override double calculate(List <double> arguments, CalculatorEngine engine)
        {
            if (arguments.Count == 1)
            {
                return(Math.Floor(Math.Abs(arguments[0])));
            }
            double res = Math.Floor(Math.Abs(arguments[0]));

            for (int i = 1; i < arguments.Count; i++)
            {
                double n  = Math.Floor(Math.Abs(arguments[i]));
                double t  = n;
                double t2 = res;
                while (t2 != 0)
                {
                    double temp = t2;
                    t2 = mod(n, t2);
                    n  = temp;
                }
                res = div(res * t, n);
            }
            return(res);
        }
Beispiel #25
0
 public abstract double calculate(double val1, double val2, CalculatorEngine engine);
Beispiel #26
0
 public override double calculate(double val1, double val2, CalculatorEngine engine)
 {
     throw new ExpressionInvalidException("braceInvolved");
 }
 public void Init()
 {
     calculatorEngine = new CalculatorEngine();
 }
 public void Setup()
 {
     //Arrange
     calcEngine = new CalculatorEngine();
 }
Beispiel #29
0
        public void TestSubtractMethod()
        {
            double result = CalculatorEngine.Subtract(10.0, 20.0);

            Assert.AreEqual(-10.0, result);
        }
Beispiel #30
0
        public void TestAddMethod()
        {
            double result = CalculatorEngine.Add(10.0, 20.0);

            Assert.AreEqual(30.0, result);
        }
Beispiel #31
0
 public void CreateCalculatorEngine()
 {
     engine = new CalculatorEngine();
 }
Beispiel #32
0
 public override double calculate(double val1, double val2, CalculatorEngine engine)
 {
     return(val1 * val2);
 }
Beispiel #33
0
 public override double calculate(double val1, double val2, CalculatorEngine engine)
 {
     return(CoreUtils.power(val1, val2, engine));
 }
Beispiel #34
0
 public void Setup()
 {
     display    = new CalculatorDisplay();
     calculator = new CalculatorEngine(display);
     rnd        = new Random(123);
 }