U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1 | #include "tinyxml2.h"
|
| 2 |
|
| 3 | #include <stdio.h>
|
| 4 | #include <stdlib.h>
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 5 | #include <string.h>
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 6 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 7 | #if defined( WIN32 )
|
| 8 | #include <crtdbg.h>
|
| 9 | _CrtMemState startMemState;
|
| 10 | _CrtMemState endMemState;
|
| 11 | #endif
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 12 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 13 | using namespace tinyxml2;
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 14 | int gPass = 0;
|
| 15 | int gFail = 0;
|
| 16 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 17 | //#define DREAM_ONLY
|
| 18 |
|
| 19 | /*
|
| 20 | int gNew = 0;
|
| 21 | int gNewTotal = 0;
|
| 22 |
|
| 23 | void* operator new( size_t size )
|
| 24 | {
|
| 25 | ++gNew;
|
| 26 | return malloc( size );
|
| 27 | }
|
| 28 |
|
| 29 | void* operator new[]( size_t size )
|
| 30 | {
|
| 31 | ++gNew;
|
| 32 | return malloc( size );
|
| 33 | }
|
| 34 |
|
| 35 | void operator delete[]( void* mem )
|
| 36 | {
|
| 37 | free( mem );
|
| 38 | }
|
| 39 |
|
| 40 | void operator delete( void* mem )
|
| 41 | {
|
| 42 | free( mem );
|
| 43 | }
|
| 44 | */
|
| 45 |
|
| 46 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 47 | bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 48 | {
|
| 49 | bool pass = !strcmp( expected, found );
|
| 50 | if ( pass )
|
| 51 | printf ("[pass]");
|
| 52 | else
|
| 53 | printf ("[fail]");
|
| 54 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 55 | if ( !echo )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 56 | printf (" %s\n", testString);
|
| 57 | else
|
| 58 | printf (" %s [%s][%s]\n", testString, expected, found);
|
| 59 |
|
| 60 | if ( pass )
|
| 61 | ++gPass;
|
| 62 | else
|
| 63 | ++gFail;
|
| 64 | return pass;
|
| 65 | }
|
| 66 |
|
| 67 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 68 | bool XMLTest( const char* testString, int expected, int found, bool echo=true )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 69 | {
|
| 70 | bool pass = ( expected == found );
|
| 71 | if ( pass )
|
| 72 | printf ("[pass]");
|
| 73 | else
|
| 74 | printf ("[fail]");
|
| 75 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 76 | if ( !echo )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 77 | printf (" %s\n", testString);
|
| 78 | else
|
| 79 | printf (" %s [%d][%d]\n", testString, expected, found);
|
| 80 |
|
| 81 | if ( pass )
|
| 82 | ++gPass;
|
| 83 | else
|
| 84 | ++gFail;
|
| 85 | return pass;
|
| 86 | }
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 87 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 88 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 89 | void NullLineEndings( char* p )
|
| 90 | {
|
| 91 | while( p && *p ) {
|
| 92 | if ( *p == '\n' || *p == '\r' ) {
|
| 93 | *p = 0;
|
| 94 | return;
|
| 95 | }
|
| 96 | ++p;
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 101 | int main( int argc, const char* argv )
|
| 102 | {
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 103 | #if defined( WIN32 )
|
| 104 | _CrtMemCheckpoint( &startMemState );
|
| 105 | #endif
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 106 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 107 | {
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 108 | static const char* test[] = { "<element />",
|
| 109 | "<element></element>",
|
| 110 | "<element><subelement/></element>",
|
| 111 | "<element><subelement></subelement></element>",
|
| 112 | "<element><subelement><subsub/></subelement></element>",
|
| 113 | "<!--comment beside elements--><element><subelement></subelement></element>",
|
| 114 | "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
|
| 115 | "<element attrib1='foo' attrib2=\"bar\" ></element>",
|
| 116 | "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
|
| 117 | "<element>Text inside element.</element>",
|
| 118 | "<element><b></b></element>",
|
| 119 | "<element>Text inside and <b>bolded</b> in the element.</element>",
|
| 120 | "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 121 | "<element>This & That.</element>",
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 122 | "<element attrib='This<That' />",
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 123 | 0
|
| 124 | };
|
Lee Thomason | 6ee99fc | 2012-01-21 18:45:16 -0800 | [diff] [blame] | 125 | for( int i=0; test[i]; ++i ) {
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 126 | XMLDocument doc;
|
Lee Thomason | 6ee99fc | 2012-01-21 18:45:16 -0800 | [diff] [blame] | 127 | doc.Parse( test[i] );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 128 | doc.Print();
|
Lee Thomason | ec975ce | 2012-01-23 11:42:06 -0800 | [diff] [blame] | 129 | printf( "----------------------------------------------\n" );
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 130 | }
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 131 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame^] | 132 | #if 1
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 133 | {
|
| 134 | static const char* test = "<!--hello world\n"
|
| 135 | " line 2\r"
|
| 136 | " line 3\r\n"
|
| 137 | " line 4\n\r"
|
| 138 | " line 5\r-->";
|
| 139 |
|
| 140 | XMLDocument doc;
|
| 141 | doc.Parse( test );
|
| 142 | doc.Print();
|
| 143 | }
|
| 144 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 145 | {
|
| 146 | static const char* test = "<element>Text before.</element>";
|
| 147 | XMLDocument doc;
|
| 148 | doc.Parse( test );
|
| 149 | XMLElement* root = doc.FirstChildElement();
|
| 150 | XMLElement* newElement = doc.NewElement( "Subelement" );
|
| 151 | root->InsertEndChild( newElement );
|
| 152 | doc.Print();
|
| 153 | }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 154 | {
|
| 155 | XMLDocument* doc = new XMLDocument();
|
| 156 | static const char* test = "<element><sub/></element>";
|
| 157 | doc->Parse( test );
|
| 158 | delete doc;
|
| 159 | }
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 160 | {
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 161 | // Test: Programmatic DOM
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 162 | // Build:
|
| 163 | // <element>
|
| 164 | // <!--comment-->
|
| 165 | // <sub attrib="1" />
|
| 166 | // <sub attrib="2" />
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 167 | // <sub attrib="3" >& Text!</sub>
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 168 | // <element>
|
| 169 |
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 170 | XMLDocument* doc = new XMLDocument();
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 171 | XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
|
| 172 |
|
| 173 | XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
|
| 174 | for( int i=0; i<3; ++i ) {
|
| 175 | sub[i]->SetAttribute( "attrib", i );
|
| 176 | }
|
| 177 | element->InsertEndChild( sub[2] );
|
| 178 | XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
|
| 179 | element->InsertAfterChild( comment, sub[0] );
|
| 180 | element->InsertAfterChild( sub[0], sub[1] );
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 181 | sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 182 | doc->Print();
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 183 | XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
|
| 184 | XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
|
| 185 | XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
|
| 186 | XMLTest( "Programmatic DOM", "& Text!",
|
| 187 | doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 188 |
|
| 189 | // And now deletion:
|
| 190 | element->DeleteChild( sub[2] );
|
| 191 | doc->DeleteNode( comment );
|
| 192 |
|
| 193 | element->FirstChildElement()->SetAttribute( "attrib", true );
|
| 194 | element->LastChildElement()->DeleteAttribute( "attrib" );
|
| 195 |
|
| 196 | XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
|
| 197 | int value = 10;
|
| 198 | int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
|
| 199 | XMLTest( "Programmatic DOM", result, NO_ATTRIBUTE );
|
| 200 | XMLTest( "Programmatic DOM", value, 10 );
|
| 201 |
|
| 202 | doc->Print();
|
| 203 |
|
| 204 | XMLStreamer streamer;
|
| 205 | doc->Print( &streamer );
|
| 206 | printf( "%s", streamer.CStr() );
|
| 207 |
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 208 | delete doc;
|
| 209 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 210 | {
|
| 211 | // Test: Dream
|
| 212 | // XML1 : 1,187,569 bytes in 31,209 allocations
|
| 213 | // XML2 : 469,073 bytes in 323 allocations
|
| 214 | //int newStart = gNew;
|
| 215 | XMLDocument doc;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 216 | doc.LoadFile( "dream.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 217 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 218 | doc.SaveFile( "dreamout.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 219 | doc.PrintError();
|
| 220 |
|
| 221 | XMLTest( "Dream", "xml version=\"1.0\"",
|
| 222 | doc.FirstChild()->ToDeclaration()->Value() );
|
| 223 | XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
|
| 224 | XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
|
| 225 | doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
|
| 226 | XMLTest( "Dream", "And Robin shall restore amends.",
|
| 227 | doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
| 228 | XMLTest( "Dream", "And Robin shall restore amends.",
|
| 229 | doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
| 230 |
|
| 231 | XMLDocument doc2;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 232 | doc2.LoadFile( "dreamout.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 233 | XMLTest( "Dream-out", "xml version=\"1.0\"",
|
| 234 | doc2.FirstChild()->ToDeclaration()->Value() );
|
| 235 | XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
|
| 236 | XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
|
| 237 | doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
|
| 238 | XMLTest( "Dream-out", "And Robin shall restore amends.",
|
| 239 | doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
| 240 |
|
| 241 | //gNewTotal = gNew - newStart;
|
| 242 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 243 | {
|
| 244 | const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
|
| 245 | "<passages count=\"006\" formatversion=\"20020620\">\n"
|
| 246 | " <wrong error>\n"
|
| 247 | "</passages>";
|
| 248 |
|
| 249 | XMLDocument doc;
|
| 250 | doc.Parse( error );
|
| 251 | XMLTest( "Bad XML", doc.ErrorID(), ERROR_PARSING_ATTRIBUTE );
|
| 252 | }
|
| 253 |
|
| 254 | {
|
| 255 | const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
|
| 256 |
|
| 257 | XMLDocument doc;
|
| 258 | doc.Parse( str );
|
| 259 |
|
| 260 | XMLElement* ele = doc.FirstChildElement();
|
| 261 |
|
| 262 | int iVal, result;
|
| 263 | double dVal;
|
| 264 |
|
| 265 | result = ele->QueryDoubleAttribute( "attr0", &dVal );
|
| 266 | XMLTest( "Query attribute: int as double", result, XML_NO_ERROR );
|
| 267 | XMLTest( "Query attribute: int as double", (int)dVal, 1 );
|
| 268 | result = ele->QueryDoubleAttribute( "attr1", &dVal );
|
| 269 | XMLTest( "Query attribute: double as double", (int)dVal, 2 );
|
| 270 | result = ele->QueryIntAttribute( "attr1", &iVal );
|
| 271 | XMLTest( "Query attribute: double as int", result, XML_NO_ERROR );
|
| 272 | XMLTest( "Query attribute: double as int", iVal, 2 );
|
| 273 | result = ele->QueryIntAttribute( "attr2", &iVal );
|
| 274 | XMLTest( "Query attribute: not a number", result, WRONG_ATTRIBUTE_TYPE );
|
| 275 | result = ele->QueryIntAttribute( "bar", &iVal );
|
| 276 | XMLTest( "Query attribute: does not exist", result, NO_ATTRIBUTE );
|
| 277 | }
|
| 278 |
|
| 279 | {
|
| 280 | const char* str = "<doc/>";
|
| 281 |
|
| 282 | XMLDocument doc;
|
| 283 | doc.Parse( str );
|
| 284 |
|
| 285 | XMLElement* ele = doc.FirstChildElement();
|
| 286 |
|
| 287 | int iVal;
|
| 288 | double dVal;
|
| 289 |
|
| 290 | ele->SetAttribute( "str", "strValue" );
|
| 291 | ele->SetAttribute( "int", 1 );
|
| 292 | ele->SetAttribute( "double", -1.0 );
|
| 293 |
|
| 294 | const char* cStr = ele->Attribute( "str" );
|
| 295 | ele->QueryIntAttribute( "int", &iVal );
|
| 296 | ele->QueryDoubleAttribute( "double", &dVal );
|
| 297 |
|
| 298 | XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
|
| 299 | XMLTest( "Attribute round trip. int.", 1, iVal );
|
| 300 | XMLTest( "Attribute round trip. double.", -1, (int)dVal );
|
| 301 | }
|
| 302 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 303 | {
|
| 304 | XMLDocument doc;
|
| 305 | doc.LoadFile( "utf8test.xml" );
|
| 306 |
|
| 307 | // Get the attribute "value" from the "Russian" element and check it.
|
| 308 | XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
|
| 309 | const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
|
| 310 | 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
|
| 311 |
|
| 312 | XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
|
| 313 |
|
| 314 | const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
|
| 315 | 0xd1U, 0x81U, 0xd1U, 0x81U,
|
| 316 | 0xd0U, 0xbaU, 0xd0U, 0xb8U,
|
| 317 | 0xd0U, 0xb9U, 0 };
|
| 318 | const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
|
| 319 |
|
| 320 | XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
|
| 321 | XMLTest( "UTF-8: Browsing russian element name.",
|
| 322 | russianText,
|
| 323 | text->Value() );
|
| 324 |
|
| 325 | // Now try for a round trip.
|
| 326 | doc.SaveFile( "utf8testout.xml" );
|
| 327 |
|
| 328 | // Check the round trip.
|
| 329 | char savedBuf[256];
|
| 330 | char verifyBuf[256];
|
| 331 | int okay = 0;
|
| 332 |
|
| 333 | FILE* saved = fopen( "utf8testout.xml", "r" );
|
| 334 | FILE* verify = fopen( "utf8testverify.xml", "r" );
|
| 335 |
|
| 336 | if ( saved && verify )
|
| 337 | {
|
| 338 | okay = 1;
|
| 339 | while ( fgets( verifyBuf, 256, verify ) )
|
| 340 | {
|
| 341 | fgets( savedBuf, 256, saved );
|
| 342 | NullLineEndings( verifyBuf );
|
| 343 | NullLineEndings( savedBuf );
|
| 344 |
|
| 345 | if ( strcmp( verifyBuf, savedBuf ) )
|
| 346 | {
|
| 347 | printf( "verify:%s<\n", verifyBuf );
|
| 348 | printf( "saved :%s<\n", savedBuf );
|
| 349 | okay = 0;
|
| 350 | break;
|
| 351 | }
|
| 352 | }
|
| 353 | }
|
| 354 | if ( saved )
|
| 355 | fclose( saved );
|
| 356 | if ( verify )
|
| 357 | fclose( verify );
|
| 358 | XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
|
| 359 | }
|
| 360 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame^] | 361 | // --------GetText()-----------
|
| 362 | {
|
| 363 | const char* str = "<foo>This is text</foo>";
|
| 364 | XMLDocument doc;
|
| 365 | doc.Parse( str );
|
| 366 | const XMLElement* element = doc.RootElement();
|
| 367 |
|
| 368 | XMLTest( "GetText() normal use.", "This is text", element->GetText() );
|
| 369 |
|
| 370 | str = "<foo><b>This is text</b></foo>";
|
| 371 | doc.Parse( str );
|
| 372 | element = doc.RootElement();
|
| 373 |
|
| 374 | XMLTest( "GetText() contained element.", element->GetText() == 0, true );
|
| 375 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 376 |
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 377 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame^] | 378 | // ---------- CDATA ---------------
|
| 379 | {
|
| 380 | const char* str = "<xmlElement>"
|
| 381 | "<![CDATA["
|
| 382 | "I am > the rules!\n"
|
| 383 | "...since I make symbolic puns"
|
| 384 | "]]>"
|
| 385 | "</xmlElement>";
|
| 386 | XMLDocument doc;
|
| 387 | doc.Parse( str );
|
| 388 | doc.Print();
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 389 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame^] | 390 | XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
|
| 391 | "I am > the rules!\n...since I make symbolic puns",
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 392 | false );
|
| 393 | }
|
| 394 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame^] | 395 | // ----------- CDATA -------------
|
| 396 | {
|
| 397 | const char* str = "<xmlElement>"
|
| 398 | "<![CDATA["
|
| 399 | "<b>I am > the rules!</b>\n"
|
| 400 | "...since I make symbolic puns"
|
| 401 | "]]>"
|
| 402 | "</xmlElement>";
|
| 403 | XMLDocument doc;
|
| 404 | doc.Parse( str );
|
| 405 | doc.Print();
|
| 406 |
|
| 407 | XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
|
| 408 | "<b>I am > the rules!</b>\n...since I make symbolic puns",
|
| 409 | false );
|
| 410 | }
|
| 411 |
|
| 412 | // InsertAfterChild causes crash.
|
| 413 | {
|
| 414 | // InsertBeforeChild and InsertAfterChild causes crash.
|
| 415 | XMLDocument doc;
|
| 416 | XMLElement* parent = doc.NewElement( "Parent" );
|
| 417 | doc.InsertFirstChild( parent );
|
| 418 |
|
| 419 | XMLElement* childText0 = doc.NewElement( "childText0" );
|
| 420 | XMLElement* childText1 = doc.NewElement( "childText1" );
|
| 421 |
|
| 422 | XMLNode* childNode0 = parent->InsertEndChild( childText0 );
|
| 423 | XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
|
| 424 |
|
| 425 | XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
|
| 426 | }
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 427 |
|
| 428 | {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame^] | 429 | // Entities not being written correctly.
|
| 430 | // From Lynn Allen
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 431 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame^] | 432 | const char* passages =
|
| 433 | "<?xml version=\"1.0\" standalone=\"no\" ?>"
|
| 434 | "<passages count=\"006\" formatversion=\"20020620\">"
|
| 435 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
|
| 436 | " It also has <, >, and &, as well as a fake copyright ©.\"> </psg>"
|
| 437 | "</passages>";
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 438 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame^] | 439 | XMLDocument doc;
|
| 440 | doc.Parse( passages );
|
| 441 | XMLElement* psg = doc.RootElement()->FirstChildElement();
|
| 442 | const char* context = psg->Attribute( "context" );
|
| 443 | const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 444 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame^] | 445 | XMLTest( "Entity transformation: read. ", expected, context, true );
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 446 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame^] | 447 | FILE* textfile = fopen( "textfile.txt", "w" );
|
| 448 | if ( textfile )
|
| 449 | {
|
| 450 | XMLStreamer streamer( textfile );
|
| 451 | psg->Accept( &streamer );
|
| 452 | fclose( textfile );
|
| 453 | }
|
| 454 | textfile = fopen( "textfile.txt", "r" );
|
| 455 | TIXMLASSERT( textfile );
|
| 456 | if ( textfile )
|
| 457 | {
|
| 458 | char buf[ 1024 ];
|
| 459 | fgets( buf, 1024, textfile );
|
| 460 | XMLTest( "Entity transformation: write. ",
|
| 461 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
|
| 462 | " It also has <, >, and &, as well as a fake copyright \xC2\xA9.\"/>\n",
|
| 463 | buf, false );
|
| 464 | }
|
| 465 | fclose( textfile );
|
| 466 | }
|
| 467 |
|
| 468 | {
|
| 469 | const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
|
| 470 |
|
| 471 | XMLDocument doc;
|
| 472 | doc.Parse( test );
|
| 473 | XMLTest( "dot in names", doc.Error(), 0);
|
| 474 | XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
|
| 475 | XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
|
| 476 | }
|
| 477 |
|
| 478 | {
|
| 479 | const char* test = "<element><Name>1.1 Start easy ignore fin thickness
</Name></element>";
|
| 480 |
|
| 481 | XMLDocument doc;
|
| 482 | doc.Parse( test );
|
| 483 |
|
| 484 | XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
|
| 485 | XMLTest( "Entity with one digit.",
|
| 486 | text->Value(), "1.1 Start easy ignore fin thickness\n",
|
| 487 | false );
|
| 488 | }
|
| 489 |
|
| 490 | {
|
| 491 | // DOCTYPE not preserved (950171)
|
| 492 | //
|
| 493 | const char* doctype =
|
| 494 | "<?xml version=\"1.0\" ?>"
|
| 495 | "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
|
| 496 | "<!ELEMENT title (#PCDATA)>"
|
| 497 | "<!ELEMENT books (title,authors)>"
|
| 498 | "<element />";
|
| 499 |
|
| 500 | XMLDocument doc;
|
| 501 | doc.Parse( doctype );
|
| 502 | doc.SaveFile( "test7.xml" );
|
| 503 | doc.DeleteChild( doc.RootElement() );
|
| 504 | doc.LoadFile( "test7.xml" );
|
| 505 | doc.Print();
|
| 506 |
|
| 507 | const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
|
| 508 | XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
|
| 509 |
|
| 510 | }
|
| 511 |
|
| 512 | {
|
| 513 | // Comments do not stream out correctly.
|
| 514 | const char* doctype =
|
| 515 | "<!-- Somewhat<evil> -->";
|
| 516 | XMLDocument doc;
|
| 517 | doc.Parse( doctype );
|
| 518 |
|
| 519 | XMLComment* comment = doc.FirstChild()->ToComment();
|
| 520 |
|
| 521 | XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
|
| 522 | }
|
| 523 | {
|
| 524 | // Double attributes
|
| 525 | const char* doctype = "<element attr='red' attr='blue' />";
|
| 526 |
|
| 527 | XMLDocument doc;
|
| 528 | doc.Parse( doctype );
|
| 529 |
|
| 530 | XMLTest( "Parsing repeated attributes.", ERROR_PARSING_ATTRIBUTE, doc.ErrorID() ); // is an error to tinyxml (didn't use to be, but caused issues)
|
| 531 | }
|
| 532 |
|
| 533 | {
|
| 534 | // Embedded null in stream.
|
| 535 | const char* doctype = "<element att\0r='red' attr='blue' />";
|
| 536 |
|
| 537 | XMLDocument doc;
|
| 538 | doc.Parse( doctype );
|
| 539 | XMLTest( "Embedded null throws error.", true, doc.Error() );
|
| 540 | }
|
| 541 |
|
| 542 | {
|
| 543 | // Empty documents should return TIXML_ERROR_PARSING_EMPTY, bug 1070717
|
| 544 | const char* str = " ";
|
| 545 | XMLDocument doc;
|
| 546 | doc.Parse( str );
|
| 547 | XMLTest( "Empty document error", ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
|
| 548 | }
|
| 549 |
|
| 550 | {
|
| 551 | // Low entities
|
| 552 | XMLDocument doc;
|
| 553 | doc.Parse( "<test></test>" );
|
| 554 | const char result[] = { 0x0e, 0 };
|
| 555 | XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
|
| 556 | doc.Print();
|
| 557 | }
|
| 558 |
|
| 559 | {
|
| 560 | // Attribute values with trailing quotes not handled correctly
|
| 561 | XMLDocument doc;
|
| 562 | doc.Parse( "<foo attribute=bar\" />" );
|
| 563 | XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
|
| 564 | }
|
| 565 |
|
| 566 | {
|
| 567 | // [ 1663758 ] Failure to report error on bad XML
|
| 568 | XMLDocument xml;
|
| 569 | xml.Parse("<x>");
|
| 570 | XMLTest("Missing end tag at end of input", xml.Error(), true);
|
| 571 | xml.Parse("<x> ");
|
| 572 | XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
|
| 573 | xml.Parse("<x></y>");
|
| 574 | XMLTest("Mismatched tags", xml.ErrorID(), ERROR_MISMATCHED_ELEMENT);
|
| 575 | }
|
| 576 |
|
| 577 |
|
| 578 | {
|
| 579 | // [ 1475201 ] TinyXML parses entities in comments
|
| 580 | XMLDocument xml;
|
| 581 | xml.Parse("<!-- declarations for <head> & <body> -->"
|
| 582 | "<!-- far & away -->" );
|
| 583 |
|
| 584 | XMLNode* e0 = xml.FirstChild();
|
| 585 | XMLNode* e1 = e0->NextSibling();
|
| 586 | XMLComment* c0 = e0->ToComment();
|
| 587 | XMLComment* c1 = e1->ToComment();
|
| 588 |
|
| 589 | XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
|
| 590 | XMLTest( "Comments ignore entities.", " far & away ", c1->Value(), true );
|
| 591 | }
|
| 592 |
|
| 593 | {
|
| 594 | XMLDocument xml;
|
| 595 | xml.Parse( "<Parent>"
|
| 596 | "<child1 att=''/>"
|
| 597 | "<!-- With this comment, child2 will not be parsed! -->"
|
| 598 | "<child2 att=''/>"
|
| 599 | "</Parent>" );
|
| 600 | xml.Print();
|
| 601 |
|
| 602 | int count = 0;
|
| 603 |
|
| 604 | for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
|
| 605 | ele;
|
| 606 | ele = ele->NextSibling() )
|
| 607 | {
|
| 608 | ++count;
|
| 609 | }
|
| 610 |
|
| 611 | XMLTest( "Comments iterate correctly.", 3, count );
|
| 612 | }
|
| 613 |
|
| 614 | {
|
| 615 | // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
|
| 616 | unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
|
| 617 | buf[60] = 239;
|
| 618 | buf[61] = 0;
|
| 619 |
|
| 620 | XMLDocument doc;
|
| 621 | doc.Parse( (const char*)buf);
|
| 622 | }
|
| 623 |
|
| 624 |
|
| 625 | {
|
| 626 | // bug 1827248 Error while parsing a little bit malformed file
|
| 627 | // Actually not malformed - should work.
|
| 628 | XMLDocument xml;
|
| 629 | xml.Parse( "<attributelist> </attributelist >" );
|
| 630 | XMLTest( "Handle end tag whitespace", false, xml.Error() );
|
| 631 | }
|
| 632 |
|
| 633 | {
|
| 634 | // This one must not result in an infinite loop
|
| 635 | XMLDocument xml;
|
| 636 | xml.Parse( "<infinite>loop" );
|
| 637 | XMLTest( "Infinite loop test.", true, true );
|
| 638 | }
|
| 639 | #endif
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 640 | #if defined( WIN32 )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 641 | _CrtMemCheckpoint( &endMemState );
|
| 642 | //_CrtMemDumpStatistics( &endMemState );
|
| 643 |
|
| 644 | _CrtMemState diffMemState;
|
| 645 | _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
|
| 646 | _CrtMemDumpStatistics( &diffMemState );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 647 | //printf( "new total=%d\n", gNewTotal );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 648 | #endif
|
| 649 |
|
| 650 | printf ("\nPass %d, Fail %d\n", gPass, gFail);
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 651 | return 0;
|
| 652 | } |