testExpressionEvaluator.cc
Go to the documentation of this file.
1 #include <cppunit/extensions/HelperMacros.h>
2 
3 #include <boost/assign/std/vector.hpp>
4 #include <boost/assign/list_of.hpp>
5 
6 #include <tst/Verify.h>
7 #include <utl/MathExpressionGrammar.h>
8 #include <utl/ErrorLogger.h>
9 #include <utl/AugerException.h>
10 #include <utl/SymbolTable.h>
11 #include <utl/ExpressionParser.h>
12 
13 #include <map>
14 #include <string>
15 #include <iostream>
16 
17 using std::map;
18 using std::vector;
19 using std::string;
20 using namespace utl;
21 using namespace tst;
22 using namespace boost::assign;
23 
24 
25 #define ASSERT_CLOSE(x, y) CPPUNIT_ASSERT(Verify<CloseTo>(x, y))
26 #define ASSERT_NOTCLOSE(x, y) CPPUNIT_ASSERT(Verify<Not<CloseTo> >(x, y))
27 
28 
29 struct TestSymbols {
30  static const utl::SymbolTable& GetSymbolMap() { return fgUnitMap; }
31  static void SetSymbols(const utl::SymbolTable& s) { fgUnitMap=s; }
33 };
35 
36 
37 
38 class ExpressionEvaluatorTest : public CppUnit::TestFixture {
39 
40  CPPUNIT_TEST_SUITE(ExpressionEvaluatorTest);
41  CPPUNIT_TEST(testEvaluating);
42  CPPUNIT_TEST(testMath);
43  CPPUNIT_TEST_SUITE_END();
44 
45 public:
46  void
48  {
50  }
51 
52  void
53  tearDown() { }
54 
55  void
57  {
59  map_list_of
60  ("a", 0.0)
61  ("b", 1.0)
62  ("c", 2.0)
63  ("d", 3.0)
64  ("scient1", 1.234e-1)
65  ("scient2", 1.234e-2)
66  ("scient3", 1.234e-3)
67  ("doub1", 42.001)
68  ("doub2", 42.002)
69  ("doub3", 42.003)
70  ;
71 
72  const map<string, double> expressionValue =
73  map_list_of
74  ("a", t["a"])
75  ("a * b * c", t["a"] * t["b"] * t["c"])
76  ("b * c", t["b"] * t["c"])
77  ("c^2", pow(t["c"], 2))
78  ("c^0.5", sqrt(2))
79  ("c*d^2", t["c"] * pow(t["d"], 2))
80  ("b/c*d", t["b"] / t["c"] * t["d"])
81  ("scient1 * scient2 * scient3", t["scient1"] * t["scient2"] * t["scient3"])
82  ("doub1 / doub2 * scient3", t["doub1"] / t["doub2"] * t["scient3"])
83  ;
84 
87 
88  for (map<string, double>::const_iterator it = expressionValue.begin(), end = expressionValue.end();
89  it != end; ++it) {
90  ASSERT_CLOSE(eval.Evaluate(it->first), it->second);
91  }
92 
93 
94 
96  map_list_of
97  ("x", 26.3)
98  ;
99 
100  const map<string, double> expressionValue2 =
101  map_list_of
102  ("c+x*doub1", t["c"]+t2["x"]*t["doub1"])
103  ;
104 
106  for (map<string, double>::const_iterator it = expressionValue2.begin(), end = expressionValue2.end();
107  it != end; ++it) {
108  ASSERT_CLOSE(evalx.Evaluate(it->first), it->second);
109  }
110 
111  }
112 
113 
114  void
116  {
117  utl::SymbolTable t =
118  map_list_of
119  ("a", 0.1)
120  ("b", 1.0)
121  ("c", 2.0)
122  ("d", 3.0)
123  ("scient1", 1.234e-1)
124  ("scient2", 1.234e-2)
125  ("scient3", 1.234e-3)
126  ("doub1", 42.001)
127  ("doub2", 42.002)
128  ("doub3", 42.003)
129  ;
130 
131  utl::SymbolTable tOne = t;
132  tOne["x"] = 0.5;
133 
134  const map<string, double> expressionValueOne =
135  map_list_of
136  ("x*a", tOne["a"]*tOne["x"])
137  ("a+x", tOne["a"]+tOne["x"])
138  ("(x*a)", tOne["a"]*tOne["x"])
139  ("(a+x)", tOne["a"]+tOne["x"])
140  ("(x)+a", tOne["a"]+tOne["x"])
141  ("x+(a)", tOne["a"]+tOne["x"])
142  ("(x)*a", tOne["a"]*tOne["x"])
143  ("x*(a)", tOne["a"]*tOne["x"])
144  ("a+b*x+a", tOne["a"]+tOne["b"]*tOne["x"]+tOne["a"])
145  ("scient1+b*doub1+scient1*x^2-b*(-4.)", tOne["scient1"]+tOne["b"]*tOne["doub1"]+tOne["scient1"]*pow(tOne["x"],2)-tOne["b"]*(-4.))
146  ("(doub3-scient2)^2/c+(d/a)", pow(tOne["doub3"]-tOne["scient2"],2)/tOne["c"]+tOne["d"]/tOne["a"])
147  ("-(doub3-scient2)^2/c+(d/a)", -pow(tOne["doub3"]-tOne["scient2"],2)/tOne["c"]+tOne["d"]/tOne["a"])
148  ("-(doub3-scient2)^2+c+(d/a)", -pow(tOne["doub3"]-tOne["scient2"],2)+tOne["c"]+tOne["d"]/tOne["a"])
149  ("-(doub3-scient2)^2-c+(d/a)", -pow(tOne["doub3"]-tOne["scient2"],2)-tOne["c"]+tOne["d"]/tOne["a"])
150  ("c-(doub3-scient2)^2-c+(d/a)", tOne["c"]-pow(tOne["doub3"]-tOne["scient2"],2)-tOne["c"]+tOne["d"]/tOne["a"])
151  //("a+b*x+a+0.4", tOne["a"]+tOne["b"]*tOne["x"]+tOne["a"]+0.4)
152  ("b+c*x^2", tOne["b"]+tOne["c"]*pow(tOne["x"],2))
153  ("b+c*log10(x)", tOne["b"]+tOne["c"]*log10(tOne["x"]))
154  ("b+c*exp(x)", tOne["b"]+tOne["c"]*exp(tOne["x"]))
155  ("b+c*exp(-x)", tOne["b"]+tOne["c"]*exp(-tOne["x"]))
156  ("b+c*log10(x+a*exp(x)+b*x^(2-a))", tOne["b"]+tOne["c"]*log10(tOne["x"]+tOne["a"]*exp(tOne["x"])+tOne["b"]*pow(tOne["x"],2-tOne["a"])))
157  ("b+log(x+5)", tOne["b"]+log(tOne["x"]+5))
158  ("b+sin(x+5)", tOne["b"]+sin(tOne["x"]+5))
159  ("b+cos(x+5)", tOne["b"]+cos(tOne["x"]+5))
160  ("b+tan(x+5)", tOne["b"]+tan(tOne["x"]+5))
161  ;
162 
163  utl::FunctionParser evalOne(tOne);
164 
165  for (map<string, double>::const_iterator it = expressionValueOne.begin(), end = expressionValueOne.end();
166  it != end; ++it) {
167  ASSERT_CLOSE(evalOne.Evaluate(it->first), it->second);
168  }
169 
170 
171 
172  utl::SymbolTable tTwo = t;
173  tTwo["Xmax"] = 500*g/cm2;
174 
175  const map<string, double> expressionValueTwo =
176  map_list_of
177  ("(Xmax-400*g/cm2)+20*g/cm2", (tTwo["Xmax"]-400*g/cm2)+20*g/cm2)
178  ("Xmax/(100*g/cm2)-0.5", tTwo["Xmax"]/(100*g/cm2)-0.5)
179  ("-Xmax/(100*g/cm2)+10.5", -tTwo["Xmax"]/(100*g/cm2)+10.5)
180  ("exp(-Xmax/(100*g/cm2)+10.5)", exp(-tTwo["Xmax"]/(100*g/cm2)+10.5));
181 
182  utl::FunctionParser evalTwo(tTwo);
183 
184  for (map<string, double>::const_iterator it = expressionValueTwo.begin(), end = expressionValueTwo.end();
185  it != end; ++it) {
186  ASSERT_CLOSE(evalTwo.Evaluate(it->first), it->second);
187  }
188  }
189 };
190 
191 
void SetVerbosity(const EVerbosity verbosity)
Set the verbosity level.
Definition: ErrorLogger.h:88
double pow(const double x, const unsigned int i)
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
constexpr double s
Definition: AugerUnits.h:163
really everything
Definition: Verbosity.h:18
#define ASSERT_CLOSE(x, y)
constexpr double g
Definition: AugerUnits.h:200
static utl::SymbolTable fgUnitMap
std::map< std::string, double > SymbolTable
Definition: SymbolTable.h:10
a second level trigger
Definition: XbT2.h:8
static const utl::SymbolTable & GetSymbolMap()
static ErrorLogger & GetInstance()
Definition: Singleton.h:128
static void SetSymbols(const utl::SymbolTable &s)
constexpr double cm2
Definition: AugerUnits.h:118

, generated on Tue Sep 26 2023.