Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/InfixExpressionParserTest.cs @ 18168

Last change on this file since 18168 was 18168, checked in by gkronber, 2 years ago

#2938 changed the expected outputs to reflect the expected behaviour

File size: 7.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22
23using System;
24using Microsoft.VisualStudio.TestTools.UnitTesting;
25namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
26
27
28  [TestClass]
29  public class InfixExpressionParserTest {
30    [TestMethod]
31    [TestCategory("Problems.DataAnalysis.Symbolic")]
32    [TestProperty("Time", "short")]
33    public void InfixExpressionParserTestFormatting() {
34      var formatter = new InfixExpressionFormatter();
35      var parser = new InfixExpressionParser();
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
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      // round-trip
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
61
62      Assert.AreEqual("LOG(3)",formatter.Format(parser.Parse("log(3)")));
63      Assert.AreEqual("LOG(-3)",formatter.Format(parser.Parse("log(-3)")));
64      Assert.AreEqual("EXP(3)",formatter.Format(parser.Parse("exp(3)")));
65      Assert.AreEqual("EXP(-3)",formatter.Format(parser.Parse("exp(-3)")));
66      Assert.AreEqual("SQRT(3)", formatter.Format(parser.Parse("sqrt(3)")));
67
68      Assert.AreEqual("SQR(-3)", formatter.Format(parser.Parse("sqr((-3))")));
69
70      Assert.AreEqual("((3/3) + (2/2) + (1/1))",formatter.Format(parser.Parse("3/3+2/2+1/1")));
71      Assert.AreEqual("-3 + 30 - 2 + 20 - 1 + 10", formatter.Format(parser.Parse("-3+30-2+20-1+10")));
72
73      Assert.AreEqual("'x1'",formatter.Format(parser.Parse("\"x1\"")));
74      Assert.AreEqual("'var name'",formatter.Format(parser.Parse("\'var name\'")));
75      Assert.AreEqual("'var name'",formatter.Format(parser.Parse("\"var name\"")));
76      Assert.AreEqual("'1'",formatter.Format(parser.Parse("\"1\"")));
77
78      Assert.AreEqual("'var \" name'",formatter.Format(parser.Parse("'var \" name\'")));
79      Assert.AreEqual("\"var ' name\"", formatter.Format(parser.Parse("\"var \' name\"")));
80
81
82      Assert.AreEqual("('x1' * 'x2')",formatter.Format(parser.Parse("\"x1\"*\"x2\"")));
83      Assert.AreEqual("(('x1' * 'x2') + ('x3' * 'x4'))",formatter.Format(parser.Parse("\"x1\"*\"x2\"+\"x3\"*\"x4\"")));
84      Assert.AreEqual("(('x1' * 'x2') + ('x3' * 'x4'))", formatter.Format(parser.Parse("x1*x2+x3*x4")));
85
86
87      Assert.AreEqual("(3 ^ 2)",formatter.Format(parser.Parse("POW(3, 2)")));
88      Assert.AreEqual("(3.1 ^ 2.1)",formatter.Format(parser.Parse("POW(3.1, 2.1)")));
89      Assert.AreEqual("(3.1 ^ 2.1)",formatter.Format(parser.Parse("POW(3.1 , 2.1)")));
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("ROOT(3, 2)",formatter.Format(parser.Parse("ROOT(3, 2)")));
93      Assert.AreEqual("ROOT(3.1, 2.1)",formatter.Format(parser.Parse("ROOT(3.1, 2.1)")));
94      Assert.AreEqual("ROOT(3.1, 2.1)",formatter.Format(parser.Parse("ROOT(3.1 , 2.1)")));
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
98      Assert.AreEqual("IF(GT(0, 1), 1, 0)",formatter.Format(parser.Parse("IF(GT( 0, 1), 1, 0)")));
99      Assert.AreEqual("IF(LT(0, 1), 1, 0)",formatter.Format(parser.Parse("IF(LT(0,1), 1 , 0)")));
100      Assert.AreEqual("LAG('x', 1)",formatter.Format(parser.Parse("LAG(x, 1)")));
101      Assert.AreEqual("LAG('x', -1)",formatter.Format(parser.Parse("LAG(x, -1)")));
102      Assert.AreEqual("LAG('x', 1)",formatter.Format(parser.Parse("LAG(x, +1)")));
103      Assert.AreEqual("('x' * LAG('x', 1))", formatter.Format(parser.Parse("x * LAG('x', +1)")));
104
105      // factor variables
106      Assert.AreEqual("('x'[1] * 'y')",formatter.Format(parser.Parse("x [1.0] * y")));
107      Assert.AreEqual("('x'[1, 2] * 'y'[1, 2])",formatter.Format(parser.Parse("x [1.0, 2.0] * y [1.0, 2.0]")));
108      Assert.AreEqual("('x'[1] * 'y')",formatter.Format(parser.Parse("x[1] * y")));
109      Assert.AreEqual("('x'[1, 2] * 'y'[1, 2])",formatter.Format(parser.Parse("x[1, 2] * y [1, 2]")));
110      Assert.AreEqual("('x'[1] * 'y')",formatter.Format(parser.Parse("x [+1.0] * y")));
111      Assert.AreEqual("('x'[-1] * 'y')",formatter.Format(parser.Parse("x [-1.0] * y")));
112      Assert.AreEqual("('x'[-1, -2] * 'y'[1, 2])", formatter.Format(parser.Parse("x [-1.0, -2.0] * y [+1.0, +2.0]")));
113
114      // one-hot for factor
115      Assert.AreEqual("('x' = 'val' * 'y')",formatter.Format(parser.Parse("x='val' * y")));
116      Assert.AreEqual("'x' = 'val'",formatter.Format(parser.Parse("x = 'val'")));
117      Assert.AreEqual("'x' = 'val'",formatter.Format(parser.Parse("x = \"val\"")));
118      Assert.AreEqual("(1 * 'x' = 'val')",formatter.Format(parser.Parse("1.0 * x = val")));
119      Assert.AreEqual("(-((1 * 'x' = 'val')))",formatter.Format(parser.Parse("-1.0 * x = val")));
120      Assert.AreEqual("((1 * 'x' = 'val1') + 'y' = 'val2')", formatter.Format(parser.Parse("+1.0 * \"x\" = val1 + y = \"val2\"")));
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.