AK.ExpressionSolverTests.TestStringFuncs C# (CSharp) Method

TestStringFuncs() public static method

public static TestStringFuncs ( ) : void
return void
        public static void TestStringFuncs()
        {
            ExpressionSolver solver = new ExpressionSolver();
            solver.AddCustomFunction("strlen",1, delegate(object[] p) {
                return ((string)p[0]).Length;
            },true);
            var exp = solver.SymbolicateExpression("strlen('123')");
            AssertSameValue(exp.Evaluate(),3.0);
            exp = solver.SymbolicateExpression("strlen('12\\'3')");
            AssertSameValue(exp.Evaluate(),4.0);
            exp = solver.SymbolicateExpression("strlen('12\\'3 4')");
            AssertSameValue(exp.Evaluate(),6.0);
            solver.AddCustomFunction("strlen2",2, delegate(object[] p) {
                return ((string)p[0]).Length*(double)p[1];
            });
            exp = solver.SymbolicateExpression("strlen2('12\\'3 4',2.5)");
            AssertSameValue(exp.Evaluate(),6.0*2.5);

            string[] erroneousStrings = new string[]{"strlen(''')","strlen('''')","''"};
            foreach (var errorString in erroneousStrings)
            {
                try
                {
                    exp = solver.SymbolicateExpression(errorString);
                    throw new System.Exception("ExpressionSolverTest failed");
                }
                catch (ESSyntaxErrorException)
                {
                    // Parameters were not given correctly - syntax error expected
                }
                catch (System.Exception)
                {
                    throw new System.Exception("ExpressionSolverTest failed");
                }
            }

            // Because strlen should be evaluated at symbolication time, the following should reduce to one real value symbol:
            exp = solver.SymbolicateExpression("(1+strlen('123')+1)/strlen('12345')");
            Assert(exp.root.type == SymbolType.RealValue);
            AssertSameValue(exp.root.value,1);
            // But if one of the parameters is not constant, then we cant do it:
            exp = solver.SymbolicateExpression("strlen('123')+x", new string[]{"x"});
            Assert(exp.root.type == SymbolType.SubExpression);

            // Test string variables. Both exp-local and global
            exp = solver.SymbolicateExpression("strlen(stringVariableTest)","$stringVariableTest");
            exp.SetVariable("stringVariableTest","test");
            AssertSameValue(exp.Evaluate(),4);
            try
            {
                exp.SetVariable("stringVariableTest",121);
                Assert(false);
            }
            catch (ESParameterTypeChangedException)
            {
            }
            solver.SetGlobalVariable("striva","123");
            exp = solver.SymbolicateExpression("strlen( (  striva  )  )/3");
            AssertSameValue(exp.Evaluate(),1);
            try
            {
                solver.SetGlobalVariable("striva",42141.0);
                Assert(false);
            }
            catch (ESParameterTypeChangedException)
            {
            }
        }