Line data Source code
1 : #define BOOST_TEST_MODULE interface 2 : #include <boost/test/included/unit_test.hpp> 3 : 4 : #include <cmath> 5 : #include <limits> 6 : #include <map> 7 : #include <stdexcept> 8 : #include <string> 9 : 10 : #include "matheval.hpp" 11 : 12 4 : BOOST_AUTO_TEST_CASE(integration1) { 13 4 : std::string expr = "pow(x/2 + sqrt(x**2/4 + y**3/24), 1/3)"; 14 2 : double x = 2.0, y = -1.0; 15 : 16 4 : matheval::Parser parser; 17 2 : BOOST_CHECK_NO_THROW(parser.parse(expr)); 18 : 19 4 : std::map<std::string, double> symbol_table; 20 2 : symbol_table.insert(std::make_pair("x", x)); 21 2 : symbol_table.insert(std::make_pair("y", y)); 22 : 23 2 : double result = 0; 24 2 : BOOST_CHECK_NO_THROW(result = parser.evaluate(symbol_table)); 25 : 26 2 : double expected = std::pow( 27 2 : x / 2. + std::sqrt(std::pow(x, 2.) / 4. + std::pow(y, 3.) / 24.), 28 2 : 1. / 3.); 29 2 : BOOST_CHECK_CLOSE_FRACTION(result, expected, 30 : std::numeric_limits<double>::epsilon()); 31 2 : } 32 : 33 4 : BOOST_AUTO_TEST_CASE(integration2) { 34 4 : std::string expr = "("; 35 : 36 4 : matheval::Parser parser; 37 : 38 : // Parsing should fail 39 6 : BOOST_CHECK_THROW(parser.parse(expr), std::runtime_error); 40 2 : } 41 : 42 4 : BOOST_AUTO_TEST_CASE(integration3) { 43 4 : std::string expr = "x"; 44 : 45 4 : matheval::Parser parser; 46 : 47 2 : BOOST_CHECK_NO_THROW(parser.parse(expr)); 48 : 49 : // Evaluating should fail 50 8 : BOOST_CHECK_THROW(parser.evaluate(), 51 : std::invalid_argument); 52 2 : } 53 : 54 4 : BOOST_AUTO_TEST_CASE(integration4) { 55 4 : std::string expr = "1 + 1"; 56 : 57 4 : matheval::Parser parser; 58 : 59 2 : BOOST_CHECK_NO_THROW(parser.parse(expr)); 60 : 61 2 : BOOST_CHECK_NO_THROW(parser.optimize()); 62 2 : }