public void ConvertShouldConvertToReversePolishNotation()
        {
            var input = new[] // 3 + 4 * 2 / (1 - 5)^2
            {
                InputCell.Number(3),
                InputCell.Symbol("+"),
                InputCell.Number(4),
                InputCell.Symbol("*"),
                InputCell.Number(2),
                InputCell.Symbol("/"),
                InputCell.Symbol("("),
                InputCell.Number(1),
                InputCell.Symbol("-"),
                InputCell.Number(5),
                InputCell.Symbol(")"),
                InputCell.Symbol("^"),
                InputCell.Number(2),
            };

            var result = new RpnConverter(new InputChecker()).Convert(input);

            // 3 4 2 * 1 5 - 2 ^ / +

            Assert.AreEqual(3, result[0].Value);
            Assert.AreEqual(4, result[1].Value);
            Assert.AreEqual(2, result[2].Value);
            Assert.AreEqual("*", result[3].Value);
            Assert.AreEqual(1, result[4].Value);
            Assert.AreEqual(5, result[5].Value);
            Assert.AreEqual("-", result[6].Value);
            Assert.AreEqual(2, result[7].Value);
            Assert.AreEqual("^", result[8].Value);
            Assert.AreEqual("/", result[9].Value);
            Assert.AreEqual("+", result[10].Value);
        }
Example #2
0
 public void IsOperationShouldReturnTrue()
 {
     Assert.IsTrue(InputCell.Symbol("+").IsOperation());
     Assert.IsTrue(InputCell.Symbol("-").IsOperation());
     Assert.IsTrue(InputCell.Symbol("*").IsOperation());
     Assert.IsTrue(InputCell.Symbol("/").IsOperation());
     Assert.IsTrue(InputCell.Symbol("^").IsOperation());
 }
Example #3
0
 public void WeightShouldReturnCorrectWeigth()
 {
     Assert.AreEqual(0, InputCell.Number(1).Weight);
     Assert.AreEqual(1, InputCell.Symbol(")").Weight);
     Assert.AreEqual(1, InputCell.Symbol("(").Weight);
     Assert.AreEqual(2, InputCell.Symbol("+").Weight);
     Assert.AreEqual(2, InputCell.Symbol("-").Weight);
     Assert.AreEqual(3, InputCell.Symbol("*").Weight);
     Assert.AreEqual(3, InputCell.Symbol("/").Weight);
     Assert.AreEqual(4, InputCell.Symbol("^").Weight);
 }
        public void CheckShouldThrowExceptionIfThereIsOpeartionsInLastCell()
        {
            var input = new[]
            {
                InputCell.Number(2),
                InputCell.Symbol("+"),
                InputCell.Number(2),
                InputCell.Symbol("+")
            };

            Assert.Throws <ArgumentException>(() =>
                                              new InputChecker().Check(input));
        }
        public void CountShouldReturn3Dot5()
        {
            // 3 + 4 * 2 / (1 - 5)^2 = 3.5
            // 3 4 2 * 1 5 - 2 ^ / +
            var input = new[]
            {
                InputCell.Number(3),
                InputCell.Number(4),
                InputCell.Number(2),
                InputCell.Symbol("*"),
                InputCell.Number(1),
                InputCell.Number(5),
                InputCell.Symbol("-"),
                InputCell.Number(2),
                InputCell.Symbol("^"),
                InputCell.Symbol("/"),
                InputCell.Symbol("+")
            };

            var result = new RpnCounter().Count(input);

            Assert.AreEqual(3.5, result);
        }
Example #6
0
 public void SymbolShouldTrowExceptionInputContainsInvalidSymbols()
 {
     Assert.Throws <ArgumentException>(() => InputCell.Symbol("$"));
     Assert.Throws <ArgumentException>(() => InputCell.Symbol("1"));
     Assert.Throws <ArgumentException>(() => InputCell.Symbol("a"));
 }
Example #7
0
 public void IsNumberShouldReturnFalse()
 {
     Assert.IsFalse(InputCell.Symbol(")").IsNumber());
 }
Example #8
0
 public void IsOperationShouldReturnFalse()
 {
     Assert.IsFalse(InputCell.Symbol("(").IsOperation());
     Assert.IsFalse(InputCell.Symbol(")").IsOperation());
 }