Esempio n. 1
0
        public void DivideStrategy_Compute_HandlesDivideByZero()
        {
            DivideStrategy strategy = new DivideStrategy();
            List <double>  inputs   = new List <double> {
                1, 0, 2, 3
            };

            Assert.ThrowsAsync <CalculatorException>(() => strategy.Compute(inputs));
        }
Esempio n. 2
0
 public PrimitiveCalculator(AdditionStrategy additionStrategy, SubtractionStrategy subtractionStrategy,
                            MultiplyStrategy multiplyStrategy, DivideStrategy divideStrategy)
 {
     this.additionStrategy    = additionStrategy;
     this.subtractionStrategy = subtractionStrategy;
     this.multiplyStrategy    = multiplyStrategy;
     this.divideStrategy      = divideStrategy;
     this.additionValue       = additionStrategy;
 }
Esempio n. 3
0
        public async Task DivideStrategy_Compute_HandlesAllPositiveValues()
        {
            DivideStrategy strategy = new DivideStrategy();
            List <double>  inputs   = new List <double> {
                1, 2, 3, 4, 5
            };
            var result = await strategy.Compute(inputs);

            Assert.AreEqual(0.008, result, 0.001); // The tolerance delta used here is due to the fact that double is used instead of decimal for speed
        }
Esempio n. 4
0
        public async Task DivideStrategy_Compute_ReturnsCorrectResult()
        {
            DivideStrategy strategy = new DivideStrategy();
            List <double>  inputs   = new List <double> {
                3, 4.2, 6
            };
            var result = await strategy.Compute(inputs);

            Assert.AreEqual(0.12, result, 0.01); // The tolerance delta used here is due to the fact that double is used instead of decimal for speed
        }
Esempio n. 5
0
        public async Task DivideStrategy_Compute_HandlesNegativeValues()
        {
            DivideStrategy strategy = new DivideStrategy();
            List <double>  inputs   = new List <double> {
                -1, 2, -4, 1, 7, 8, 12, 5730203
            };
            var result = await strategy.Compute(inputs);

            // This is a real edgecase with precision going up to E11
            Assert.AreEqual(3.2461660566284434E-11, result, 0.1); // The tolerance delta used here is due to the fact that double is used instead of decimal for speed
        }
Esempio n. 6
0
    static void Main(string[] args)
    {
        PrimitiveCalculator calculator = new PrimitiveCalculator(new AdditionStrategy());

        string input;

        while ((input = Console.ReadLine()) != "End")
        {
            string[] tokens   = input.Split();
            string   firstArg = tokens[0];

            if (firstArg == "mode")
            {
                char operand = tokens[1][0];
                ICalculationStrategy strategy = null;
                switch (operand)
                {
                case '+':
                    strategy = new AdditionStrategy();
                    break;

                case '-':
                    strategy = new SubtractionStrategy();
                    break;

                case '*':
                    strategy = new MultyplicationStrategy();
                    break;

                case '/':
                    strategy = new DivideStrategy();
                    break;
                }
                if (strategy == null)
                {
                    throw new InvalidOperationException("Invalid Mode!");
                }
                calculator.ChangeStrategy(strategy);
            }
            else
            {
                int first  = int.Parse(firstArg);
                int second = int.Parse(tokens[1]);

                int result = calculator.PerformCalculation(first, second);

                Console.WriteLine(result);
            }
        }
    }
Esempio n. 7
0
        public void DivideStrategy_Compute_HandlesNullInput()
        {
            DivideStrategy strategy = new DivideStrategy();

            Assert.ThrowsAsync <CalculatorException>(() => strategy.Compute(MockInputs.NullList));
        }