public void TestMethodLiteralVariableInteger() { var variableDataTypeMap = new Dictionary <string, DataType>(); variableDataTypeMap.Add("testVariable", DataType.Integer); var variableMap = new Dictionary <string, Variable>(); variableMap.Add("testVariable", new Variable(12345)); var literalVariable = new LiteralVariable(); literalVariable.value = "testVariable"; Assert.AreEqual("Var[testVariable]", literalVariable.getRPolish()); Assert.AreEqual(DataType.Integer, literalVariable.getDataType(variableDataTypeMap)); UnitTestUtil.AssertMathTreeNodeValue(12345, literalVariable.eval(variableMap)); }
public void TestMethodUnaryOperatorNot() { var functionDataTypeMap = new Dictionary <string, DataType>(); var functionMap = new Dictionary <string, Function>(); var unaryOperatorNot = new UnaryOperatorNot(); Assert.AreEqual("!", unaryOperatorNot.getRPolish()); Assert.AreEqual(8, unaryOperatorNot.getPriority()); Assert.AreEqual(DataType.Boolean, unaryOperatorNot.getDataType(DataType.Boolean, functionDataTypeMap)); Assert.AreEqual(DataType.None, unaryOperatorNot.getDataType(DataType.Integer, functionDataTypeMap)); Assert.AreEqual(DataType.None, unaryOperatorNot.getDataType(DataType.Decimal, functionDataTypeMap)); Assert.AreEqual(DataType.None, unaryOperatorNot.getDataType(DataType.DecimalList, functionDataTypeMap)); UnitTestUtil.AssertMathTreeNodeValue(false, unaryOperatorNot.eval(new MathTreeNodeValue(true), functionMap)); try { unaryOperatorNot.eval(new MathTreeNodeValue(12345), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentException)); } try { unaryOperatorNot.eval(new MathTreeNodeValue(12345.678), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentException)); } try { unaryOperatorNot.eval(new MathTreeNodeValue(new List <double>() { 1.2, 3.4, 5.6 }), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentException)); } }
public void TestMethodUnaryOperatorFunction() { var functionDataTypeMap = new Dictionary <string, DataType>(); functionDataTypeMap.Add("testFunction1", DataType.Boolean); functionDataTypeMap.Add("testFunction2", DataType.Boolean); functionDataTypeMap.Add("testFunction3", DataType.Boolean); functionDataTypeMap.Add("testFunction4", DataType.Integer); functionDataTypeMap.Add("testFunction5", DataType.Integer); functionDataTypeMap.Add("testFunction6", DataType.Integer); functionDataTypeMap.Add("testFunction7", DataType.Decimal); functionDataTypeMap.Add("testFunction8", DataType.Decimal); functionDataTypeMap.Add("testFunction9", DataType.Decimal); var functionMap = new Dictionary <string, Function>(); functionMap.Add("testFunction1", new Function(this.testFunction1)); functionMap.Add("testFunction2", new Function(this.testFunction2)); functionMap.Add("testFunction3", new Function(this.testFunction3)); functionMap.Add("testFunction4", new Function(this.testFunction4)); functionMap.Add("testFunction5", new Function(this.testFunction5)); functionMap.Add("testFunction6", new Function(this.testFunction6)); functionMap.Add("testFunction7", new Function(this.testFunction7)); functionMap.Add("testFunction8", new Function(this.testFunction8)); functionMap.Add("testFunction9", new Function(this.testFunction9)); var unaryOperatorFunction = new UnaryOperatorFunction(); Assert.AreEqual(9, unaryOperatorFunction.getPriority()); unaryOperatorFunction.functionName = "testFunction1"; Assert.AreEqual("Func[testFunction1]", unaryOperatorFunction.getRPolish()); Assert.AreEqual(DataType.None, unaryOperatorFunction.getDataType(DataType.Boolean, functionDataTypeMap)); Assert.AreEqual(DataType.Boolean, unaryOperatorFunction.getDataType(DataType.Integer, functionDataTypeMap)); Assert.AreEqual(DataType.Boolean, unaryOperatorFunction.getDataType(DataType.Decimal, functionDataTypeMap)); Assert.AreEqual(DataType.Boolean, unaryOperatorFunction.getDataType(DataType.DecimalList, functionDataTypeMap)); try { unaryOperatorFunction.eval(new MathTreeNodeValue(true), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentException)); } UnitTestUtil.AssertMathTreeNodeValue(true, unaryOperatorFunction.eval(new MathTreeNodeValue(12345), functionMap)); UnitTestUtil.AssertMathTreeNodeValue(false, unaryOperatorFunction.eval(new MathTreeNodeValue(-123.45), functionMap)); UnitTestUtil.AssertMathTreeNodeValue(true, unaryOperatorFunction.eval(new MathTreeNodeValue(new List <double>() { 1, 2, 3, 4 }), functionMap)); unaryOperatorFunction.functionName = "testFunction2"; Assert.AreEqual("Func[testFunction2]", unaryOperatorFunction.getRPolish()); Assert.AreEqual(DataType.None, unaryOperatorFunction.getDataType(DataType.Boolean, functionDataTypeMap)); Assert.AreEqual(DataType.Boolean, unaryOperatorFunction.getDataType(DataType.Integer, functionDataTypeMap)); Assert.AreEqual(DataType.Boolean, unaryOperatorFunction.getDataType(DataType.Decimal, functionDataTypeMap)); Assert.AreEqual(DataType.Boolean, unaryOperatorFunction.getDataType(DataType.DecimalList, functionDataTypeMap)); try { unaryOperatorFunction.eval(new MathTreeNodeValue(true), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentException)); } try { unaryOperatorFunction.eval(new MathTreeNodeValue(12345), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentOutOfRangeException)); } try { unaryOperatorFunction.eval(new MathTreeNodeValue(-123.45), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentOutOfRangeException)); } UnitTestUtil.AssertMathTreeNodeValue(true, unaryOperatorFunction.eval(new MathTreeNodeValue(new List <double>() { 1, 2, 3, 4 }), functionMap)); unaryOperatorFunction.functionName = "testFunction3"; Assert.AreEqual("Func[testFunction3]", unaryOperatorFunction.getRPolish()); Assert.AreEqual(DataType.None, unaryOperatorFunction.getDataType(DataType.Boolean, functionDataTypeMap)); Assert.AreEqual(DataType.Boolean, unaryOperatorFunction.getDataType(DataType.Integer, functionDataTypeMap)); Assert.AreEqual(DataType.Boolean, unaryOperatorFunction.getDataType(DataType.Decimal, functionDataTypeMap)); Assert.AreEqual(DataType.Boolean, unaryOperatorFunction.getDataType(DataType.DecimalList, functionDataTypeMap)); try { unaryOperatorFunction.eval(new MathTreeNodeValue(true), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentException)); } UnitTestUtil.AssertMathTreeNodeValue(true, unaryOperatorFunction.eval(new MathTreeNodeValue(12345), functionMap)); UnitTestUtil.AssertMathTreeNodeValue(false, unaryOperatorFunction.eval(new MathTreeNodeValue(-123.45), functionMap)); UnitTestUtil.AssertMathTreeNodeValue(true, unaryOperatorFunction.eval(new MathTreeNodeValue(new List <double>() { 1, 2, 3, 4 }), functionMap)); unaryOperatorFunction.functionName = "testFunction4"; Assert.AreEqual("Func[testFunction4]", unaryOperatorFunction.getRPolish()); Assert.AreEqual(DataType.None, unaryOperatorFunction.getDataType(DataType.Boolean, functionDataTypeMap)); Assert.AreEqual(DataType.Integer, unaryOperatorFunction.getDataType(DataType.Integer, functionDataTypeMap)); Assert.AreEqual(DataType.Integer, unaryOperatorFunction.getDataType(DataType.Decimal, functionDataTypeMap)); Assert.AreEqual(DataType.Integer, unaryOperatorFunction.getDataType(DataType.DecimalList, functionDataTypeMap)); try { unaryOperatorFunction.eval(new MathTreeNodeValue(true), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentException)); } UnitTestUtil.AssertMathTreeNodeValue(12348, unaryOperatorFunction.eval(new MathTreeNodeValue(12345), functionMap)); UnitTestUtil.AssertMathTreeNodeValue(-121, unaryOperatorFunction.eval(new MathTreeNodeValue(-123.45), functionMap)); UnitTestUtil.AssertMathTreeNodeValue(4, unaryOperatorFunction.eval(new MathTreeNodeValue(new List <double>() { 1, 2, 3, 4 }), functionMap)); unaryOperatorFunction.functionName = "testFunction5"; Assert.AreEqual("Func[testFunction5]", unaryOperatorFunction.getRPolish()); Assert.AreEqual(DataType.None, unaryOperatorFunction.getDataType(DataType.Boolean, functionDataTypeMap)); Assert.AreEqual(DataType.Integer, unaryOperatorFunction.getDataType(DataType.Integer, functionDataTypeMap)); Assert.AreEqual(DataType.Integer, unaryOperatorFunction.getDataType(DataType.Decimal, functionDataTypeMap)); Assert.AreEqual(DataType.Integer, unaryOperatorFunction.getDataType(DataType.DecimalList, functionDataTypeMap)); try { unaryOperatorFunction.eval(new MathTreeNodeValue(true), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentException)); } try { unaryOperatorFunction.eval(new MathTreeNodeValue(12345), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentOutOfRangeException)); } try { unaryOperatorFunction.eval(new MathTreeNodeValue(12345), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentOutOfRangeException)); } UnitTestUtil.AssertMathTreeNodeValue(3, unaryOperatorFunction.eval(new MathTreeNodeValue(new List <double>() { 1, 2, 3, 4 }), functionMap)); unaryOperatorFunction.functionName = "testFunction6"; Assert.AreEqual("Func[testFunction6]", unaryOperatorFunction.getRPolish()); Assert.AreEqual(DataType.None, unaryOperatorFunction.getDataType(DataType.Boolean, functionDataTypeMap)); Assert.AreEqual(DataType.Integer, unaryOperatorFunction.getDataType(DataType.Integer, functionDataTypeMap)); Assert.AreEqual(DataType.Integer, unaryOperatorFunction.getDataType(DataType.Decimal, functionDataTypeMap)); Assert.AreEqual(DataType.Integer, unaryOperatorFunction.getDataType(DataType.DecimalList, functionDataTypeMap)); try { unaryOperatorFunction.eval(new MathTreeNodeValue(true), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentException)); } UnitTestUtil.AssertMathTreeNodeValue(12345, unaryOperatorFunction.eval(new MathTreeNodeValue(12345), functionMap)); UnitTestUtil.AssertMathTreeNodeValue(-124, unaryOperatorFunction.eval(new MathTreeNodeValue(-123.45), functionMap)); UnitTestUtil.AssertMathTreeNodeValue(10, unaryOperatorFunction.eval(new MathTreeNodeValue(new List <double>() { 1, 2, 3, 4 }), functionMap)); unaryOperatorFunction.functionName = "testFunction7"; Assert.AreEqual("Func[testFunction7]", unaryOperatorFunction.getRPolish()); Assert.AreEqual(DataType.None, unaryOperatorFunction.getDataType(DataType.Boolean, functionDataTypeMap)); Assert.AreEqual(DataType.Decimal, unaryOperatorFunction.getDataType(DataType.Integer, functionDataTypeMap)); Assert.AreEqual(DataType.Decimal, unaryOperatorFunction.getDataType(DataType.Decimal, functionDataTypeMap)); Assert.AreEqual(DataType.Decimal, unaryOperatorFunction.getDataType(DataType.DecimalList, functionDataTypeMap)); try { unaryOperatorFunction.eval(new MathTreeNodeValue(true), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentException)); } UnitTestUtil.AssertMathTreeNodeValue(Math.Sqrt((double)12345), unaryOperatorFunction.eval(new MathTreeNodeValue(12345), functionMap)); UnitTestUtil.AssertMathTreeNodeValue(Double.NaN, unaryOperatorFunction.eval(new MathTreeNodeValue(-123.45), functionMap)); UnitTestUtil.AssertMathTreeNodeValue((double)1, unaryOperatorFunction.eval(new MathTreeNodeValue(new List <double>() { 1, 2, 3, 4 }), functionMap)); unaryOperatorFunction.functionName = "testFunction8"; Assert.AreEqual("Func[testFunction8]", unaryOperatorFunction.getRPolish()); Assert.AreEqual(DataType.None, unaryOperatorFunction.getDataType(DataType.Boolean, functionDataTypeMap)); Assert.AreEqual(DataType.Decimal, unaryOperatorFunction.getDataType(DataType.Integer, functionDataTypeMap)); Assert.AreEqual(DataType.Decimal, unaryOperatorFunction.getDataType(DataType.Decimal, functionDataTypeMap)); Assert.AreEqual(DataType.Decimal, unaryOperatorFunction.getDataType(DataType.DecimalList, functionDataTypeMap)); try { unaryOperatorFunction.eval(new MathTreeNodeValue(true), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentException)); } try { unaryOperatorFunction.eval(new MathTreeNodeValue(12345), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentOutOfRangeException)); } try { unaryOperatorFunction.eval(new MathTreeNodeValue(-123.45), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentOutOfRangeException)); } UnitTestUtil.AssertMathTreeNodeValue((double)0, unaryOperatorFunction.eval(new MathTreeNodeValue(new List <double>() { 1, 2, 3, 4 }), functionMap)); unaryOperatorFunction.functionName = "testFunction9"; Assert.AreEqual("Func[testFunction9]", unaryOperatorFunction.getRPolish()); Assert.AreEqual(DataType.None, unaryOperatorFunction.getDataType(DataType.Boolean, functionDataTypeMap)); Assert.AreEqual(DataType.Decimal, unaryOperatorFunction.getDataType(DataType.Integer, functionDataTypeMap)); Assert.AreEqual(DataType.Decimal, unaryOperatorFunction.getDataType(DataType.Decimal, functionDataTypeMap)); Assert.AreEqual(DataType.Decimal, unaryOperatorFunction.getDataType(DataType.DecimalList, functionDataTypeMap)); try { unaryOperatorFunction.eval(new MathTreeNodeValue(true), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentException)); } UnitTestUtil.AssertMathTreeNodeValue((double)152399025, unaryOperatorFunction.eval(new MathTreeNodeValue(12345), functionMap)); UnitTestUtil.AssertMathTreeNodeValue(15239.9025, unaryOperatorFunction.eval(new MathTreeNodeValue(-123.45), functionMap)); UnitTestUtil.AssertMathTreeNodeValue((double)30, unaryOperatorFunction.eval(new MathTreeNodeValue(new List <double>() { 1, 2, 3, 4 }), functionMap)); unaryOperatorFunction.functionName = "testFunction10"; Assert.AreEqual("Func[testFunction10]", unaryOperatorFunction.getRPolish()); Assert.AreEqual(DataType.None, unaryOperatorFunction.getDataType(DataType.Boolean, functionDataTypeMap)); Assert.AreEqual(DataType.None, unaryOperatorFunction.getDataType(DataType.Integer, functionDataTypeMap)); Assert.AreEqual(DataType.None, unaryOperatorFunction.getDataType(DataType.Decimal, functionDataTypeMap)); Assert.AreEqual(DataType.None, unaryOperatorFunction.getDataType(DataType.DecimalList, functionDataTypeMap)); try { unaryOperatorFunction.eval(new MathTreeNodeValue(true), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentException)); } try { unaryOperatorFunction.eval(new MathTreeNodeValue(12345), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentException)); } try { unaryOperatorFunction.eval(new MathTreeNodeValue(-123.45), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentException)); } try { unaryOperatorFunction.eval(new MathTreeNodeValue(new List <double>() { 1, 2, 3, 4 }), functionMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentException)); } }
public void TestMethodBinaryOperatorComma() { var binaryOperatorComma = new BinaryOperatorComma(); Assert.AreEqual(1, binaryOperatorComma.getPriority()); Assert.AreEqual(",", binaryOperatorComma.getRPolish()); Assert.AreEqual(DataType.None, binaryOperatorComma.getDataType(DataType.Boolean, DataType.Boolean)); Assert.AreEqual(DataType.None, binaryOperatorComma.getDataType(DataType.Boolean, DataType.Integer)); Assert.AreEqual(DataType.None, binaryOperatorComma.getDataType(DataType.Boolean, DataType.Decimal)); Assert.AreEqual(DataType.None, binaryOperatorComma.getDataType(DataType.Boolean, DataType.DecimalList)); Assert.AreEqual(DataType.None, binaryOperatorComma.getDataType(DataType.Integer, DataType.Boolean)); Assert.AreEqual(DataType.DecimalList, binaryOperatorComma.getDataType(DataType.Integer, DataType.Integer)); Assert.AreEqual(DataType.DecimalList, binaryOperatorComma.getDataType(DataType.Integer, DataType.Decimal)); Assert.AreEqual(DataType.DecimalList, binaryOperatorComma.getDataType(DataType.Integer, DataType.DecimalList)); Assert.AreEqual(DataType.None, binaryOperatorComma.getDataType(DataType.Decimal, DataType.Boolean)); Assert.AreEqual(DataType.DecimalList, binaryOperatorComma.getDataType(DataType.Decimal, DataType.Integer)); Assert.AreEqual(DataType.DecimalList, binaryOperatorComma.getDataType(DataType.Decimal, DataType.Decimal)); Assert.AreEqual(DataType.DecimalList, binaryOperatorComma.getDataType(DataType.Decimal, DataType.DecimalList)); Assert.AreEqual(DataType.None, binaryOperatorComma.getDataType(DataType.DecimalList, DataType.Boolean)); Assert.AreEqual(DataType.DecimalList, binaryOperatorComma.getDataType(DataType.DecimalList, DataType.Integer)); Assert.AreEqual(DataType.DecimalList, binaryOperatorComma.getDataType(DataType.DecimalList, DataType.Decimal)); Assert.AreEqual(DataType.DecimalList, binaryOperatorComma.getDataType(DataType.DecimalList, DataType.DecimalList)); UnitTestUtil.AssertMathTreeNodeValue(new List <double>() { 2, 2 }, binaryOperatorComma.eval(new MathTreeNodeValue(2), new MathTreeNodeValue(2))); UnitTestUtil.AssertMathTreeNodeValue(new List <double>() { 2, 4 }, binaryOperatorComma.eval(new MathTreeNodeValue(2), new MathTreeNodeValue(4.0))); UnitTestUtil.AssertMathTreeNodeValue(new List <double>() { 2, 5.6, 7.8 }, binaryOperatorComma.eval(new MathTreeNodeValue(2), new MathTreeNodeValue(new List <double>() { 5.6, 7.8 }))); UnitTestUtil.AssertMathTreeNodeValue(new List <double>() { 4, 2 }, binaryOperatorComma.eval(new MathTreeNodeValue(4.0), new MathTreeNodeValue(2))); UnitTestUtil.AssertMathTreeNodeValue(new List <double>() { 4, 4 }, binaryOperatorComma.eval(new MathTreeNodeValue(4.0), new MathTreeNodeValue(4.0))); UnitTestUtil.AssertMathTreeNodeValue(new List <double>() { 4, 5.6, 7.8 }, binaryOperatorComma.eval(new MathTreeNodeValue(4.0), new MathTreeNodeValue(new List <double>() { 5.6, 7.8 }))); UnitTestUtil.AssertMathTreeNodeValue(new List <double>() { 1.2, 3.4, 2 }, binaryOperatorComma.eval(new MathTreeNodeValue(new List <double>() { 1.2, 3.4 }), new MathTreeNodeValue(2))); UnitTestUtil.AssertMathTreeNodeValue(new List <double>() { 1.2, 3.4, 4 }, binaryOperatorComma.eval(new MathTreeNodeValue(new List <double>() { 1.2, 3.4 }), new MathTreeNodeValue(4.0))); UnitTestUtil.AssertMathTreeNodeValue(new List <double>() { 1.2, 3.4, 5.6, 7.8 }, binaryOperatorComma.eval(new MathTreeNodeValue(new List <double>() { 1.2, 3.4 }), new MathTreeNodeValue(new List <double>() { 5.6, 7.8 }))); }