Esempio n. 1
0
        static void Main(string[] args)
        {
            var rpnEngine = new RpnEngine();
            var log       = new Logger(eLogLevel.Debug);

            while (true)
            {
                try
                {
                    Console.WriteLine("Please enter a valid RPN string to be calculated:");
                    var input = Console.ReadLine();
                    rpnEngine.CalculateRpn(input);

                    Console.WriteLine("Another? (y/n)");
                    var response = Console.ReadLine();
                    if (response == "n" || response == "N")
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    var message = $"Caught exception running.  Message was {ex.Message}.  Don't do that!";
                    log.Fatal(message);
                    Console.WriteLine(message);
                }
            }
        }
Esempio n. 2
0
        public void rpn_add_test()
        {
            var input    = "12 + 12";
            var fakeLog  = A.Fake <Logger>();
            var fakeCalc = A.Fake <Calculator>();
            var testRPN  = new RpnEngine(fakeLog, fakeCalc);

            testRPN.CalculateRpn(input);
            A.CallTo(() => fakeCalc.Add(12, 12)).MustHaveHappened();
        }
Esempio n. 3
0
        public void rpn_exp_test()
        {
            var input    = "10 ^ 5";
            var fakeLog  = A.Fake <Logger>();
            var fakeCalc = A.Fake <Calculator>();
            var testRPN  = new RpnEngine(fakeLog, fakeCalc);

            testRPN.CalculateRpn(input);
            A.CallTo(() => fakeCalc.Power(10, 5)).MustHaveHappened();
        }
        public void RpnEngine_Calls_Logger_Debug()
        {
            Calculator calc        = A.Fake <Calculator>();
            Logger     log         = A.Fake <Logger>();
            RpnEngine  myRpnEngine = new RpnEngine(calc, log);

            string input = "2 2 +";

            myRpnEngine.CalculateRpn(input);
            A.CallTo(() => log.Debug("2")).MustHaveHappened();
        }
        public void RpnEngine_BadInput_Exception()
        {
            Calculator calc        = A.Fake <Calculator>();
            Logger     log         = A.Fake <Logger>();
            RpnEngine  myRpnEngine = new RpnEngine(calc, log);

            string input = "3 4 applesauce";
            Action act   = () => myRpnEngine.CalculateRpn(input);

            act.Should().Throw <ArgumentException>();
        }
        public void RpnEngine_ValidRpnOperation_Failure()
        {
            var rpnEngine = new RpnEngine();

            var input = " 1 ! t < _";

            var rpnOperators = input.Split(' ');

            foreach (var rpnOperation in rpnOperators)
            {
                var isValidOperation = rpnEngine.ValidRpnOperation(rpnOperation);

                isValidOperation.Should().BeFalse("Because these are not valid rpn operations.");
            }
        }
        public void RpnEngine_ValidRpnOperation_Success()
        {
            var rpnEngine = new RpnEngine();

            var input = "^ * - / +";

            var rpnOperators = input.Split(' ');

            foreach (var rpnOperation in rpnOperators)
            {
                var isValidOperation = rpnEngine.ValidRpnOperation(rpnOperation);

                isValidOperation.Should().BeTrue("Because these are all valid rpn operations.");
            }
        }
        public void RpnEngine_TryParseRpnToken_Failure()
        {
            var rpnEngine = new RpnEngine();

            var input = "* * *";

            var rpnTokens = input.Split(' ');

            foreach (var rpnToken in rpnTokens)
            {
                var isDecimal = rpnEngine.TryParseDecimal(rpnToken);

                isDecimal.Should().BeFalse("It's set to false");
            }
        }
        public void RpnEngine_TryParseRpnToken_Success()
        {
            var rpnEngine = new RpnEngine();

            var input = "3 5";

            var rpnTokens = input.Split(' ');

            foreach (var rpnToken in rpnTokens)
            {
                var isDecimal = rpnEngine.TryParseDecimal(rpnToken);

                isDecimal.Should().BeTrue("It's set to true");
            }
        }
        public void RpnEngine_BadInput_Logger_Fatal()
        {
            Calculator calc        = A.Fake <Calculator>();
            Logger     log         = A.Fake <Logger>();
            RpnEngine  myRpnEngine = new RpnEngine(calc, log);

            string input = "3 4 applesauce";

            try
            {
                myRpnEngine.CalculateRpn(input);
            }
            catch (Exception e)
            {
                A.CallTo(() => log.Fatal($"Could not parse [applesauce]")).MustHaveHappened();
            }
        }
        public void RpnEngine_SplitInput_Success()
        {
            var rpnEngine = new RpnEngine();

            var input = "3 5 +";

            string[] inputArray = new string[3];
            inputArray[0] = "3";
            inputArray[1] = "5";
            inputArray[2] = "+";


            var rpnTokens = rpnEngine.SplitInput(input);

            int count = rpnTokens.Length;

            rpnTokens.Should().Equal(inputArray, "Because these should be equal");
        }
        public void RpnEngine_Power_Success()
        {
            Calculator calc        = A.Fake <Calculator>();
            Logger     log         = A.Fake <Logger>();
            RpnEngine  myRpnEngine = new RpnEngine(calc, log);

            string input  = "3 3 ^";
            var    output = new StringWriter();

            Console.SetOut(output);
            myRpnEngine.CalculateRpn(input);

            var stdout = new StreamWriter(Console.OpenStandardOutput());

            stdout.AutoFlush = true;
            Console.SetOut(stdout);
            output.ToString().Should().Be("27\r\n", "Because power...");
        }
        public void RpnEngine_CalculateLoop_Failure(params string[] operation)
        {
            var rpnEngine = new RpnEngine();
            var _stack    = new Stack <decimal>();

            var expectedValue = decimal.Parse(operation[3]);

            string[] rpnTokens = new string[3];
            for (int i = 0; i < 3; ++i)
            {
                rpnTokens[i] = operation[i];
            }



            Action calculation = () => rpnEngine.CalculateLoop(rpnTokens, _stack);

            calculation.Should().Throw <ArgumentException>();
        }
        public void RpnEngine_CalculateLoop_Success(params string[] operation)
        {
            var rpnEngine = new RpnEngine();
            var _stack    = new Stack <decimal>();

            var expectedValue = decimal.Parse(operation[3]);

            string[] rpnTokens = new string[3];
            for (int i = 0; i < 3; ++i)
            {
                rpnTokens[i] = operation[i];
            }



            var calculation = rpnEngine.CalculateLoop(rpnTokens, _stack);

            calculation.Should().Be(expectedValue);
        }