tinyxml2/xmltest.cpp

164 lines
4.6 KiB
C++
Raw Normal View History

2011-12-28 22:36:55 +00:00
#include "tinyxml2.h"
#include <stdio.h>
#include <stdlib.h>
2012-02-14 02:16:52 +00:00
#include <string.h>
2011-12-28 22:36:55 +00:00
#if defined( WIN32 )
#include <crtdbg.h>
_CrtMemState startMemState;
_CrtMemState endMemState;
#endif
2012-02-14 02:11:20 +00:00
2011-12-28 22:36:55 +00:00
using namespace tinyxml2;
2012-02-14 02:16:52 +00:00
int gPass = 0;
int gFail = 0;
bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true )
{
bool pass = !strcmp( expected, found );
if ( pass )
printf ("[pass]");
else
printf ("[fail]");
if ( !echo )
printf (" %s\n", testString);
else
printf (" %s [%s][%s]\n", testString, expected, found);
if ( pass )
++gPass;
else
++gFail;
return pass;
}
bool XMLTest( const char* testString, int expected, int found, bool echo=true )
{
bool pass = ( expected == found );
if ( pass )
printf ("[pass]");
else
printf ("[fail]");
if ( !echo )
printf (" %s\n", testString);
else
printf (" %s [%d][%d]\n", testString, expected, found);
if ( pass )
++gPass;
else
++gFail;
return pass;
}
2012-02-14 02:16:52 +00:00
2011-12-28 22:36:55 +00:00
int main( int argc, const char* argv )
{
#if defined( WIN32 )
_CrtMemCheckpoint( &startMemState );
#endif
2012-02-14 02:11:20 +00:00
#if 0
2011-12-31 22:58:18 +00:00
{
2012-01-15 02:08:12 +00:00
static const char* test = "<!--hello world\n"
2012-01-19 01:43:40 +00:00
" line 2\r"
" line 3\r\n"
" line 4\n\r"
" line 5\r-->";
XMLDocument doc;
doc.Parse( test );
2012-02-13 23:07:09 +00:00
doc.Print();
2012-01-19 01:43:40 +00:00
}
#endif
2012-02-13 23:07:09 +00:00
#if 0
2012-01-19 01:43:40 +00:00
{
static const char* test[] = { "<element />",
"<element></element>",
"<element><subelement/></element>",
"<element><subelement></subelement></element>",
"<element><subelement><subsub/></subelement></element>",
"<!--comment beside elements--><element><subelement></subelement></element>",
"<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
"<element attrib1='foo' attrib2=\"bar\" ></element>",
"<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
"<element>Text inside element.</element>",
"<element><b></b></element>",
"<element>Text inside and <b>bolded</b> in the element.</element>",
"<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
2012-01-26 01:44:30 +00:00
"<element>This &amp; That.</element>",
"<element attrib='This&lt;That' />",
2012-01-19 01:55:48 +00:00
0
};
2012-01-22 02:45:16 +00:00
for( int i=0; test[i]; ++i ) {
2012-01-19 01:55:48 +00:00
XMLDocument doc;
2012-01-22 02:45:16 +00:00
doc.Parse( test[i] );
2012-01-25 02:03:07 +00:00
doc.Print();
2012-01-23 19:42:06 +00:00
printf( "----------------------------------------------\n" );
2012-01-19 01:55:48 +00:00
}
2011-12-31 22:58:18 +00:00
}
2012-02-13 23:07:09 +00:00
#endif
2012-02-14 02:11:20 +00:00
#if 0
{
static const char* test = "<element>Text before.</element>";
XMLDocument doc;
doc.Parse( test );
XMLElement* root = doc.FirstChildElement();
XMLElement* newElement = doc.NewElement( "Subelement" );
root->InsertEndChild( newElement );
doc.Print();
}
{
XMLDocument* doc = new XMLDocument();
static const char* test = "<element><sub/></element>";
doc->Parse( test );
delete doc;
}
#endif
2012-02-14 02:11:20 +00:00
{
// Test: Programmatic DOM
2012-02-14 02:16:52 +00:00
// Build:
// <element>
// <!--comment-->
// <sub attrib="1" />
// <sub attrib="2" />
// <sub attrib="3" >& Text!</sub>
2012-02-14 02:16:52 +00:00
// <element>
2012-02-14 02:11:20 +00:00
XMLDocument* doc = new XMLDocument();
XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
for( int i=0; i<3; ++i ) {
sub[i]->SetAttribute( "attrib", i );
}
element->InsertEndChild( sub[2] );
XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
element->InsertAfterChild( comment, sub[0] );
element->InsertAfterChild( sub[0], sub[1] );
sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
2012-02-14 02:11:20 +00:00
doc->Print();
XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
XMLTest( "Programmatic DOM", "& Text!",
doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
2012-02-14 02:11:20 +00:00
delete doc;
}
2012-02-14 02:16:52 +00:00
#if defined( WIN32 )
_CrtMemCheckpoint( &endMemState );
//_CrtMemDumpStatistics( &endMemState );
_CrtMemState diffMemState;
_CrtMemDifference( &diffMemState, &startMemState, &endMemState );
_CrtMemDumpStatistics( &diffMemState );
#endif
printf ("\nPass %d, Fail %d\n", gPass, gFail);
2011-12-28 22:36:55 +00:00
return 0;
}