| 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 |
| 89 | MITK_TEST_FOR_EXCEPTION(FormulaParserException, parser->parse("")); |
| 90 | |
| 91 | // grammar can't process string |
| 92 | MITK_TEST_FOR_EXCEPTION(FormulaParserException, parser->parse("_")); |
| 93 | |
| 94 | // unexpected character |
| 95 | MITK_TEST_FOR_EXCEPTION(FormulaParserException, parser->parse("5=")); |
| 96 | |
| 97 | // unknown variable |
| 98 | MITK_TEST_FOR_EXCEPTION(FormulaParserException, parser->parse("a")); |
| 99 | |
| 100 | double d; |
| 101 | |
| 102 | // addition |
| 103 | TEST_NOTHROW(d = parser->parse("1+2"), |
| 104 | "Testing if addition throws an unwanted exception"); |
| 105 | MITK_TEST_CONDITION_REQUIRED(d == 3, |
| 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"); |
| 111 | MITK_TEST_CONDITION_REQUIRED(d == 4, |
| 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"); |
| 117 | MITK_TEST_CONDITION_REQUIRED(d == 12, |
| 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"); |
| 123 | MITK_TEST_CONDITION_REQUIRED(d == 7, |
| 124 | "Testing if division produces the correct result"); |
| 125 | |
| 126 | // exponentiation |
| 127 | TEST_NOTHROW(d = parser->parse("2^3"), |
| 128 | "Testing if exponentiation throws an unwanted exception"); |
| 129 | MITK_TEST_CONDITION_REQUIRED(d == 8, |
| 130 | "Testing if exponentiation produces the correct result"); |
| 131 | |
| 132 | // algebraic signs |
| 133 | TEST_NOTHROW(d = parser->parse("-7 + +1 - -1"), |
| 134 | "Testing if algebraic signs throw an unwanted exception"); |
| 135 | MITK_TEST_CONDITION_REQUIRED(d == -5, |
| 136 | "Testing if algebraic signs produce the correct result"); |
| 137 | |
| 138 | // parentheses |
| 139 | TEST_NOTHROW(d = parser->parse("(1+2)*(4-2)"), |