Update of /cvsroot/boost/boost/libs/xpressive/test
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv5038
Modified Files:
Jamfile.v2
Added Files:
test_symbols.cpp
Log Message:
add tests for attributes and symbols parser, thanks for David Jenkins
--- NEW FILE: test_symbols.cpp ---
///////////////////////////////////////////////////////////////////////////////
// test_symbols.cpp
//
// Copyright 2007 David Jenkins.
// Copyright 2007 Eric Niebler.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <map>
#include <boost/version.hpp>
#include <boost/xpressive/xpressive_static.hpp>
#include <boost/xpressive/regex_actions.hpp>
#include <boost/foreach.hpp>
#include <boost/test/unit_test.hpp>
///////////////////////////////////////////////////////////////////////////////
// test1
// simple action which builds a *translated* string
void test1()
{
using namespace boost::xpressive;
std::string result;
std::string str("foo bar baz foo bar baz");
std::map<std::string,std::string> map1;
map1["foo"] = "1";
map1["bar"] = "2";
map1["baz"] = "3";
sregex rx = (a1=map1)[ ref(result) = a1 ] >> *(' ' >> (a1=map1)[
ref(result) += ',' + a1 ]);
if(!regex_match(str, rx))
{
BOOST_ERROR("oops");
}
else
{
BOOST_CHECK_EQUAL(result, "1,2,3,1,2,3");
}
}
///////////////////////////////////////////////////////////////////////////////
// test2
// find longest match in symbol table
void test2()
{
using namespace boost::xpressive;
std::string result;
std::string str("foobarbazfoobazbazfoobazbar");
std::map<std::string,std::string> map1;
map1["foo"] = "1";
map1["bar"] = "2";
map1["baz"] = "3";
map1["foobaz"] = "4";
map1["foobazbaz"] = "5";
sregex rx = (a1=map1)[ ref(result) = a1 ]
>> *((a1=map1)[ ref(result) += ',', ref(result) += a1 ]);
if(!regex_match(str, rx))
{
BOOST_ERROR("oops");
}
else
{
BOOST_CHECK_EQUAL(result, "1,2,3,5,4,2");
}
}
///////////////////////////////////////////////////////////////////////////////
// test3
// *map* string to int, push back into list, use alternate ->* syntax
void test3()
{
using namespace boost::xpressive;
std::list<int> result;
std::string str("foo bar baz bop");
std::map<std::string,int> map1;
map1["foo"] = 1;
map1["bar"] = 23;
map1["baz"] = 456;
map1["bop"] = 7890;
#if BOOST_VERSION >= 103500
sregex rx = (a1=map1)[ ref(result)->*push_back( a1 ) ]
>> *(' ' >> (a1=map1)[ ref(result)->*push_back( a1 ) ]);
#else
sregex rx = (a1=map1)[ push_back(ref(result), a1 ) ]
>> *(' ' >> (a1=map1)[ push_back(ref(result), a1 ) ]);
#endif
if(!regex_match(str, rx))
{
BOOST_ERROR("oops");
}
else
{
BOOST_REQUIRE_EQUAL(result.size(), 4u);
BOOST_CHECK_EQUAL(*result.begin(), 1);
BOOST_CHECK_EQUAL(*++result.begin(), 23);
BOOST_CHECK_EQUAL(*++++result.begin(), 456);
BOOST_CHECK_EQUAL(*++++++result.begin(), 7890);
}
}
///////////////////////////////////////////////////////////////////////////////
// test4
// use two input maps to build an output map, with a late-bound action
argument.
void test4()
{
using namespace boost::xpressive;
placeholder< std::map<std::string, int> > const _map = {};
std::string str("aaa=>1 bbb=>2 ccc=>3");
std::map<std::string,std::string> map1;
map1["aaa"] = "foo";
map1["bbb"] = "bar";
map1["ccc"] = "baz";
std::map<std::string,int> map2;
map2["1"] = 1;
map2["2"] = 23;
map2["3"] = 456;
sregex pair = ( (a1=map1) >> "=>" >> (a2= map2) )[ _map[a1] = a2 ];
sregex rx = pair >> *(+_s >> pair);
smatch what;
std::map<std::string, int> result;
what.let(_map = result); // bind the argument!
if(!regex_match(str, what, rx))
{
BOOST_ERROR("oops");
}
else
{
BOOST_REQUIRE_EQUAL(result.size(), 3u);
BOOST_CHECK_EQUAL(result["foo"], 1);
BOOST_CHECK_EQUAL(result["bar"], 23);
BOOST_CHECK_EQUAL(result["baz"], 456);
}
}
///////////////////////////////////////////////////////////////////////////////
// test5
// test nine maps and attributes
void test5()
{
using namespace boost::xpressive;
int result = 0;
std::string str("abcdefghi");
std::map<std::string,int> map1;
std::map<std::string,int> map2;
std::map<std::string,int> map3;
std::map<std::string,int> map4;
std::map<std::string,int> map5;
std::map<std::string,int> map6;
std::map<std::string,int> map7;
std::map<std::string,int> map8;
std::map<std::string,int> map9;
map1["a"] = 1;
map2["b"] = 2;
map3["c"] = 3;
map4["d"] = 4;
map5["e"] = 5;
map6["f"] = 6;
map7["g"] = 7;
map8["h"] = 8;
map9["i"] = 9;
sregex rx =
(a1=map1)[ ref(result) += a1 ]
>> (a2=map2)[ ref(result) += a2 ]
>> (a3=map3)[ ref(result) += a3 ]
>> (a4=map4)[ ref(result) += a4 ]
>> (a5=map5)[ ref(result) += a5 ]
>> (a6=map6)[ ref(result) += a6 ]
>> (a7=map7)[ ref(result) += a7 ]
>> (a8=map8)[ ref(result) += a8 ]
>> (a9=map9)[ ref(result) += a9 ];
if(!regex_match(str, rx))
{
BOOST_ERROR("oops");
}
else
{
BOOST_CHECK_EQUAL(result, 45);
}
}
///////////////////////////////////////////////////////////////////////////////
// test6
// test case-sensitivity
void test6()
{
using namespace boost::xpressive;
std::string result;
std::map<std::string,std::string> map1;
map1["a"] = "1";
map1["A"] = "2";
map1["b"] = "3";
map1["B"] = "4";
std::string str("a A b B a A b B");
sregex rx = icase(a1= map1) [ ref(result) = a1 ]
>> repeat<3>( (' ' >> icase(a1= map1) [ ref(result) += ',', ref(result)
+= a1 ]) )
>> repeat<4>( (' ' >> (a1= map1) [ ref(result) += ',', ref(result)
+= a1 ]) );
if(!regex_match(str, rx))
{
BOOST_ERROR("oops");
}
else
{
BOOST_CHECK_EQUAL(result, "1,1,3,3,1,2,3,4");
}
}
///////////////////////////////////////////////////////////////////////////////
// test7
// test multiple mutually-exclusive maps and default attribute value
void test7()
{
using namespace boost::xpressive;
std::string result;
std::map<std::string,std::string> map1;
map1["a"] = "1";
map1["b"] = "2";
std::map<std::string,std::string> map2;
map2["c"] = "3";
map2["d"] = "4";
std::string str("abcde");
sregex rx = *((a1= map1) | (a1= map2) | 'e') [ ref(result) += (a1 | "9") ];
if(!regex_match(str, rx))
{
BOOST_ERROR("oops");
}
else
{
BOOST_CHECK_EQUAL(result, "12349");
}
}
struct City
{
std::string name;
char* nickname;
int population;
};
///////////////////////////////////////////////////////////////////////////////
// test8
// test wide strings with structure result
void test8()
{
using namespace boost::xpressive;
City cities[] = {
{"Chicago", "The Windy City", 945000},
{"New York", "The Big Apple", 16626000},
{"\u041c\u043E\u0441\u043A\u0432\u0430", "Moscow", 9299000}
};
std::map<std::string, City> map1;
BOOST_FOREACH(const City & c, cities)
map1[c.name] = c;
std::string str("Chicago \u041c\u043E\u0441\u043A\u0432\u0430");
City result1, result2;
//int result1p;
// ERROR "error C2039: 'population' : is not a member of
'boost::proto::expr<Tag,Args>'
c:\boost\libs\xpressive\test\test_symbols.cpp 277"
//sregex rx = (a1= map1)[ ref(result1p) = a1.population ];
sregex rx = (a1= map1)[ ref(result1) = a1 ] >> +_s
>> (a1= map1)[ ref(result2) = a1 ];
if(!regex_match(str, rx))
{
BOOST_ERROR("oops");
}
else
{
BOOST_CHECK_EQUAL(result1.nickname, "The Windy City");
BOOST_CHECK_EQUAL(result2.nickname, "Moscow");
}
}
///////////////////////////////////////////////////////////////////////////////
// test9
// test "not before" using a map
void test9()
{
using namespace boost::xpressive;
std::string result;
std::string str("foobar");
std::map<std::string,int> map1;
map1["foo"] = 1;
int xx = 0;
sregex rx = ~before((a1=map1)[ref(xx)=a1]) >> (s1=*_w)[ ref(result) = s1 ];
if(!regex_match(str, rx))
{
BOOST_CHECK_EQUAL(result, "");
}
else
{
BOOST_ERROR("oops");
}
}
using namespace boost::unit_test;
///////////////////////////////////////////////////////////////////////////////
// init_unit_test_suite
//
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
test_suite *test = BOOST_TEST_SUITE("test_symbols");
test->add(BOOST_TEST_CASE(&test1));
test->add(BOOST_TEST_CASE(&test2));
test->add(BOOST_TEST_CASE(&test3));
test->add(BOOST_TEST_CASE(&test4));
test->add(BOOST_TEST_CASE(&test5));
test->add(BOOST_TEST_CASE(&test6));
test->add(BOOST_TEST_CASE(&test7));
test->add(BOOST_TEST_CASE(&test8));
test->add(BOOST_TEST_CASE(&test9));
return test;
}
Index: Jamfile.v2
===================================================================
RCS file: /cvsroot/boost/boost/libs/xpressive/test/Jamfile.v2,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -d -r1.30 -r1.31
--- Jamfile.v2 15 Apr 2007 06:40:26 -0000 1.30
+++ Jamfile.v2 22 Jun 2007 00:38:16 -0000 1.31
@@ -49,6 +49,7 @@
[ run test_non_char.cpp ]
[ run test_static.cpp ]
[ run test_actions.cpp ]
+ [ run test_symbols.cpp ]
[ run test_dynamic.cpp ]
[ run test_dynamic_grammar.cpp ]
[ link multiple_defs1.cpp multiple_defs2.cpp : : multiple_defs ]
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Boost-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/boost-cvs