コード例 #1
0
        public void NonEqualPrecedence(CalcOperatorType op1, CalcOperatorType op2)
        {
            CalcOperator calcop1 = new CalcOperator(op1);
            CalcOperator calcop2 = new CalcOperator(op2);

            Assert.AreNotEqual(calcop1.Precedence, calcop2.Precedence);
        }
コード例 #2
0
ファイル: CalcOperator.cs プロジェクト: liamday359/EQTest
        public CalcOperator(string token)
        {
            switch (token.Trim().ToUpper())
            {
            case "+":
                OperatorType = CalcOperatorType.Addition;
                break;

            case "-":
                OperatorType = CalcOperatorType.Subtraction;
                break;

            case "/":
                OperatorType = CalcOperatorType.Division;
                break;

            case "*":
            case "X":
                OperatorType = CalcOperatorType.Multiplication;
                break;

            default:
                throw new Exception("Invalid operator token.");
            }
        }
コード例 #3
0
        [TestCase(-2, CalcOperatorType.Division, 3, 2, -0.67)]  // 1 / 6
        public void Precision(double op1, CalcOperatorType calcType, double op2, int precision, double expected)
        {
            var sut = new CalculatorRPN();

            sut.AddNumber(op1);
            sut.AddOperator(new CalcOperator(calcType));
            sut.AddNumber(op2);

            Assert.AreEqual(expected, sut.Value(precision));
        }
コード例 #4
0
        [TestCase(6, CalcOperatorType.Division, 0.5, 12)]        // 6 / 0.5
        public void TwoOperands(double op1, CalcOperatorType calcType, double op2, double expected)
        {
            var sut = new CalculatorRPN();

            sut.AddNumber(op1);
            sut.AddOperator(new CalcOperator(calcType));
            sut.AddNumber(op2);

            Assert.AreEqual(expected, sut.Value());
        }
コード例 #5
0
ファイル: ParserTests.cs プロジェクト: liamday359/EQTest
        public void TwoOperands(string parseString, double op1, CalcOperatorType calcType, double op2)
        {
            var sut       = new Parser();
            var tokenList = sut.Parse(parseString);

            Assert.AreEqual(3, tokenList.Count);
            Assert.AreEqual(op1, tokenList[0]);
            Assert.AreEqual(calcType, ((CalcOperator)tokenList[1]).OperatorType);
            Assert.AreEqual(op2, tokenList[2]);
        }
コード例 #6
0
        [TestCase(3, CalcOperatorType.Addition)] // 3 +
        public void SingleOperandException(double op1, CalcOperatorType calcType)
        {
            var sut = new CalculatorRPN();

            sut.AddNumber(op1);
            sut.AddOperator(new CalcOperator(calcType));

            double x;
            var    ex = Assert.Throws <TokenException>(() => x = sut.Value());

            Assert.AreEqual("Not enough values to perform operation.", ex.Message);
        }
コード例 #7
0
ファイル: ParserTests.cs プロジェクト: liamday359/EQTest
        public void ThreeOperands(string parseString, double op1, CalcOperatorType calcType1, double op2, CalcOperatorType calcType2, double op3)
        {
            var sut       = new Parser();
            var tokenList = sut.Parse(parseString);

            Assert.AreEqual(5, tokenList.Count);
            Assert.AreEqual(op1, tokenList[0]);
            Assert.AreEqual(calcType1, ((CalcOperator)tokenList[1]).OperatorType);
            Assert.AreEqual(op2, tokenList[2]);
            Assert.AreEqual(calcType2, ((CalcOperator)tokenList[3]).OperatorType);
            Assert.AreEqual(op3, tokenList[4]);
        }
コード例 #8
0
        [TestCase(3, CalcOperatorType.Addition)] // 3 +
        public void SingleOperandExceptionAPI(double op1, CalcOperatorType calcType)
        {
            var sut = new CalculatorAPI();

            sut.AddNumber(op1);
            sut.AddOperator(new CalcOperator(calcType));

            double x;
            var    ex = Assert.Throws <System.AggregateException>(() => x = sut.Value());

            Assert.AreEqual("One or more errors occurred. (Response status code does not indicate success: 400 (Bad Request).)", ex.Message);
        }
コード例 #9
0
        [TestCase(10, CalcOperatorType.Subtraction, 4, CalcOperatorType.Division, 2, 8)]   // 10 - 4 / 2
        public void ThreeOperandsException(double op1, CalcOperatorType calcType1, double op2, CalcOperatorType calcType2, double op3, double result)
        {
            var sut = new CalculatorAPI();

            sut.AddNumber(op1);
            sut.AddOperator(new CalcOperator(calcType1));
            sut.AddNumber(op2);

            var ex = Assert.Throws <CalculatorException>(() => sut.AddOperator(new CalcOperator(calcType2)));

            Assert.AreEqual("API does not support BEDMAS processing.", ex.Message);
        }
コード例 #10
0
        [TestCase(1, CalcOperatorType.Multiplication, 4, CalcOperatorType.Addition, 5, 9)] // 1 * 4 + 5
        public void ThreeOperandsAPI(double op1, CalcOperatorType calcType1, double op2, CalcOperatorType calcType2, double op3, double expected)
        {
            var sut = new CalculatorAPI();

            sut.AddNumber(op1);
            sut.AddOperator(new CalcOperator(calcType1));
            sut.AddNumber(op2);
            sut.AddOperator(new CalcOperator(calcType2));
            sut.AddNumber(op3);

            Assert.AreEqual(expected, sut.Value());
        }
コード例 #11
0
        [TestCase(3, CalcOperatorType.Addition, 2, 1)] // 3 + 2 1
        public void TooManyOperandException(double op1, CalcOperatorType calcType, double op2, double op3)
        {
            var sut = new CalculatorRPN();

            sut.AddNumber(op1);
            sut.AddOperator(new CalcOperator(calcType));
            sut.AddNumber(op2);
            sut.AddNumber(op3);

            double x;
            var    ex = Assert.Throws <TokenException>(() => x = sut.Value());

            Assert.AreEqual("Too many remaining operands.", ex.Message);
        }
コード例 #12
0
ファイル: CalcOperator.cs プロジェクト: liamday359/EQTest
 public CalcOperator(CalcOperatorType typeIn)
 {
     OperatorType = typeIn;
 }
コード例 #13
0
        public void ParsedOperator(string operatorString, CalcOperatorType op)
        {
            CalcOperator calcOp = new CalcOperator(operatorString);

            Assert.AreEqual(calcOp.OperatorType, op);
        }