📄 xpath_test.cpp
字号:
#include "../../tag.h"using namespace gloox;#include <stdio.h>#include <locale.h>#include <string>int fail = 0;void printResult( const std::string& name, Tag::TagList& result ){ printf( ">-- %s --------------------------------------\n", name.c_str() ); int i = 0; Tag::TagList::const_iterator it = result.begin(); for( ; it != result.end(); ++it, ++i ) { printf( "tag #%d: %s\n", i, (*it)->xml().c_str() ); } printf( "<-- %s --------------------------------------\n", name.c_str() );}// void testLexer( const std::string& name )// {// int len = 0;// XPathToken *t = XPath::parse( name, len );// if( !t || t->toString() != name )// {// ++fail;// printf( "test 'lexer: %s' failed: %s\n", name.c_str(), t->toString().c_str() );// }// printf( "str: %s\n", t->toString().c_str() );// printf( "xml: %s\n", t->xml().c_str() );// delete t;// }int main( int /*argc*/, char** /*argv*/ ){ std::string name; Tag *aaa = new Tag( "aaa" ); Tag *bbb = new Tag( aaa, "bbb" ); bbb->addAttribute( "name", "b1" ); Tag *ccc = new Tag( aaa, "ccc" ); Tag *ddd = new Tag( ccc, "ddd" ); Tag *eee = new Tag( ccc, "eee" ); Tag *fff = new Tag( aaa, "fff" ); Tag *ggg = new Tag( fff, "ggg" ); Tag *hhh = new Tag( bbb, "hhh" ); hhh->addAttribute( "name", "h1" ); Tag *iii = new Tag( bbb, "bbb" ); iii->addAttribute( "name", "b2" ); Tag *jjj = new Tag( hhh, "bbb" ); jjj->addAttribute( "name", "b3" ); Tag::TagList result; Tag::TagList::const_iterator it;// XPathToken *t = 0;// <aaa>// <bbb name='b1'>// <hhh>// <bbb name='b3'/>// </hhh>// <bbb name='b2'/>// </bbb>// <ccc>// <ddd/>// <eee/>// </ccc>// <fff>// <ggg/>// </fff>// </aaa> /* * Lexer tests */// // -------// name = "/";// t = XPath::parse( name );// if( t != 0 )// {// ++fail;// printf( "test '%s' failed\n", name.c_str() );// }// delete t;// // -------// name = "//";// t = XPath::parse( name );// if( t != 0 )// {// ++fail;// printf( "test 'lexer: %s' failed\n", name.c_str() );// }// delete t; // ------- working// testLexer( "/abc" );// testLexer( "/abc/def" );// testLexer( "/abc//def" );//// testLexer( "/abc/def[//dgh]" );//// testLexer( "count(//dgh)" );//// testLexer( "/*/abc" );//// testLexer( "count(count(//dgh))" );//// testLexer( "*/abc" );//// testLexer( "*" );//// testLexer( "//*" );//// testLexer( "count(count(//dgh[//abc]))" );//// testLexer( "//c[id>count(//aaa|//bbb)]" );//// testLexer( "a/*/b" );//// testLexer( "a<b" );//// testLexer( "/./*" );//// testLexer( "/./../*" );//// testLexer( "/./../../." );//// testLexer( "a=b" );//// testLexer( "/b[@a='b']" ); // ------- ~working// testLexer( "a*b" );// testLexer( "a*b" );// testLexer( "a*b" ); // testLexer( "//c[id>count(//aaa|//bbb*//ccc)]" );// testLexer( "//aaa|//bbb*(//ccc+//abc)" );// // testLexer( "//a|(//b*//c)+//d" );// testLexer( "//a|//b*//c+//d" );// testLexer( "//c[id>count(//aaa|//bbb*//ccc)+//abc]" ); // -- simple paths -- // ------- name = "get root: /"; if( aaa->findTag( "/" ) != 0 ) { ++fail; printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "get deeproot: //"; if( aaa->findTag( "//" ) != 0 ) { ++fail; printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "get root tag: aaa"; if( aaa->findTag( "aaa" ) != aaa ) { ++fail; printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "get root tag: /aaa"; if( aaa->findTag( "/aaa" ) != aaa ) { ++fail; printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "simple child: /aaa/bbb"; if( aaa->findTag( "/aaa/bbb" ) != bbb ) { ++fail; printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "simple child: /aaa/ccc"; if( aaa->findTag( "/aaa/ccc" ) != ccc ) { ++fail; printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "simple child: /aaa/ccc/ddd"; if( aaa->findTag( "/aaa/ccc/ddd" ) != ddd ) { ++fail; printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "find all: //aaa"; if( aaa->findTag( "//aaa" ) != aaa ) { ++fail; printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "find all: //eee"; if( aaa->findTag( "//eee" ) != eee ) { ++fail; printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "find all: //bbb"; if( aaa->findTag( "//bbb" ) != bbb ) { ++fail; printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "get root tag from child: /aaa"; if( bbb->findTag( "/aaa" ) != aaa ) { ++fail; printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "fail test 1: /abc"; if( aaa->findTag( "/abc" ) != 0 ) { ++fail; printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "fail test 2: /bbb"; if( aaa->findTag( "/bbb" ) != 0 ) { ++fail; printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "relative find 1: aaa"; if( aaa->findTag( "aaa" ) != aaa ) { ++fail; printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "relative find 2: bbb"; if( bbb->findTag( "bbb" ) != bbb ) { ++fail; printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "find TagList: //bbb"; result = aaa->findTagList( "//bbb" ); it = result.begin(); if( result.size() != 3 || (*it) != bbb || (*++it) != jjj || (*++it) != iii ) { ++fail; printResult( name, result ); printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "find TagList: //ggg"; result = aaa->findTagList( "//ggg" ); it = result.begin(); if( result.size() != 1 || (*it) != ggg ) { ++fail; printResult( name, result ); printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "find all: //*"; result = aaa->findTagList( "//*" ); it = result.begin(); if( result.size() != 10 || (*it) != aaa || (*++it) != bbb || (*++it) != hhh || (*++it) != jjj || (*++it) != iii || (*++it) != ccc || (*++it) != ddd || (*++it) != eee || (*++it) != fff || (*++it) != ggg ) { ++fail; printResult( name, result ); printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "find first level: /*"; result = aaa->findTagList( "/*" ); if( result.size() != 1 || result.front() != aaa ) { ++fail; printResult( name, result ); printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "find second level: /*/*"; result = aaa->findTagList( "/*/*" ); it = result.begin(); if( result.size() != 3 || (*it) != bbb || (*++it) != ccc || (*++it) != fff ) { ++fail; printResult( name, result ); printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "find third level: /*/*/*"; result = aaa->findTagList( "/*/*/*" ); it = result.begin(); if( result.size() != 5 || (*it) != hhh || (*++it) != iii || (*++it) != ddd || (*++it) != eee || (*++it) != ggg ) { ++fail; printResult( name, result ); printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "find fourth level: /*/*/*/*"; result = aaa->findTagList( "/*/*/*/*" ); if( result.size() != 1 || result.front() != jjj ) { ++fail; printResult( name, result ); printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "find fith level: /*/*/*/*/*"; result = aaa->findTagList( "/*/*/*/*/*" ); if( result.size() != 0 ) { ++fail; printResult( name, result ); printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "find all sub-bbb: /*/*//bbb"; result = aaa->findTagList( "/*/*//bbb" ); if( result.size() != 2 || result.front() != jjj || result.back() != iii ) { ++fail; printResult( name, result ); printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "find second level bbb: /*/bbb"; result = aaa->findTagList( "/*/bbb" ); if( result.size() != 1 || result.front() != bbb ) { ++fail; printResult( name, result ); printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "find second level via self/noop: /*/./*"; result = aaa->findTagList( "/*/./*" ); it = result.begin(); if( result.size() != 3 || (*it) != bbb || (*++it) != ccc || (*++it) != fff ) { ++fail; printResult( name, result ); printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "find second level via repeated self/noop: /*/././*"; result = aaa->findTagList( "/*/././*" ); it = result.begin(); if( result.size() != 3 || (*it) != bbb || (*++it) != ccc || (*++it) != fff ) { ++fail; printResult( name, result ); printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "find first level via parent: /*/../*"; result = aaa->findTagList( "/*/../*" ); it = result.begin(); if( result.size() != 1 || (*it) != aaa ) { ++fail; printResult( name, result ); printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "invalid parent: /../*"; result = aaa->findTagList( "/../*" ); it = result.begin(); if( result.size() != 0 ) { ++fail; printResult( name, result ); printf( "test '%s' failed\n", name.c_str() ); } // ------- name = "deepsearch && * combined 1: //fff/*"; result = aaa->findTagList( "//fff/*" ); it = result.begin(); if( result.size() != 1 || (*it) != ggg ) { ++fail; printResult( name, result ); printf( "test '%s' failed\n", name.c_str() ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -