- Timestamp:
- 12/26/21 21:10:35 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/InfixExpressionParserTest.cs
r17180 r18167 34 34 var formatter = new InfixExpressionFormatter(); 35 35 var parser = new InfixExpressionParser(); 36 Console.WriteLine(formatter.Format(parser.Parse("3")));37 Console.WriteLine(formatter.Format(parser.Parse("3*3")));38 Console.WriteLine(formatter.Format(parser.Parse("3 * 4")));39 Console.WriteLine(formatter.Format(parser.Parse("123E-03")));40 Console.WriteLine(formatter.Format(parser.Parse("123e-03")));41 Console.WriteLine(formatter.Format(parser.Parse("123e+03")));42 Console.WriteLine(formatter.Format(parser.Parse("123E+03")));43 Console.WriteLine(formatter.Format(parser.Parse("123.0E-03")));44 Console.WriteLine(formatter.Format(parser.Parse("123.0e-03")));45 Console.WriteLine(formatter.Format(parser.Parse("123.0e+03")));46 Console.WriteLine(formatter.Format(parser.Parse("123.0E+03")));47 Console.WriteLine(formatter.Format(parser.Parse("123.0E-3")));48 Console.WriteLine(formatter.Format(parser.Parse("123.0e-3")));49 Console.WriteLine(formatter.Format(parser.Parse("123.0e+3")));50 Console.WriteLine(formatter.Format(parser.Parse("123.0E+3")));36 Assert.AreEqual("3", formatter.Format(parser.Parse("3"))); 37 Assert.AreEqual("(3 * 3)", formatter.Format(parser.Parse("3*3"))); 38 Assert.AreEqual("(3 * 4)",formatter.Format(parser.Parse("3 * 4"))); 39 Assert.AreEqual("0.123",formatter.Format(parser.Parse("123E-03"))); 40 Assert.AreEqual("0.123",formatter.Format(parser.Parse("123e-03"))); 41 Assert.AreEqual("123000",formatter.Format(parser.Parse("123e+03"))); 42 Assert.AreEqual("123000",formatter.Format(parser.Parse("123E+03"))); 43 Assert.AreEqual("0.123",formatter.Format(parser.Parse("123.0E-03"))); 44 Assert.AreEqual("0.123",formatter.Format(parser.Parse("123.0e-03"))); 45 Assert.AreEqual("123000",formatter.Format(parser.Parse("123.0e+03"))); 46 Assert.AreEqual("123000",formatter.Format(parser.Parse("123.0E+03"))); 47 Assert.AreEqual("0.123",formatter.Format(parser.Parse("123.0E-3"))); 48 Assert.AreEqual("0.123",formatter.Format(parser.Parse("123.0e-3"))); 49 Assert.AreEqual("123000",formatter.Format(parser.Parse("123.0e+3"))); 50 Assert.AreEqual("123000",formatter.Format(parser.Parse("123.0E+3"))); 51 51 52 Console.WriteLine(formatter.Format(parser.Parse("3.1415+2.0")));53 Console.WriteLine(formatter.Format(parser.Parse("3.1415/2.0")));54 Console.WriteLine(formatter.Format(parser.Parse("3.1415*2.0")));55 Console.WriteLine(formatter.Format(parser.Parse("3.1415-2.0")));52 Assert.AreEqual("(3.1415 + 2)",formatter.Format(parser.Parse("3.1415+2.0"))); 53 Assert.AreEqual("(3.1415 * 1/2)",formatter.Format(parser.Parse("3.1415/2.0"))); 54 Assert.AreEqual("(3.1415 * 2)",formatter.Format(parser.Parse("3.1415*2.0"))); 55 Assert.AreEqual("(3.1415 + (-(2)))", formatter.Format(parser.Parse("3.1415-2.0"))); 56 56 // round-trip 57 Console.WriteLine(formatter.Format(parser.Parse(formatter.Format(parser.Parse("3.1415-2.0")))));58 Console.WriteLine(formatter.Format(parser.Parse("3.1415+(2.0)")));59 Console.WriteLine(formatter.Format(parser.Parse("(3.1415+(2.0))")));57 Assert.AreEqual("(3.1415 + (-(2)))",formatter.Format(parser.Parse(formatter.Format(parser.Parse("3.1415-2.0"))))); 58 Assert.AreEqual("(3.1415 + 2)",formatter.Format(parser.Parse("3.1415+(2.0)"))); 59 Assert.AreEqual("(3.1415 + 2)", formatter.Format(parser.Parse("(3.1415+(2.0))"))); 60 60 61 61 62 Console.WriteLine(formatter.Format(parser.Parse("log(3)")));63 Console.WriteLine(formatter.Format(parser.Parse("log(-3)"))); // should produce log(-3) as output64 Console.WriteLine(formatter.Format(parser.Parse("exp(3)")));65 Console.WriteLine(formatter.Format(parser.Parse("exp(-3)"))); // should produce exp(-3) as output66 Console.WriteLine(formatter.Format(parser.Parse("sqrt(3)")));62 Assert.AreEqual("LOG(3)",formatter.Format(parser.Parse("log(3)"))); 63 Assert.AreEqual("LOG((-(3)))",formatter.Format(parser.Parse("log(-3)"))); // should produce log(-3) as output 64 Assert.AreEqual("EXP(3)",formatter.Format(parser.Parse("exp(3)"))); 65 Assert.AreEqual("EXP((-(3)))",formatter.Format(parser.Parse("exp(-3)"))); // should produce exp(-3) as output 66 Assert.AreEqual("SQRT(3)", formatter.Format(parser.Parse("sqrt(3)"))); 67 67 68 Console.WriteLine(formatter.Format(parser.Parse("sqr((-3))"))); // should produce sqr((-3)) as output68 Assert.AreEqual("SQR((-(3)))", formatter.Format(parser.Parse("sqr((-3))"))); // should produce sqr((-3)) as output 69 69 70 Console.WriteLine(formatter.Format(parser.Parse("3/3+2/2+1/1"))); // ((3 * 1 / 3) + (2 * 1 / 2) + (1 * 1 / 1))71 Console.WriteLine(formatter.Format(parser.Parse("-3+30-2+20-1+10"))); // not (30 + 20 + 10 + ((-1) * (3 + 2 + 1)))70 Assert.AreEqual("((3 * 1/3) + (2 * 1/2) + (1 * 1/1))",formatter.Format(parser.Parse("3/3+2/2+1/1"))); // ((3 * 1 / 3) + (2 * 1 / 2) + (1 * 1 / 1)) 71 Assert.AreEqual("(30 + 20 + 10 + ((-1) * (3 + 2 + 1)))", formatter.Format(parser.Parse("-3+30-2+20-1+10"))); // not (30 + 20 + 10 + ((-1) * (3 + 2 + 1))) 72 72 // round trip 73 Console.WriteLine(formatter.Format(parser.Parse(formatter.Format(parser.Parse("-3+30-2+20-1+10"))))); // not (30 + 20 + 10 + ((-(1)) * (3 + 2 + 1)))73 Assert.AreEqual("(30 + 20 + 10 + ((-(1)) * (3 + 2 + 1)))", formatter.Format(parser.Parse(formatter.Format(parser.Parse("-3+30-2+20-1+10"))))); // not (30 + 20 + 10 + ((-(1)) * (3 + 2 + 1))) 74 74 75 Console.WriteLine(formatter.Format(parser.Parse("\"x1\"")));76 Console.WriteLine(formatter.Format(parser.Parse("\'var name\'")));77 Console.WriteLine(formatter.Format(parser.Parse("\"var name\"")));78 Console.WriteLine(formatter.Format(parser.Parse("\"1\"")));75 Assert.AreEqual("'x1'",formatter.Format(parser.Parse("\"x1\""))); 76 Assert.AreEqual("'var name'",formatter.Format(parser.Parse("\'var name\'"))); 77 Assert.AreEqual("'var name'",formatter.Format(parser.Parse("\"var name\""))); 78 Assert.AreEqual("'1'",formatter.Format(parser.Parse("\"1\""))); 79 79 80 Console.WriteLine(formatter.Format(parser.Parse("'var \" name\'")));81 Console.WriteLine(formatter.Format(parser.Parse("\"var \' name\"")));80 Assert.AreEqual("'var \" name'",formatter.Format(parser.Parse("'var \" name\'"))); 81 Assert.AreEqual("\"var ' name\"", formatter.Format(parser.Parse("\"var \' name\""))); 82 82 83 83 84 Console.WriteLine(formatter.Format(parser.Parse("\"x1\"*\"x2\"")));85 Console.WriteLine(formatter.Format(parser.Parse("\"x1\"*\"x2\"+\"x3\"*\"x4\"")));86 Console.WriteLine(formatter.Format(parser.Parse("x1*x2+x3*x4")));84 Assert.AreEqual("('x1' * 'x2')",formatter.Format(parser.Parse("\"x1\"*\"x2\""))); 85 Assert.AreEqual("(('x1' * 'x2') + ('x3' * 'x4'))",formatter.Format(parser.Parse("\"x1\"*\"x2\"+\"x3\"*\"x4\""))); 86 Assert.AreEqual("(('x1' * 'x2') + ('x3' * 'x4'))", formatter.Format(parser.Parse("x1*x2+x3*x4"))); 87 87 88 88 89 Console.WriteLine(formatter.Format(parser.Parse("POW(3, 2)")));90 Console.WriteLine(formatter.Format(parser.Parse("POW(3.1, 2.1)")));91 Console.WriteLine(formatter.Format(parser.Parse("POW(3.1 , 2.1)")));92 Console.WriteLine(formatter.Format(parser.Parse("POW(3.1 ,2.1)")));93 Console.WriteLine(formatter.Format(parser.Parse("POW(-3.1 , - 2.1)")));94 Console.WriteLine(formatter.Format(parser.Parse("ROOT(3, 2)")));95 Console.WriteLine(formatter.Format(parser.Parse("ROOT(3.1, 2.1)")));96 Console.WriteLine(formatter.Format(parser.Parse("ROOT(3.1 , 2.1)")));97 Console.WriteLine(formatter.Format(parser.Parse("ROOT(3.1 ,2.1)")));98 Console.WriteLine(formatter.Format(parser.Parse("ROOT(-3.1 , - 2.1)")));89 Assert.AreEqual("(3 ^ 2)",formatter.Format(parser.Parse("POW(3, 2)"))); 90 Assert.AreEqual("(3.1 ^ 2.1)",formatter.Format(parser.Parse("POW(3.1, 2.1)"))); 91 Assert.AreEqual("(3.1 ^ 2.1)",formatter.Format(parser.Parse("POW(3.1 , 2.1)"))); 92 Assert.AreEqual("(3.1 ^ 2.1)",formatter.Format(parser.Parse("POW(3.1 ,2.1)"))); 93 Assert.AreEqual("((-(3.1)) ^ (-(2.1)))",formatter.Format(parser.Parse("POW(-3.1 , - 2.1)"))); 94 Assert.AreEqual("ROOT(3, 2)",formatter.Format(parser.Parse("ROOT(3, 2)"))); 95 Assert.AreEqual("ROOT(3.1, 2.1)",formatter.Format(parser.Parse("ROOT(3.1, 2.1)"))); 96 Assert.AreEqual("ROOT(3.1, 2.1)",formatter.Format(parser.Parse("ROOT(3.1 , 2.1)"))); 97 Assert.AreEqual("ROOT(3.1, 2.1)",formatter.Format(parser.Parse("ROOT(3.1 ,2.1)"))); 98 Assert.AreEqual("ROOT((-(3.1)), (-(2.1)))", formatter.Format(parser.Parse("ROOT(-3.1 , - 2.1)"))); 99 99 100 Console.WriteLine(formatter.Format(parser.Parse("IF(GT( 0, 1), 1, 0)"))); 101 Console.WriteLine(formatter.Format(parser.Parse("IF(LT(0,1), 1 , 0)"))); 100 Assert.AreEqual("IF(GT(0, 1), 1, 0)",formatter.Format(parser.Parse("IF(GT( 0, 1), 1, 0)"))); 101 Assert.AreEqual("IF(LT(0, 1), 1, 0)",formatter.Format(parser.Parse("IF(LT(0,1), 1 , 0)"))); 102 Assert.AreEqual("LAG('x', 1)",formatter.Format(parser.Parse("LAG(x, 1)"))); 103 Assert.AreEqual("LAG('x', -1)",formatter.Format(parser.Parse("LAG(x, -1)"))); 104 Assert.AreEqual("LAG('x', 1)",formatter.Format(parser.Parse("LAG(x, +1)"))); 105 Assert.AreEqual("('x' * LAG('x', 1))", formatter.Format(parser.Parse("x * LAG('x', +1)"))); 102 106 103 Console.WriteLine(formatter.Format(parser.Parse("LAG(x, 1)"))); 104 Console.WriteLine(formatter.Format(parser.Parse("LAG(x, -1)"))); 105 Console.WriteLine(formatter.Format(parser.Parse("LAG(x, +1)"))); 106 Console.WriteLine(formatter.Format(parser.Parse("x * LAG('x', +1)"))); 107 // factor variables 108 Assert.AreEqual("('x'[1] * 'y')",formatter.Format(parser.Parse("x [1.0] * y"))); 109 Assert.AreEqual("('x'[1, 2] * 'y'[1, 2])",formatter.Format(parser.Parse("x [1.0, 2.0] * y [1.0, 2.0]"))); 110 Assert.AreEqual("('x'[1] * 'y')",formatter.Format(parser.Parse("x[1] * y"))); 111 Assert.AreEqual("('x'[1, 2] * 'y'[1, 2])",formatter.Format(parser.Parse("x[1, 2] * y [1, 2]"))); 112 Assert.AreEqual("('x'[1] * 'y')",formatter.Format(parser.Parse("x [+1.0] * y"))); 113 Assert.AreEqual("('x'[-1] * 'y')",formatter.Format(parser.Parse("x [-1.0] * y"))); 114 Assert.AreEqual("('x'[-1, -2] * 'y'[1, 2])", formatter.Format(parser.Parse("x [-1.0, -2.0] * y [+1.0, +2.0]"))); 107 115 108 Console.WriteLine(formatter.Format(parser.Parse("x [1.0] * y"))); 109 Console.WriteLine(formatter.Format(parser.Parse("x [1.0, 2.0] * y [1.0, 2.0]"))); 110 Console.WriteLine(formatter.Format(parser.Parse("x[1] * y"))); 111 Console.WriteLine(formatter.Format(parser.Parse("x[1, 2] * y [1, 2]"))); 112 113 Console.WriteLine(formatter.Format(parser.Parse("x [+1.0] * y"))); 114 Console.WriteLine(formatter.Format(parser.Parse("x [-1.0] * y"))); 115 Console.WriteLine(formatter.Format(parser.Parse("x [-1.0, -2.0] * y [+1.0, +2.0]"))); 116 117 Console.WriteLine(formatter.Format(parser.Parse("x='bla' * y"))); 118 Console.WriteLine(formatter.Format(parser.Parse("x = 'bla'"))); 119 Console.WriteLine(formatter.Format(parser.Parse("x = \"bla\""))); 120 Console.WriteLine(formatter.Format(parser.Parse("1.0 * x = bla"))); 121 Console.WriteLine(formatter.Format(parser.Parse("-1.0 * x = bla"))); 122 Console.WriteLine(formatter.Format(parser.Parse("+1.0 * \"x\" = bla + y = \"bla2\""))); 116 // one-hot for factor 117 Assert.AreEqual("('x' = 'val' * 'y')",formatter.Format(parser.Parse("x='val' * y"))); 118 Assert.AreEqual("'x' = 'val'",formatter.Format(parser.Parse("x = 'val'"))); 119 Assert.AreEqual("'x' = 'val'",formatter.Format(parser.Parse("x = \"val\""))); 120 Assert.AreEqual("(1 * 'x' = 'val')",formatter.Format(parser.Parse("1.0 * x = val"))); 121 Assert.AreEqual("(-((1 * 'x' = 'val')))",formatter.Format(parser.Parse("-1.0 * x = val"))); 122 Assert.AreEqual("((1 * 'x' = 'val1') + 'y' = 'val2')", formatter.Format(parser.Parse("+1.0 * \"x\" = val1 + y = \"val2\""))); 123 123 } 124 124 }
Note: See TracChangeset
for help on using the changeset viewer.