public void StackDoesntEnumerateEmpty() { //Arrange var stack = new StaticStack <string>(); var expected = false; //Act foreach (var item in stack) { expected = true; } stack.Push("one"); stack.Push("two"); stack.Push("three"); stack.Push("four"); stack.Clear(); foreach (var item in stack) { expected = true; } //Assert Assert.IsFalse(expected); }
public void StackEnumeratesCorrectly() { //Arrange var stack = new StaticStack <string>(5); var expected = new List <string>() { "five", "four", "three", "two", "one" }; //Act stack.Push("one"); stack.Push("two"); stack.Push("three"); stack.Push("four"); stack.Push("five"); var list = new List <string>(); foreach (var item in stack) { list.Add(item); } //Assert CollectionAssert.AreEqual(expected, list); }
public void StackPopWorksCorrectly() { //Arrange var stack = new StaticStack <string>(5); var expected = "three"; //Act stack.Push("one"); stack.Push("two"); stack.Push("three"); var result = stack.Pop(); //Assert Assert.AreEqual(expected, result); }
public void StackPushWorksCorrectly() { //Arrange var stack = new StaticStack <string>(5); var expected = new string[] { "three", "two", "one" }; //Act stack.Push("one"); stack.Push("two"); stack.Push("three"); var result = stack.ToArray(); //Assert CollectionAssert.AreEqual(expected, result); }
public void StackCopyToWorksCorrectly() { //Arrange var stack = new StaticStack <string>(5); var emptyArray = new string[3]; var expectedArray = new string[] { "three", "two", "one" }; //Act stack.Push("one"); stack.Push("two"); stack.Push("three"); stack.CopyTo(emptyArray, 0); //Assert CollectionAssert.AreEqual(expectedArray, emptyArray); }
public void StackCopyToWithNullArrayThrowsException() { //Arrange var stack = new StaticStack <string>(5); var emptyArray = default(string[]); //Act stack.Push("one"); stack.Push("two"); stack.Push("three"); stack.Push("four"); stack.Push("five"); stack.CopyTo(emptyArray, 0); //Assert }
public void StackCopyToWithBiggerIndexThanStackLengthThrowsException() { //Arrange var stack = new StaticStack <string>(5); var emptyArray = new string[5]; //Act stack.Push("one"); stack.Push("two"); stack.Push("three"); stack.Push("four"); stack.Push("five"); stack.CopyTo(emptyArray, 6); //Assert }
public void StackCountWorksCorrectly() { //Arrange var stack = new StaticStack <string>(5); int expected = 5; //Act stack.Push("one"); stack.Push("two"); stack.Push("three"); stack.Push("four"); stack.Push("five"); int count = stack.Count; //Assert Assert.AreEqual(expected, count); }
public void StackContainsWorksCorrectly() { //Arrange var stack = new StaticStack <string>(5); //Act stack.Push("one"); stack.Push("two"); stack.Push("three"); bool found = stack.Contains("three"); stack.Pop(); bool notFound = stack.Contains("three"); //Assert Assert.IsTrue(found); Assert.IsFalse(notFound); }
public void PushTest_PushSeveralItems() { StaticStack <int> stack = new StaticStack <int>(); for (int i = 1; i <= 5; i++) { stack.Push(i); } Assert.AreEqual(5, stack.Count); Assert.AreEqual(5, stack.Peek()); }
public void TestToString_NonEmptyStack() { StaticStack <int> stack = new StaticStack <int>(); int stackCount = 10; for (int i = 0; i < stackCount; i++) { stack.Push(i); } Assert.AreEqual("9876543210", stack.ToString()); }
public void TestPeek_NonEmptyStack() { StaticStack <int> stack = new StaticStack <int>(); int stackCount = 5; for (int i = 0; i < stackCount; i++) { stack.Push(i); } Assert.AreEqual(4, stack.Peek()); }
public MathExpression(StaticStack<MathOperation> _ExpressionStack,Dictionary<string,float> defaultVariables, string originalExpression) { m_ExpressionStack = _ExpressionStack; OriginalExpression = originalExpression; m_Variables = defaultVariables; if (m_ExpressionStack.Count == 0) m_ExpressionStack.Push(new Number(0)); if (m_ExpressionStack.Peek() is Operator) { IsImplicit = ((Operator)(m_ExpressionStack.Peek())).IsLogical; } UpdateExpressionTree(); }
public void TestPush_TenItems() { StaticStack <int> stack = new StaticStack <int>(); int stackCount = 10; for (int i = 0; i < stackCount; i++) { stack.Push(i); } Assert.AreEqual(stackCount, stack.Count); Assert.AreEqual("9876543210", stack.ToString()); }
public StaticStack<MathOperation> BuildExpressionStack(StaticStack<MathOperations.MathOperation> Stack) { if (FunctionParameters != null) foreach (var v in FunctionParameters) v.BuildExpressionStack(Stack); else { if(Left != null) Left.BuildExpressionStack(Stack); if (Right != null) Right.BuildExpressionStack(Stack); } Stack.Push(Value); return Stack; }
public void TestClear_NonEmptyStack() { StaticStack <int> stack = new StaticStack <int>(); int stackCount = 5; for (int i = 0; i < stackCount; i++) { stack.Push(i); } stack.Clear(); Assert.AreEqual(0, stack.Count); Assert.AreEqual(string.Empty, stack.ToString()); }
public void ToArrayTest_ValidStack() { StaticStack <int> stack = new StaticStack <int>(); for (int i = 1; i <= 5; i++) { stack.Push(i); } int[] arr = stack.ToArray(); for (int i = 0; i < 5; i++) { Assert.AreEqual(stack.Pop(), arr[arr.Length - 1 - i]); } }
public void PopTest_PoPSeveralItems() { StaticStack <int> stack = new StaticStack <int>(); int[] arr = new int[5]; for (int i = 0; i < 5; i++) { stack.Push(i + 1); arr[arr.Length - 1 - i] = i + 1; } for (int i = 0; i < 5; i++) { Assert.AreEqual(stack.Pop(), arr[i]); } }
public void TestClone_EmptyStack() { StaticStack <int> stack = new StaticStack <int>(); StaticStack <int> clone = (StaticStack <int>)stack.Clone(); int count = 5; for (int i = 0; i < count; i++) { stack.Push(i); } Assert.AreEqual(5, stack.Count); Assert.AreEqual(0, clone.Count); Assert.AreEqual(string.Empty, clone.ToString()); }
public void TestPop_OneItem() { StaticStack <int> stack = new StaticStack <int>(); int stackCount = 5; for (int i = 0; i < stackCount; i++) { stack.Push(i); } int popped = stack.Pop(); Assert.AreEqual(4, popped); Assert.AreEqual(4, stack.Count); Assert.AreEqual("3210", stack.ToString()); }
public void TestPop_MultipleItems() { StaticStack <int> stack = new StaticStack <int>(); int stackCount = 10; for (int i = 0; i < stackCount; i++) { stack.Push(i); } for (int i = 0; i < 5; i++) { int popped = stack.Pop(); Assert.AreEqual(9 - i, popped); } Assert.AreEqual(5, stack.Count); Assert.AreEqual("43210", stack.ToString()); }
public void TestForeachLoop_Enumerator() { StaticStack <int> stack = new StaticStack <int>(); int stackCount = 5; for (int i = 0; i < stackCount; i++) { stack.Push(i); } string expected = "43210"; StringBuilder actual = new StringBuilder(); foreach (var item in stack) { actual.Append(item.ToString()); } Assert.AreEqual(expected, actual.ToString()); }
public void TestClone_NonEmptyStack() { StaticStack <int> stack = new StaticStack <int>(); int stackCount = 5; for (int i = 0; i < stackCount; i++) { stack.Push(i); } StaticStack <int> clone = (StaticStack <int>)stack.Clone(); for (int i = 0; i < stackCount; i++) { stack.Pop(); } Assert.AreEqual(0, stack.Count); Assert.AreEqual(5, clone.Count); Assert.AreEqual("43210", clone.ToString()); }
public override void DeriveToStack(ExpressionTree cur, MathState State, StaticStack<MathOperation> DeriveStack) { DeriveStack.Push((Number)(_name.ToLower() == "x" ? 1 : 0)); }
public override void CopyToStack(ExpressionTree cur, StaticStack<MathOperation> Stack) { Stack.Push(this); }
public override void DeriveToStack(ExpressionTree cur, MathState State, StaticStack<MathOperation> DeriveStack) { DeriveStack.Push((Number)0); }
public StaticStack<MathOperation> GetExpressionStack(StaticStack<MathOperations.MathOperation> Stack) { if (FunctionParameters != null) foreach (var v in FunctionParameters) v.GetExpressionStack(Stack); else { Stack.Push(Left.Value); Stack.Push(Right.Value); } Stack.Push(Value); return Stack; }
public override void DeriveToStack(ExpressionTree cur, MathState State, StaticStack<MathOperation> DeriveStack) { cur.Left.DeriveToStack(DeriveStack, State); cur.Right.DeriveToStack(DeriveStack, State); DeriveStack.Push(this); }
public override void DeriveToStack(ExpressionTree cur, MathState State, StaticStack<MathOperation> DeriveStack) { //f(x) = u(v(x)) //f'(x) = u'(v(x))*v'(x) // v u' v' * if(ArgumentCount != 1) throw new MathEvaluator.Exceptions.UndefinedResultException(ToString() + " is not derivable"); ExpressionTree inner = cur.FunctionParameters[0]; CopyDerivedFunctionToStack(cur,inner, DeriveStack,State); inner.DeriveToStack(DeriveStack,State); DeriveStack.Push(new Multiplication()); }
public override void CopyDerivedFunctionToStack(ExpressionTree cur,ExpressionTree inner, StaticStack<MathOperation> DeriveStack,MathState State) { inner.CopyToStack(DeriveStack); DeriveStack.Push(State.GetFunction("cos")); }
public override void DeriveToStack(ExpressionTree cur, MathState State, StaticStack<MathOperation> DeriveStack) { //(u * v)' = (u'v - uv')/v² //PF: = u' v * u v' * - v 2 ^ / ExpressionTree left = cur.Left; ExpressionTree right = cur.Right; left.DeriveToStack(DeriveStack,State); right.CopyToStack(DeriveStack); DeriveStack.Push(State.GetOperator("*")); left.CopyToStack(DeriveStack); right.DeriveToStack(DeriveStack,State); DeriveStack.Push(State.GetOperator("*"), State.GetOperator("-")); right.CopyToStack(DeriveStack); DeriveStack.Push((Number)2, State.GetOperator("^"), State.GetOperator("/")); }
public override void CopyDerivedFunctionToStack(ExpressionTree cur, ExpressionTree inner, StaticStack<MathOperation> DeriveStack, MathState State) { //ln(x)' = 1/x // 1 x / DeriveStack.Push((Number)1); inner.CopyToStack(DeriveStack); DeriveStack.Push(State.GetOperator("/")); }
public override void DeriveToStack(ExpressionTree cur, MathState State, StaticStack<MathOperation> DeriveStack) { // (f(x)^g(x))' // f(x)^(g(x)-1) (g(x) f'(x)+f(x) ln(f(x)) g'(x)) // f(x) g(x) 1 - ^ g(x) f'(x) * f(x) f(x) ln * + * g'(x) * // f g 1 - ^ g v * f f ln * b * + * ExpressionTree f = cur.Left; ExpressionTree g = cur.Right; f.CopyToStack(DeriveStack); g.CopyToStack(DeriveStack); DeriveStack.Push((Number)1, State.GetOperator("-"), State.GetOperator("^")); g.CopyToStack(DeriveStack); f.DeriveToStack(DeriveStack,State); DeriveStack.Push(State.GetOperator("*")); f.CopyToStack(DeriveStack); f.CopyToStack(DeriveStack); DeriveStack.Push(State.GetFunction("ln"), State.GetOperator("*")); g.DeriveToStack(DeriveStack,State); DeriveStack.Push(State.GetOperator("*"), State.GetOperator("+"), State.GetOperator("*")); /* if (cur.Left.ContainsVariable("x")) { //(x^n)' = n * x^(n-1) ExpressionTree left = cur.Left; ExpressionTree right = cur.Right; right.CopyToStack(DeriveStack); left.CopyToStack(DeriveStack); right.CopyToStack(DeriveStack); DeriveStack.Push((Number)1,State.GetOperator("-"),State.GetOperator("^"),State.GetOperator("*")); } else if (cur.Right.ContainsVariable("x")) { // (a^x)' = a^x * ln a // a x ^ a ln * ExpressionTree left = cur.Left; ExpressionTree right = cur.Right; left.CopyToStack(DeriveStack); right.CopyToStack(DeriveStack); DeriveStack.Push(State.GetOperator("^")); left.CopyToStack(DeriveStack); DeriveStack.Push(State.GetFunction("ln"), State.GetOperator("*")); //chain rule right.DeriveToStack(DeriveStack, State); DeriveStack.Push(State.GetOperator("*")); } else if (cur.Left.ToString() == "e" && cur.Right.ToString() == "x") { cur.Left.CopyToStack(DeriveStack); cur.Right.CopyToStack(DeriveStack); DeriveStack.Push(this); } else { DeriveStack.Push((Number)0); }*/ }
public override void CopyToStack(ExpressionTree cur, StaticStack<MathOperation> Stack) { foreach (var b in cur.FunctionParameters) b.CopyToStack(Stack); Stack.Push(this); }
public override void DeriveToStack(ExpressionTree cur, MathState State, StaticStack<MathOperation> DeriveStack) { //(u * v)' = u' * v + u * v' //PF: = u' v * u v' * + ExpressionTree left = cur.Left; ExpressionTree right = cur.Right; left.DeriveToStack(DeriveStack,State); right.CopyToStack(DeriveStack); DeriveStack.Push(State.GetOperator("*")); left.CopyToStack(DeriveStack); right.DeriveToStack(DeriveStack,State); DeriveStack.Push(State.GetOperator("*"), State.GetOperator("+")); }
public override void CopyDerivedFunctionToStack(ExpressionTree cur, ExpressionTree inner, StaticStack<MathOperation> DeriveStack,MathState State) { inner.CopyToStack(DeriveStack); DeriveStack.Push(State.GetFunction("sin"), (Number)(-1), State.GetOperator("*")); }
public override void CopyDerivedFunctionToStack(ExpressionTree cur, ExpressionTree inner, StaticStack<MathOperation> DeriveStack, MathState State) { //sqrt(x)' = 1/(2*sqrt(x)) // 1 2 x sqrt * / DeriveStack.Push((Number)1,(Number)2); inner.CopyToStack(DeriveStack); DeriveStack.Push(State.GetFunction("sqrt"), State.GetOperator("*"), State.GetOperator("/")); }
public StaticStack<MathOperation> Convert(string expression) { StaticStack<MathOperation> Output = new StaticStack<MathOperation>(); Stack<MathOperation> OperatorStack = new Stack<MathOperation>(); NumberState NumberState = NumberState.None; float CommaMultiplier = 0; float minus = 1; bool useMinus = true; for (int i = 0; i < expression.Length; i++) { char token = expression[i]; if (char.IsWhiteSpace(token)) { //fixes 5 2 = 52 //new bug: 5 2 + = 7 (infix) NumberState = NumberState.None; continue; } if(token == '-') { if (useMinus) { minus = -1; continue; } } if (char.IsDigit(token)) { float numeric = (float)char.GetNumericValue(token); switch (NumberState) { case NumberState.None: Output.Push((Number)(numeric * minus)); NumberState = NumberState.Number; break; case NumberState.Number: float num = (Number)Output.PopReal(); Output.Push((Number)(num * 10.0 + numeric * NonNullSign(num))); break; case NumberState.Comma: float num2 = (Number)Output.PopReal(); Output.Push((Number)(num2 + numeric * CommaMultiplier * NonNullSign(num2))); CommaMultiplier *= 0.1f; break; default: throw new NotImplementedException(); } minus = 1; useMinus = false; } else if (token == '.') { minus = 1; NumberState = NumberState.Comma; CommaMultiplier = 0.1f; } else if (token == ',') { NumberState = NumberState.None; if (OperatorStack.Count == 0) throw new MismatchedParenthesisException("\"" + expression.Substring(i) + "\": Can't find opening parenthesis"); while (OperatorStack.Count > 0) { if (OperatorStack.Peek() is Parenthesis) break; Output.Push(OperatorStack.Pop()); } if (OperatorStack.Count == 0 || !(OperatorStack.Peek() is Parenthesis)) throw new MismatchedParenthesisException("\"" + expression.Substring(i) + "\": Can't find opening parenthesis"); } else if (token == ')') { if (OperatorStack.Count == 0) throw new MismatchedParenthesisException("\"" + expression.Substring(i) + "\": Can't find opening parenthesis"); while (OperatorStack.Count > 0) { MathOperation TopOperator = OperatorStack.Pop(); if (TopOperator is Parenthesis) break; else Output.Push(TopOperator); if (OperatorStack.Count == 0) throw new MismatchedParenthesisException("\"" + expression.Substring(i) + "\": Can't find opening parenthesis"); } if (OperatorStack.Count > 0 && OperatorStack.Peek() is Function) Output.Push(OperatorStack.Pop()); useMinus = false; } else if (char.IsSymbol(token) || char.IsPunctuation(token)) { string fullname = ""; for (; i < expression.Length; i++) { char letter = expression[i]; if ((char.IsSymbol(letter) || char.IsPunctuation(letter))) //we cant use letters or 5 + sin(x) is going to break fullname += char.ToLower(letter); else { --i; break; } if (m_Operators.ContainsKey(fullname)) break; } if (m_Operators.ContainsKey(fullname)) { MathOperation TokenOperator = m_Operators[fullname]; if (NumberState != MathState.NumberState.None) { NumberState = NumberState.None; if (TokenOperator is Parenthesis) { // makes 5(x) possible OperatorStack.Push(new MathOperations.Operators.Multiplication()); } } while (OperatorStack.Count > 0) { MathOperation TopOperator = OperatorStack.Peek(); if (TokenOperator.Association == Association.Left && TokenOperator.Precedence <= TopOperator.Precedence) Output.Push(OperatorStack.Pop()); else if (TokenOperator.Association == Association.Right && TokenOperator.Precedence < TopOperator.Precedence) Output.Push(OperatorStack.Pop()); else break; } OperatorStack.Push(TokenOperator); } useMinus = true; } else if (char.IsLetter(token)) { minus = 1; string fullname = ""; for (; i < expression.Length; i++) { char letter = expression[i]; if (char.IsLetter(letter)) fullname += char.ToLower(letter); else { --i; break; } } if (NumberState != MathState.NumberState.None) { NumberState = NumberState.None; OperatorStack.Push(new MathOperations.Operators.Multiplication()); } if (m_Functions.ContainsKey(fullname)) OperatorStack.Push(m_Functions[fullname]); else if (m_Variables.ContainsKey(fullname)) { Output.Push(new Variable(fullname, m_Variables)); } else if (m_Constants.ContainsKey(fullname)) { Output.Push(new Constant(fullname, m_Constants[fullname])); } else if (DefineUnknownVariables) Output.Push(new Variable(fullname,m_Variables)); else throw new UndefinedFunctionException("Function \"" + fullname + "\" not defined"); useMinus = false; } else { throw new NotSupportedException(); } } while (OperatorStack.Count > 0) { if (OperatorStack.Peek() is Parenthesis) throw new MismatchedParenthesisException("Can't find closing parenthesis"); Output.Push(OperatorStack.Pop()); } return Output; }
public override void CopyDerivedFunctionToStack(ExpressionTree cur, ExpressionTree inner, StaticStack<MathOperation> DeriveStack, MathState State) { //sqrt(x)' = 1/(x * ln 10) // 1 x 10 ln * / DeriveStack.Push((Number)1); inner.CopyToStack(DeriveStack); DeriveStack.Push((Number)10,State.GetFunction("ln"),State.GetOperator("*"), State.GetOperator("/")); }
public override void CopyToStack(ExpressionTree cur, StaticStack<MathOperation> Stack) { cur.Left.CopyToStack(Stack); cur.Right.CopyToStack(Stack); Stack.Push(this); }