public void TestIncompleteOperatorExpression() { const string testExpression = "1+"; var testTree = new ExpTree(testExpression); var isValidExpression = testTree.IsValidExpression(); var value = testTree.Eval(); Console.WriteLine("TestNonsenseStringExpression:"); Console.WriteLine(" ExpTree.IsValidExpression() should be false"); Console.WriteLine(" IsValidExpression() returned {0}", isValidExpression); Console.WriteLine(" Expected value: 0"); Console.WriteLine(" Actual value: {0}", value); Assert.IsFalse(isValidExpression); Assert.AreEqual(value, 0.0d); }
public void TestSingleConstantExpression() { const string testExpression = "pi"; var testTree = new ExpTree(testExpression); var isValidExpression = testTree.IsValidExpression(); var value = testTree.Eval(); Console.WriteLine("TestSingleConstantExpression:"); Console.WriteLine(" ExpTree.IsValidExpression() should be true"); Console.WriteLine(" IsValidExpression() returned {0}", isValidExpression); Console.WriteLine(" Expected value: 3.14159265358979"); Console.WriteLine(" Actual value: {0}", value); Assert.IsTrue(isValidExpression); Assert.AreEqual(value, Math.PI); }
public void TestCosineFunctionExpression() { const string testExpression = "COS(2)*2"; var testTree = new ExpTree(testExpression); var isValidExpression = testTree.IsValidExpression(); var value = testTree.Eval(); Console.WriteLine("TestCosineFunctionExpression:"); Console.WriteLine(" ExpTree.IsValidExpression() should be true"); Console.WriteLine(" IsValidExpression() returned {0}", isValidExpression); Console.WriteLine(" Expected value: -0.832293673094285"); Console.WriteLine(" Actual value: {0}", value); Assert.IsTrue(isValidExpression); Assert.AreEqual(value, (Math.Cos(2) * 2)); }
public void TestLogFunctionExpression() { const string testExpression = "LOG(10^2,10)"; var testTree = new ExpTree(testExpression); var isValidExpression = testTree.IsValidExpression(); var value = testTree.Eval(); Console.WriteLine("TestLogFunctionExpression:"); Console.WriteLine(" ExpTree.IsValidExpression() should be true"); Console.WriteLine(" IsValidExpression() returned {0}", isValidExpression); Console.WriteLine(" Expected value: 2"); Console.WriteLine(" Actual value: {0}", value); Assert.IsTrue(isValidExpression); Assert.AreEqual(value, (Math.Log(100, 5 + 5))); }
public void TestWhitespaceSeparatedExpression() { const string testExpression = " 1 + 1 "; var testTree = new ExpTree(testExpression); var isValidExpression = testTree.IsValidExpression(); var value = testTree.Eval(); Console.WriteLine("TestWhitespaceSeparatedExpression:"); Console.WriteLine(" ExpTree.IsValidExpression() should be true"); Console.WriteLine(" IsValidExpression() returned {0}", isValidExpression); Console.WriteLine(" Expected value: 2"); Console.WriteLine(" Actual value: {0}", value); Assert.IsTrue(isValidExpression); Assert.AreEqual(value, (1 + 1)); }
public void TestSineFunctionExpression() { const string testExpression = "SIN(1+1)"; var testTree = new ExpTree(testExpression); var isValidExpression = testTree.IsValidExpression(); var value = testTree.Eval(); Console.WriteLine("TestSineFunctionExpression:"); Console.WriteLine(" ExpTree.IsValidExpression() should be true"); Console.WriteLine(" IsValidExpression() returned {0}", isValidExpression); Console.WriteLine(" Expected value: 0.909297426825682"); Console.WriteLine(" Actual value: {0}", value); Assert.IsTrue(isValidExpression); Assert.AreEqual(value, (Math.Sin(1 + 1))); }
public void TestParentheseExpression() { const string testExpression = "(2+2)^(4/2)"; var testTree = new ExpTree(testExpression); var isValidExpression = testTree.IsValidExpression(); var value = testTree.Eval(); Console.WriteLine("TestParentheseExpression:"); Console.WriteLine(" ExpTree.IsValidExpression() should be true"); Console.WriteLine(" IsValidExpression() returned {0}", isValidExpression); Console.WriteLine(" Expected value: 16"); Console.WriteLine(" Actual value: {0}", value); Assert.IsTrue(isValidExpression); Assert.AreEqual(value, Math.Pow((2 + 2), (4d / 2d))); }
public void TestExponentiateExpression() { const string testExpression = "2^4"; var testTree = new ExpTree(testExpression); var isValidExpression = testTree.IsValidExpression(); var value = testTree.Eval(); Console.WriteLine("TestExponentiateExpression:"); Console.WriteLine(" ExpTree.IsValidExpression() should be true"); Console.WriteLine(" IsValidExpression() returned {0}", isValidExpression); Console.WriteLine(" Expected value: 16"); Console.WriteLine(" Actual value: {0}", value); Assert.IsTrue(isValidExpression); Assert.AreEqual(value, (Math.Pow(2, 4))); }
public void TestDivideExpression() { const string testExpression = "3/2"; var testTree = new ExpTree(testExpression); var isValidExpression = testTree.IsValidExpression(); var value = testTree.Eval(); Console.WriteLine("TestDividExpression:"); Console.WriteLine(" ExpTree.IsValidExpression() should be true"); Console.WriteLine(" IsValidExpression() returned {0}", isValidExpression); Console.WriteLine(" Expected value: 1.5"); Console.WriteLine(" Actual value: {0}", value); Assert.IsTrue(isValidExpression); Assert.AreEqual(value, (3d / 2d)); }
public void TestSingleVariableExpression() { const string testVarName = "a1"; const double testVarValue = 123.45d; const string testExpression = testVarName; var testTree = new ExpTree(testExpression); testTree.SetVar(testVarName, testVarValue); var isValidExpression = testTree.IsValidExpression(); var value = testTree.Eval(); Console.WriteLine("TestSingleVariableExpression:"); Console.WriteLine(" ExpTree.IsValidExpression() should be true"); Console.WriteLine(" IsValidExpression() returned {0}", isValidExpression); Console.WriteLine(" Expected value: 123.45"); Console.WriteLine(" Actual value: {0}", value); Assert.IsTrue(isValidExpression); Assert.AreEqual(value, testVarValue); }
//===================================================================== // Menu3() -- evaluate the expression tree //===================================================================== private static void Menu3() { if (null != _expTree && null != _expStr) { if (_expTree.IsValidExpression()) { string result = _expTree.Eval().ToString(); //Console.WriteLine(_expStr + " = " + result); Console.WriteLine(result); } else { Console.WriteLine("Invalid expression"); } } else { Console.WriteLine("No expresion entered"); } }
public void TestMultivariableExpression() { const string testVarName1 = "a1"; const string testVarName2 = "Z50"; const double testVarValue1 = 123.45d; const double testVarValue2 = double.MaxValue; const string testExpression = "a1-Z50"; var testTree = new ExpTree(testExpression); testTree.SetVar(testVarName1, testVarValue1); testTree.SetVar(testVarName2, testVarValue2); var isValidExpression = testTree.IsValidExpression(); var value = testTree.Eval(); Assert.IsTrue(isValidExpression); Assert.AreEqual(value, (testVarValue1 - testVarValue2)); Console.WriteLine("TestMultiVariableExpression:"); Console.WriteLine(" ExpTree.IsValidExpression() should be true"); Console.WriteLine(" IsValidExpression() returned {0}", isValidExpression); Console.WriteLine(" Expected value: -1.79769313486232E+308"); Console.WriteLine(" Actual value: {0}", value); }