blob: 0991e2d766e7a59b464a9d56642f4ab232c30f94 [file] [log] [blame]
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001#include "tinyxml2.h"
2
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07003#include <cstdlib>
4#include <cstring>
5#include <ctime>
U-Lama\Leee13c3e62011-12-28 14:36:55 -08006
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08007#if defined( _MSC_VER )
Lee Thomason1ff38e02012-02-14 18:18:16 -08008 #include <crtdbg.h>
Lee Thomason6f381b72012-03-02 12:59:39 -08009 #define WIN32_LEAN_AND_MEAN
10 #include <windows.h>
Lee Thomason1ff38e02012-02-14 18:18:16 -080011 _CrtMemState startMemState;
12 _CrtMemState endMemState;
13#endif
Lee Thomasone9ecdab2012-02-13 18:11:20 -080014
U-Lama\Leee13c3e62011-12-28 14:36:55 -080015using namespace tinyxml2;
Lee Thomasonec5a7b42012-02-13 18:16:52 -080016int gPass = 0;
17int gFail = 0;
18
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -080019
U-Stream\Lee09a11c52012-02-17 08:31:16 -080020bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080021{
22 bool pass = !strcmp( expected, found );
23 if ( pass )
24 printf ("[pass]");
25 else
26 printf ("[fail]");
27
U-Stream\Lee09a11c52012-02-17 08:31:16 -080028 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080029 printf (" %s\n", testString);
30 else
31 printf (" %s [%s][%s]\n", testString, expected, found);
32
33 if ( pass )
34 ++gPass;
35 else
36 ++gFail;
37 return pass;
38}
39
40
U-Stream\Lee09a11c52012-02-17 08:31:16 -080041bool XMLTest( const char* testString, int expected, int found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080042{
43 bool pass = ( expected == found );
44 if ( pass )
45 printf ("[pass]");
46 else
47 printf ("[fail]");
48
U-Stream\Lee09a11c52012-02-17 08:31:16 -080049 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080050 printf (" %s\n", testString);
51 else
52 printf (" %s [%d][%d]\n", testString, expected, found);
53
54 if ( pass )
55 ++gPass;
56 else
57 ++gFail;
58 return pass;
59}
Lee Thomasonec5a7b42012-02-13 18:16:52 -080060
U-Lama\Leee13c3e62011-12-28 14:36:55 -080061
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -080062void NullLineEndings( char* p )
63{
64 while( p && *p ) {
65 if ( *p == '\n' || *p == '\r' ) {
66 *p = 0;
67 return;
68 }
69 ++p;
70 }
71}
72
73
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -070074// Comments in the header. (Don't know how to get Doxygen to read comments in this file.)
75int example_1()
76{
77 XMLDocument doc;
78 doc.LoadFile( "dream.xml" );
79
80 return doc.ErrorID();
81}
82
83
84// Comments in the header. (Don't know how to get Doxygen to read comments in this file.)
85int example_2()
86{
87 static const char* xml = "<element/>";
88 XMLDocument doc;
89 doc.Parse( xml );
90
91 return doc.ErrorID();
92}
93
94
95int example_3()
96{
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -070097 static const char* xml =
98 "<?xml version=\"1.0\"?>"
99 "<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
100 "<PLAY>"
101 "<TITLE>A Midsummer Night's Dream</TITLE>"
102 "</PLAY>";
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -0700103
104 XMLDocument doc;
105 doc.Parse( xml );
106
107 XMLElement* titleElement = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" );
108 const char* title = titleElement->GetText();
109 printf( "Name of play (1): %s\n", title );
110
111 XMLText* textNode = titleElement->FirstChild()->ToText();
112 title = textNode->Value();
113 printf( "Name of play (2): %s\n", title );
114
115 return doc.ErrorID();
116}
117
118
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700119int main( int /*argc*/, const char ** /*argv*/ )
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800120{
Lee Thomason (grinliz)0a4df402012-02-27 20:50:52 -0800121 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -0800122 _CrtMemCheckpoint( &startMemState );
123 #endif
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800124
Lee Thomason7f7b1622012-03-24 12:49:03 -0700125 #if defined(_MSC_VER)
126 #pragma warning ( push )
127 #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
128 #endif
129
130 FILE* fp = fopen( "dream.xml", "r" );
131 if ( !fp ) {
132 printf( "Error opening test file 'dream.xml'.\n"
133 "Is your working directory the same as where \n"
134 "the xmltest.cpp and dream.xml file are?\n\n"
135 #if defined( _MSC_VER )
136 "In windows Visual Studio you may need to set\n"
137 "Properties->Debugging->Working Directory to '..'\n"
138 #endif
139 );
140 exit( 1 );
141 }
142 fclose( fp );
143
144 #if defined(_MSC_VER)
145 #pragma warning ( pop )
146 #endif
147
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -0700148 XMLTest( "Example-1", 0, example_1() );
149 XMLTest( "Example-2", 0, example_2() );
150 XMLTest( "Example-3", 0, example_3() );
Lee Thomason87e475a2012-03-20 11:55:29 -0700151
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -0700152 /* ------ Example 2: Lookup information. ---- */
Lee Thomason87e475a2012-03-20 11:55:29 -0700153
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800154 {
Lee Thomason43f59302012-02-06 18:18:11 -0800155 static const char* test[] = { "<element />",
156 "<element></element>",
157 "<element><subelement/></element>",
158 "<element><subelement></subelement></element>",
159 "<element><subelement><subsub/></subelement></element>",
160 "<!--comment beside elements--><element><subelement></subelement></element>",
161 "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
162 "<element attrib1='foo' attrib2=\"bar\" ></element>",
163 "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
164 "<element>Text inside element.</element>",
165 "<element><b></b></element>",
166 "<element>Text inside and <b>bolded</b> in the element.</element>",
167 "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
Lee Thomason8ee79892012-01-25 17:44:30 -0800168 "<element>This &amp; That.</element>",
Lee Thomason18d68bd2012-01-26 18:17:26 -0800169 "<element attrib='This&lt;That' />",
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800170 0
171 };
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800172 for( int i=0; test[i]; ++i ) {
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800173 XMLDocument doc;
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800174 doc.Parse( test[i] );
Lee Thomason5cae8972012-01-24 18:03:07 -0800175 doc.Print();
Lee Thomasonec975ce2012-01-23 11:42:06 -0800176 printf( "----------------------------------------------\n" );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800177 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800178 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800179#if 1
Lee Thomasond6277762012-02-22 16:00:12 -0800180 {
181 static const char* test = "<!--hello world\n"
182 " line 2\r"
183 " line 3\r\n"
184 " line 4\n\r"
185 " line 5\r-->";
186
187 XMLDocument doc;
188 doc.Parse( test );
189 doc.Print();
190 }
191
Lee Thomason2c85a712012-01-31 08:24:24 -0800192 {
193 static const char* test = "<element>Text before.</element>";
194 XMLDocument doc;
195 doc.Parse( test );
196 XMLElement* root = doc.FirstChildElement();
197 XMLElement* newElement = doc.NewElement( "Subelement" );
198 root->InsertEndChild( newElement );
199 doc.Print();
200 }
Lee Thomasond1983222012-02-06 08:41:24 -0800201 {
202 XMLDocument* doc = new XMLDocument();
203 static const char* test = "<element><sub/></element>";
204 doc->Parse( test );
205 delete doc;
206 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800207 {
Lee Thomason1ff38e02012-02-14 18:18:16 -0800208 // Test: Programmatic DOM
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800209 // Build:
210 // <element>
211 // <!--comment-->
212 // <sub attrib="1" />
213 // <sub attrib="2" />
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800214 // <sub attrib="3" >& Text!</sub>
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800215 // <element>
216
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800217 XMLDocument* doc = new XMLDocument();
Lee Thomason1ff38e02012-02-14 18:18:16 -0800218 XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
219
220 XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
221 for( int i=0; i<3; ++i ) {
222 sub[i]->SetAttribute( "attrib", i );
223 }
224 element->InsertEndChild( sub[2] );
225 XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
226 element->InsertAfterChild( comment, sub[0] );
227 element->InsertAfterChild( sub[0], sub[1] );
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800228 sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800229 doc->Print();
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800230 XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
231 XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
232 XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
233 XMLTest( "Programmatic DOM", "& Text!",
234 doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800235
236 // And now deletion:
237 element->DeleteChild( sub[2] );
238 doc->DeleteNode( comment );
239
240 element->FirstChildElement()->SetAttribute( "attrib", true );
241 element->LastChildElement()->DeleteAttribute( "attrib" );
242
243 XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
244 int value = 10;
245 int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700246 XMLTest( "Programmatic DOM", result, XML_NO_ATTRIBUTE );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800247 XMLTest( "Programmatic DOM", value, 10 );
248
249 doc->Print();
250
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800251 XMLPrinter streamer;
U-Stream\Leeae25a442012-02-17 17:48:16 -0800252 doc->Print( &streamer );
253 printf( "%s", streamer.CStr() );
254
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800255 delete doc;
256 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800257 {
258 // Test: Dream
259 // XML1 : 1,187,569 bytes in 31,209 allocations
260 // XML2 : 469,073 bytes in 323 allocations
261 //int newStart = gNew;
262 XMLDocument doc;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800263 doc.LoadFile( "dream.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800264
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800265 doc.SaveFile( "dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800266 doc.PrintError();
267
268 XMLTest( "Dream", "xml version=\"1.0\"",
269 doc.FirstChild()->ToDeclaration()->Value() );
270 XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
271 XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
272 doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
273 XMLTest( "Dream", "And Robin shall restore amends.",
274 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
275 XMLTest( "Dream", "And Robin shall restore amends.",
276 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
277
278 XMLDocument doc2;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800279 doc2.LoadFile( "dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800280 XMLTest( "Dream-out", "xml version=\"1.0\"",
281 doc2.FirstChild()->ToDeclaration()->Value() );
282 XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
283 XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
284 doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
285 XMLTest( "Dream-out", "And Robin shall restore amends.",
286 doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
287
288 //gNewTotal = gNew - newStart;
289 }
Lee Thomason6f381b72012-03-02 12:59:39 -0800290
291
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800292 {
293 const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
294 "<passages count=\"006\" formatversion=\"20020620\">\n"
295 " <wrong error>\n"
296 "</passages>";
297
298 XMLDocument doc;
299 doc.Parse( error );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700300 XMLTest( "Bad XML", doc.ErrorID(), XML_ERROR_PARSING_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800301 }
302
303 {
304 const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
305
306 XMLDocument doc;
307 doc.Parse( str );
308
309 XMLElement* ele = doc.FirstChildElement();
310
311 int iVal, result;
312 double dVal;
313
314 result = ele->QueryDoubleAttribute( "attr0", &dVal );
315 XMLTest( "Query attribute: int as double", result, XML_NO_ERROR );
316 XMLTest( "Query attribute: int as double", (int)dVal, 1 );
317 result = ele->QueryDoubleAttribute( "attr1", &dVal );
318 XMLTest( "Query attribute: double as double", (int)dVal, 2 );
319 result = ele->QueryIntAttribute( "attr1", &iVal );
320 XMLTest( "Query attribute: double as int", result, XML_NO_ERROR );
321 XMLTest( "Query attribute: double as int", iVal, 2 );
322 result = ele->QueryIntAttribute( "attr2", &iVal );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700323 XMLTest( "Query attribute: not a number", result, XML_WRONG_ATTRIBUTE_TYPE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800324 result = ele->QueryIntAttribute( "bar", &iVal );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700325 XMLTest( "Query attribute: does not exist", result, XML_NO_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800326 }
327
328 {
329 const char* str = "<doc/>";
330
331 XMLDocument doc;
332 doc.Parse( str );
333
334 XMLElement* ele = doc.FirstChildElement();
335
336 int iVal;
337 double dVal;
338
339 ele->SetAttribute( "str", "strValue" );
340 ele->SetAttribute( "int", 1 );
341 ele->SetAttribute( "double", -1.0 );
342
343 const char* cStr = ele->Attribute( "str" );
344 ele->QueryIntAttribute( "int", &iVal );
345 ele->QueryDoubleAttribute( "double", &dVal );
346
Lee Thomason8ba7f7d2012-03-24 13:04:04 -0700347 XMLTest( "Attribute match test", ele->Attribute( "str", "strValue" ), "strValue" );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800348 XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
349 XMLTest( "Attribute round trip. int.", 1, iVal );
350 XMLTest( "Attribute round trip. double.", -1, (int)dVal );
351 }
352
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800353 {
354 XMLDocument doc;
355 doc.LoadFile( "utf8test.xml" );
356
357 // Get the attribute "value" from the "Russian" element and check it.
358 XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
359 const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
360 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
361
362 XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
363
364 const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
365 0xd1U, 0x81U, 0xd1U, 0x81U,
366 0xd0U, 0xbaU, 0xd0U, 0xb8U,
367 0xd0U, 0xb9U, 0 };
368 const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
369
370 XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
371 XMLTest( "UTF-8: Browsing russian element name.",
372 russianText,
373 text->Value() );
374
375 // Now try for a round trip.
376 doc.SaveFile( "utf8testout.xml" );
377
378 // Check the round trip.
379 char savedBuf[256];
380 char verifyBuf[256];
381 int okay = 0;
382
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800383
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800384#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800385#pragma warning ( push )
386#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800387#endif
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800388 FILE* saved = fopen( "utf8testout.xml", "r" );
389 FILE* verify = fopen( "utf8testverify.xml", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800390#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800391#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800392#endif
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800393
394 if ( saved && verify )
395 {
396 okay = 1;
397 while ( fgets( verifyBuf, 256, verify ) )
398 {
399 fgets( savedBuf, 256, saved );
400 NullLineEndings( verifyBuf );
401 NullLineEndings( savedBuf );
402
403 if ( strcmp( verifyBuf, savedBuf ) )
404 {
405 printf( "verify:%s<\n", verifyBuf );
406 printf( "saved :%s<\n", savedBuf );
407 okay = 0;
408 break;
409 }
410 }
411 }
412 if ( saved )
413 fclose( saved );
414 if ( verify )
415 fclose( verify );
416 XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
417 }
418
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800419 // --------GetText()-----------
420 {
421 const char* str = "<foo>This is text</foo>";
422 XMLDocument doc;
423 doc.Parse( str );
424 const XMLElement* element = doc.RootElement();
425
426 XMLTest( "GetText() normal use.", "This is text", element->GetText() );
427
428 str = "<foo><b>This is text</b></foo>";
429 doc.Parse( str );
430 element = doc.RootElement();
431
432 XMLTest( "GetText() contained element.", element->GetText() == 0, true );
433 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800434
Lee Thomasond6277762012-02-22 16:00:12 -0800435
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800436 // ---------- CDATA ---------------
437 {
438 const char* str = "<xmlElement>"
439 "<![CDATA["
440 "I am > the rules!\n"
441 "...since I make symbolic puns"
442 "]]>"
443 "</xmlElement>";
444 XMLDocument doc;
445 doc.Parse( str );
446 doc.Print();
Lee Thomasond6277762012-02-22 16:00:12 -0800447
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800448 XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
449 "I am > the rules!\n...since I make symbolic puns",
Lee Thomasond6277762012-02-22 16:00:12 -0800450 false );
451 }
452
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800453 // ----------- CDATA -------------
454 {
455 const char* str = "<xmlElement>"
456 "<![CDATA["
457 "<b>I am > the rules!</b>\n"
458 "...since I make symbolic puns"
459 "]]>"
460 "</xmlElement>";
461 XMLDocument doc;
462 doc.Parse( str );
463 doc.Print();
464
465 XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
466 "<b>I am > the rules!</b>\n...since I make symbolic puns",
467 false );
468 }
469
470 // InsertAfterChild causes crash.
471 {
472 // InsertBeforeChild and InsertAfterChild causes crash.
473 XMLDocument doc;
474 XMLElement* parent = doc.NewElement( "Parent" );
475 doc.InsertFirstChild( parent );
476
477 XMLElement* childText0 = doc.NewElement( "childText0" );
478 XMLElement* childText1 = doc.NewElement( "childText1" );
479
480 XMLNode* childNode0 = parent->InsertEndChild( childText0 );
481 XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
482
483 XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
484 }
Lee Thomasond6277762012-02-22 16:00:12 -0800485
486 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800487 // Entities not being written correctly.
488 // From Lynn Allen
Lee Thomasond6277762012-02-22 16:00:12 -0800489
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800490 const char* passages =
491 "<?xml version=\"1.0\" standalone=\"no\" ?>"
492 "<passages count=\"006\" formatversion=\"20020620\">"
493 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
494 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
495 "</passages>";
Lee Thomasond6277762012-02-22 16:00:12 -0800496
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800497 XMLDocument doc;
498 doc.Parse( passages );
499 XMLElement* psg = doc.RootElement()->FirstChildElement();
500 const char* context = psg->Attribute( "context" );
501 const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
Lee Thomasond6277762012-02-22 16:00:12 -0800502
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800503 XMLTest( "Entity transformation: read. ", expected, context, true );
Lee Thomasond6277762012-02-22 16:00:12 -0800504
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800505#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800506#pragma warning ( push )
507#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800508#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800509 FILE* textfile = fopen( "textfile.txt", "w" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800510#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800511#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800512#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800513 if ( textfile )
514 {
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800515 XMLPrinter streamer( textfile );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800516 psg->Accept( &streamer );
517 fclose( textfile );
518 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800519#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800520#pragma warning ( push )
521#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800522#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800523 textfile = fopen( "textfile.txt", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800524#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800525#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800526#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800527 TIXMLASSERT( textfile );
528 if ( textfile )
529 {
530 char buf[ 1024 ];
531 fgets( buf, 1024, textfile );
532 XMLTest( "Entity transformation: write. ",
533 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
534 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.\"/>\n",
535 buf, false );
536 }
537 fclose( textfile );
538 }
539
540 {
Lee Thomason6f381b72012-03-02 12:59:39 -0800541 // Suppress entities.
542 const char* passages =
543 "<?xml version=\"1.0\" standalone=\"no\" ?>"
544 "<passages count=\"006\" formatversion=\"20020620\">"
545 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;.\">Crazy &ttk;</psg>"
546 "</passages>";
547
548 XMLDocument doc( false );
549 doc.Parse( passages );
550
551 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
552 "Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;." );
553 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
554 "Crazy &ttk;" );
555 doc.Print();
556 }
557
558 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800559 const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
560
561 XMLDocument doc;
562 doc.Parse( test );
563 XMLTest( "dot in names", doc.Error(), 0);
564 XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
565 XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
566 }
567
568 {
569 const char* test = "<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>";
570
571 XMLDocument doc;
572 doc.Parse( test );
573
574 XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
575 XMLTest( "Entity with one digit.",
576 text->Value(), "1.1 Start easy ignore fin thickness\n",
577 false );
578 }
579
580 {
581 // DOCTYPE not preserved (950171)
582 //
583 const char* doctype =
584 "<?xml version=\"1.0\" ?>"
585 "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
586 "<!ELEMENT title (#PCDATA)>"
587 "<!ELEMENT books (title,authors)>"
588 "<element />";
589
590 XMLDocument doc;
591 doc.Parse( doctype );
592 doc.SaveFile( "test7.xml" );
593 doc.DeleteChild( doc.RootElement() );
594 doc.LoadFile( "test7.xml" );
595 doc.Print();
596
597 const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
598 XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
599
600 }
601
602 {
603 // Comments do not stream out correctly.
604 const char* doctype =
605 "<!-- Somewhat<evil> -->";
606 XMLDocument doc;
607 doc.Parse( doctype );
608
609 XMLComment* comment = doc.FirstChild()->ToComment();
610
611 XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
612 }
613 {
614 // Double attributes
615 const char* doctype = "<element attr='red' attr='blue' />";
616
617 XMLDocument doc;
618 doc.Parse( doctype );
619
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700620 XMLTest( "Parsing repeated attributes.", XML_ERROR_PARSING_ATTRIBUTE, doc.ErrorID() ); // is an error to tinyxml (didn't use to be, but caused issues)
Lee Thomason (grinliz)0a4df402012-02-27 20:50:52 -0800621 doc.PrintError();
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800622 }
623
624 {
625 // Embedded null in stream.
626 const char* doctype = "<element att\0r='red' attr='blue' />";
627
628 XMLDocument doc;
629 doc.Parse( doctype );
630 XMLTest( "Embedded null throws error.", true, doc.Error() );
631 }
632
633 {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700634 // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800635 const char* str = " ";
636 XMLDocument doc;
637 doc.Parse( str );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700638 XMLTest( "Empty document error", XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800639 }
640
641 {
642 // Low entities
643 XMLDocument doc;
644 doc.Parse( "<test>&#x0e;</test>" );
645 const char result[] = { 0x0e, 0 };
646 XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
647 doc.Print();
648 }
649
650 {
651 // Attribute values with trailing quotes not handled correctly
652 XMLDocument doc;
653 doc.Parse( "<foo attribute=bar\" />" );
654 XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
655 }
656
657 {
658 // [ 1663758 ] Failure to report error on bad XML
659 XMLDocument xml;
660 xml.Parse("<x>");
661 XMLTest("Missing end tag at end of input", xml.Error(), true);
662 xml.Parse("<x> ");
663 XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
664 xml.Parse("<x></y>");
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700665 XMLTest("Mismatched tags", xml.ErrorID(), XML_ERROR_MISMATCHED_ELEMENT);
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800666 }
667
668
669 {
670 // [ 1475201 ] TinyXML parses entities in comments
671 XMLDocument xml;
672 xml.Parse("<!-- declarations for <head> & <body> -->"
673 "<!-- far &amp; away -->" );
674
675 XMLNode* e0 = xml.FirstChild();
676 XMLNode* e1 = e0->NextSibling();
677 XMLComment* c0 = e0->ToComment();
678 XMLComment* c1 = e1->ToComment();
679
680 XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
681 XMLTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
682 }
683
684 {
685 XMLDocument xml;
686 xml.Parse( "<Parent>"
687 "<child1 att=''/>"
688 "<!-- With this comment, child2 will not be parsed! -->"
689 "<child2 att=''/>"
690 "</Parent>" );
691 xml.Print();
692
693 int count = 0;
694
695 for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
696 ele;
697 ele = ele->NextSibling() )
698 {
699 ++count;
700 }
701
702 XMLTest( "Comments iterate correctly.", 3, count );
703 }
704
705 {
706 // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
707 unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
708 buf[60] = 239;
709 buf[61] = 0;
710
711 XMLDocument doc;
712 doc.Parse( (const char*)buf);
713 }
714
715
716 {
717 // bug 1827248 Error while parsing a little bit malformed file
718 // Actually not malformed - should work.
719 XMLDocument xml;
720 xml.Parse( "<attributelist> </attributelist >" );
721 XMLTest( "Handle end tag whitespace", false, xml.Error() );
722 }
723
724 {
725 // This one must not result in an infinite loop
726 XMLDocument xml;
727 xml.Parse( "<infinite>loop" );
728 XMLTest( "Infinite loop test.", true, true );
729 }
730#endif
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800731 {
732 const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
733 XMLDocument doc;
734 doc.Parse( pub );
735
736 XMLDocument clone;
737 for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
738 XMLNode* copy = node->ShallowClone( &clone );
739 clone.InsertEndChild( copy );
740 }
741
742 clone.Print();
743
744 int count=0;
745 const XMLNode* a=clone.FirstChild();
746 const XMLNode* b=doc.FirstChild();
747 for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
748 ++count;
749 XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
750 }
751 XMLTest( "Clone and Equal", 4, count );
752 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800753
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -0700754 {
755 // This shouldn't crash.
756 XMLDocument doc;
757 if(XML_NO_ERROR != doc.LoadFile( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ))
758 {
759 doc.PrintError();
760 }
761 XMLTest( "Error in snprinf handling.", true, doc.Error() );
762 }
763
Lee Thomason5708f812012-03-28 17:46:41 -0700764 // -------- Handles ------------
765 {
766 static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
767 XMLDocument doc;
768 doc.Parse( xml );
Lee Thomason5708f812012-03-28 17:46:41 -0700769
770 XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
771 XMLTest( "Handle, success, mutable", ele->Value(), "sub" );
772
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700773 XMLHandle docH( doc );
774 ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
Lee Thomasond0b19df2012-04-12 08:41:37 -0700775 XMLTest( "Handle, dne, mutable", false, ele != 0 );
Lee Thomason5708f812012-03-28 17:46:41 -0700776 }
777
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700778 {
779 static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
780 XMLDocument doc;
781 doc.Parse( xml );
782 XMLConstHandle docH( doc );
783
784 const XMLElement* ele = docH.FirstChildElement( "element" ).FirstChild().ToElement();
785 XMLTest( "Handle, success, const", ele->Value(), "sub" );
786
787 ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
Lee Thomasond0b19df2012-04-12 08:41:37 -0700788 XMLTest( "Handle, dne, const", false, ele != 0 );
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700789 }
790
791
Lee Thomason6f381b72012-03-02 12:59:39 -0800792 // ----------- Performance tracking --------------
793 {
794#if defined( _MSC_VER )
795 __int64 start, end, freq;
796 QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
797#endif
798
799#if defined(_MSC_VER)
800#pragma warning ( push )
801#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
802#endif
803 FILE* fp = fopen( "dream.xml", "r" );
804#if defined(_MSC_VER)
805#pragma warning ( pop )
806#endif
807 fseek( fp, 0, SEEK_END );
808 long size = ftell( fp );
809 fseek( fp, 0, SEEK_SET );
810
811 char* mem = new char[size+1];
812 fread( mem, size, 1, fp );
813 fclose( fp );
814 mem[size] = 0;
815
816#if defined( _MSC_VER )
817 QueryPerformanceCounter( (LARGE_INTEGER*) &start );
818#else
819 clock_t cstart = clock();
820#endif
821 static const int COUNT = 10;
822 for( int i=0; i<COUNT; ++i ) {
823 XMLDocument doc;
824 doc.Parse( mem );
825 }
826#if defined( _MSC_VER )
827 QueryPerformanceCounter( (LARGE_INTEGER*) &end );
828#else
829 clock_t cend = clock();
830#endif
831
832 delete [] mem;
833
834 static const char* note =
835#ifdef DEBUG
836 "DEBUG";
837#else
838 "Release";
839#endif
840
841#if defined( _MSC_VER )
842 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
843#else
844 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
845#endif
846 }
847
Lee Thomason (grinliz)7ca55582012-03-07 21:54:57 -0800848 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -0800849 _CrtMemCheckpoint( &endMemState );
850 //_CrtMemDumpStatistics( &endMemState );
851
852 _CrtMemState diffMemState;
853 _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
854 _CrtMemDumpStatistics( &diffMemState );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800855 //printf( "new total=%d\n", gNewTotal );
Lee Thomason1ff38e02012-02-14 18:18:16 -0800856 #endif
857
858 printf ("\nPass %d, Fail %d\n", gPass, gFail);
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800859 return 0;
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -0800860}