static void Main(string[] args) { var mathOperationsContainer = new MathOperationsContainer(); var parser = new Parser(); var notationConverter = new NotationConverter(); var mathProcessor = new MathProcessor(); var calculator = new MathCalculator(parser, notationConverter, mathProcessor, mathOperationsContainer); Console.WriteLine("Calculator"); Console.WriteLine("To complete the work, type exit."); while (true) { Console.Write("Enter a math expression: "); var expression = Console.ReadLine(); if (expression == "exit") { Console.WriteLine("Work completed."); break; } var result = calculator.Calculate(expression); Console.WriteLine("Result: {0}", result); } }
public void MathProcessor_returns_result_of_digit_with_comma() { var mathProcessor = new MathProcessor(); var inputExpression = new List <ExpressionUnit>() { new NumberExpressionUnit("23,50", "23.50") }; var result = mathProcessor.Process(inputExpression, _mathOperationsContainer); Assert.Equal(23.50, result); }
public void MathProcessor_returns_one_number() { var mathProcessor = new MathProcessor(); var inputExpression = new List <ExpressionUnit>() { new NumberExpressionUnit("2.00") }; var result = mathProcessor.Process(inputExpression, _mathOperationsContainer); Assert.Equal(2.00, result); }
public void MathProcessor_returns_result_of_digit_with_unary_minus() { var mathProcessor = new MathProcessor(); var inputExpression = new List <ExpressionUnit>() { new NumberExpressionUnit("8.00"), new OperationExpressionUnit("-", "~") }; var result = mathProcessor.Process(inputExpression, _mathOperationsContainer); Assert.Equal(-8.00, result); }
public void MathProcessor_returns_result_of_div_operation() { var mathProcessor = new MathProcessor(); var inputExpression = new List <ExpressionUnit>() { new NumberExpressionUnit("6.00"), new NumberExpressionUnit("2.00"), new OperationExpressionUnit("/") }; var result = mathProcessor.Process(inputExpression, _mathOperationsContainer); Assert.Equal(3.00, result); }
public void MathProcessor_returns_result_of_add_operation() { var mathProcessor = new MathProcessor(); var inputExpression = new List <ExpressionUnit>() { new NumberExpressionUnit("2.50"), new NumberExpressionUnit("3.25"), new OperationExpressionUnit("+") }; var result = mathProcessor.Process(inputExpression, _mathOperationsContainer); Assert.Equal(5.75, result); }
public void SubtractWrapAroundTest() { // given MathProcessor byte[] bytes = new byte[] { 5, 11, 12, 13, 14, 15, 16, 17, 18, 19 }; EveryNthByteIndexProvider byteRule = new EveryNthByteIndexProvider(new ByteRange(0, (uint)bytes.Length), 1); MathProcessor proc = new MathProcessor(byteRule, new FixedByteProvider(10), Operation.Subtract, true); // when: applying the processor var actual = proc.Apply(bytes); // then: correctly modified byte[] expected = new byte[] { 251, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; CollectionAssert.AreEqual(expected, actual); }
public void MathProcessor_should_throw_custom_exception_for_unsupported_operation() { var mathProcessor = new MathProcessor(); var inputExpression = new List <ExpressionUnit>() { new NumberExpressionUnit("3.00"), new NumberExpressionUnit("2.00"), new OperationExpressionUnit("$") }; Action action = () => mathProcessor.Process(inputExpression, _mathOperationsContainer); var exception = Assert.Throws <ArgumentException>(action); Assert.Equal("Invalid mathematical expression or unsupported operation.", exception.Message); }
public void MathProcessor_returns_divide_by_zero_exception() { var mathProcessor = new MathProcessor(); var inputExpression = new List <ExpressionUnit>() { new NumberExpressionUnit("3.00"), new NumberExpressionUnit("0.00"), new OperationExpressionUnit("/") }; Action action = () => mathProcessor.Process(inputExpression, _mathOperationsContainer); var exception = Assert.Throws <DivideByZeroException>(action); Assert.Equal("You cannot divide by zero.", exception.Message); }
public void MultipleProcessorTest() { // given: glitcher configuration with multiple processors byte[] bytes = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; GlitcherConfiguration config = new GlitcherConfiguration(); BitShiftProcessor bitShiftProcessor = new BitShiftProcessor(new EveryNthByteIndexProvider(new ByteRange(0, 5), 1), new FixedByteProvider(1), ShiftDirection.Left); MathProcessor mathProcessor = new MathProcessor(new EveryNthByteIndexProvider(new ByteRange(0, (uint)bytes.Length), 1), new FixedByteProvider(1), Operation.Add, false); config.ProcessorChain.Add(bitShiftProcessor); config.ProcessorChain.Add(mathProcessor); // when: glitching byte[] actual = Glitcher.GlitchBytes(bytes, config); // then: bytes are correct byte[] expected = new byte[] { 1, 3, 5, 7, 9, 6, 7, 8, 9, 10 }; CollectionAssert.AreEqual(expected, actual); }
public static void Main() { Console.WriteLine("Please, type in an arithmetic expression:"); try { var input = Console.ReadLine(); if (string.IsNullOrEmpty(input)) { Console.WriteLine("The input string is empty!"); return; } decimal result = MathProcessor.Calculate(input); Console.WriteLine($"Expression result: {result}"); } catch (Exception ex) { Console.WriteLine(ex.Message); return; } }
public void MathProcessor_should_be() { var mathProcessor = new MathProcessor(); }
public void ModuloBehaviourUndefinedException() { Assert.Throws <ModuloBehaviourUndefinedException>(() => MathProcessor.Calculate("6%0")); }
public void DivideByZeroException() { Assert.Throws <DivideByZeroException>(() => MathProcessor.Calculate("1/0")); }
public void MismatchedParenthesisException(string expression) { Assert.Throws <ExpressionMismatchedParenthesisException>(() => MathProcessor.Calculate(expression)); }
public void CalculateValues(string expression, decimal expected) { decimal actual = MathProcessor.Calculate(expression); Assert.AreEqual(expected, actual); }
public void ConvertFromInfixToPostfixNotation(string expression, string[] expected) { string[] actual = MathProcessor.ConvertExpressionToPostfixNotation(expression); Assert.AreEqual(expected, actual); }
public void ExpressionInvalidCharsException(string expression) { Assert.Throws <ExpressionInvalidCharsException>(() => MathProcessor.Calculate(expression)); }