Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkFormulaParserTest.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkTestingMacros.h"
14 #include "mitkFormulaParser.h"
15 
16 using namespace mitk;
17 
18 #define TEST_NOTHROW(expression, MSG) \
19  do \
20  { \
21  MITK_TEST_OUTPUT_NO_ENDL(<< MSG) \
22  bool test_caught = false; \
23  try \
24  { \
25  expression; \
26  } \
27  catch(...) \
28  { \
29  test_caught = true; \
30  MITK_TEST_FAILED_MSG(<< "An unwanted exception was thrown"); \
31  } \
32  if(!test_caught) \
33  { \
34  MITK_TEST_OUTPUT(<< " [PASSED]") \
35  mitk::TestManager::GetInstance()->TestPassed(); \
36  } \
37  } while(0)
38 
42 class FormulaParserTests
43 {
44 public:
45  static void TestConstructor()
46  {
47  std::map<std::string, double> varMap;
48  FormulaParser *nullParser = nullptr, *parser = nullptr;
49 
50  TEST_NOTHROW(nullParser = new FormulaParser(nullptr),
51  "Testing constructor with NULL argument");
52  TEST_NOTHROW(parser = new FormulaParser(&varMap),
53  "Testing constructor with valid argument");
54 
55  delete nullParser;
56  delete parser;
57  }
58 
59  static void TestLookupVariable()
60  {
61  // variable map is NULL
62  FormulaParser *nullParser = new FormulaParser(nullptr);
64  delete nullParser;
65 
66  // variable map is empty
67  std::map<std::string, double> varMap;
68  FormulaParser *parser = new FormulaParser(&varMap);
70 
71  // lookup should succeed
72  double var;
73  varMap["test"] = 17;
74  TEST_NOTHROW(var = parser->lookupVariable("test"),
75  "Testing if lookupVariable throws unwanted exceptions");
77  "Testing if lookupVariable returns the correct value");
78 
79  delete parser;
80  }
81 
82  static void TestParse()
83  {
84  std::map<std::string, double> varMap;
85  varMap["test"] = 17;
86  FormulaParser *parser = new FormulaParser(&varMap);
87 
88  // empty string
90 
91  // grammar can't process string
93 
94  // unexpected character
96 
97  // unknown variable
99 
100  double d;
101 
102  // addition
103  TEST_NOTHROW(d = parser->parse("1+2"),
104  "Testing if addition throws an unwanted exception");
106  "Testing if addition produces the correct result");
107 
108  // subtraction
109  TEST_NOTHROW(d = parser->parse("5-1"),
110  "Testing if subtraction throws an unwanted exception");
112  "Testing if subtraction produces the correct result");
113 
114  // multiplication
115  TEST_NOTHROW(d = parser->parse("3*4"),
116  "Testing if multiplication throws an unwanted exception");
118  "Testing if multiplication produces the correct result");
119 
120  // division
121  TEST_NOTHROW(d = parser->parse("28/4"),
122  "Testing if division throws an unwanted exception");
124  "Testing if division produces the correct result");
125 
127  // exponentiation
128  //TEST_NOTHROW(d = parser->parse("2^3"),
129  // "Testing if exponentiation throws an unwanted exception");
130  //MITK_TEST_CONDITION_REQUIRED(d == 8,
131  // "Testing if exponentiation produces the correct result");
132 
133  // algebraic signs
134  TEST_NOTHROW(d = parser->parse("-7 + +1 - -1"),
135  "Testing if algebraic signs throw an unwanted exception");
137  "Testing if algebraic signs produce the correct result");
138 
139  // parentheses
140  TEST_NOTHROW(d = parser->parse("(1+2)*(4-2)"),
141  "Testing if parentheses throw an unwanted exception");
143  "Testing if parentheses produce the correct result");
144 
145  // variables
146  TEST_NOTHROW(d = parser->parse("2*test-test"),
147  "Testing if variables throw an unwanted exception");
149  "Testing if variables produce the correct result");
150 
151  // abs
152  TEST_NOTHROW(d = parser->parse("abs(-5)"),
153  "Testing if abs throws an unwanted exception");
155  "Testing if abs produces the correct result");
156 
157  const double eps = 0.0001;
158 
159  // exp
160  TEST_NOTHROW(d = parser->parse("exp(1)"),
161  "Testing if exp throws an unwanted exception");
162  MITK_TEST_CONDITION_REQUIRED(std::abs(d - 2.71828182846) < eps,
163  "Testing if exp produces the correct result");
164 
165  // sin
166  TEST_NOTHROW(d = parser->parse("sin(1.57079632679)"),
167  "Testing if sin throws an unwanted exception");
168  MITK_TEST_CONDITION_REQUIRED(std::abs(d - 1) < eps,
169  "Testing if sin produces the correct result");
170 
171  // cos
172  TEST_NOTHROW(d = parser->parse("cos(3.14159265359)"),
173  "Testing if cos throws an unwanted exception");
174  MITK_TEST_CONDITION_REQUIRED(std::abs(d + 1) < eps,
175  "Testing if cos produces the correct result");
176 
177  // tan
178  TEST_NOTHROW(d = parser->parse("tan(0.46364760899)"),
179  "Testing if tan throws an unwanted exception");
180  MITK_TEST_CONDITION_REQUIRED(std::abs(d - 0.5) < eps,
181  "Testing if tan produces the correct result");
182 
183  // sind
184  TEST_NOTHROW(d = parser->parse("sind(145)"),
185  "Testing if sind throws an unwanted exception");
186  MITK_TEST_CONDITION_REQUIRED(std::abs(d - 0.57357643635) < eps,
187  "Testing if sind produces the correct result");
188 
189  // cosd
190  TEST_NOTHROW(d = parser->parse("cosd(90)"),
191  "Testing if cosd throws an unwanted exception");
193  "Testing if cosd produces the correct result");
194 
195  // tand
196  TEST_NOTHROW(d = parser->parse("tand(15)"),
197  "Testing if tand throws an unwanted exception");
198  MITK_TEST_CONDITION_REQUIRED(std::abs(d - 0.26794919243) < eps,
199  "Testing if tand produces the correct result");
200 
201  // fresnelS
202  TEST_NOTHROW(d = parser->parse("fresnelS(1)"),
203  "Testing if fresnelS throws an unwanted exception");
204  MITK_TEST_CONDITION_REQUIRED(std::abs(d - 0.310268) < eps,
205  "Testing if fresnelS produces the correct result");
206 
207  TEST_NOTHROW(d = parser->parse("fresnelC(1)"),
208  "Testing if fresnelC throws an unwanted exception");
209  MITK_TEST_CONDITION_REQUIRED(std::abs(d - 0.904524) < eps,
210  "Testing if fresnelC produces the correct result");
211 
212  delete parser;
213  }
214 };
215 
216 int mitkFormulaParserTest(int, char *[])
217 {
218  MITK_TEST_BEGIN("FormulaParser Test");
219 
220  FormulaParserTests::TestConstructor();
221  FormulaParserTests::TestLookupVariable();
222  FormulaParserTests::TestParse();
223 
224  MITK_TEST_END();
225 }
This class offers the functionality to evaluate simple mathematical formula strings (e...
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
DataCollection - Class to facilitate loading/accessing structured data.
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
ValueType lookupVariable(const std::string var)
Looks up the associated value of the given string var in the variables map.
#define MITK_TEST_FOR_EXCEPTION(EXCEPTIONCLASS, STATEMENT)
Simplified version of MITK_TEST_FOR_EXCEPTION_BEGIN / END for a single statement. ...
Exception class for all exceptions that are generated in the FormulaParser module.
ValueType parse(const std::string &input)
Evaluates the input string and returns the resulting value.
MITKCORE_EXPORT const ScalarType eps
and MITK_TEST_END()
#define TEST_NOTHROW(expression, MSG)
int mitkFormulaParserTest(int, char *[])