1 : // $Id: ExpressionParserTest.cc 59 2007-07-17 14:43:23Z tb $
2 :
3 : /*
4 : * STX Expression Parser C++ Framework v0.7
5 : * Copyright (C) 2007 Timo Bingmann
6 : *
7 : * This library is free software; you can redistribute it and/or modify it
8 : * under the terms of the GNU Lesser General Public License as published by the
9 : * Free Software Foundation; either version 2.1 of the License, or (at your
10 : * option) any later version.
11 : *
12 : * This library is distributed in the hope that it will be useful, but WITHOUT
13 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15 : * for more details.
16 : *
17 : * You should have received a copy of the GNU Lesser General Public License
18 : * along with this library; if not, write to the Free Software Foundation,
19 : * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 : */
21 :
22 : #include <cppunit/extensions/HelperMacros.h>
23 :
24 : #include "ExpressionParser.h"
25 :
26 : #include <stdlib.h>
27 : #include <boost/lexical_cast.hpp>
28 :
29 : class ExpressionParserTest : public CPPUNIT_NS::TestFixture
30 2 : {
31 3 : CPPUNIT_TEST_SUITE( ExpressionParserTest );
32 1 : CPPUNIT_TEST(test1);
33 2 : CPPUNIT_TEST_SUITE_END();
34 :
35 : protected:
36 :
37 18 : inline stx::AnyScalar eval(const std::string &str)
38 : {
39 18 : stx::ParseTree pt = stx::parseExpression(str);
40 :
41 : // test reevaluation of the toString().
42 : {
43 17 : std::string strexpr = pt.toString();
44 17 : stx::ParseTree pt2 = stx::parseExpression(strexpr);
45 :
46 17 : CPPUNIT_ASSERT( strexpr == pt2.toString() );
47 : }
48 :
49 17 : stx::BasicSymbolTable bst;
50 17 : bst.setVariable("a", 42);
51 17 : bst.setVariable("e", 2.7183);
52 :
53 17 : return pt.evaluate(bst);
54 : }
55 :
56 1 : void test1()
57 : {
58 : using namespace stx;
59 :
60 1 : CPPUNIT_ASSERT( eval("5 + 4.5") == 9.5 );
61 :
62 2 : CPPUNIT_ASSERT( eval(" \"abc\" + \"def\" ") == "abcdef" );
63 :
64 2 : CPPUNIT_ASSERT( eval("5 * 6 + 7 * 2 - 2") == 42 );
65 :
66 2 : CPPUNIT_ASSERT( eval("(integer)(5 * 1.5)") == 7 );
67 :
68 2 : CPPUNIT_ASSERT( eval("((integer)(5 * 1.5 >= 1)) + 5") == 6 );
69 :
70 2 : CPPUNIT_ASSERT( eval("(integer)(e)") == 2 );
71 :
72 2 : CPPUNIT_ASSERT( eval(" logn(exp(21)) + sqrt(pow(21,2)) == 42 ") == true );
73 :
74 2 : CPPUNIT_ASSERT( eval("10 > 5 AND 5.2 <= 42.2 OR 2 == 4") == true );
75 :
76 2 : CPPUNIT_ASSERT( eval("(a * 2 + 4) / 2 == 44") == true );
77 :
78 2 : CPPUNIT_ASSERT( eval("-5 + 4.5") == -0.5 );
79 :
80 2 : CPPUNIT_ASSERT( eval("-5 + 4.5 + (-a)") == -42.5 );
81 :
82 2 : CPPUNIT_ASSERT( eval(" \"newline \\n quoted \\\" a\\\\bc\" + \"def\" ") == "newline \n quoted \" a\\bcdef" );
83 :
84 2 : CPPUNIT_ASSERT( eval("(0 < 1) && (4 > 2) && (1 = 1) && (2 == 2) && (2 != 4) && (1 <= 1) && (1 >= 1) && (2 =< 3) && (4 => 1)") == true );
85 :
86 2 : CPPUNIT_ASSERT( eval("!(true AND false) || (NOT true OR FALSE)") == true );
87 :
88 2 : CPPUNIT_ASSERT_THROW( eval("5 +"), stx::ExpressionParserException );
89 :
90 1 : CPPUNIT_ASSERT_THROW( eval("5 + FUNCXYZ()"), stx::UnknownSymbolException );
91 :
92 1 : CPPUNIT_ASSERT_THROW( eval("5 + xyz"), stx::UnknownSymbolException );
93 :
94 1 : CPPUNIT_ASSERT_THROW( eval("5 + COS(2,2)"), stx::BadFunctionCallException );
95 :
96 : {
97 1 : std::string xmlstr = stx::parseExpressionXML("((integer)(5 * 1.5 >= 1)) + 5");
98 2 : CPPUNIT_ASSERT( xmlstr.size() == 1010 );
99 : }
100 1 : }
101 : };
102 0 :
103 3 : CPPUNIT_TEST_SUITE_REGISTRATION( ExpressionParserTest );
|